Bayonne2 / Common C++ 2 Framework
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
persist.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_PERSIST_H_
45 #define CCXX_PERSIST_H_
46 
47 #ifndef CCXX_CONFIG_H_
48 #include <cc++/config.h>
49 #endif
50 
51 #ifndef CCXX_EXCEPTIONS_H_
52 #include <cc++/exception.h>
53 #endif
54 
55 #ifndef CCXX_MISSING_H_
56 #include <cc++/missing.h>
57 #endif
58 
59 #ifndef CCXX_STRING_H_
60 #include <cc++/string.h>
61 #endif
62 
63 #ifdef HAVE_ZLIB_H
64 #ifndef NO_COMPRESSION
65 #include <zlib.h>
66 #endif
67 #else
68 #define NO_COMPRESSION
69 #endif
70 
71 #include <iostream>
72 #include <string>
73 #include <vector>
74 #include <deque>
75 #include <map>
76 
77 #ifdef CCXX_NAMESPACES
78 namespace ost {
79 #define NS_PREFIX ost::
80 #else
81 #define NS_PREFIX
82 #endif
83 
84 #ifdef CCXX_EXCEPTIONS
85 #ifdef COMMON_STD_EXCEPTION
86 
87 class __EXPORT PersistException : public Exception
88 {
89 public:
90  PersistException(const String &what) : Exception(what) {};
91 };
92 
93 #else
94 
95 class __EXPORT PersistException
96 {
97 public:
98  PersistException(const String& reason);
99  inline const String& getString() const
100  {return Exception::getString();};
101 
102  virtual ~PersistException() {} throw();
103 protected:
104  String _what;
105 };
106 
107 #endif
108 #endif
109 
110 // This typedef allows us to declare NewBaseObjectFunction now
111 typedef class BaseObject* (*NewBaseObjectFunction) (void);
112 
122 {
123 public:
124 
130  {
131  public:
132  Registration(const char* name, NewBaseObjectFunction func);
133  virtual ~Registration();
134  private:
136  };
137 
141  static void add(const char* name, NewBaseObjectFunction construction);
142 
146  static void remove(const char* name);
147 
153  static BaseObject* createInstanceOf(const char* name);
154 
155  typedef std::map<String,NewBaseObjectFunction> StringFunctionMap;
156 };
157 
158 
159 /*
160  * The following defines are used to declare and define the relevant code
161  * to allow a class to use the Persistence::Engine code.
162  */
163 
164 #define DECLARE_PERSISTENCE(ClassType) \
165  public: \
166  friend NS_PREFIX Engine& operator>>( NS_PREFIX Engine& ar, ClassType *&ob); \
167  friend NS_PREFIX Engine& operator<<( NS_PREFIX Engine& ar, ClassType const &ob); \
168  friend NS_PREFIX BaseObject *createNew##ClassType(); \
169  virtual const char* getPersistenceID() const; \
170  static NS_PREFIX TypeManager::Registration registrationFor##ClassType;
171 
172 #define IMPLEMENT_PERSISTENCE(ClassType, FullyQualifiedName) \
173  NS_PREFIX BaseObject *createNew##ClassType() { return new ClassType; } \
174  const char* ClassType::getPersistenceID() const {return FullyQualifiedName;} \
175  NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType &ob) \
176  { ar >> (NS_PREFIX BaseObject &) ob; return ar; } \
177  NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType *&ob) \
178  { ar >> (NS_PREFIX BaseObject *&) ob; return ar; } \
179  NS_PREFIX Engine& operator<<(NS_PREFIX Engine& ar, ClassType const &ob) \
180  { ar << (NS_PREFIX BaseObject const *)&ob; return ar; } \
181  NS_PREFIX TypeManager::Registration \
182  ClassType::registrationFor##ClassType(FullyQualifiedName, \
183  createNew##ClassType);
184 
185 class Engine;
186 
207 {
208 public:
214  BaseObject();
215 
219  virtual ~BaseObject();
220 
224  virtual const char* getPersistenceID() const;
225 
231  virtual bool write(Engine& archive) const;
232 
238  virtual bool read(Engine& archive);
239 };
240 
241 
253 {
254 public:
258  enum EngineMode {
260  modeWrite
261  };
262 
269  Engine(std::iostream& stream, EngineMode mode, bool compress=true) THROWS (PersistException);
270 
275  void sync();
276 
280  bool more();
281 
282  virtual ~Engine();
283 
284 
285  // Write operations
286 
290  void write(const BaseObject &object) THROWS (PersistException)
291  { write(&object); };
292 
296  void write(const BaseObject *object) THROWS (PersistException);
297 
298  // writes supported primitive types
299  // shortcut, to make the following more readable
300 #define CCXX_ENGINEWRITE_REF(valref) writeBinary((const uint8*)&valref,sizeof(valref))
301  void write(int8 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
302  void write(uint8 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
303  void write(int16 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
304  void write(uint16 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
305  void write(int32 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
306  void write(uint32 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
307 #ifdef HAVE_64_BITS
308  void write(int64 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
309  void write(uint64 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
310 #endif
311  void write(float i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
312  void write(double i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
313 #undef CCXX_ENGINEWRITE_REF
314 
315  void write(const String& str) THROWS (PersistException);
316  void write(const std::string& str) THROWS (PersistException);
317 
318  // Every write operation boils down to one or more of these
319  void writeBinary(const uint8* data, const uint32 size) THROWS (PersistException);
320 
321 
322  // Read Operations
323 
327  void read(BaseObject &object) THROWS (PersistException);
328 
332  void read(BaseObject *&object) THROWS (PersistException);
333 
334  // reads supported primitive types
335  // shortcut, to make the following more readable
336 #define CCXX_ENGINEREAD_REF(valref) readBinary((uint8*)&valref,sizeof(valref))
337  void read(int8& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
338  void read(uint8& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
339  void read(int16& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
340  void read(uint16& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
341  void read(int32& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
342  void read(uint32& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
343 #ifdef HAVE_64_BITS
344  void read(int64& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
345  void read(uint64& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
346 #endif
347  void read(float& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
348  void read(double& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
349 #undef CCXX_ENGINEREAD_REF
350 
351  void read(String& str) THROWS (PersistException);
352  void read(std::string& str) THROWS (PersistException);
353 
354  // Every read operation boild down to one or more of these
355  void readBinary(uint8* data, uint32 size) THROWS (PersistException);
356 
357 private:
362  void readObject(BaseObject* object) THROWS (PersistException);
363 
367  const String readClass() THROWS (PersistException);
368 
369 
373  std::iostream& myUnderlyingStream;
374 
378  EngineMode myOperationalMode;
379 
383  typedef std::vector<BaseObject*> ArchiveVector;
384  typedef std::map<BaseObject const*, int32> ArchiveMap;
385  typedef std::vector<String> ClassVector;
386  typedef std::map<String, int32> ClassMap;
387 
388  ArchiveVector myArchiveVector;
389  ArchiveMap myArchiveMap;
390  ClassVector myClassVector;
391  ClassMap myClassMap;
392 
393  // Compression support
394  bool use_compression; // valid onlry if NO_COMPRESSION is false
395 #ifndef NO_COMPRESSION
396  z_stream myZStream;
397  uint8* myCompressedDataBuffer;
398  uint8* myUncompressedDataBuffer;
399  uint8* myLastUncompressedDataRead;
400 #endif
401 };
402 
403 // Standard >> and << stream operators for BaseObject
405 __EXPORT Engine& operator >>( Engine& ar, BaseObject &ob) THROWS (PersistException);
407 __EXPORT Engine& operator >>( Engine& ar, BaseObject *&ob) THROWS (PersistException);
409 __EXPORT Engine& operator <<( Engine& ar, BaseObject const &ob) THROWS (PersistException);
411 __EXPORT Engine& operator <<( Engine& ar, BaseObject const *ob) THROWS (PersistException);
412 
414 __EXPORT Engine& operator >>( Engine& ar, int8& ob) THROWS (PersistException);
416 __EXPORT Engine& operator <<( Engine& ar, int8 ob) THROWS (PersistException);
417 
419 __EXPORT Engine& operator >>( Engine& ar, uint8& ob) THROWS (PersistException);
421 __EXPORT Engine& operator <<( Engine& ar, uint8 ob) THROWS (PersistException);
422 
424 __EXPORT Engine& operator >>( Engine& ar, int16& ob) THROWS (PersistException);
426 __EXPORT Engine& operator <<( Engine& ar, int16 ob) THROWS (PersistException);
427 
429 __EXPORT Engine& operator >>( Engine& ar, uint16& ob) THROWS (PersistException);
431 __EXPORT Engine& operator <<( Engine& ar, uint16 ob) THROWS (PersistException);
432 
434 __EXPORT Engine& operator >>( Engine& ar, int32& ob) THROWS (PersistException);
436 __EXPORT Engine& operator <<( Engine& ar, int32 ob) THROWS (PersistException);
437 
439 __EXPORT Engine& operator >>( Engine& ar, uint32& ob) THROWS (PersistException);
441 __EXPORT Engine& operator <<( Engine& ar, uint32 ob) THROWS (PersistException);
442 
443 #ifdef HAVE_64_BITS
444 
445 __EXPORT Engine& operator >>( Engine& ar, int64& ob) THROWS (PersistException);
447 __EXPORT Engine& operator <<( Engine& ar, int64 ob) THROWS (PersistException);
448 
450 __EXPORT Engine& operator >>( Engine& ar, uint64& ob) THROWS (PersistException);
452 __EXPORT Engine& operator <<( Engine& ar, uint64 ob) THROWS (PersistException);
453 #endif
454 
456 __EXPORT Engine& operator >>( Engine& ar, float& ob) THROWS (PersistException);
458 __EXPORT Engine& operator <<( Engine& ar, float ob) THROWS (PersistException);
459 
461 __EXPORT Engine& operator >>( Engine& ar, double& ob) THROWS (PersistException);
463 __EXPORT Engine& operator <<( Engine& ar, double ob) THROWS (PersistException);
464 
466 __EXPORT Engine& operator >>( Engine& ar, String& ob) THROWS (PersistException);
468 __EXPORT Engine& operator <<( Engine& ar, String ob) THROWS (PersistException);
469 
471 __EXPORT Engine& operator >>( Engine& ar, std::string& ob) THROWS (PersistException);
473 __EXPORT Engine& operator <<( Engine& ar, std::string ob) THROWS (PersistException);
474 
476 __EXPORT Engine& operator >>( Engine& ar, bool& ob) THROWS (PersistException);
478 __EXPORT Engine& operator <<( Engine& ar, bool ob) THROWS (PersistException);
479 
489 template<class T>
490 Engine& operator <<( Engine& ar, typename std::vector<T> const& ob) THROWS (PersistException)
491 {
492  ar << (uint32)ob.size();
493  for(unsigned int i=0; i < ob.size(); ++i)
494  ar << ob[i];
495  return ar;
496 }
497 
503 template<class T>
504 Engine& operator >>( Engine& ar, typename std::vector<T>& ob) THROWS (PersistException)
505 {
506  ob.clear();
507  uint32 siz;
508  ar >> siz;
509  ob.resize(siz);
510  for(uint32 i=0; i < siz; ++i)
511  ar >> ob[i];
512  return ar;
513 }
514 
520 template<class T>
521 Engine& operator <<( Engine& ar, typename std::deque<T> const& ob) THROWS (PersistException)
522 {
523  ar << (uint32)ob.size();
524  for(typename std::deque<T>::const_iterator it=ob.begin(); it != ob.end(); ++it)
525  ar << *it;
526  return ar;
527 }
528 
534 template<class T>
535 Engine& operator >>( Engine& ar, typename std::deque<T>& ob) THROWS (PersistException)
536 {
537  ob.clear();
538  uint32 siz;
539  ar >> siz;
540  //ob.resize(siz);
541  for(uint32 i=0; i < siz; ++i) {
542  T node;
543  ar >> node;
544  ob.push_back(node);
545  //ar >> ob[i];
546  }
547  return ar;
548 }
549 
555 template<class Key, class Value>
556 Engine& operator <<( Engine& ar, typename std::map<Key,Value> const & ob) THROWS (PersistException)
557 {
558  ar << (uint32)ob.size();
559  for(typename std::map<Key,Value>::const_iterator it = ob.begin();it != ob.end();++it)
560  ar << it->first << it->second;
561  return ar;
562 }
563 
569 template<class Key, class Value>
570 Engine& operator >>( Engine& ar, typename std::map<Key,Value>& ob) THROWS (PersistException)
571 {
572  ob.clear();
573  uint32 siz;
574  ar >> siz;
575  for(uint32 i=0; i < siz; ++i) {
576  Key a;
577  ar >> a;
578  ar >> ob[a];
579  }
580  return ar;
581 }
582 
587 template<class x, class y>
588 Engine& operator <<( Engine& ar, std::pair<x,y> &ob) THROWS (PersistException)
589 {
590  ar << ob.first << ob.second;
591  return ar;
592 }
593 
598 template<class x, class y>
599 Engine& operator >>(Engine& ar, std::pair<x, y> &ob) THROWS (PersistException)
600 {
601  ar >> ob.first >> ob.second;
602  return ar;
603 }
604 
605 #ifdef CCXX_NAMESPACES
606 }
607 #endif
608 
609 #endif
610 
void write(int8 i) THROWS(PersistException)
Definition: persist.h:301
void write(uint32 i) THROWS(PersistException)
Definition: persist.h:306
void read(uint8 &i) THROWS(PersistException)
Definition: persist.h:338
Common C++ generic string class.
std::map< BaseObject const *, int32 > ArchiveMap
Definition: persist.h:384
void read(double &i) THROWS(PersistException)
Definition: persist.h:348
BaseObject.
Definition: persist.h:206
substitute functions which may be missing in target platform libc.
void write(int32 i) THROWS(PersistException)
Definition: persist.h:305
std::vector< BaseObject * > ArchiveVector
Typedefs for the Persistence::BaseObject support.
Definition: persist.h:383
Engine.
Definition: persist.h:252
This manages a registration to the typemanager - attempting to remove problems with the optimisers...
Definition: persist.h:129
This is a generic and portable string class.
Definition: string.h:77
EngineMode
These are the modes the Persistence::Engine can work in.
Definition: persist.h:258
GNU Common C++ exception model base classes.
class BaseObject *(* NewBaseObjectFunction)(void)
Definition: persist.h:111
void write(double i) THROWS(PersistException)
Definition: persist.h:312
__EXPORT std::ostream & operator<<(std::ostream &os, const IPV4Address &ia)
void write(uint8 i) THROWS(PersistException)
Definition: persist.h:302
void read(float &i) THROWS(PersistException)
Definition: persist.h:347
std::map< String, int32 > ClassMap
Definition: persist.h:386
void write(int16 i) THROWS(PersistException)
Definition: persist.h:303
#define CCXX_ENGINEREAD_REF(valref)
Definition: persist.h:336
This class manages the types for generation of the persistent objects.
Definition: persist.h:121
void read(uint16 &i) THROWS(PersistException)
Definition: persist.h:340
#define __EXPORT
Definition: audio2.h:51
void read(int32 &i) THROWS(PersistException)
Definition: persist.h:341
void read(int16 &i) THROWS(PersistException)
Definition: persist.h:339
void write(uint16 i) THROWS(PersistException)
Definition: persist.h:304
#define CCXX_ENGINEWRITE_REF(valref)
Definition: persist.h:300
void read(uint32 &i) THROWS(PersistException)
Definition: persist.h:342
void read(int8 &i) THROWS(PersistException)
Definition: persist.h:337
std::vector< String > ClassVector
Definition: persist.h:385
std::map< String, NewBaseObjectFunction > StringFunctionMap
Definition: persist.h:155
void write(float i) THROWS(PersistException)
Definition: persist.h:311