From b7e40536df9bf9bbad3b2b7a59f33ebad25fc981 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 4 Jun 2007 23:49:26 +0000 Subject: Added rudimentary object tracking to Archive, and rearranged the hash and archive dependancies a little. I'll add docs for object tracking later... --- src/archive.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- src/archive.h | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/hash.h | 7 ++++--- 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 @@ Bu::Archive::Archive( Stream &rStream, bool bLoading ) : bLoading( bLoading ), - rStream( rStream ) + rStream( rStream ), + nNextID( 1 ) { } @@ -345,3 +346,41 @@ Bu::Archive &Bu::operator>>( Bu::Archive &ar, std::string &s ) return ar; } +uint32_t Bu::Archive::getID( const void *ptr ) +{ + if( hPtrID.has( (int)ptr ) ) + return hPtrID.get( (int)ptr ); + hPtrID.insert( (int)ptr, nNextID ); + return nNextID++; +} + +void Bu::Archive::assocPtrID( void **ptr, uint32_t id ) +{ + if( hPtrID.has( id ) ) + { + *ptr = (void *)hPtrID.get( id ); + return; + } + + if( !hPtrDest.has( id ) ) + hPtrDest.insert( id, List() ); + + hPtrDest[id].value().append( ptr ); +} + +void Bu::Archive::readID( const void *ptr, uint32_t id ) +{ + hPtrID.insert( id, (int)ptr ); + + if( hPtrDest.has( id ) ) + { + Bu::List &l = hPtrDest.get( id ); + for( Bu::List::iterator i = l.begin(); i != l.end(); i++ ) + { + *(*i) = (void *)ptr; + } + + hPtrDest.erase( id ); + } +} + 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 @@ #include #include -#include "archival.h" -#include "stream.h" +#include "bu/archival.h" +#include "bu/stream.h" #include +#include "bu/hash.h" +#include "bu/list.h" namespace Bu { @@ -117,8 +119,15 @@ namespace Bu virtual Archive &operator&&(double &); virtual Archive &operator&&(long double &); + uint32_t getID( const void *ptr ); + void assocPtrID( void **ptr, uint32_t id ); + void readID( const void *ptr, uint32_t id ); + private: Stream &rStream; + uint32_t nNextID; + Hash hPtrID; + Hash > hPtrDest; }; Archive &operator<<(Archive &, class Bu::Archival &); @@ -168,6 +177,37 @@ namespace Bu return ar; } + + template + Archive &operator<<( Archive &ar, Hash &h ) + { + ar << h.size(); + for( typename Hash::iterator i = h.begin(); i != h.end(); i++ ) + { + std::pair p = *i; + ar << p.first << p.second; + } + + return ar; + } + + template + Archive &operator>>( Archive &ar, Hash &h ) + { + h.clear(); + uint32_t nSize; + ar >> nSize; + + for( uint32_t j = 0; j < nSize; j++ ) + { + key k; value v; + ar >> k >> v; + h.insert( k, v ); + } + + return ar; + } + } #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 @@ #include #include #include "exceptionbase.h" -#include "archival.h" -#include "archive.h" +///#include "archival.h" +///#include "archive.h" #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) @@ -697,6 +697,7 @@ namespace Bu template<> uint32_t __calcHashCode( const std::string &k ); template<> bool __cmpHashKeys( const std::string &a, const std::string &b ); + /* template Archive &operator<<( Archive &ar, Hash &h ) { @@ -725,7 +726,7 @@ namespace Bu } return ar; - } + }*/ /* template -- cgit v1.2.3