Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

persist.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 // 
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however    
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.    
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00043 #ifndef CCXX_PERSIST_H_
00044 #define CCXX_PERSIST_H_
00045 
00046 #ifndef CCXX_CONFIG_H_
00047 #include <cc++/config.h>
00048 #endif
00049 
00050 #ifndef CCXX_EXCEPTIONS_H_
00051 #include <cc++/exception.h>
00052 #endif
00053 
00054 #ifndef CCXX_MISSING_H_
00055 #include <cc++/missing.h>
00056 #endif
00057 
00058 #ifndef CCXX_STRING_H_
00059 #include <cc++/string.h>
00060 #endif
00061 
00062 #ifdef HAVE_ZLIB_H
00063 #ifndef NO_COMPRESSION
00064 #include <zlib.h>
00065 #endif
00066 #else
00067 #define NO_COMPRESSION
00068 #endif
00069 
00070 #include <iostream>
00071 #include <string>
00072 #include <vector>
00073 #include <deque>
00074 #include <map>
00075 
00076 #ifdef CCXX_NAMESPACES
00077 namespace ost {
00078 #define NS_PREFIX ost::
00079 #else
00080 #define NS_PREFIX
00081 #endif
00082 
00083 #ifdef  CCXX_EXCEPTIONS
00084 #ifdef  COMMON_STD_EXCEPTION
00085 
00086 class __EXPORT PersistException : public Exception
00087 {
00088 public:
00089         PersistException(const String &what) : Exception(what) {};
00090 };
00091 
00092 #else
00093 
00094 class __EXPORT PersistException
00095 {
00096 public:
00097         PersistException(const String& reason);
00098         inline const String& getString() const
00099                 {return Exception::getString();};
00100 
00101         virtual ~PersistException() {} throw();
00102 protected:
00103         String _what;
00104 };
00105 
00106 #endif
00107 #endif
00108 
00109 // This typedef allows us to declare NewBaseObjectFunction now
00110 typedef class BaseObject* (*NewBaseObjectFunction) (void);
00111 
00120 class __EXPORT TypeManager
00121 {
00122 public:
00123         
00128         class Registration
00129         {
00130         public:
00131                 Registration(const char* name, NewBaseObjectFunction func);
00132                 virtual ~Registration();
00133         private:
00134                 String myName;
00135         };
00136         
00140         static void add(const char* name, NewBaseObjectFunction construction);
00141         
00145         static void remove(const char* name);
00146         
00152         static BaseObject* createInstanceOf(const char* name);
00153         
00154         typedef std::map<String,NewBaseObjectFunction> StringFunctionMap;
00155 };
00156 
00157 
00158 /*
00159  * The following defines are used to declare and define the relevant code
00160  * to allow a class to use the Persistence::Engine code.
00161  */
00162 
00163 #define DECLARE_PERSISTENCE(ClassType)                                  \
00164   public:                                                               \
00165     friend NS_PREFIX Engine& operator>>( NS_PREFIX Engine& ar, ClassType *&ob);         \
00166     friend NS_PREFIX Engine& operator<<( NS_PREFIX Engine& ar, ClassType const &ob);    \
00167     friend NS_PREFIX BaseObject *createNew##ClassType();                                \
00168     virtual const char* getPersistenceID() const;                       \
00169     static NS_PREFIX TypeManager::Registration registrationFor##ClassType;
00170 
00171 #define IMPLEMENT_PERSISTENCE(ClassType, FullyQualifiedName)                  \
00172   NS_PREFIX BaseObject *createNew##ClassType() { return new ClassType; }                      \
00173   const char* ClassType::getPersistenceID() const {return FullyQualifiedName;} \
00174   NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType &ob)                           \
00175     { ar >> (NS_PREFIX BaseObject &) ob; return ar; }                                 \
00176   NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType *&ob)                          \
00177     { ar >> (NS_PREFIX BaseObject *&) ob; return ar; }                                \
00178   NS_PREFIX Engine& operator<<(NS_PREFIX Engine& ar, ClassType const &ob)                             \
00179     { ar << (NS_PREFIX BaseObject const *)&ob; return ar; }                           \
00180   NS_PREFIX TypeManager::Registration                                                 \
00181         ClassType::registrationFor##ClassType(FullyQualifiedName,             \
00182                                               createNew##ClassType);
00183 
00184 class Engine;
00185 
00205 class __EXPORT BaseObject
00206 {
00207 public:
00213          BaseObject();
00214         
00218          virtual ~BaseObject();
00219         
00223          virtual const char* getPersistenceID() const;
00224         
00230          virtual bool write(Engine& archive) const;
00231 
00237          virtual bool read(Engine& archive);
00238 };
00239 
00240 
00251 class __EXPORT Engine
00252 {
00253 public:
00258 #ifdef  CCXX_EXCEPTIONS
00259         class Exception : public PersistException
00260         {
00261         public:
00262                 Exception(const String &reason);
00263         };
00264 #endif
00265         
00269         enum EngineMode
00270         {
00271                 modeRead,
00272                 modeWrite
00273         };
00274         
00280         Engine(std::iostream& stream, EngineMode mode) THROWS (PersistException);
00281         
00286   void sync();
00287   
00288         virtual ~Engine();
00289 
00290 
00291         // Write operations
00292 
00296         void write(const BaseObject &object) THROWS (Exception)
00297         { write(&object); };
00298 
00302         void write(const BaseObject *object) THROWS (Exception);
00303 
00304         // writes supported primitive types
00305   // shortcut, to make the following more readable
00306 #define CCXX_ENGINEWRITE_REF(valref) writeBinary((const uint8*)&valref,sizeof(valref))
00307         void write(int8 i)   THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00308         void write(uint8 i)  THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00309         void write(int16 i)  THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00310         void write(uint16 i) THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00311         void write(int32 i)  THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00312         void write(uint32 i) THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00313 #ifdef  HAVE_64_BITS
00314         void write(int64 i)  THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00315         void write(uint64 i) THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00316 #endif
00317         void write(float i)  THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00318         void write(double i) THROWS (Exception) { CCXX_ENGINEWRITE_REF(i); }
00319 #undef CCXX_ENGINEWRITE_REF
00320 
00321         void write(const String& str) THROWS (Exception);
00322         void write(const std::string& str) THROWS (Exception);
00323 
00324         // Every write operation boils down to one or more of these
00325         void writeBinary(const uint8* data, const uint32 size) THROWS (Exception);
00326 
00327 
00328         // Read Operations
00329 
00333         void read(BaseObject &object) THROWS (Exception);
00334 
00338         void read(BaseObject *&object) THROWS (Exception);
00339 
00340         // reads supported primitive types
00341   // shortcut, to make the following more readable
00342 #define CCXX_ENGINEREAD_REF(valref) readBinary((uint8*)&valref,sizeof(valref))
00343         void read(int8& i)   THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00344         void read(uint8& i)  THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00345         void read(int16& i)  THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00346         void read(uint16& i) THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00347         void read(int32& i)  THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00348         void read(uint32& i) THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00349 #ifdef  HAVE_64_BITS
00350         void read(int64& i)  THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00351         void read(uint64& i) THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00352 #endif
00353         void read(float& i)  THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00354         void read(double& i) THROWS (Exception) { CCXX_ENGINEREAD_REF(i); }
00355 #undef CCXX_ENGINEREAD_REF
00356 
00357         void read(String& str) THROWS (Exception);
00358         void read(std::string& str) THROWS (Exception);
00359 
00360         // Every read operation boild down to one or more of these
00361         void readBinary(uint8* data, uint32 size) THROWS (Exception);
00362 
00363 private:
00368   void readObject(BaseObject* object);
00369 
00373   const String readClass();
00374 
00375 
00379         std::iostream& myUnderlyingStream;
00380         
00384         EngineMode myOperationalMode;
00385 
00389         typedef std::vector<BaseObject*>           ArchiveVector;
00390         typedef std::map<BaseObject const*, int32> ArchiveMap;
00391         typedef std::vector<String>                ClassVector;
00392         typedef std::map<String, int32>            ClassMap;
00393         
00394         ArchiveVector myArchiveVector;
00395         ArchiveMap myArchiveMap;
00396         ClassVector myClassVector;
00397         ClassMap myClassMap;
00398 
00399         // Compression support
00400 #ifndef NO_COMPRESSION
00401         z_stream myZStream;
00402         uint8* myCompressedDataBuffer;
00403         uint8* myUncompressedDataBuffer;
00404         uint8* myLastUncompressedDataRead;
00405 #endif
00406 };
00407 
00408 // Standard >> and << stream operators for BaseObject
00410 __EXPORT Engine& operator >>( Engine& ar, BaseObject &ob)      THROWS (Engine::Exception);
00412 __EXPORT Engine& operator >>( Engine& ar, BaseObject *&ob)      THROWS (Engine::Exception);
00414 __EXPORT Engine& operator <<( Engine& ar, BaseObject const &ob) THROWS (Engine::Exception);
00416 __EXPORT Engine& operator <<( Engine& ar, BaseObject const *ob) THROWS (Engine::Exception);
00417 
00419 __EXPORT Engine& operator >>( Engine& ar, int8& ob) THROWS (Engine::Exception);
00421 __EXPORT Engine& operator <<( Engine& ar, int8 ob)  THROWS (Engine::Exception);
00422 
00424 __EXPORT Engine& operator >>( Engine& ar, uint8& ob) THROWS (Engine::Exception);
00426 __EXPORT Engine& operator <<( Engine& ar, uint8 ob)  THROWS (Engine::Exception);
00427 
00429 __EXPORT Engine& operator >>( Engine& ar, int16& ob) THROWS (Engine::Exception);
00431 __EXPORT Engine& operator <<( Engine& ar, int16 ob)  THROWS (Engine::Exception);
00432 
00434 __EXPORT Engine& operator >>( Engine& ar, uint16& ob) THROWS (Engine::Exception);
00436 __EXPORT Engine& operator <<( Engine& ar, uint16 ob)  THROWS (Engine::Exception);
00437 
00439 __EXPORT Engine& operator >>( Engine& ar, int32& ob) THROWS (Engine::Exception);
00441 __EXPORT Engine& operator <<( Engine& ar, int32 ob)  THROWS (Engine::Exception);
00442 
00444 __EXPORT Engine& operator >>( Engine& ar, uint32& ob) THROWS (Engine::Exception);
00446 __EXPORT Engine& operator <<( Engine& ar, uint32 ob)  THROWS (Engine::Exception);
00447 
00448 #ifdef  HAVE_64_BITS
00449 
00450 __EXPORT Engine& operator >>( Engine& ar, int64& ob) THROWS (Engine::Exception);
00452 __EXPORT Engine& operator <<( Engine& ar, int64 ob)  THROWS (Engine::Exception);
00453 
00455 __EXPORT Engine& operator >>( Engine& ar, uint64& ob) THROWS (Engine::Exception);
00457 __EXPORT Engine& operator <<( Engine& ar, uint64 ob)  THROWS (Engine::Exception);
00458 #endif
00459 
00461 __EXPORT Engine& operator >>( Engine& ar, float& ob) THROWS (Engine::Exception);
00463 __EXPORT Engine& operator <<( Engine& ar, float ob)  THROWS (Engine::Exception);
00464 
00466 __EXPORT Engine& operator >>( Engine& ar, double& ob) THROWS (Engine::Exception);
00468 __EXPORT Engine& operator <<( Engine& ar, double ob)  THROWS (Engine::Exception);
00469 
00471 __EXPORT Engine& operator >>( Engine& ar, String& ob) THROWS (Engine::Exception);
00473 __EXPORT Engine& operator <<( Engine& ar, String ob)  THROWS (Engine::Exception);
00474 
00476 __EXPORT Engine& operator >>( Engine& ar, std::string& ob) THROWS (Engine::Exception);
00478 __EXPORT Engine& operator <<( Engine& ar, std::string ob)  THROWS (Engine::Exception);
00479 
00481 __EXPORT Engine& operator >>( Engine& ar, bool& ob) THROWS (Engine::Exception);
00483 __EXPORT Engine& operator <<( Engine& ar, bool ob)  THROWS (Engine::Exception);
00484 
00494 template<class T>
00495 Engine& operator <<( Engine& ar, typename std::vector<T> const& ob) THROWS (Engine::Exception)
00496 {
00497         ar << (uint32)ob.size();
00498         for(unsigned int i=0; i < ob.size(); ++i)
00499                 ar << ob[i];
00500         return ar;
00501 }
00502 
00508 template<class T>
00509 Engine& operator >>( Engine& ar, typename std::vector<T>& ob) THROWS (Engine::Exception)
00510 {
00511         ob.clear();
00512         uint32 siz;
00513         ar >> siz;
00514         ob.resize(siz);
00515         for(uint32 i=0; i < siz; ++i)
00516                 ar >> ob[i];
00517         return ar;
00518 }
00519 
00525 template<class T>
00526 Engine& operator <<( Engine& ar, typename std::deque<T> const& ob) THROWS (Engine::Exception)
00527 {
00528         ar << (uint32)ob.size();
00529   for(typename std::deque<T>::const_iterator it=ob.begin(); it != ob.end(); ++it)
00530                 ar << *it;
00531         return ar;
00532 }
00533 
00539 template<class T>
00540 Engine& operator >>( Engine& ar, typename std::deque<T>& ob) THROWS (Engine::Exception)
00541 {
00542         ob.clear();
00543         uint32 siz;
00544         ar >> siz;
00545         //ob.resize(siz);
00546         for(uint32 i=0; i < siz; ++i)
00547   {
00548     T node;
00549     ar >> node;
00550     ob.push_back(node);
00551                 //ar >> ob[i];
00552   }
00553         return ar;
00554 }
00555 
00561 template<class Key, class Value>
00562 Engine& operator <<( Engine& ar, typename std::map<Key,Value> const & ob) THROWS (Engine::Exception)
00563 {
00564         ar << (uint32)ob.size();
00565         for(typename std::map<Key,Value>::const_iterator it = ob.begin();it != ob.end();++it)
00566                 ar << it->first << it->second;
00567         return ar;
00568 }
00569 
00575 template<class Key, class Value>
00576 Engine& operator >>( Engine& ar, typename std::map<Key,Value>& ob) THROWS (Engine::Exception)
00577 {
00578         ob.clear();
00579         uint32 siz;
00580         ar >> siz;
00581         for(uint32 i=0; i < siz; ++i) {
00582                 Key a;
00583                 ar >> a;
00584                 ar >> ob[a];
00585         }
00586         return ar;
00587 }
00588 
00593 template<class x, class y>
00594 Engine& operator <<( Engine& ar, std::pair<x,y> &ob) THROWS (Engine::Exception)
00595 {
00596         ar << ob.first << ob.second;
00597         return ar;
00598 }
00599 
00604 template<class x, class y>
00605 Engine& operator >>(Engine& ar, std::pair<x, y> &ob) THROWS (Engine::Exception)
00606 {
00607         ar >> ob.first >> ob.second;
00608         return ar;
00609 }
00610 
00611 #ifdef  CCXX_NAMESPACES
00612 }
00613 #endif
00614 
00615 #endif
00616 

Generated on Fri Dec 9 22:30:36 2005 for GNU CommonC++ by  doxygen 1.4.4