Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
XArchive.h
1// ExArchive.h: interface for the CXArchive class.
2//
3//////////////////////////////////////////////////////////////////////
4
5#if !defined(CARCHIVE_INCLUDE_H)
6#define CARCHIVE_INCLUDE_H
7
8#ifdef _MSC_VER
9#pragma once
10#endif // _MSC_VER
11
12#include "HashMap.h"
13
14BEGIN_MOOTOOLS_NAMESPACE
15
16 class DLL_TOOLSFUNCTION CXArchive : public CXObject
17 {
18 public:
19
20 // Flag values
21 typedef enum
22 {
23 store = 0,
24 load = 1,
25 bNoFlushOnDelete = 2
26 } XArchiveFlags;
27
28 CXArchive(CXFile* pFile, unsigned int nMode, unsigned int nBufSize = 4096, void *bufferPtr = NULL);
29 ~CXArchive();
30
31 // Attributes
32 void *GetData() const;
33 void SetData(void *data);
34 bool IsLoading() const;
35 bool IsStoring() const;
36
37 CXFile *GetFile() const;
38
39 // Operations
40 SIZET Read(void *bufferPtr, SIZET maxToRead);
41 void EnsureRead(void *bufferPtr, SIZET checkReadCount);
42 void Write(const void *bufferPtr, SIZET maxToWrite);
43 void Flush();
44 void Close();
45 void Abort();
46
47 public:
48 CXArchive& operator<<(const CXObject* pOb);
49
50 CXArchive& operator>>(CXObject*& pOb);
51 CXArchive& operator>>(const CXObject*& pOb);
52
53 // save operations
54 CXArchive& operator<<(bool b);
55 CXArchive& operator<<(char by);
56 CXArchive& operator<<(wchar_t ch);
57 CXArchive& operator<<(unsigned char by);
58 CXArchive& operator<<(short w);
59 CXArchive& operator<<(unsigned short w);
61 CXArchive& operator<<(unsigned int i);
62 //CXArchive& operator<<(long l);
63 //CXArchive& operator<<(unsigned long l);
64 CXArchive& operator<<(float f);
65 CXArchive& operator<<(double d);
70
71 // load operations
72 CXArchive& operator>>(bool& b);
73 CXArchive& operator>>(char& by);
74 CXArchive& operator>>(wchar_t& ch);
75 CXArchive& operator>>(unsigned char& by);
76 CXArchive& operator>>(short& w);
77 CXArchive& operator>>(unsigned short& w);
78 CXArchive& operator>>(int& i);
79 CXArchive& operator>>(unsigned int& i);
80 //CXArchive& operator>>(long& l);
81 //CXArchive& operator>>(unsigned long& l);
82 CXArchive& operator>>(float& f);
83 CXArchive& operator>>(double& d);
84 CXArchive& operator>>(longint& dwdw);
85 CXArchive& operator>>(longuint& dwdw);
86 CXArchive& operator>>(CXStringA&);
87 CXArchive& operator>>(CXStringW&);
88
89 // XObject read/write
90 CXObject* ReadObject(const CXRuntimeClassPtr pClass);
91 void WriteObject(const CXObject* pOb);
92
93 void WriteStringLength(longuint nLength, xStringEncoding encoding);
94 longuint ReadStringLength(xStringEncoding& encoding);
95 void WriteCount(longuint count);
96 longuint ReadCount();
97
98 // advanced versioning support
99 void WriteClass(const CXRuntimeClassPtr refruntimeClass);
100 const CXRuntimeClassPtr ReadClass(const CXRuntimeClassPtr runtimeClass = NULL, unsigned int *storedClassVersion = NULL, unsigned int *objectIndex = NULL);
101 void SerializeClass(const CXRuntimeClassPtr refruntimeClass);
102
103 unsigned int GetObjectSchema() const; // only valid when reading a CXObject*
104
105 // Implementation
106 protected:
107 // archive objects cannot be copied or assigned
108 CXArchive(const CXArchive& arSrc) { XASSERT(0); };
109 void operator=(const CXArchive& arSrc) { XASSERT(0); };
110
111 CXString filename;
112 CXFile *file;
113
114 unsigned int flags;
115 bool userBuffer, directBuffer;
116
117 SIZET bufferSize;
118 unsigned char *buffer; // Allocated buffer
119 unsigned char *curBufferPos; // Current position in the buffer
120 unsigned char *maxBufferPos; // max (potentially filled) buffer and max position
121
122 void FillBuffer(SIZET nAdditionalBytesNeeded);
123
124 // reading and writing strings
125 void WriteString(LPCSTR lpsz, xStringEncoding encoding);
126 void WriteString(LPCWSTR lpsz, xStringEncoding encoding);
127
128 private:
129 typedef const void *storedPtr;
130 typedef struct loadedPtr
131 {
132 storedPtr ptr;
133 enum
134 {
135 XUndefined = 0,
136 XRuntimePtr,
137 XObjectPtr,
138 } type;
139
140 unsigned int storedVersion;
141
142 loadedPtr() : ptr(NULL), type(XUndefined), storedVersion((unsigned int)-1)
143 {};
144 loadedPtr(const CXRuntimeClassPtr runtime, unsigned int version) : ptr(runtime), type(XRuntimePtr), storedVersion(version)
145 {};
146 loadedPtr(const CXObject *object) : ptr(object), type(XObjectPtr), storedVersion((unsigned int)-1)
147 {};
148 } loadedRuntime;
149
150 unsigned int lastObjectVersion;
151 unsigned int curStoredCount;
152 CHashMap<storedPtr, const storedPtr, unsigned int> storedMap; // runtime / object map
154
155 void *customData;
156
157#ifdef MOOTOOLS_PRIVATE_DEBUG
159#endif
160 };
161
162 /////////////////////////////////////////////////////
163 // CXArchive inlines
164 inline bool CXArchive::IsLoading() const
165 {
166 return (flags & CXArchive::load) != 0;
167 }
168
169 inline bool CXArchive::IsStoring() const
170 {
171 return (flags & CXArchive::load) == 0;
172 }
173
174 inline CXFile* CXArchive::GetFile() const
175 {
176 return file;
177 }
178
179 ///////////////////////// << ////////////////////////////////////////////////////////////////
180 #define EXARCHIVE_STORE(TYPE, val) \
181 if (!IsStoring()) \
182 XThrowExceptionWithArgs(CXArchiveException, CXArchiveException::readOnly, filename); \
183 if (curBufferPos + sizeof(TYPE) > maxBufferPos) \
184 Flush(); \
185 *((TYPE *)curBufferPos) = val; \
186 curBufferPos += sizeof(TYPE); \
187 return *this;
188
189 inline CXArchive& CXArchive::operator<<(char by)
190 {
191 EXARCHIVE_STORE(char, by);
192 }
193
194 inline CXArchive& CXArchive::operator<<(unsigned char by)
195 {
196 return CXArchive::operator << (static_cast<char>(by));
197 }
198
199 inline CXArchive& CXArchive::operator<<(bool b)
200 {
201 return CXArchive::operator << ((char)(b ? 1 : 0));
202 }
203
204 inline CXArchive& CXArchive::operator<<(wchar_t tch)
205 {
206 EXARCHIVE_STORE(wchar_t, tch);
207 }
208
209 inline CXArchive& CXArchive::operator<<(longint mylongint)
210 {
211 EXARCHIVE_STORE(longint, mylongint);
212 }
213
214 inline CXArchive& CXArchive::operator<<(longuint mylonguint)
215 {
216 return CXArchive::operator << (static_cast<longint>(mylonguint));
217 }
218
219 inline CXArchive& CXArchive::operator<<(short w)
220 {
221 EXARCHIVE_STORE(short, w);
222 }
223
224 inline CXArchive& CXArchive::operator<<(unsigned short w)
225 {
226 return CXArchive::operator << (static_cast<short>(w));
227 }
228
229 inline CXArchive& CXArchive::operator<<(int i)
230 {
231 EXARCHIVE_STORE(int, i);
232 }
233
234 inline CXArchive& CXArchive::operator<<(unsigned int i)
235 {
236 return CXArchive::operator << (static_cast<int>(i));
237 }
238
239 //inline CXArchive& CXArchive::operator<<(long l)
240 //{
241 // EXARCHIVE_STORE(long, l);
242 //}
243
244 //inline CXArchive& CXArchive::operator<<(unsigned long l)
245 //{
246 // return CXArchive::operator << (static_cast<long>(l));
247 //}
248
249 inline CXArchive& CXArchive::operator<<(float f)
250 {
251 EXARCHIVE_STORE(float, f);
252 }
253
254 inline CXArchive& CXArchive::operator<<(double d)
255 {
256 EXARCHIVE_STORE(double, d);
257 }
258
259 #undef EXARCHIVE_STORE
260
261 ///////////////////////// >> ////////////////////////////////////////////////////////////////
262 #define EXARCHIVE_LOAD(TYPE, val) \
263 if(!IsLoading()) \
264 XThrowExceptionWithArgs(CXArchiveException, CXArchiveException::writeOnly, filename); \
265 if (curBufferPos + sizeof(TYPE) > maxBufferPos) \
266 FillBuffer(sizeof(TYPE) - (SIZET)(maxBufferPos - curBufferPos)); \
267 val = *((TYPE *)curBufferPos); \
268 curBufferPos += sizeof(TYPE); \
269 return *this;
270
271 inline CXArchive& CXArchive::operator>>(char& by)
272 {
273 EXARCHIVE_LOAD(char, by);
274 }
275
276 inline CXArchive& CXArchive::operator>>(unsigned char& by)
277 {
278 EXARCHIVE_LOAD(unsigned char, by);
279 }
280
281 inline CXArchive& CXArchive::operator>>(bool& b)
282 {
283 char tmp;
284 EXARCHIVE_LOAD(char, tmp);
285 b = !!tmp;
286 }
287
288 inline CXArchive& CXArchive::operator>>(wchar_t& tch)
289 {
290 EXARCHIVE_LOAD(wchar_t, tch);
291 }
292
293 inline CXArchive& CXArchive::operator>>(longint& mylongint)
294 {
295 EXARCHIVE_LOAD(longint, mylongint);
296 }
297
298 inline CXArchive& CXArchive::operator>>(longuint& mylonguint)
299 {
300 EXARCHIVE_LOAD(longuint, mylonguint);
301 }
302
303 inline CXArchive& CXArchive::operator>>(short& s)
304 {
305 EXARCHIVE_LOAD(short, s);
306 }
307
308 inline CXArchive& CXArchive::operator>>(unsigned short& s)
309 {
310 EXARCHIVE_LOAD(unsigned short, s);
311 }
312
313 inline CXArchive& CXArchive::operator>>(int& i)
314 {
315 EXARCHIVE_LOAD(int, i);
316 }
317
318 inline CXArchive& CXArchive::operator>>(unsigned int& i)
319 {
320 EXARCHIVE_LOAD(unsigned int, i);
321 }
322
323 //inline CXArchive& CXArchive::operator>>(long& l)
324 //{
325 // EXARCHIVE_LOAD(long, l);
326 //}
327
328 //inline CXArchive& CXArchive::operator>>(unsigned long& l)
329 //{
330 // EXARCHIVE_LOAD(unsigned long, l);
331 //}
332
333 inline CXArchive& CXArchive::operator>>(float& f)
334 {
335 EXARCHIVE_LOAD(float, f);
336 }
337
338 inline CXArchive& CXArchive::operator>>(double& d)
339 {
340 EXARCHIVE_LOAD(double, d);
341 }
342
343 inline CXArchive& CXArchive::operator<<(const CXObject* pOb)
344 {
345 WriteObject(pOb);
346 return *this;
347 }
348
349 inline CXArchive& CXArchive::operator>>(CXObject*& pOb)
350 {
351 pOb = ReadObject(NULL);
352 return *this;
353 }
354
355 inline CXArchive& CXArchive::operator>>(const CXObject*& pOb)
356 {
357 pOb = ReadObject(NULL);
358 return *this;
359 }
360
361 #undef EXARCHIVE_LOAD
362 #undef SIZET
363
364END_MOOTOOLS_NAMESPACE
365
366#endif // !defined(CARCHIVE_INCLUDE_H)
Associates key to a single value through an hash table.
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Definition XArchive.h:17
CXFile is the base class for CXStdioFile, CXStdioFileEx, CXMemFile.
Definition XFile.h:42