23#include NMT_CREATE_HEADER_NAME
28#undef START_NMT_CLASS_DERIVED
31#undef NMT_FIELD_SECRET
39#ifdef NMT_CREATE_HEADER_NAME
41#ifndef ARRAY_STRUCT_PREFIX
42#define ARRAY_STRUCT_PREFIX(_nm) S_##_nm
47#ifndef NMT_CREATOR_IMPLEMENT
53#define NMT_CREATOR_PASS_CLASSDEF
57#define START_NMT_CLASS(_nm, _tp) \
58 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
68#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
69CNetMessage *Deserialize##_nm(const std::uint8_t *, size_t); \
70class _nm: public _base \
73 _nm(NetMessageType type): _base(type) {}\
77 CStr ToStringRaw() const;\
79 _nm(): _base(_tp) {} \
80 virtual size_t GetSerializedLength() const; \
81 virtual std::uint8_t *Serialize(std::uint8_t *buffer) const; \
82 virtual const std::uint8_t *Deserialize(const std::uint8_t *pos, const std::uint8_t *end); \
83 virtual CStr ToString() const; \
84 inline operator CStr () const \
85 { return ToString(); }
97#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
108#define NMT_FIELD(_tp, _nm) \
115#define NMT_FIELD_SECRET(_tp, _nm) \
118#define NMT_START_ARRAY(_nm) \
119 struct ARRAY_STRUCT_PREFIX(_nm); \
120 std::vector <ARRAY_STRUCT_PREFIX(_nm)> _nm; \
121 struct ARRAY_STRUCT_PREFIX(_nm) {
123#define NMT_END_ARRAY() \
126#define END_NMT_CLASS() };
129#undef NMT_CREATOR_PASS_CLASSDEF
139#define NMT_CREATOR_PASS_GETLENGTH
143#define START_NMT_CLASS(_nm, _tp) \
144 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
145#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
146size_t _nm::GetSerializedLength() const \
148 size_t ret=_base::GetSerializedLength(); \
149 [[maybe_unused]] const _nm *thiz=this;
151#define NMT_START_ARRAY(_nm) \
152 std::vector <ARRAY_STRUCT_PREFIX(_nm)>::const_iterator it=_nm.begin(); \
153 while (it != _nm.end()) \
155 [[maybe_unused]] const ARRAY_STRUCT_PREFIX(_nm) *thiz=&*it;
157#define NMT_END_ARRAY() \
161#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
164#define NMT_FIELD(_tp, _nm) \
165 ret += thiz->_nm.GetSerializedLength();
167#define NMT_FIELD_SECRET(_tp, _nm) \
168 ret += thiz->_nm.GetSerializedLength();
170#define END_NMT_CLASS() \
175#undef NMT_CREATOR_PASS_GETLENGTH
180#define NMT_CREATOR_PASS_SERIALIZE
185#define START_NMT_CLASS(_nm, _tp) \
186 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
187#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
188std::uint8_t* _nm::Serialize(std::uint8_t *buffer) const \
191 std::uint8_t *pos=_base::Serialize(buffer); \
192 [[maybe_unused]] const _nm *thiz=this;
194#define NMT_START_ARRAY(_nm) \
195 std::vector <ARRAY_STRUCT_PREFIX(_nm)>::const_iterator it=_nm.begin(); \
196 while (it != _nm.end()) \
198 [[maybe_unused]] const ARRAY_STRUCT_PREFIX(_nm) *thiz=&*it;
200#define NMT_END_ARRAY() \
204#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
205 Serialize_int_##_netsz(pos, thiz->_nm); \
207#define NMT_FIELD(_tp, _nm) \
208 pos=thiz->_nm.Serialize(pos);
210#define NMT_FIELD_SECRET(_tp, _nm) \
211 pos=thiz->_nm.Serialize(pos);
213#define END_NMT_CLASS() \
219#undef NMT_CREATOR_PASS_SERIALIZE
224#define NMT_CREATOR_PASS_DESERIALIZE
229#define BAIL_DESERIALIZER return NULL
231#define START_NMT_CLASS(_nm, _tp) \
232 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
233#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
234const std::uint8_t* _nm::Deserialize(const std::uint8_t *pos, const std::uint8_t *end) \
236 pos=_base::Deserialize(pos, end); \
237 if (pos == NULL) BAIL_DESERIALIZER;\
238 [[maybe_unused]] _nm *thiz=this; \
242#define NMT_START_ARRAY(_nm) \
245 [[maybe_unused]] ARRAY_STRUCT_PREFIX(_nm) *thiz = \
246 &*_nm.insert(_nm.end(), ARRAY_STRUCT_PREFIX(_nm)());
248#define NMT_END_ARRAY() \
251#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
252 if (pos+_netsz > end) BAIL_DESERIALIZER; \
253 Deserialize_int_##_netsz(pos, thiz->_nm); \
256#define NMT_FIELD(_tp, _nm) \
257 if ((pos=thiz->_nm.Deserialize(pos, end)) == NULL) BAIL_DESERIALIZER;
259#define NMT_FIELD_SECRET(_tp, _nm) \
260 if ((pos=thiz->_nm.Deserialize(pos, end)) == NULL) BAIL_DESERIALIZER;
262#define END_NMT_CLASS() \
268#undef BAIL_DESERIALIZER
270#undef NMT_CREATOR_PASS_DESERIALIZE
278#define START_NMT_CLASS(_nm, _tp) \
279CStr _nm::ToString() const \
281 CStr ret=#_nm " { "; \
282 return ret + ToStringRaw() + " }"; \
284CStr _nm::ToStringRaw() const \
287 [[maybe_unused]] const _nm *thiz=this;
289#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
290CStr _nm::ToString() const \
292 CStr ret=#_nm " { "; \
293 return ret + ToStringRaw() + " }"; \
295CStr _nm::ToStringRaw() const \
297 CStr ret=_base::ToStringRaw() + ", "; \
298 [[maybe_unused]] const _nm *thiz=this;
300#define NMT_START_ARRAY(_nm) \
302 std::vector < ARRAY_STRUCT_PREFIX(_nm) >::const_iterator it=_nm.begin(); \
303 while (it != _nm.end()) \
306 [[maybe_unused]] const ARRAY_STRUCT_PREFIX(_nm) *thiz=&*it;
308#define NMT_END_ARRAY() \
310 ret=ret.substr(0, ret.length()-2)+" }, "; \
312 ret=ret.substr(0, ret.length()-2)+" }, ";
314#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
316 ret += NetMessageStringConvert(thiz->_nm); \
319#define NMT_FIELD(_tp, _nm) \
321 ret += NetMessageStringConvert(thiz->_nm); \
324#define NMT_FIELD_SECRET(_tp, _nm) \
325 ret += #_nm ": [secret], ";
327#define END_NMT_CLASS() \
328 return ret.substr(0, ret.length()-2); \
337#undef NMT_CREATE_HEADER_NAME
338#undef NMT_CREATOR_IMPLEMENT