00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef __CCXX_PERSIST_H__
00042 #define __CCXX_PERSIST_H__
00043
00044 #ifndef __CCXX_CONFIG_H__
00045 #include <cc++/config.h>
00046 #endif
00047
00048 #ifndef __CCXX_MACROS_H__
00049 #include <cc++/macros.h>
00050 #else
00051 #ifdef __CCXX_NAMESPACE_H__
00052 #include <cc++/macros.h>
00053 #endif
00054 #endif
00055
00056 #ifdef HAVE_ZLIB_H
00057 #ifndef NO_COMPRESSION
00058 #include <zlib.h>
00059 #endif
00060 #else
00061 #define NO_COMPRESSION
00062 #endif
00063
00064 #include <iostream>
00065 #include <string>
00066 #include <vector>
00067 #include <map>
00068
00069 #if defined(__WIN32__)
00070 class __EXPORT PersistException;
00071 class __EXPORT Engine;
00072 class __EXPORT TypeManager;
00073 class __EXPORT BaseObject;
00074 #endif
00075
00076 class PersistException
00077 {
00078 public:
00079 __MEMBER_EXPORT PersistException(const std::string& reason);
00080 __MEMBER_EXPORT const std::string& GetMessage() const;
00081 __MEMBER_EXPORT virtual ~PersistException();
00082 protected:
00083 std::string myMessage;
00084 };
00085
00086
00087 typedef class BaseObject* (*NewBaseObjectFunction) (void);
00088
00097 class TypeManager
00098 {
00099 public:
00100
00105 class Registration
00106 {
00107 public:
00108 Registration(const char* name, NewBaseObjectFunction func)
00109 : myName(name) { TypeManager::Add(name,func); }
00110 ~Registration() { TypeManager::Remove(myName.c_str()); }
00111 private:
00112 std::string myName;
00113 };
00114
00118 __MEMBER_EXPORT static void Add(const char* name, NewBaseObjectFunction construction);
00119
00123 __MEMBER_EXPORT static void Remove(const char* name);
00124
00130 static BaseObject* CreateInstanceOf(const char* name);
00131
00132 typedef std::map<std::string,NewBaseObjectFunction> StringFunctionMap;
00133 };
00134
00135
00136
00137
00138
00139
00140
00141 #define DECLARE_PERSISTENCE(ClassType) \
00142 public: \
00143 friend Engine& operator>>(Engine& ar, ClassType *&ob); \
00144 friend Engine& operator<<(Engine& ar, ClassType const *&ob); \
00145 friend BaseObject *CreateNew##ClassType(); \
00146 virtual const char* GetPersistenceID() const; \
00147 static TypeManager::Registration RegistrationFor##ClassType;
00148
00149 #define IMPLEMENT_PERSISTENCE(ClassType,FullyQualifiedName) \
00150 BaseObject *CreateNew##ClassType() { return new ClassType; } \
00151 const char* ClassType::GetPersistenceID()const {return FullyQualifiedName;} \
00152 Engine& operator>>(Engine& ar, ClassType *&ob) \
00153 { ar >> (BaseObject *&) ob; return ar; } \
00154 Engine& operator<<(Engine& ar, ClassType const *ob) \
00155 { ar << (BaseObject const *)ob; return ar; } \
00156 TypeManager::Registration \
00157 ClassType::RegistrationFor##ClassType(FullyQualifiedName, \
00158 CreateNew##ClassType);
00159
00160 class Engine;
00161
00176 class __EXPORT BaseObject
00177 {
00178 public:
00184 BaseObject();
00185
00189 virtual ~BaseObject();
00190
00194 virtual const char* GetPersistenceID() const;
00195
00201 virtual bool Write(Engine& archive) const;
00202
00208 virtual bool Read(Engine& archive);
00209 };
00210
00211
00222 class Engine
00223 {
00224 public:
00229 class Exception : public PersistException
00230 {
00231 public:
00232 Exception(const std::string &reason) : PersistException(reason) {}
00233 };
00234
00238 enum EngineMode
00239 {
00240 modeRead,
00241 modeWrite
00242 };
00243
00249 __MEMBER_EXPORT Engine(std::iostream& stream, EngineMode mode) THROWS (PersistException);
00250
00255 __MEMBER_EXPORT ~Engine();
00256
00257
00258
00259 void Write(const BaseObject *object) THROWS (Exception);
00260
00261
00262
00263 #define _ENGINEWRITE_REF(valref) WriteBinary((const uint8*)&valref,sizeof(valref))
00264 void Write(int8 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00265 void Write(uint8 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00266 void Write(int16 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00267 void Write(uint16 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00268 void Write(int32 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00269 void Write(uint32 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00270 void Write(int64 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00271 void Write(uint64 i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00272 void Write(float i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00273 void Write(double i) THROWS (Exception) { _ENGINEWRITE_REF(i); }
00274 #undef _ENGINEWRITE_REF
00275
00276 void Write(const std::string& str) THROWS (Exception);
00277
00278 void WriteBinary(const uint8* data, const uint32 size) THROWS (Exception);
00279
00280
00281
00282 void Read(BaseObject *&object) THROWS (Exception);
00283 #define _ENGINEREAD_REF(valref) ReadBinary((uint8*)&valref,sizeof(valref))
00284 void Read(int8& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00285 void Read(uint8& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00286 void Read(int16& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00287 void Read(uint16& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00288 void Read(int32& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00289 void Read(uint32& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00290 void Read(int64& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00291 void Read(uint64& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00292 void Read(float& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00293 void Read(double& i) THROWS (Exception) { _ENGINEREAD_REF(i); }
00294 #undef _ENGINEREAD_REF
00295
00296 void Read(std::string& str) THROWS (Exception);
00297
00298 void ReadBinary(uint8* data, uint32 size) THROWS (Exception);
00299
00300 private:
00304 std::iostream& myUnderlyingStream;
00305
00309 EngineMode myOperationalMode;
00310
00314 typedef std::vector<BaseObject*> ArchiveVector;
00315 typedef std::map<BaseObject const*, int32> ArchiveMap;
00316 typedef std::vector<std::string> ClassVector;
00317 typedef std::map<std::string, int32> ClassMap;
00318
00319 ArchiveVector myArchiveVector;
00320 ArchiveMap myArchiveMap;
00321 ClassVector myClassVector;
00322 ClassMap myClassMap;
00323
00324
00325 #ifndef NO_COMPRESSION
00326 z_stream myZStream;
00327 uint8* myCompressedDataBuffer;
00328 uint8* myUncompressedDataBuffer;
00329 uint8* myLastUncompressedDataRead;
00330 #endif
00331 };
00332
00333
00334 __EXPORT Engine& operator >>( Engine& ar, BaseObject *&ob) THROWS (Engine::Exception);
00335 __EXPORT Engine& operator <<( Engine& ar, BaseObject const *ob) THROWS (Engine::Exception);
00336
00337 __EXPORT Engine& operator >>( Engine& ar, int8& ob) THROWS (Engine::Exception);
00338 __EXPORT Engine& operator <<( Engine& ar, int8 ob) THROWS (Engine::Exception);
00339
00340 __EXPORT Engine& operator >>( Engine& ar, uint8& ob) THROWS (Engine::Exception);
00341 __EXPORT Engine& operator <<( Engine& ar, uint8 ob) THROWS (Engine::Exception);
00342
00343 __EXPORT Engine& operator >>( Engine& ar, int16& ob) THROWS (Engine::Exception);
00344 __EXPORT Engine& operator <<( Engine& ar, int16 ob) THROWS (Engine::Exception);
00345
00346 __EXPORT Engine& operator >>( Engine& ar, uint16& ob) THROWS (Engine::Exception);
00347 __EXPORT Engine& operator <<( Engine& ar, uint16 ob) THROWS (Engine::Exception);
00348
00349 __EXPORT Engine& operator >>( Engine& ar, int32& ob) THROWS (Engine::Exception);
00350 __EXPORT Engine& operator <<( Engine& ar, int32 ob) THROWS (Engine::Exception);
00351
00352 __EXPORT Engine& operator >>( Engine& ar, uint32& ob) THROWS (Engine::Exception);
00353 __EXPORT Engine& operator <<( Engine& ar, uint32 ob) THROWS (Engine::Exception);
00354
00355 __EXPORT Engine& operator >>( Engine& ar, int64& ob) THROWS (Engine::Exception);
00356 __EXPORT Engine& operator <<( Engine& ar, int64 ob) THROWS (Engine::Exception);
00357
00358 __EXPORT Engine& operator >>( Engine& ar, uint64& ob) THROWS (Engine::Exception);
00359 __EXPORT Engine& operator <<( Engine& ar, uint64 ob) THROWS (Engine::Exception);
00360
00361 __EXPORT Engine& operator >>( Engine& ar, float& ob) THROWS (Engine::Exception);
00362 __EXPORT Engine& operator <<( Engine& ar, float ob) THROWS (Engine::Exception);
00363
00364 __EXPORT Engine& operator >>( Engine& ar, double& ob) THROWS (Engine::Exception);
00365 __EXPORT Engine& operator <<( Engine& ar, double ob) THROWS (Engine::Exception);
00366
00367 __EXPORT Engine& operator >>( Engine& ar, std::string& ob) THROWS (Engine::Exception);
00368 __EXPORT Engine& operator <<( Engine& ar, std::string ob) THROWS (Engine::Exception);
00369
00370 __EXPORT Engine& operator >>( Engine& ar, bool& ob) THROWS (Engine::Exception);
00371 __EXPORT Engine& operator <<( Engine& ar, bool ob) THROWS (Engine::Exception);
00372
00381 template<class T>
00382 Engine& operator <<( Engine& ar, std::vector<T> const& ob) THROWS (Engine::Exception)
00383 {
00384 ar << (uint32)ob.size();
00385 for(uint i=0; i < ob.size(); ++i)
00386 ar << ob[i];
00387 return ar;
00388 }
00389
00394 template<class T>
00395 Engine& operator >>( Engine& ar, std::vector<T>& ob) THROWS (Engine::Exception)
00396 {
00397 ob.clear();
00398 uint32 siz;
00399 ar >> siz;
00400 ob.resize(siz);
00401 for(uint32 i=0; i < siz; ++i)
00402 ar >> ob[i];
00403 return ar;
00404 }
00405
00410 template<class Key, class Value>
00411 Engine& operator <<( Engine& ar, std::map<Key,Value> const & ob) THROWS (Engine::Exception)
00412 {
00413 ar << (uint32)ob.size();
00414 for(std::map<Key,Value>::const_iterator it = ob.begin();it != ob.end();++it)
00415 ar << it->first << it->second;
00416 return ar;
00417 }
00418
00423 template<class Key, class Value>
00424 Engine& operator >>( Engine& ar, std::map<Key,Value>& ob) THROWS (Engine::Exception)
00425 {
00426 ob.clear();
00427 uint32 siz;
00428 ar >> siz;
00429 for(uint32 i=0; i < siz; ++i) {
00430 Key a;
00431 ar >> a;
00432 ar >> ob[a];
00433 }
00434 return ar;
00435 }
00436
00437 #ifdef __CCXX_NAMESPACE_H__
00438 #undef __CCXX_NAMESPACE_H__
00439 #include <cc++/namespace.h>
00440 #endif
00441
00442 #endif
00443