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_
53 #endif
54
55 #ifndef CCXX_MISSING_H_
57 #endif
58
59 #ifndef CCXX_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
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
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:
105 };
106
107 #endif
108 #endif
109
110 // This typedef allows us to declare NewBaseObjectFunction now
112
122 {
123 public:
124
130 {
131 public:
134 private:
136 };
137
142
146 static void remove(const char* name);
147
153 static BaseObject* createInstanceOf(
const char* name);
154
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
186
207 {
208 public:
215
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:
261 };
262
269 Engine(std::iostream& stream, EngineMode mode,
bool compress=
true) THROWS (PersistException);
270
275 void sync();
276
280 bool more();
281
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))
307 #ifdef HAVE_64_BITS
310 #endif
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))
343 #ifdef HAVE_64_BITS
346 #endif
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
379
387
388 ArchiveVector myArchiveVector;
389 ArchiveMap myArchiveMap;
390 ClassVector myClassVector;
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
412
417
422
427
432
437
442
443 #ifdef HAVE_64_BITS
444
448
453 #endif
454
459
464
469
474
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)
void write(uint32 i) THROWS(PersistException)
void read(uint8 &i) THROWS(PersistException)
Common C++ generic string class.
std::map< BaseObject const *, int32 > ArchiveMap
void read(double &i) THROWS(PersistException)
substitute functions which may be missing in target platform libc.
void write(int32 i) THROWS(PersistException)
std::vector< BaseObject * > ArchiveVector
Typedefs for the Persistence::BaseObject support.
This manages a registration to the typemanager - attempting to remove problems with the optimisers...
This is a generic and portable string class.
EngineMode
These are the modes the Persistence::Engine can work in.
GNU Common C++ exception model base classes.
class BaseObject *(* NewBaseObjectFunction)(void)
void write(double i) THROWS(PersistException)
__EXPORT std::ostream & operator<<(std::ostream &os, const IPV4Address &ia)
void write(uint8 i) THROWS(PersistException)
void read(float &i) THROWS(PersistException)
std::map< String, int32 > ClassMap
void write(int16 i) THROWS(PersistException)
#define CCXX_ENGINEREAD_REF(valref)
This class manages the types for generation of the persistent objects.
void read(uint16 &i) THROWS(PersistException)
void read(int32 &i) THROWS(PersistException)
void read(int16 &i) THROWS(PersistException)
void write(uint16 i) THROWS(PersistException)
#define CCXX_ENGINEWRITE_REF(valref)
void read(uint32 &i) THROWS(PersistException)
void read(int8 &i) THROWS(PersistException)
std::vector< String > ClassVector
std::map< String, NewBaseObjectFunction > StringFunctionMap
void write(float i) THROWS(PersistException)