diff options
Diffstat (limited to '')
-rw-r--r-- | src/stable/archive.h | 159 |
1 files changed, 59 insertions, 100 deletions
diff --git a/src/stable/archive.h b/src/stable/archive.h index b6cc2a9..45d9af6 100644 --- a/src/stable/archive.h +++ b/src/stable/archive.h | |||
@@ -5,123 +5,82 @@ | |||
5 | * terms of the license contained in the file LICENSE. | 5 | * terms of the license contained in the file LICENSE. |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #ifndef BU_ARCHIVE_H | 8 | #ifndef BU_ARCHIVE_BASE_H |
9 | #define BU_ARCHIVE_H | 9 | #define BU_ARCHIVE_BASE_H |
10 | 10 | ||
11 | #include <stdint.h> | 11 | #include <stdint.h> |
12 | #include "bu/archivebase.h" | 12 | #include <unistd.h> |
13 | #include "bu/hash.h" | 13 | |
14 | #include "bu/util.h" | ||
15 | #include "bu/variant.h" | 14 | #include "bu/variant.h" |
16 | #include "bu/blob.h" | 15 | #include "bu/blob.h" |
17 | 16 | ||
18 | namespace Bu | 17 | namespace Bu |
19 | { | 18 | { |
20 | class Archival; | 19 | class Archive |
21 | class Stream; | ||
22 | |||
23 | /** | ||
24 | * Provides a framework for serialization of objects and primitives. The | ||
25 | * archive will handle any basic primitive, a few special types, like char * | ||
26 | * strings, as well as STL classes and anything that inherits from the | ||
27 | * Archival class. Each Archive operates on a Stream, so you can send the | ||
28 | * data using an Archive almost anywhere. | ||
29 | * | ||
30 | * In order to use an Archive to store something to a file, try something | ||
31 | * like: | ||
32 | *@code | ||
33 | * File sOut("output", "wb"); // This is a stream subclass | ||
34 | * Archive ar( sOut, Archive::save ); | ||
35 | * ar << myClass; | ||
36 | @endcode | ||
37 | * In this example myClass is any class that inherits from Archival. When | ||
38 | * the storage operator is called, the Archival::archive() function in the | ||
39 | * myClass object is called with a reference to the Archive. This can be | ||
40 | * handled in one of two ways: | ||
41 | *@code | ||
42 | * void MyClass::archive( Archive &ar ) | ||
43 | * { | ||
44 | * ar && sName && nAge && sJob; | ||
45 | * } | ||
46 | @endcode | ||
47 | * Here we don't worry about weather we're loading or saving by using the | ||
48 | * smart && operator. This allows us to write very consistent, very simple | ||
49 | * archive functions that really do a lot of work. If we wanted to do | ||
50 | * something different in the case of loading or saving we would do: | ||
51 | *@code | ||
52 | * void MyClass::archive( Archive &ar ) | ||
53 | * { | ||
54 | * if( ar.isLoading() ) | ||
55 | * { | ||
56 | * ar >> sName >> nAge >> sJob; | ||
57 | * } else | ||
58 | * { | ||
59 | * ar << sName << nAge << sJob; | ||
60 | * } | ||
61 | * } | ||
62 | @endcode | ||
63 | * Archive currently does not provide facility to make fully portable | ||
64 | * archives. For example, it will not convert between endianness for you, | ||
65 | * nor will it take into account differences between primitive sizes on | ||
66 | * different platforms. This, at the moment, is up to the user to ensure. | ||
67 | * One way of dealing with the latter problem is to make sure and use | ||
68 | * explicit primitive types from the stdint.h header, i.e. int32_t. | ||
69 | */ | ||
70 | class Archive : public ArchiveBase | ||
71 | { | 20 | { |
72 | private: | ||
73 | bool bLoading; | ||
74 | public: | 21 | public: |
75 | bool isLoading(); | 22 | Archive(); |
76 | 23 | virtual ~Archive(); | |
24 | |||
77 | enum | 25 | enum |
78 | { | 26 | { |
79 | load = true, | 27 | load = true, |
80 | save = false | 28 | save = false |
81 | }; | 29 | }; |
82 | 30 | ||
83 | Archive( Stream &rStream, bool bLoading ); | 31 | virtual void close()=0; |
84 | virtual ~Archive(); | 32 | virtual void write( const void *pData, size_t iLength )=0; |
85 | virtual void close(); | 33 | virtual void read( void *pData, size_t iLength )=0; |
86 | 34 | virtual bool isLoading()=0; | |
87 | virtual void write( const void *pData, size_t iSize ); | ||
88 | virtual void read( void *pData, size_t iSize ); | ||
89 | |||
90 | /** | ||
91 | * For storage, get an ID for the pointer to the object you're going to | ||
92 | * write. | ||
93 | */ | ||
94 | uint32_t getID( const void *ptr ); | ||
95 | |||
96 | /** | ||
97 | * For loading. Assosiates an empty pointer with an id. When you wind | ||
98 | * up loading an id reference to a pointer for an object that may or | ||
99 | * may not have loaded yet, call this with the id, if it has been loaded | ||
100 | * already, you'll immediately get a pointer, if not, it will write one | ||
101 | * for you when the time comes. | ||
102 | */ | ||
103 | void assocPtrID( void **ptr, uint32_t id ); | ||
104 | |||
105 | /** | ||
106 | * For loading. Call this when you load an object that other things may | ||
107 | * have pointers to. It will assosiate every pointer that's been | ||
108 | * registered with assocPtrID to the pointer passed in, and id passed | ||
109 | * in. It will also set things up so future calls to assocPtrID will | ||
110 | * automatically succeed immediately. | ||
111 | */ | ||
112 | void readID( const void *ptr, uint32_t id ); | ||
113 | 35 | ||
114 | virtual void setProperty( const Bu::Blob &rKey, | 36 | virtual void setProperty( const Bu::Blob &rKey, |
115 | const Bu::Variant &rValue ); | 37 | const Bu::Variant &rValue )=0; |
116 | virtual Bu::Variant getProperty( const Bu::Blob &rKey ) const; | 38 | virtual Bu::Variant getProperty( const Bu::Blob &rKey ) const=0; |
117 | |||
118 | private: | ||
119 | Stream &rStream; | ||
120 | uint32_t nNextID; | ||
121 | Hash<uint32_t,ptrdiff_t> hPtrID; | ||
122 | Hash<uint32_t,List<void **> > hPtrDest; | ||
123 | Hash<Bu::Blob, Variant> hProps; | ||
124 | }; | 39 | }; |
125 | } | 40 | |
41 | template<typename T> Archive &operator&&( Archive &ar, T &dat ) | ||
42 | { | ||
43 | if( ar.isLoading() ) | ||
44 | { | ||
45 | return ar >> dat; | ||
46 | } | ||
47 | else | ||
48 | { | ||
49 | return ar << dat; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | Archive &operator<<( Archive &ar, bool p ); | ||
54 | Archive &operator<<( Archive &ar, char p ); | ||
55 | Archive &operator<<( Archive &ar, signed char p ); | ||
56 | Archive &operator<<( Archive &ar, unsigned char p ); | ||
57 | Archive &operator<<( Archive &ar, signed short p ); | ||
58 | Archive &operator<<( Archive &ar, unsigned short p ); | ||
59 | Archive &operator<<( Archive &ar, signed int p ); | ||
60 | Archive &operator<<( Archive &ar, unsigned int p ); | ||
61 | Archive &operator<<( Archive &ar, signed long p ); | ||
62 | Archive &operator<<( Archive &ar, unsigned long p ); | ||
63 | Archive &operator<<( Archive &ar, signed long long p ); | ||
64 | Archive &operator<<( Archive &ar, unsigned long long p ); | ||
65 | Archive &operator<<( Archive &ar, float p ); | ||
66 | Archive &operator<<( Archive &ar, double p ); | ||
67 | Archive &operator<<( Archive &ar, long double p ); | ||
68 | |||
69 | Archive &operator>>( Archive &ar, bool &p ); | ||
70 | Archive &operator>>( Archive &ar, char &p ); | ||
71 | Archive &operator>>( Archive &ar, signed char &p ); | ||
72 | Archive &operator>>( Archive &ar, unsigned char &p ); | ||
73 | Archive &operator>>( Archive &ar, signed short &p ); | ||
74 | Archive &operator>>( Archive &ar, unsigned short &p ); | ||
75 | Archive &operator>>( Archive &ar, signed int &p ); | ||
76 | Archive &operator>>( Archive &ar, unsigned int &p ); | ||
77 | Archive &operator>>( Archive &ar, signed long &p ); | ||
78 | Archive &operator>>( Archive &ar, unsigned long &p ); | ||
79 | Archive &operator>>( Archive &ar, signed long long &p ); | ||
80 | Archive &operator>>( Archive &ar, unsigned long long &p ); | ||
81 | Archive &operator>>( Archive &ar, float &p ); | ||
82 | Archive &operator>>( Archive &ar, double &p ); | ||
83 | Archive &operator>>( Archive &ar, long double &p ); | ||
84 | }; | ||
126 | 85 | ||
127 | #endif | 86 | #endif |