24 #ifndef UCOMMON_SYSRUNTIME
25 #ifndef _UCOMMON_PERSIST_H_
26 #define _UCOMMON_PERSIST_H_
28 #ifndef _UCOMMON_PLATFORM_H_
41 typedef class PersistObject* (*NewPersistObjectFunction) (void);
43 class __EXPORT PersistException
46 PersistException(
const std::string& reason);
47 const std::string& getString()
const;
49 virtual ~PersistException() throw();
73 registration(
const char* name, NewPersistObjectFunction func);
82 static void add(
const char* name, NewPersistObjectFunction construction);
87 static void remove(
const char* name);
96 typedef std::map<std::string,NewPersistObjectFunction> StringFunctionMap;
104 #define DECLARE_PERSISTENCE(ClassType) \
106 friend ucommon::PersistEngine& operator>>( ucommon::PersistEngine& ar, ClassType *&ob); \
107 friend ucommon::PersistEngine& operator<<( ucommon::PersistEngine& ar, ClassType const &ob); \
108 friend ucommon::PersistObject *createNew##ClassType(); \
109 virtual const char* getPersistenceID() const; \
110 static ucommon::TypeManager::Registration registrationFor##ClassType;
112 #define IMPLEMENT_PERSISTENCE(ClassType, FullyQualifiedName) \
113 ucommon::PersistObject *createNew##ClassType() { return new ClassType; } \
114 const char* ClassType::getPersistenceID() const {return FullyQualifiedName;} \
115 ucommon::PersistEngine& operator>>(ucommon::PersistEngine& ar, ClassType &ob) \
116 { ar >> (ucommon::PersistObject &) ob; return ar; } \
117 ucommon::PersistEngine& operator>>(ucommon::PersistEngine& ar, ClassType *&ob) \
118 { ar >> (ucommon::PersistObject *&) ob; return ar; } \
119 ucommon::PersistEngine& operator<<(ucommon::PersistEngine& ar, ClassType const &ob) \
120 { ar << (ucommon::PersistObject const *)&ob; return ar; } \
121 ucommon::TypeManager::Registration \
122 ClassType::registrationFor##ClassType(FullyQualifiedName, \
123 createNew##ClassType);
164 virtual const char* getPersistenceID()
const;
205 PersistEngine(std::iostream& stream, EngineMode mode)
throw(PersistException);
220 void write(
const PersistObject *
object)
throw(PersistException);
224 #define CCXX_ENGINEWRITE_REF(valref) writeBinary((const uint8_t*)&valref,sizeof(valref))
225 inline void write(int8_t i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
226 inline void write(uint8_t i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
227 inline void write(int16_t i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
228 inline void write(uint16_t i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
229 inline void write(int32_t i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
230 inline void write(uint32_t i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
231 inline void write(
float i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
232 inline void write(
double i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
233 inline void write(
bool i)
throw(PersistException) { CCXX_ENGINEWRITE_REF(i); }
234 #undef CCXX_ENGINEWRITE_REF
236 void write(
const std::string& str)
throw(PersistException);
239 void writeBinary(
const uint8_t* data,
const uint32_t size)
throw(PersistException);
246 void read(PersistObject &
object)
throw(PersistException);
251 void read(PersistObject *&
object)
throw(PersistException);
255 #define CCXX_ENGINEREAD_REF(valref) readBinary((uint8_t*)&valref,sizeof(valref))
256 inline void read(int8_t& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
257 inline void read(uint8_t& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
258 inline void read(int16_t& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
259 inline void read(uint16_t& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
260 inline void read(int32_t& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
261 inline void read(uint32_t& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
262 inline void read(
float& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
263 inline void read(
double& i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
264 inline void read(
bool &i)
throw(PersistException) { CCXX_ENGINEREAD_REF(i); }
265 #undef CCXX_ENGINEREAD_REF
267 void read(std::string& str)
throw(PersistException);
270 void readBinary(uint8_t* data, uint32_t size)
throw(PersistException);
277 void readObject(PersistObject*
object)
throw(PersistException);
282 const std::string readClass() throw(PersistException);
288 std::iostream& myUnderlyingStream;
293 EngineMode myOperationalMode;
298 typedef
std::vector<PersistObject*> ArchiveVector;
299 typedef
std::map<PersistObject const*, int32_t> ArchiveMap;
300 typedef
std::vector<
std::
string> ClassVector;
301 typedef
std::map<
std::
string, int32_t> ClassMap;
303 ArchiveVector myArchiveVector;
304 ArchiveMap myArchiveMap;
305 ClassVector myClassVector;
309 #define CCXX_RE(ar,ob) ar.read(ob); return ar
310 #define CCXX_WE(ar,ob) ar.write(ob); return ar
385 PersistEngine& operator <<( PersistEngine& ar, typename std::vector<T>
const& ob)
throw(PersistException)
387 ar << (uint32_t)ob.size();
388 for(
unsigned int i=0; i < ob.size(); ++i)
405 for(uint32_t i=0; i < siz; ++i)
416 PersistEngine& operator <<( PersistEngine& ar, typename std::deque<T>
const& ob)
throw(PersistException)
418 ar << (uint32_t)ob.size();
419 for(
typename std::deque<T>::const_iterator it=ob.begin(); it != ob.end(); ++it)
436 for(uint32_t i=0; i < siz; ++i) {
450 template<
class Key,
class Value>
451 PersistEngine& operator <<( PersistEngine& ar, typename std::map<Key,Value>
const & ob)
throw(PersistException)
453 ar << (uint32_t)ob.size();
454 for(
typename std::map<Key,Value>::const_iterator it = ob.begin();it != ob.end();++it)
455 ar << it->first << it->second;
464 template<
class Key,
class Value>
470 for(uint32_t i=0; i < siz; ++i) {
482 template<
class x,
class y>
483 PersistEngine& operator <<( PersistEngine& ar, std::pair<x,y> &ob)
throw(PersistException)
485 ar << ob.first << ob.second;
493 template<
class x,
class y>
496 ar >> ob.first >> ob.second;
Various miscellaneous platform specific headers and defines.
Type manager for persistence engine.
EngineMode
These are the modes the Persistence::Engine can work in.
void write(const PersistObject &object)
writes a PersistObject from a reference.
This manages a registration to the typemanager - attempting to remove problems with the optimizers...
Common namespace for all ucommon objects.
Stream serialization of persistent classes.