-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.hpp
66 lines (49 loc) · 1.67 KB
/
serialization.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef __SERIALIZATION_HPP__
#define __SERIALIZATION_HPP__
#include <map>
#include <string>
#include <vector>
#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>
#include <cstdio>
#include <sys/errno.h>
#include "geom.hpp"
namespace battlemints {
struct thing;
namespace serialization {
enum flags {
SPAWN = 1
};
}
boost::optional<std::string> resource_filename(std::string const &name, std::string const &type);
std::string data_filename(std::string const &name);
thing *thing_from_bin(FILE *bin);
struct invalid_board : std::runtime_error {
invalid_board(std::string const &s) : std::runtime_error(s) {}
};
bool _safe_fread(void *p, size_t size, size_t nmemb, FILE *stream);
bool _safe_fwrite(void const *p, size_t size, size_t nmemb, FILE *stream);
template<typename T>
inline bool safe_fread(T *p, size_t size, size_t nmemb, FILE *stream)
{ return _safe_fread((void*)p, size, nmemb, stream); }
template<typename T>
inline bool safe_fwrite(T const *p, size_t size, size_t nmemb, FILE *stream)
{ return _safe_fwrite((void const *)p, size, nmemb, stream); }
#define BATTLEMINTS_READ_SLOTS(object, from_slot, through_slot, stream) \
(::battlemints::safe_fread(&((object).from_slot), (char*)(&((object).through_slot)+1) - (char*)&((object).from_slot), 1, stream))
boost::optional<std::string> pascal_string_from_bin(FILE *bin);
boost::optional<std::string> long_pascal_string_from_bin(FILE *bin);
template<typename T>
inline T data_from_bin(FILE *bin)
{
T r;
safe_fread(&r, sizeof(r), 1, bin);
return r;
}
template<typename T>
inline void n_from_bin(FILE *bin, T *into, int n)
{
safe_fread((void*)into, sizeof(*into), n, bin);
}
}
#endif