aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-06-04 23:49:26 +0000
committerMike Buland <eichlan@xagasoft.com>2007-06-04 23:49:26 +0000
commitb7e40536df9bf9bbad3b2b7a59f33ebad25fc981 (patch)
tree1555062abe0e01e8d6d5b9b63ba81ab3c3d0168c /src
parent326125aee0b8cd807a6a1d158398078ff6bfb1e1 (diff)
downloadlibbu++-b7e40536df9bf9bbad3b2b7a59f33ebad25fc981.tar.gz
libbu++-b7e40536df9bf9bbad3b2b7a59f33ebad25fc981.tar.bz2
libbu++-b7e40536df9bf9bbad3b2b7a59f33ebad25fc981.tar.xz
libbu++-b7e40536df9bf9bbad3b2b7a59f33ebad25fc981.zip
Added rudimentary object tracking to Archive, and rearranged the hash and
archive dependancies a little. I'll add docs for object tracking later...
Diffstat (limited to 'src')
-rw-r--r--src/archive.cpp41
-rw-r--r--src/archive.h44
-rw-r--r--src/hash.h7
3 files changed, 86 insertions, 6 deletions
diff --git a/src/archive.cpp b/src/archive.cpp
index c759477..7208bae 100644
--- a/src/archive.cpp
+++ b/src/archive.cpp
@@ -2,7 +2,8 @@
2 2
3Bu::Archive::Archive( Stream &rStream, bool bLoading ) : 3Bu::Archive::Archive( Stream &rStream, bool bLoading ) :
4 bLoading( bLoading ), 4 bLoading( bLoading ),
5 rStream( rStream ) 5 rStream( rStream ),
6 nNextID( 1 )
6{ 7{
7} 8}
8 9
@@ -345,3 +346,41 @@ Bu::Archive &Bu::operator>>( Bu::Archive &ar, std::string &s )
345 return ar; 346 return ar;
346} 347}
347 348
349uint32_t Bu::Archive::getID( const void *ptr )
350{
351 if( hPtrID.has( (int)ptr ) )
352 return hPtrID.get( (int)ptr );
353 hPtrID.insert( (int)ptr, nNextID );
354 return nNextID++;
355}
356
357void Bu::Archive::assocPtrID( void **ptr, uint32_t id )
358{
359 if( hPtrID.has( id ) )
360 {
361 *ptr = (void *)hPtrID.get( id );
362 return;
363 }
364
365 if( !hPtrDest.has( id ) )
366 hPtrDest.insert( id, List<void **>() );
367
368 hPtrDest[id].value().append( ptr );
369}
370
371void Bu::Archive::readID( const void *ptr, uint32_t id )
372{
373 hPtrID.insert( id, (int)ptr );
374
375 if( hPtrDest.has( id ) )
376 {
377 Bu::List<void **> &l = hPtrDest.get( id );
378 for( Bu::List<void **>::iterator i = l.begin(); i != l.end(); i++ )
379 {
380 *(*i) = (void *)ptr;
381 }
382
383 hPtrDest.erase( id );
384 }
385}
386
diff --git a/src/archive.h b/src/archive.h
index 2f782d3..05c6f57 100644
--- a/src/archive.h
+++ b/src/archive.h
@@ -3,9 +3,11 @@
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include <string> 5#include <string>
6#include "archival.h" 6#include "bu/archival.h"
7#include "stream.h" 7#include "bu/stream.h"
8#include <list> 8#include <list>
9#include "bu/hash.h"
10#include "bu/list.h"
9 11
10namespace Bu 12namespace Bu
11{ 13{
@@ -117,8 +119,15 @@ namespace Bu
117 virtual Archive &operator&&(double &); 119 virtual Archive &operator&&(double &);
118 virtual Archive &operator&&(long double &); 120 virtual Archive &operator&&(long double &);
119 121
122 uint32_t getID( const void *ptr );
123 void assocPtrID( void **ptr, uint32_t id );
124 void readID( const void *ptr, uint32_t id );
125
120 private: 126 private:
121 Stream &rStream; 127 Stream &rStream;
128 uint32_t nNextID;
129 Hash<uint32_t,uint32_t> hPtrID;
130 Hash<uint32_t,List<void **> > hPtrDest;
122 }; 131 };
123 132
124 Archive &operator<<(Archive &, class Bu::Archival &); 133 Archive &operator<<(Archive &, class Bu::Archival &);
@@ -168,6 +177,37 @@ namespace Bu
168 177
169 return ar; 178 return ar;
170 } 179 }
180
181 template<typename key, typename value>
182 Archive &operator<<( Archive &ar, Hash<key,value> &h )
183 {
184 ar << h.size();
185 for( typename Hash<key,value>::iterator i = h.begin(); i != h.end(); i++ )
186 {
187 std::pair<key,value> p = *i;
188 ar << p.first << p.second;
189 }
190
191 return ar;
192 }
193
194 template<typename key, typename value>
195 Archive &operator>>( Archive &ar, Hash<key,value> &h )
196 {
197 h.clear();
198 uint32_t nSize;
199 ar >> nSize;
200
201 for( uint32_t j = 0; j < nSize; j++ )
202 {
203 key k; value v;
204 ar >> k >> v;
205 h.insert( k, v );
206 }
207
208 return ar;
209 }
210
171} 211}
172 212
173#endif 213#endif
diff --git a/src/hash.h b/src/hash.h
index dc097df..f25c258 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -8,8 +8,8 @@
8#include <list> 8#include <list>
9#include <utility> 9#include <utility>
10#include "exceptionbase.h" 10#include "exceptionbase.h"
11#include "archival.h" 11///#include "archival.h"
12#include "archive.h" 12///#include "archive.h"
13 13
14#define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) 14#define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0))
15 15
@@ -697,6 +697,7 @@ namespace Bu
697 template<> uint32_t __calcHashCode<std::string>( const std::string &k ); 697 template<> uint32_t __calcHashCode<std::string>( const std::string &k );
698 template<> bool __cmpHashKeys<std::string>( const std::string &a, const std::string &b ); 698 template<> bool __cmpHashKeys<std::string>( const std::string &a, const std::string &b );
699 699
700 /*
700 template<typename key, typename value> 701 template<typename key, typename value>
701 Archive &operator<<( Archive &ar, Hash<key,value> &h ) 702 Archive &operator<<( Archive &ar, Hash<key,value> &h )
702 { 703 {
@@ -725,7 +726,7 @@ namespace Bu
725 } 726 }
726 727
727 return ar; 728 return ar;
728 } 729 }*/
729 730
730 /* 731 /*
731 template<typename key, typename value> 732 template<typename key, typename value>