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
00044 #ifndef CCXX_PERSIST_H_
00045 #define CCXX_PERSIST_H_
00046
00047 #ifndef CCXX_CONFIG_H_
00048 #include <cc++/config.h>
00049 #endif
00050
00051 #ifndef CCXX_EXCEPTIONS_H_
00052 #include <cc++/exception.h>
00053 #endif
00054
00055 #ifndef CCXX_MISSING_H_
00056 #include <cc++/missing.h>
00057 #endif
00058
00059 #ifndef CCXX_STRING_H_
00060 #include <cc++/string.h>
00061 #endif
00062
00063 #ifdef HAVE_ZLIB_H
00064 #ifndef NO_COMPRESSION
00065 #include <zlib.h>
00066 #endif
00067 #else
00068 #define NO_COMPRESSION
00069 #endif
00070
00071 #include <iostream>
00072 #include <string>
00073 #include <vector>
00074 #include <deque>
00075 #include <map>
00076
00077 #ifdef CCXX_NAMESPACES
00078 namespace ost {
00079 #define NS_PREFIX ost::
00080 #else
00081 #define NS_PREFIX
00082 #endif
00083
00084 #ifdef CCXX_EXCEPTIONS
00085 #ifdef COMMON_STD_EXCEPTION
00086
00087 class __EXPORT PersistException : public Exception
00088 {
00089 public:
00090 PersistException(const String &what) : Exception(what) {};
00091 };
00092
00093 #else
00094
00095 class __EXPORT PersistException
00096 {
00097 public:
00098 PersistException(const String& reason);
00099 inline const String& getString() const
00100 {return Exception::getString();};
00101
00102 virtual ~PersistException() {} throw();
00103 protected:
00104 String _what;
00105 };
00106
00107 #endif
00108 #endif
00109
00110
00111 typedef class BaseObject* (*NewBaseObjectFunction) (void);
00112
00121 class __EXPORT TypeManager
00122 {
00123 public:
00124
00129 class Registration
00130 {
00131 public:
00132 Registration(const char* name, NewBaseObjectFunction func);
00133 virtual ~Registration();
00134 private:
00135 String myName;
00136 };
00137
00141 static void add(const char* name, NewBaseObjectFunction construction);
00142
00146 static void remove(const char* name);
00147
00153 static BaseObject* createInstanceOf(const char* name);
00154
00155 typedef std::map<String,NewBaseObjectFunction> StringFunctionMap;
00156 };
00157
00158
00159
00160
00161
00162
00163
00164 #define DECLARE_PERSISTENCE(ClassType) \
00165 public: \
00166 friend NS_PREFIX Engine& operator>>( NS_PREFIX Engine& ar, ClassType *&ob); \
00167 friend NS_PREFIX Engine& operator<<( NS_PREFIX Engine& ar, ClassType const &ob); \
00168 friend NS_PREFIX BaseObject *createNew##ClassType(); \
00169 virtual const char* getPersistenceID() const; \
00170 static NS_PREFIX TypeManager::Registration registrationFor##ClassType;
00171
00172 #define IMPLEMENT_PERSISTENCE(ClassType, FullyQualifiedName) \
00173 NS_PREFIX BaseObject *createNew##ClassType() { return new ClassType; } \
00174 const char* ClassType::getPersistenceID() const {return FullyQualifiedName;} \
00175 NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType &ob) \
00176 { ar >> (NS_PREFIX BaseObject &) ob; return ar; } \
00177 NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType *&ob) \
00178 { ar >> (NS_PREFIX BaseObject *&) ob; return ar; } \
00179 NS_PREFIX Engine& operator<<(NS_PREFIX Engine& ar, ClassType const &ob) \
00180 { ar << (NS_PREFIX BaseObject const *)&ob; return ar; } \
00181 NS_PREFIX TypeManager::Registration \
00182 ClassType::registrationFor##ClassType(FullyQualifiedName, \
00183 createNew##ClassType);
00184
00185 class Engine;
00186
00206 class __EXPORT BaseObject
00207 {
00208 public:
00214 BaseObject();
00215
00219 virtual ~BaseObject();
00220
00224 virtual const char* getPersistenceID() const;
00225
00231 virtual bool write(Engine& archive) const;
00232
00238 virtual bool read(Engine& archive);
00239 };
00240
00241
00252 class __EXPORT Engine
00253 {
00254 public:
00258 enum EngineMode {
00259 modeRead,
00260 modeWrite
00261 };
00262
00268 Engine(std::iostream& stream, EngineMode mode) THROWS (PersistException);
00269
00274 void sync();
00275
00276 virtual ~Engine();
00277
00278
00279
00280
00284 void write(const BaseObject &object) THROWS (PersistException)
00285 { write(&object); };
00286
00290 void write(const BaseObject *object) THROWS (PersistException);
00291
00292
00293
00294 #define CCXX_ENGINEWRITE_REF(valref) writeBinary((const uint8*)&valref,sizeof(valref))
00295 void write(int8 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00296 void write(uint8 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00297 void write(int16 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00298 void write(uint16 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00299 void write(int32 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00300 void write(uint32 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00301 #ifdef HAVE_64_BITS
00302 void write(int64 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00303 void write(uint64 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00304 #endif
00305 void write(float i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00306 void write(double i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
00307 #undef CCXX_ENGINEWRITE_REF
00308
00309 void write(const String& str) THROWS (PersistException);
00310 void write(const std::string& str) THROWS (PersistException);
00311
00312
00313 void writeBinary(const uint8* data, const uint32 size) THROWS (PersistException);
00314
00315
00316
00317
00321 void read(BaseObject &object) THROWS (PersistException);
00322
00326 void read(BaseObject *&object) THROWS (PersistException);
00327
00328
00329
00330 #define CCXX_ENGINEREAD_REF(valref) readBinary((uint8*)&valref,sizeof(valref))
00331 void read(int8& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00332 void read(uint8& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00333 void read(int16& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00334 void read(uint16& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00335 void read(int32& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00336 void read(uint32& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00337 #ifdef HAVE_64_BITS
00338 void read(int64& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00339 void read(uint64& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00340 #endif
00341 void read(float& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00342 void read(double& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
00343 #undef CCXX_ENGINEREAD_REF
00344
00345 void read(String& str) THROWS (PersistException);
00346 void read(std::string& str) THROWS (PersistException);
00347
00348
00349 void readBinary(uint8* data, uint32 size) THROWS (PersistException);
00350
00351 private:
00356 void readObject(BaseObject* object) THROWS (PersistException);
00357
00361 const String readClass() THROWS (PersistException);
00362
00363
00367 std::iostream& myUnderlyingStream;
00368
00372 EngineMode myOperationalMode;
00373
00377 typedef std::vector<BaseObject*> ArchiveVector;
00378 typedef std::map<BaseObject const*, int32> ArchiveMap;
00379 typedef std::vector<String> ClassVector;
00380 typedef std::map<String, int32> ClassMap;
00381
00382 ArchiveVector myArchiveVector;
00383 ArchiveMap myArchiveMap;
00384 ClassVector myClassVector;
00385 ClassMap myClassMap;
00386
00387
00388 #ifndef NO_COMPRESSION
00389 z_stream myZStream;
00390 uint8* myCompressedDataBuffer;
00391 uint8* myUncompressedDataBuffer;
00392 uint8* myLastUncompressedDataRead;
00393 #endif
00394 };
00395
00396
00398 __EXPORT Engine& operator >>( Engine& ar, BaseObject &ob) THROWS (PersistException);
00400 __EXPORT Engine& operator >>( Engine& ar, BaseObject *&ob) THROWS (PersistException);
00402 __EXPORT Engine& operator <<( Engine& ar, BaseObject const &ob) THROWS (PersistException);
00404 __EXPORT Engine& operator <<( Engine& ar, BaseObject const *ob) THROWS (PersistException);
00405
00407 __EXPORT Engine& operator >>( Engine& ar, int8& ob) THROWS (PersistException);
00409 __EXPORT Engine& operator <<( Engine& ar, int8 ob) THROWS (PersistException);
00410
00412 __EXPORT Engine& operator >>( Engine& ar, uint8& ob) THROWS (PersistException);
00414 __EXPORT Engine& operator <<( Engine& ar, uint8 ob) THROWS (PersistException);
00415
00417 __EXPORT Engine& operator >>( Engine& ar, int16& ob) THROWS (PersistException);
00419 __EXPORT Engine& operator <<( Engine& ar, int16 ob) THROWS (PersistException);
00420
00422 __EXPORT Engine& operator >>( Engine& ar, uint16& ob) THROWS (PersistException);
00424 __EXPORT Engine& operator <<( Engine& ar, uint16 ob) THROWS (PersistException);
00425
00427 __EXPORT Engine& operator >>( Engine& ar, int32& ob) THROWS (PersistException);
00429 __EXPORT Engine& operator <<( Engine& ar, int32 ob) THROWS (PersistException);
00430
00432 __EXPORT Engine& operator >>( Engine& ar, uint32& ob) THROWS (PersistException);
00434 __EXPORT Engine& operator <<( Engine& ar, uint32 ob) THROWS (PersistException);
00435
00436 #ifdef HAVE_64_BITS
00437
00438 __EXPORT Engine& operator >>( Engine& ar, int64& ob) THROWS (PersistException);
00440 __EXPORT Engine& operator <<( Engine& ar, int64 ob) THROWS (PersistException);
00441
00443 __EXPORT Engine& operator >>( Engine& ar, uint64& ob) THROWS (PersistException);
00445 __EXPORT Engine& operator <<( Engine& ar, uint64 ob) THROWS (PersistException);
00446 #endif
00447
00449 __EXPORT Engine& operator >>( Engine& ar, float& ob) THROWS (PersistException);
00451 __EXPORT Engine& operator <<( Engine& ar, float ob) THROWS (PersistException);
00452
00454 __EXPORT Engine& operator >>( Engine& ar, double& ob) THROWS (PersistException);
00456 __EXPORT Engine& operator <<( Engine& ar, double ob) THROWS (PersistException);
00457
00459 __EXPORT Engine& operator >>( Engine& ar, String& ob) THROWS (PersistException);
00461 __EXPORT Engine& operator <<( Engine& ar, String ob) THROWS (PersistException);
00462
00464 __EXPORT Engine& operator >>( Engine& ar, std::string& ob) THROWS (PersistException);
00466 __EXPORT Engine& operator <<( Engine& ar, std::string ob) THROWS (PersistException);
00467
00469 __EXPORT Engine& operator >>( Engine& ar, bool& ob) THROWS (PersistException);
00471 __EXPORT Engine& operator <<( Engine& ar, bool ob) THROWS (PersistException);
00472
00482 template<class T>
00483 Engine& operator <<( Engine& ar, typename std::vector<T> const& ob) THROWS (PersistException)
00484 {
00485 ar << (uint32)ob.size();
00486 for(unsigned int i=0; i < ob.size(); ++i)
00487 ar << ob[i];
00488 return ar;
00489 }
00490
00496 template<class T>
00497 Engine& operator >>( Engine& ar, typename std::vector<T>& ob) THROWS (PersistException)
00498 {
00499 ob.clear();
00500 uint32 siz;
00501 ar >> siz;
00502 ob.resize(siz);
00503 for(uint32 i=0; i < siz; ++i)
00504 ar >> ob[i];
00505 return ar;
00506 }
00507
00513 template<class T>
00514 Engine& operator <<( Engine& ar, typename std::deque<T> const& ob) THROWS (PersistException)
00515 {
00516 ar << (uint32)ob.size();
00517 for(typename std::deque<T>::const_iterator it=ob.begin(); it != ob.end(); ++it)
00518 ar << *it;
00519 return ar;
00520 }
00521
00527 template<class T>
00528 Engine& operator >>( Engine& ar, typename std::deque<T>& ob) THROWS (PersistException)
00529 {
00530 ob.clear();
00531 uint32 siz;
00532 ar >> siz;
00533
00534 for(uint32 i=0; i < siz; ++i) {
00535 T node;
00536 ar >> node;
00537 ob.push_back(node);
00538
00539 }
00540 return ar;
00541 }
00542
00548 template<class Key, class Value>
00549 Engine& operator <<( Engine& ar, typename std::map<Key,Value> const & ob) THROWS (PersistException)
00550 {
00551 ar << (uint32)ob.size();
00552 for(typename std::map<Key,Value>::const_iterator it = ob.begin();it != ob.end();++it)
00553 ar << it->first << it->second;
00554 return ar;
00555 }
00556
00562 template<class Key, class Value>
00563 Engine& operator >>( Engine& ar, typename std::map<Key,Value>& ob) THROWS (PersistException)
00564 {
00565 ob.clear();
00566 uint32 siz;
00567 ar >> siz;
00568 for(uint32 i=0; i < siz; ++i) {
00569 Key a;
00570 ar >> a;
00571 ar >> ob[a];
00572 }
00573 return ar;
00574 }
00575
00580 template<class x, class y>
00581 Engine& operator <<( Engine& ar, std::pair<x,y> &ob) THROWS (PersistException)
00582 {
00583 ar << ob.first << ob.second;
00584 return ar;
00585 }
00586
00591 template<class x, class y>
00592 Engine& operator >>(Engine& ar, std::pair<x, y> &ob) THROWS (PersistException)
00593 {
00594 ar >> ob.first >> ob.second;
00595 return ar;
00596 }
00597
00598 #ifdef CCXX_NAMESPACES
00599 }
00600 #endif
00601
00602 #endif
00603