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