aboutsummaryrefslogtreecommitdiff
path: root/src/stable
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable')
-rw-r--r--src/stable/archival.cpp35
-rw-r--r--src/stable/archival.h52
-rw-r--r--src/stable/archive.cpp200
-rw-r--r--src/stable/archive.h159
-rw-r--r--src/stable/archivebase.cpp197
-rw-r--r--src/stable/archivebase.h80
-rw-r--r--src/stable/archivebinary.cpp98
-rw-r--r--src/stable/archivebinary.h120
-rw-r--r--src/stable/array.h6
-rw-r--r--src/stable/hash.h6
-rw-r--r--src/stable/list.h6
-rw-r--r--src/stable/string.cpp4
-rw-r--r--src/stable/string.h6
13 files changed, 440 insertions, 529 deletions
diff --git a/src/stable/archival.cpp b/src/stable/archival.cpp
deleted file mode 100644
index 227e25d..0000000
--- a/src/stable/archival.cpp
+++ /dev/null
@@ -1,35 +0,0 @@
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#include "bu/archival.h"
9
10Bu::Archival::Archival()
11{
12}
13
14Bu::Archival::~Archival()
15{
16}
17
18Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, const Bu::Archival &p)
19{
20 const_cast<Bu::Archival &>(p).archive( s );
21 return s;
22}
23
24Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, Bu::Archival &p)
25{
26 p.archive( s );
27 return s;
28}
29
30Bu::ArchiveBase &Bu::operator>>(Bu::ArchiveBase &s, Bu::Archival &p)
31{
32 p.archive( s );
33 return s;
34}
35
diff --git a/src/stable/archival.h b/src/stable/archival.h
deleted file mode 100644
index f7122bc..0000000
--- a/src/stable/archival.h
+++ /dev/null
@@ -1,52 +0,0 @@
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_ARCHIVAL_H
9#define BU_ARCHIVAL_H
10
11#include "bu/archivebase.h"
12
13namespace Bu
14{
15 /**
16 * The base class for any class you want to archive. Simply include this as
17 * a base class, implement the purely virtual archive function and you've
18 * got an easily archiveable class.
19 *
20 * Archival: "of or pertaining to archives or valuable records; contained
21 * in or comprising such archives or records."
22 */
23 class Archival
24 {
25 public:
26 /**
27 * Does nothing, here for completeness.
28 */
29 Archival();
30
31 /**
32 * Here to ensure the deconstructor is virtual.
33 */
34 virtual ~Archival();
35
36 /**
37 * This is the main workhorse of the archive system, just override and
38 * you've got a archiveable class. A reference to the Archive
39 * used is passed in as your only parameter, query it to discover if
40 * you are loading or saving.
41 * @param ar A reference to the Archive object to use.
42 */
43 virtual void archive( class ArchiveBase &ar )=0;
44 };
45
46 ArchiveBase &operator<<(ArchiveBase &, const class Bu::Archival &);
47 ArchiveBase &operator<<(ArchiveBase &, class Bu::Archival &);
48 ArchiveBase &operator>>(ArchiveBase &, class Bu::Archival &);
49
50}
51
52#endif
diff --git a/src/stable/archive.cpp b/src/stable/archive.cpp
index 69447fb..df9b1ff 100644
--- a/src/stable/archive.cpp
+++ b/src/stable/archive.cpp
@@ -6,15 +6,8 @@
6 */ 6 */
7 7
8#include "bu/archive.h" 8#include "bu/archive.h"
9#include "bu/stream.h"
10#include "bu/archival.h"
11 9
12#include "bu/sio.h" 10Bu::Archive::Archive()
13
14Bu::Archive::Archive( Stream &rStream, bool bLoading ) :
15 bLoading( bLoading ),
16 rStream( rStream ),
17 nNextID( 1 )
18{ 11{
19} 12}
20 13
@@ -22,78 +15,183 @@ Bu::Archive::~Archive()
22{ 15{
23} 16}
24 17
25void Bu::Archive::write( const void *pData, size_t nSize ) 18Bu::Archive &Bu::operator<<( Bu::Archive &ar, bool p)
19{
20 ar.write( &p, sizeof(p) );
21 return ar;
22}
23
24Bu::Archive &Bu::operator<<( Bu::Archive &ar, char p)
25{
26 ar.write( &p, sizeof(p) );
27 return ar;
28}
29
30Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed char p)
31{
32 ar.write( &p, sizeof(p) );
33 return ar;
34}
35
36Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned char p)
37{
38 ar.write( &p, sizeof(p) );
39 return ar;
40}
41
42Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed short p)
43{
44 ar.write( &p, sizeof(p) );
45 return ar;
46}
47
48Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned short p)
26{ 49{
27 if( nSize == 0 || pData == NULL ) 50 ar.write( &p, sizeof(p) );
28 return; 51 return ar;
52}
29 53
30 rStream.write( (const char *)pData, nSize ); 54Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed int p)
55{
56 ar.write( &p, sizeof(p) );
57 return ar;
31} 58}
32 59
33void Bu::Archive::read( void *pData, size_t nSize ) 60Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned int p)
34{ 61{
35 if( nSize == 0 || pData == NULL ) 62 ar.write( &p, sizeof(p) );
36 return; 63 return ar;
64}
37 65
38 if( (size_t)rStream.read( (char *)pData, nSize ) < nSize ) 66Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed long p)
39 throw Bu::ExceptionBase("Insufficient data to unarchive object."); 67{
68 ar.write( &p, sizeof(p) );
69 return ar;
40} 70}
41 71
42void Bu::Archive::close() 72Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned long p)
43{ 73{
44 rStream.close(); 74 ar.write( &p, sizeof(p) );
75 return ar;
45} 76}
46 77
47bool Bu::Archive::isLoading() 78Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed long long p)
48{ 79{
49 return bLoading; 80 ar.write( &p, sizeof(p) );
81 return ar;
50} 82}
51 83
52uint32_t Bu::Archive::getID( const void *ptr ) 84Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned long long p)
53{ 85{
54 if( hPtrID.has( (ptrdiff_t)ptr ) ) 86 ar.write( &p, sizeof(p) );
55 return hPtrID.get( (ptrdiff_t)ptr ); 87 return ar;
56 hPtrID.insert( (ptrdiff_t)ptr, nNextID );
57 return nNextID++;
58} 88}
59 89
60void Bu::Archive::assocPtrID( void **ptr, uint32_t id ) 90Bu::Archive &Bu::operator<<( Bu::Archive &ar, float p)
61{ 91{
62 if( hPtrID.has( id ) ) 92 ar.write( &p, sizeof(p) );
63 { 93 return ar;
64 *ptr = (void *)hPtrID.get( id ); 94}
65 return;
66 }
67 95
68 if( !hPtrDest.has( id ) ) 96Bu::Archive &Bu::operator<<( Bu::Archive &ar, double p)
69 hPtrDest.insert( id, List<void **>() ); 97{
70 98 ar.write( &p, sizeof(p) );
71 hPtrDest[id].getValue().append( ptr ); 99 return ar;
72} 100}
73 101
74void Bu::Archive::readID( const void *ptr, uint32_t id ) 102Bu::Archive &Bu::operator<<( Bu::Archive &ar, long double p)
75{ 103{
76 hPtrID.insert( id, (ptrdiff_t)ptr ); 104 ar.write( &p, sizeof(p) );
105 return ar;
106}
77 107
78 if( hPtrDest.has( id ) ) 108Bu::Archive &Bu::operator>>( Bu::Archive &ar, bool &p)
79 { 109{
80 Bu::List<void **> &l = hPtrDest.get( id ); 110 ar.read( &p, sizeof(p) );
81 for( Bu::List<void **>::iterator i = l.begin(); i != l.end(); i++ ) 111 return ar;
82 { 112}
83 *(*i) = (void *)ptr;
84 }
85 113
86 hPtrDest.erase( id ); 114Bu::Archive &Bu::operator>>( Bu::Archive &ar, char &p)
87 } 115{
116 ar.read( &p, sizeof(p) );
117 return ar;
118}
119
120Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed char &p)
121{
122 ar.read( &p, sizeof(p) );
123 return ar;
124}
125
126Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned char &p)
127{
128 ar.read( &p, sizeof(p) );
129 return ar;
130}
131
132Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed short &p)
133{
134 ar.read( &p, sizeof(p) );
135 return ar;
136}
137
138Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned short &p)
139{
140 ar.read( &p, sizeof(p) );
141 return ar;
142}
143
144Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed int &p)
145{
146 ar.read( &p, sizeof(p) );
147 return ar;
148}
149
150Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned int &p)
151{
152 ar.read( &p, sizeof(p) );
153 return ar;
154}
155
156Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed long &p)
157{
158 ar.read( &p, sizeof(p) );
159 return ar;
160}
161
162Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned long &p)
163{
164 ar.read( &p, sizeof(p) );
165 return ar;
166}
167
168Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed long long &p)
169{
170 ar.read( &p, sizeof(p) );
171 return ar;
172}
173
174Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned long long &p)
175{
176 ar.read( &p, sizeof(p) );
177 return ar;
178}
179
180Bu::Archive &Bu::operator>>( Bu::Archive &ar, float &p)
181{
182 ar.read( &p, sizeof(p) );
183 return ar;
88} 184}
89 185
90void Bu::Archive::setProperty( const Bu::Blob &rKey, const Bu::Variant &rValue ) 186Bu::Archive &Bu::operator>>( Bu::Archive &ar, double &p)
91{ 187{
92 hProps.insert( rKey, rValue ); 188 ar.read( &p, sizeof(p) );
189 return ar;
93} 190}
94 191
95Bu::Variant Bu::Archive::getProperty( const Bu::Blob &rKey ) const 192Bu::Archive &Bu::operator>>( Bu::Archive &ar, long double &p)
96{ 193{
97 return hProps.get( rKey ); 194 ar.read( &p, sizeof(p) );
195 return ar;
98} 196}
99 197
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
18namespace Bu 17namespace 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
diff --git a/src/stable/archivebase.cpp b/src/stable/archivebase.cpp
deleted file mode 100644
index d2d80a1..0000000
--- a/src/stable/archivebase.cpp
+++ /dev/null
@@ -1,197 +0,0 @@
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#include "bu/archivebase.h"
9
10Bu::ArchiveBase::ArchiveBase()
11{
12}
13
14Bu::ArchiveBase::~ArchiveBase()
15{
16}
17
18Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, bool p)
19{
20 ar.write( &p, sizeof(p) );
21 return ar;
22}
23
24Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, char p)
25{
26 ar.write( &p, sizeof(p) );
27 return ar;
28}
29
30Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed char p)
31{
32 ar.write( &p, sizeof(p) );
33 return ar;
34}
35
36Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned char p)
37{
38 ar.write( &p, sizeof(p) );
39 return ar;
40}
41
42Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed short p)
43{
44 ar.write( &p, sizeof(p) );
45 return ar;
46}
47
48Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned short p)
49{
50 ar.write( &p, sizeof(p) );
51 return ar;
52}
53
54Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed int p)
55{
56 ar.write( &p, sizeof(p) );
57 return ar;
58}
59
60Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned int p)
61{
62 ar.write( &p, sizeof(p) );
63 return ar;
64}
65
66Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long p)
67{
68 ar.write( &p, sizeof(p) );
69 return ar;
70}
71
72Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long p)
73{
74 ar.write( &p, sizeof(p) );
75 return ar;
76}
77
78Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long long p)
79{
80 ar.write( &p, sizeof(p) );
81 return ar;
82}
83
84Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long long p)
85{
86 ar.write( &p, sizeof(p) );
87 return ar;
88}
89
90Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, float p)
91{
92 ar.write( &p, sizeof(p) );
93 return ar;
94}
95
96Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, double p)
97{
98 ar.write( &p, sizeof(p) );
99 return ar;
100}
101
102Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, long double p)
103{
104 ar.write( &p, sizeof(p) );
105 return ar;
106}
107
108Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, bool &p)
109{
110 ar.read( &p, sizeof(p) );
111 return ar;
112}
113
114Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, char &p)
115{
116 ar.read( &p, sizeof(p) );
117 return ar;
118}
119
120Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed char &p)
121{
122 ar.read( &p, sizeof(p) );
123 return ar;
124}
125
126Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned char &p)
127{
128 ar.read( &p, sizeof(p) );
129 return ar;
130}
131
132Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed short &p)
133{
134 ar.read( &p, sizeof(p) );
135 return ar;
136}
137
138Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned short &p)
139{
140 ar.read( &p, sizeof(p) );
141 return ar;
142}
143
144Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed int &p)
145{
146 ar.read( &p, sizeof(p) );
147 return ar;
148}
149
150Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned int &p)
151{
152 ar.read( &p, sizeof(p) );
153 return ar;
154}
155
156Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long &p)
157{
158 ar.read( &p, sizeof(p) );
159 return ar;
160}
161
162Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long &p)
163{
164 ar.read( &p, sizeof(p) );
165 return ar;
166}
167
168Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long long &p)
169{
170 ar.read( &p, sizeof(p) );
171 return ar;
172}
173
174Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long long &p)
175{
176 ar.read( &p, sizeof(p) );
177 return ar;
178}
179
180Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, float &p)
181{
182 ar.read( &p, sizeof(p) );
183 return ar;
184}
185
186Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, double &p)
187{
188 ar.read( &p, sizeof(p) );
189 return ar;
190}
191
192Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, long double &p)
193{
194 ar.read( &p, sizeof(p) );
195 return ar;
196}
197
diff --git a/src/stable/archivebase.h b/src/stable/archivebase.h
deleted file mode 100644
index d846c27..0000000
--- a/src/stable/archivebase.h
+++ /dev/null
@@ -1,80 +0,0 @@
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_BASE_H
9#define BU_ARCHIVE_BASE_H
10
11#include <stdint.h>
12#include <unistd.h>
13
14#include "bu/variant.h"
15#include "bu/blob.h"
16
17namespace Bu
18{
19 class ArchiveBase
20 {
21 public:
22 ArchiveBase();
23 virtual ~ArchiveBase();
24
25 virtual void close()=0;
26 virtual void write( const void *pData, size_t iLength )=0;
27 virtual void read( void *pData, size_t iLength )=0;
28 virtual bool isLoading()=0;
29
30 virtual void setProperty( const Bu::Blob &rKey,
31 const Bu::Variant &rValue )=0;
32 virtual Bu::Variant getProperty( const Bu::Blob &rKey ) const=0;
33 };
34
35 template<typename T> ArchiveBase &operator&&( ArchiveBase &ar, T &dat )
36 {
37 if( ar.isLoading() )
38 {
39 return ar >> dat;
40 }
41 else
42 {
43 return ar << dat;
44 }
45 }
46
47 ArchiveBase &operator<<( ArchiveBase &ar, bool p );
48 ArchiveBase &operator<<( ArchiveBase &ar, char p );
49 ArchiveBase &operator<<( ArchiveBase &ar, signed char p );
50 ArchiveBase &operator<<( ArchiveBase &ar, unsigned char p );
51 ArchiveBase &operator<<( ArchiveBase &ar, signed short p );
52 ArchiveBase &operator<<( ArchiveBase &ar, unsigned short p );
53 ArchiveBase &operator<<( ArchiveBase &ar, signed int p );
54 ArchiveBase &operator<<( ArchiveBase &ar, unsigned int p );
55 ArchiveBase &operator<<( ArchiveBase &ar, signed long p );
56 ArchiveBase &operator<<( ArchiveBase &ar, unsigned long p );
57 ArchiveBase &operator<<( ArchiveBase &ar, signed long long p );
58 ArchiveBase &operator<<( ArchiveBase &ar, unsigned long long p );
59 ArchiveBase &operator<<( ArchiveBase &ar, float p );
60 ArchiveBase &operator<<( ArchiveBase &ar, double p );
61 ArchiveBase &operator<<( ArchiveBase &ar, long double p );
62
63 ArchiveBase &operator>>( ArchiveBase &ar, bool &p );
64 ArchiveBase &operator>>( ArchiveBase &ar, char &p );
65 ArchiveBase &operator>>( ArchiveBase &ar, signed char &p );
66 ArchiveBase &operator>>( ArchiveBase &ar, unsigned char &p );
67 ArchiveBase &operator>>( ArchiveBase &ar, signed short &p );
68 ArchiveBase &operator>>( ArchiveBase &ar, unsigned short &p );
69 ArchiveBase &operator>>( ArchiveBase &ar, signed int &p );
70 ArchiveBase &operator>>( ArchiveBase &ar, unsigned int &p );
71 ArchiveBase &operator>>( ArchiveBase &ar, signed long &p );
72 ArchiveBase &operator>>( ArchiveBase &ar, unsigned long &p );
73 ArchiveBase &operator>>( ArchiveBase &ar, signed long long &p );
74 ArchiveBase &operator>>( ArchiveBase &ar, unsigned long long &p );
75 ArchiveBase &operator>>( ArchiveBase &ar, float &p );
76 ArchiveBase &operator>>( ArchiveBase &ar, double &p );
77 ArchiveBase &operator>>( ArchiveBase &ar, long double &p );
78};
79
80#endif
diff --git a/src/stable/archivebinary.cpp b/src/stable/archivebinary.cpp
new file mode 100644
index 0000000..3f05947
--- /dev/null
+++ b/src/stable/archivebinary.cpp
@@ -0,0 +1,98 @@
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#include "bu/archivebinary.h"
9#include "bu/stream.h"
10
11#include "bu/sio.h"
12
13Bu::ArchiveBinary::ArchiveBinary( Stream &rStream, bool bLoading ) :
14 bLoading( bLoading ),
15 rStream( rStream ),
16 nNextID( 1 )
17{
18}
19
20Bu::ArchiveBinary::~ArchiveBinary()
21{
22}
23
24void Bu::ArchiveBinary::write( const void *pData, size_t nSize )
25{
26 if( nSize == 0 || pData == NULL )
27 return;
28
29 rStream.write( (const char *)pData, nSize );
30}
31
32void Bu::ArchiveBinary::read( void *pData, size_t nSize )
33{
34 if( nSize == 0 || pData == NULL )
35 return;
36
37 if( (size_t)rStream.read( (char *)pData, nSize ) < nSize )
38 throw Bu::ExceptionBase("Insufficient data to unarchive object.");
39}
40
41void Bu::ArchiveBinary::close()
42{
43 rStream.close();
44}
45
46bool Bu::ArchiveBinary::isLoading()
47{
48 return bLoading;
49}
50
51uint32_t Bu::ArchiveBinary::getID( const void *ptr )
52{
53 if( hPtrID.has( (ptrdiff_t)ptr ) )
54 return hPtrID.get( (ptrdiff_t)ptr );
55 hPtrID.insert( (ptrdiff_t)ptr, nNextID );
56 return nNextID++;
57}
58
59void Bu::ArchiveBinary::assocPtrID( void **ptr, uint32_t id )
60{
61 if( hPtrID.has( id ) )
62 {
63 *ptr = (void *)hPtrID.get( id );
64 return;
65 }
66
67 if( !hPtrDest.has( id ) )
68 hPtrDest.insert( id, List<void **>() );
69
70 hPtrDest[id].getValue().append( ptr );
71}
72
73void Bu::ArchiveBinary::readID( const void *ptr, uint32_t id )
74{
75 hPtrID.insert( id, (ptrdiff_t)ptr );
76
77 if( hPtrDest.has( id ) )
78 {
79 Bu::List<void **> &l = hPtrDest.get( id );
80 for( Bu::List<void **>::iterator i = l.begin(); i != l.end(); i++ )
81 {
82 *(*i) = (void *)ptr;
83 }
84
85 hPtrDest.erase( id );
86 }
87}
88
89void Bu::ArchiveBinary::setProperty( const Bu::Blob &rKey, const Bu::Variant &rValue )
90{
91 hProps.insert( rKey, rValue );
92}
93
94Bu::Variant Bu::ArchiveBinary::getProperty( const Bu::Blob &rKey ) const
95{
96 return hProps.get( rKey );
97}
98
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
18namespace 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
diff --git a/src/stable/array.h b/src/stable/array.h
index ca66213..6069832 100644
--- a/src/stable/array.h
+++ b/src/stable/array.h
@@ -10,7 +10,7 @@
10 10
11#include <memory> 11#include <memory>
12#include "bu/exceptionbase.h" 12#include "bu/exceptionbase.h"
13#include "bu/archivebase.h" 13#include "bu/archive.h"
14#include "bu/sharedcore.h" 14#include "bu/sharedcore.h"
15 15
16namespace Bu 16namespace Bu
@@ -761,7 +761,7 @@ namespace Bu
761 } 761 }
762 762
763 template<typename value, int inc, typename valuealloc> 763 template<typename value, int inc, typename valuealloc>
764 ArchiveBase &operator<<( ArchiveBase &ar, 764 Archive &operator<<( Archive &ar,
765 const Array<value, inc, valuealloc> &h ) 765 const Array<value, inc, valuealloc> &h )
766 { 766 {
767 ar << h.getSize(); 767 ar << h.getSize();
@@ -775,7 +775,7 @@ namespace Bu
775 } 775 }
776 776
777 template<typename value, int inc, typename valuealloc> 777 template<typename value, int inc, typename valuealloc>
778 ArchiveBase &operator>>(ArchiveBase &ar, Array<value, inc, valuealloc> &h ) 778 Archive &operator>>(Archive &ar, Array<value, inc, valuealloc> &h )
779 { 779 {
780 h.clear(); 780 h.clear();
781 long nSize; 781 long nSize;
diff --git a/src/stable/hash.h b/src/stable/hash.h
index 0c5bd9e..e515088 100644
--- a/src/stable/hash.h
+++ b/src/stable/hash.h
@@ -12,7 +12,7 @@
12#include "bu/exceptionbase.h" 12#include "bu/exceptionbase.h"
13#include "bu/list.h" 13#include "bu/list.h"
14#include "bu/util.h" 14#include "bu/util.h"
15#include "bu/archivebase.h" 15#include "bu/archive.h"
16#include "bu/sharedcore.h" 16#include "bu/sharedcore.h"
17 17
18namespace Bu 18namespace Bu
@@ -1319,7 +1319,7 @@ namespace Bu
1319 1319
1320 template<typename key, typename value, typename a, typename b, 1320 template<typename key, typename value, typename a, typename b,
1321 typename c, typename d> 1321 typename c, typename d>
1322 ArchiveBase &operator<<( ArchiveBase &ar, const Hash<key,value,a,b,c,d> &h ) 1322 Archive &operator<<( Archive &ar, const Hash<key,value,a,b,c,d> &h )
1323 { 1323 {
1324 long iSize = h.getSize(); 1324 long iSize = h.getSize();
1325 ar << iSize; 1325 ar << iSize;
@@ -1334,7 +1334,7 @@ namespace Bu
1334 1334
1335 template<typename key, typename value, typename a, typename b, 1335 template<typename key, typename value, typename a, typename b,
1336 typename c, typename d> 1336 typename c, typename d>
1337 ArchiveBase &operator>>( ArchiveBase &ar, Hash<key,value,a,b,c,d> &h ) 1337 Archive &operator>>( Archive &ar, Hash<key,value,a,b,c,d> &h )
1338 { 1338 {
1339 h.clear(); 1339 h.clear();
1340 long nSize; 1340 long nSize;
diff --git a/src/stable/list.h b/src/stable/list.h
index c1a5559..5ef7ce0 100644
--- a/src/stable/list.h
+++ b/src/stable/list.h
@@ -11,7 +11,7 @@
11#include <memory> 11#include <memory>
12#include "bu/exceptionbase.h" 12#include "bu/exceptionbase.h"
13#include "bu/sharedcore.h" 13#include "bu/sharedcore.h"
14#include "bu/archivebase.h" 14#include "bu/archive.h"
15#include "bu/heap.h" 15#include "bu/heap.h"
16 16
17namespace Bu 17namespace Bu
@@ -1042,7 +1042,7 @@ namespace Bu
1042 } 1042 }
1043 1043
1044 template<typename value, typename a, typename b> 1044 template<typename value, typename a, typename b>
1045 ArchiveBase &operator<<( ArchiveBase &ar, const List<value,a,b> &h ) 1045 Archive &operator<<( Archive &ar, const List<value,a,b> &h )
1046 { 1046 {
1047 ar << h.getSize(); 1047 ar << h.getSize();
1048 for( typename List<value>::const_iterator i = h.begin(); i != h.end(); i++ ) 1048 for( typename List<value>::const_iterator i = h.begin(); i != h.end(); i++ )
@@ -1054,7 +1054,7 @@ namespace Bu
1054 } 1054 }
1055 1055
1056 template<typename value, typename a, typename b> 1056 template<typename value, typename a, typename b>
1057 ArchiveBase &operator>>( ArchiveBase &ar, List<value,a,b> &h ) 1057 Archive &operator>>( Archive &ar, List<value,a,b> &h )
1058 { 1058 {
1059 h.clear(); 1059 h.clear();
1060 long nSize; 1060 long nSize;
diff --git a/src/stable/string.cpp b/src/stable/string.cpp
index ed4e58b..c36b768 100644
--- a/src/stable/string.cpp
+++ b/src/stable/string.cpp
@@ -1520,7 +1520,7 @@ Bu::String &Bu::operator<<( Bu::String &dst, const Bu::String &sIn )
1520 return dst; 1520 return dst;
1521} 1521}
1522 1522
1523Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, const Bu::String &s ) 1523Bu::Archive &Bu::operator<<( Bu::Archive &ar, const Bu::String &s )
1524{ 1524{
1525 long n = s.getSize(); 1525 long n = s.getSize();
1526 ar << n; 1526 ar << n;
@@ -1528,7 +1528,7 @@ Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, const Bu::String &s )
1528 return ar; 1528 return ar;
1529} 1529}
1530 1530
1531Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, Bu::String &s ) 1531Bu::Archive &Bu::operator>>( Bu::Archive &ar, Bu::String &s )
1532{ 1532{
1533 long n; 1533 long n;
1534 ar >> n; 1534 ar >> n;
diff --git a/src/stable/string.h b/src/stable/string.h
index c469580..b4768bb 100644
--- a/src/stable/string.h
+++ b/src/stable/string.h
@@ -14,7 +14,7 @@
14#include "bu/util.h" 14#include "bu/util.h"
15#include "bu/sharedcore.h" 15#include "bu/sharedcore.h"
16#include "bu/exceptionbase.h" 16#include "bu/exceptionbase.h"
17#include "bu/archivebase.h" 17#include "bu/archive.h"
18#include "bu/list.h" 18#include "bu/list.h"
19#include "bu/fmt.h" 19#include "bu/fmt.h"
20#include "bu/variant.h" 20#include "bu/variant.h"
@@ -1051,8 +1051,8 @@ namespace Bu
1051 return ret; 1051 return ret;
1052 } 1052 }
1053 1053
1054 ArchiveBase &operator<<( ArchiveBase &ar, const String &s ); 1054 Archive &operator<<( Archive &ar, const String &s );
1055 ArchiveBase &operator>>( ArchiveBase &ar, String &s ); 1055 Archive &operator>>( Archive &ar, String &s );
1056 1056
1057 template<typename T> 1057 template<typename T>
1058 uint32_t __calcHashCode( const T &k ); 1058 uint32_t __calcHashCode( const T &k );