Split the FSM-transitions and put them in an unordered_map
[0ad.git] / source / network / Serialization.h
blob476d52aa6ed6855e33abd2453c3b2866c6c3c397
1 /* Copyright (C) 2015 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef INCLUDED_NETWORK_SERIALIZATION
19 #define INCLUDED_NETWORK_SERIALIZATION
21 #define Serialize_int_1(_pos, _val) \
22 STMT( *((_pos)++) = (u8)((_val)&0xff); )
24 #define Serialize_int_2(_pos, _val) STMT(\
25 Serialize_int_1(_pos, (_val)>>8); \
26 Serialize_int_1(_pos, (_val)); \
29 #define Serialize_int_3(_pos, _val) STMT(\
30 Serialize_int_1(_pos, (_val)>>16); \
31 Serialize_int_2(_pos, (_val)); \
34 #define Serialize_int_4(_pos, _val) STMT(\
35 Serialize_int_1(_pos, (_val)>>24); \
36 Serialize_int_3(_pos, (_val)); \
39 #define Serialize_int_8(_pos, _val) STMT(\
40 Serialize_int_4(_pos, (_val)>>32); \
41 Serialize_int_4(_pos, (_val)); \
44 #define __shift_de(_pos, _val) STMT( \
45 (_val) <<= 8; \
46 (_val) += *((_pos)++); )
48 #define Deserialize_int_1(_pos, _val) STMT(\
49 (_val) = *((_pos)++); )
51 #define Deserialize_int_2(_pos, _val) STMT(\
52 Deserialize_int_1(_pos, _val); \
53 __shift_de(_pos, _val); )
55 #define Deserialize_int_3(_pos, _val) STMT(\
56 Deserialize_int_2(_pos, _val); \
57 __shift_de(_pos, _val); )
59 #define Deserialize_int_4(_pos, _val) STMT(\
60 Deserialize_int_3(_pos, _val); \
61 __shift_de(_pos, _val); )
63 #define Deserialize_int_8(_pos, _val) STMT(\
64 uint32 _v1; uint32 _v2; \
65 Deserialize_int_4(_pos, _v1); \
66 Deserialize_int_4(_pos, _v2); \
67 _val = _v1; \
68 _val <<= 32; /* janwas: careful! (uint32 << 32) = 0 */ \
69 _val |= _v2; )
71 /**
72 * An interface for serializable objects. For a serializable object to be usable
73 * as a network message field, it must have a constructor without arguments.
75 class ISerializable
77 public:
78 virtual ~ISerializable() {}
80 /**
81 * Return the length of the serialized form of this object
83 virtual size_t GetSerializedLength() const = 0;
84 /**
85 * Serialize the object into the passed buffer.
87 * @return a pointer to the location in the buffer right after the
88 * serialized object
90 virtual u8 *Serialize(u8 *buffer) const = 0;
91 /**
92 * Deserialize the object (i.e. read in data from the buffer and initialize
93 * the object's fields). Note that it is up to the deserializer to detect
94 * errors in data format.
96 * @param buffer A pointer pointing to the start of the serialized data.
97 * @param end A pointer to the end of the message.
99 * @returns a pointer to the location in the buffer right after the
100 * serialized object, or NULL if there was a data format error
102 virtual const u8 *Deserialize(const u8 *buffer, const u8 *end) = 0;
105 #endif