ScummVM API documentation
SerializeClass.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of HPL1 Engine.
26  */
27 
28 #ifndef HPL_SERIALIZE_CLASS_H
29 #define HPL_SERIALIZE_CLASS_H
30 
31 #include "hpl1/engine/system/MemoryManager.h"
32 #include "hpl1/engine/system/SystemTypes.h"
33 #include "hpl1/static_string.h"
34 #include "common/stablemap.h"
35 
36 class TiXmlElement;
37 
38 namespace hpl {
39 
43 
44 typedef unsigned int eSerializeType;
45 
46 #define eSerializeType_Bool (1)
47 #define eSerializeType_Int32 (2)
48 #define eSerializeType_Float32 (3)
49 
50 #define eSerializeType_String (4)
51 
52 #define eSerializeType_Vector2l (5)
53 #define eSerializeType_Vector2f (6)
54 #define eSerializeType_Vector3l (7)
55 #define eSerializeType_Vector3f (8)
56 
57 #define eSerializeType_Matrixf (9)
58 
59 #define eSerializeType_Color (10)
60 
61 #define eSerializeType_Rect2l (11)
62 #define eSerializeType_Rect2f (12)
63 
64 #define eSerializeType_Planef (13)
65 
66 #define eSerializeType_WString (14)
67 
68 #define eSerializeType_Class (100)
69 
70 // Important with this type is that it will only be created
71 // if the member variable is NULL. Else it is assume that it is
72 // already created.
73 // Also, for the class to be a class pointer it has to have a
74 // constructor without any parameters.
75 // NULL Values are NOT allowed!
76 #define eSerializeType_ClassPointer (101)
77 
78 // User defined types must be after this
79 #define eSerializeType_Last (200)
80 
81 #define eSerializeType_NULL (0xFFFF)
82 
86 
87 typedef unsigned int eSerializeMainType;
88 
89 #define eSerializeMainType_Variable (1)
90 #define eSerializeMainType_Array (2)
91 #define eSerializeMainType_Container (3)
92 
93 #define eSerializeMainType_NULL (0xFFFF)
94 
98 
99 #define ClassMemberOffset(aClass, aMember) (size_t(&(((aClass *)1)->aMember)) - 1)
100 #define ClassMemberSize(aClass, aMember) (sizeof(((aClass *)1)->aMember))
101 
105 #define kSerializableClassInit(aClass) \
106 public: \
107  const static char *msSerialize_Name; \
108  const static char *msSerialize_ParentName; \
109  static cSerializeMemberField *mpSerialize_MemberFields; \
110  virtual ~aClass() = default; \
111  virtual tString Serialize_GetTopClass() { return #aClass; }
112 
113 #define kSerializableClassInit_nodestructor(aClass) \
114 public: \
115  const static char *msSerialize_Name; \
116  const static char *msSerialize_ParentName; \
117  static cSerializeMemberField *mpSerialize_MemberFields; \
118  virtual tString Serialize_GetTopClass() { return #aClass; }
119 
123 #define kBeginSerialize(aClass, aParent) \
124  namespace SerializeNamespace_##aClass { \
125  extern cSerializeMemberField mvTempMemberFields[]; \
126  } \
127  const char *aClass::msSerialize_Name = #aClass; \
128  const char *aClass::msSerialize_ParentName = #aParent; \
129  cSerializeMemberField *aClass::mpSerialize_MemberFields = SerializeNamespace_##aClass::mvTempMemberFields; \
130  namespace SerializeNamespace_##aClass { \
131  typedef aClass tVarClass; \
132  cSerializeMemberField mvTempMemberFields[] = {
133 
134 #define kBeginSerializeBase(aClass) \
135  namespace SerializeNamespace_##aClass { \
136  extern cSerializeMemberField mvTempMemberFields[]; \
137  } \
138  const char *aClass::msSerialize_Name = #aClass; \
139  const char *aClass::msSerialize_ParentName = ""; \
140  cSerializeMemberField *aClass::mpSerialize_MemberFields = SerializeNamespace_##aClass::mvTempMemberFields; \
141  namespace SerializeNamespace_##aClass { \
142  typedef aClass tVarClass; \
143  cSerializeMemberField mvTempMemberFields[] = {
144 
145 #define kBeginSerializeVirtual(aClass, aParent) \
146  namespace SerializeNamespace_##aClass { \
147  extern cSerializeMemberField mvTempMemberFields[]; \
148  } \
149  const char *aClass::msSerialize_Name = #aClass; \
150  const char *aClass::msSerialize_ParentName = #aParent; \
151  cSerializeMemberField *aClass::mpSerialize_MemberFields = SerializeNamespace_##aClass::mvTempMemberFields; \
152  namespace SerializeNamespace_##aClass { \
153  typedef aClass tVarClass; \
154  cSerializeMemberField mvTempMemberFields[] = {
155 
156 #define kBeginSerializeBaseVirtual(aClass) \
157  namespace SerializeNamespace_##aClass { \
158  extern cSerializeMemberField mvTempMemberFields[]; \
159  } \
160  const char *aClass::msSerialize_Name = #aClass; \
161  const char *aClass::msSerialize_ParentName = ""; \
162  cSerializeMemberField *aClass::mpSerialize_MemberFields = SerializeNamespace_##aClass::mvTempMemberFields; \
163  namespace SerializeNamespace_##aClass { \
164  typedef aClass tVarClass; \
165  cSerializeMemberField mvTempMemberFields[] = {
166 
170 #define kEndSerialize() \
171  cSerializeMemberField("", 0, 0, eSerializeType_NULL, 0) \
172  } \
173  ; \
174  }
175 
179 #define kSerializeVar(aVar, aType) \
180  cSerializeMemberField(#aVar, ClassMemberOffset(tVarClass, aVar), ClassMemberSize(tVarClass, aVar), aType, eSerializeMainType_Variable),
181 
185 #define kSerializeVarArray(aVar, aType, aArraySize) \
186  cSerializeMemberField(#aVar, ClassMemberOffset(tVarClass, aVar), ClassMemberSize(tVarClass, aVar), aType, eSerializeMainType_Array, aArraySize),
187 
191 #define kSerializeVarContainer(aVar, aType) \
192  cSerializeMemberField(#aVar, ClassMemberOffset(tVarClass, aVar), ClassMemberSize(tVarClass, aVar), aType, eSerializeMainType_Container),
193 
196 #define kSerializeClassContainer(aVar, aClass, aType) \
197  cSerializeMemberField(#aVar, ClassMemberOffset(tVarClass, aVar), ClassMemberSize(tVarClass, aVar), aType, eSerializeMainType_Container, #aClass),
198 
202 
203 //-------------------------------------------------
204 
206 public:
207  constexpr cSerializeMemberField(Hpl1::StaticString asName, size_t alOffset, size_t alSize, eSerializeType alType,
208  eSerializeMainType aMainType) : msName(asName), msClassName(""), mlOffset(alOffset), mlSize(alSize), mType(alType), mMainType(aMainType), mlArraySize(0) {
209  }
210 
211  constexpr cSerializeMemberField(Hpl1::StaticString asName, size_t alOffset, size_t alSize, eSerializeType alType,
212  eSerializeMainType aMainType, size_t alArraySize) : msName(asName), msClassName(""), mlOffset(alOffset), mlSize(alSize), mType(alType), mMainType(aMainType), mlArraySize(alArraySize) {
213  }
214 
215  constexpr cSerializeMemberField(Hpl1::StaticString asName, size_t alOffset, size_t alSize, eSerializeType alType,
216  eSerializeMainType aMainType, Hpl1::StaticString asClassName) : msName(asName), msClassName(asClassName), mlOffset(alOffset), mlSize(alSize), mType(alType), mMainType(aMainType), mlArraySize(0) {
217  }
218 
219  Hpl1::StaticString msName;
220  Hpl1::StaticString msClassName;
221  size_t mlOffset;
222  size_t mlSize;
223  eSerializeType mType;
224  eSerializeMainType mMainType;
225  size_t mlArraySize;
226 };
227 
228 //-------------------------------------------------
229 
231 public:
232  virtual ~iSerializable() = default;
233  virtual tString Serialize_GetTopClass() { return ""; }
234 };
235 
236 //-------------------------------------------------
237 
239 public:
240  virtual ~iSerializableType() = default;
241  virtual char *ValueToString(void *apVal) = 0;
242  virtual void ValueFromString(char *apString, void *apVal) = 0;
243 };
244 
245 //-------------------------------------------------
246 
248 public:
249  constexpr cSerializeSavedClass() = default;
250  constexpr cSerializeSavedClass(const char *asName, const char *asParent,
251  cSerializeMemberField *apMemberFields, size_t alSize,
252  iSerializable *(*apCreateFunc)()) : msName(asName), msParentName(asParent), mpMemberFields(apMemberFields),
253  mlSize(alSize), mpCreateFunc(apCreateFunc) {}
254 
255  const char *msName = "";
256  const char *msParentName = "";
257  cSerializeMemberField *mpMemberFields = nullptr;
258  size_t mlSize = 0;
259  iSerializable *(*mpCreateFunc)() = nullptr;
260 };
261 
262 //-------------------------------------------------
263 
265 public:
267 
268  bool HasNext();
269  cSerializeMemberField *GetNext();
270 
271 private:
272  cSerializeSavedClass *mpSavedClass;
273  int mlFieldNum;
274 };
275 
276 //-------------------------------------------------
277 
279 typedef tSerializeSavedClassMap::iterator tSerializeSavedClassMapIt;
280 
283 
285 public:
286  static void initSaveClassesMap();
287  static void finalizeSaveClassesMap();
288 
289  static void SetLog(bool abX);
290  static bool GetLog();
291 
292  static void PrintMembers(iSerializable *apData);
293 
294  static bool SaveToFile(iSerializable *apData, const tWString &asFile, const tString &asRoot);
295  static void SaveToElement(iSerializable *apData, const tString &asName, TiXmlElement *apParent, bool abIsPointer = false);
296 
297  static bool LoadFromFile(iSerializable *apData, const tWString &asFile);
298  static void LoadFromElement(iSerializable *apData, TiXmlElement *apElement, bool abIsPointer = false);
299 
300  static cSerializeSavedClass *GetClass(const tString &asName);
301 
302  static cSerializeMemberFieldIterator GetMemberFieldIterator(iSerializable *apData);
303 
304  static tString ValueToString(void *apData, size_t alOffset, eSerializeType aType);
305  static void StringToValue(void *apData, size_t alOffset, eSerializeType aType,
306  const char *asVal);
307 
308 private:
309  static void SaveVariable(TiXmlElement *apElement, cSerializeMemberField *apField, iSerializable *apData);
310  static void SaveArray(TiXmlElement *apElement, cSerializeMemberField *apField, iSerializable *apData);
311  static void SaveContainer(TiXmlElement *apElement, cSerializeMemberField *apField, iSerializable *apData);
312 
313  static void LoadVariable(TiXmlElement *apElement, iSerializable *apData, cSerializeSavedClass *apClass);
314  static void LoadArray(TiXmlElement *apElement, iSerializable *apData, cSerializeSavedClass *apClass);
315  static void LoadClass(TiXmlElement *apElement, iSerializable *apData, cSerializeSavedClass *apClass);
316  static void LoadClassPointer(TiXmlElement *apElement, iSerializable *apData, cSerializeSavedClass *apClass);
317  static void LoadContainer(TiXmlElement *apElement, iSerializable *apData, cSerializeSavedClass *apClass);
318 
319  static void FillSaveClassMembersList(tSerializeSavedClassList *apList, cSerializeSavedClass *apClass);
320  static void SaveSavedClassMembers(cSerializeSavedClass *apClass, iSerializable *apData);
321 
322  static cSerializeMemberField *GetMemberField(const tString &asName, cSerializeSavedClass *apClass);
323 
324  static size_t SizeOfType(eSerializeType aType);
325 
326  static void SetUpData();
327 
328  static char msTempCharArray[2048];
329 
330  static bool mbDataSetup;
331  static tSerializeSavedClassMap *m_mapSavedClasses;
332  static Common::Array<iSerializableType *> mvValueTypes;
333 };
334 
335 //-------------------------------------------------
336 
337 }
338 #endif // HPL_SERIALIZE_CLASS_H
Definition: AI.h:36
Definition: str.h:59
Definition: SerializeClass.h:238
Definition: array.h:52
Definition: SerializeClass.h:264
Definition: list.h:44
typename TreeT::BasicIterator iterator
Definition: stablemap.h:48
Definition: stablemap.h:43
Definition: SerializeClass.h:230
Definition: SerializeClass.h:284
Definition: ustr.h:57
Definition: SerializeClass.h:247
Definition: SerializeClass.h:205
Definition: tinyxml.h:864
Definition: list_intern.h:51
Definition: static_string.h:27