From da89e6d30e57bd6dbb10b4d36b093ce9bbf5c666 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 04:50:36 +0000 Subject: The first batch seem to have made it alright. Unfortunately the Archive class isn't done yet, I'm going to make it rely on streams, so those will be next, then we can make it work all sortsa' well. --- src/archive.cpp | 337 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 src/archive.cpp (limited to 'src/archive.cpp') diff --git a/src/archive.cpp b/src/archive.cpp new file mode 100644 index 0000000..5f5145c --- /dev/null +++ b/src/archive.cpp @@ -0,0 +1,337 @@ +#include "archive.h" + +Bu::Archive::Archive(bool bLoading): + bLoading(bLoading) +{ +} +Bu::Archive::~Archive() +{ +} + +bool Bu::Archive::isLoading() +{ + return bLoading; +} +Bu::Archive &Bu::Archive::operator<<(bool p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(int8_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(int16_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(int32_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(int64_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(uint8_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(uint16_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(uint32_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(uint64_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(long p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(float p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(double p) +{ + write( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator<<(long double p) +{ + write( &p, sizeof(p) ); + return *this; +} + +Bu::Archive &Bu::Archive::operator>>(bool &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(int8_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(int16_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(int32_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(int64_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(uint8_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(uint16_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(uint32_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(uint64_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(long &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(float &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(double &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Bu::Archive &Bu::Archive::operator>>(long double &p) +{ + read( &p, sizeof(p) ); + return *this; +} + +Bu::Archive &Bu::Archive::operator&&(bool &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(int8_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(int16_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(int32_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(int64_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(uint8_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(uint16_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(uint32_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(uint64_t &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(float &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(double &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + +Bu::Archive &Bu::Archive::operator&&(long double &p) +{ + if (bLoading) + { + return *this >> p; + } + else + { + return *this << p; + } +} + + +Bu::Archive &Bu::operator<<(Bu::Archive &s, Bu::Archable &p) +{ + p.archive( s ); + return s; +} + +Bu::Archive &Bu::operator>>(Bu::Archive &s, Bu::Archable &p) +{ + p.archive( s ); + return s; +} + +/* +Bu::Archive &Bu::operator&&(Bu::Archive &s, Bu::Archable &p) +{ + if (s.isLoading()) + { + return s >> p; + } + else + { + return s << p; + } +}*/ + +Bu::Archive &Bu::operator<<( Bu::Archive &ar, std::string &s ) +{ + ar << (uint32_t)s.length(); + ar.write( s.c_str(), s.length() ); + + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, std::string &s ) +{ + uint32_t l; + ar >> l; + char *tmp = new char[l+1]; + tmp[l] = '\0'; + ar.read( tmp, l ); + s = tmp; + delete[] tmp; + + return ar; +} + -- cgit v1.2.3 From c884da672645231b5ec47706c886381dab1b391a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 05:09:12 +0000 Subject: The file stream is imported and works, as does our first test, and the new tweaks to archive. The && operator is now a template function, and as such requires no special handling. It could be worth it to check this out for other types, yet dangerous, since it would let you archive anything, even a class, without writing the proper functions for it...we shall see what happens... --- src/archive.cpp | 27 ++++++++++++++-- src/archive.h | 12 ++++--- src/old/sfile.cpp | 74 ------------------------------------------ src/old/sfile.h | 29 ----------------- src/sfile.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/sfile.h | 36 +++++++++++++++++++++ src/tests/archive.cpp | 11 ++++++- 7 files changed, 169 insertions(+), 110 deletions(-) delete mode 100644 src/old/sfile.cpp delete mode 100644 src/old/sfile.h create mode 100644 src/sfile.cpp create mode 100644 src/sfile.h (limited to 'src/archive.cpp') diff --git a/src/archive.cpp b/src/archive.cpp index 5f5145c..be06c0e 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -1,13 +1,36 @@ #include "archive.h" -Bu::Archive::Archive(bool bLoading): - bLoading(bLoading) +Bu::Archive::Archive( Stream &rStream, bool bLoading ) : + bLoading( bLoading ), + rStream( rStream ) { } + Bu::Archive::~Archive() { } +void Bu::Archive::write( const void *pData, int32_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + rStream.write( (const char *)pData, nSize ); +} + +void Bu::Archive::read( void *pData, int32_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + rStream.read( (char *)pData, nSize ); +} + +void Bu::Archive::close() +{ + rStream.close(); +} + bool Bu::Archive::isLoading() { return bLoading; diff --git a/src/archive.h b/src/archive.h index 7de9220..26e430b 100644 --- a/src/archive.h +++ b/src/archive.h @@ -4,6 +4,7 @@ #include #include #include "archable.h" +#include "stream.h" namespace Bu { @@ -20,12 +21,12 @@ namespace Bu save = false }; - Archive( bool bLoading ); + Archive( Stream &rStream, bool bLoading ); virtual ~Archive(); - virtual void close()=0; + virtual void close(); - virtual void write(const void *, int32_t)=0; - virtual void read(void *, int32_t)=0; + virtual void write(const void *, int32_t); + virtual void read(void *, int32_t); virtual Archive &operator<<(bool); virtual Archive &operator<<(int8_t); @@ -67,6 +68,9 @@ namespace Bu virtual Archive &operator&&(float &); virtual Archive &operator&&(double &); virtual Archive &operator&&(long double &); + + private: + Stream &rStream; }; Archive &operator<<(Archive &, class Bu::Archable &); diff --git a/src/old/sfile.cpp b/src/old/sfile.cpp deleted file mode 100644 index f1de03c..0000000 --- a/src/old/sfile.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "sfile.h" -#include "exceptions.h" - -SFile::SFile( const char *sName, const char *sFlags ) -{ - fh = fopen( sName, sFlags ); -} - -SFile::~SFile() -{ -} - -void SFile::close() -{ - if( fh ) - { - fclose( fh ); - fh = NULL; - } -} - -size_t SFile::read( char *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - return fread( pBuf, 1, nBytes, fh ); -} - -size_t SFile::write( const char *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - return fwrite( pBuf, 1, nBytes, fh ); -} - -long SFile::tell() -{ - if( !fh ) - throw FileException("File not open."); - - return ftell( fh ); -} - -void SFile::seek( long offset ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, offset, SEEK_CUR ); -} - -void SFile::setPos( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_SET ); -} - -void SFile::setPosEnd( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_END ); -} - -bool SFile::isEOS() -{ - return feof( fh ); -} - diff --git a/src/old/sfile.h b/src/old/sfile.h deleted file mode 100644 index b51e5bc..0000000 --- a/src/old/sfile.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef SFILE_H -#define SFILE_H - -#include - -#include "stream.h" - -class SFile : public Stream -{ -public: - SFile( const char *sName, const char *sFlags ); - virtual ~SFile(); - - virtual void close(); - virtual size_t read( char *pBuf, size_t nBytes ); - virtual size_t write( const char *pBuf, size_t nBytes ); - - virtual long tell(); - virtual void seek( long offset ); - virtual void setPos( long pos ); - virtual void setPosEnd( long pos ); - virtual bool isEOS(); - -private: - FILE *fh; - -}; - -#endif diff --git a/src/sfile.cpp b/src/sfile.cpp new file mode 100644 index 0000000..d7c5c83 --- /dev/null +++ b/src/sfile.cpp @@ -0,0 +1,90 @@ +#include "sfile.h" +#include "exceptions.h" + +Bu::SFile::SFile( const char *sName, const char *sFlags ) +{ + fh = fopen( sName, sFlags ); +} + +Bu::SFile::~SFile() +{ + close(); +} + +void Bu::SFile::close() +{ + if( fh ) + { + fclose( fh ); + fh = NULL; + } +} + +size_t Bu::SFile::read( char *pBuf, size_t nBytes ) +{ + if( !fh ) + throw FileException("File not open."); + + return fread( pBuf, 1, nBytes, fh ); +} + +size_t Bu::SFile::write( const char *pBuf, size_t nBytes ) +{ + if( !fh ) + throw FileException("File not open."); + + return fwrite( pBuf, 1, nBytes, fh ); +} + +long Bu::SFile::tell() +{ + if( !fh ) + throw FileException("File not open."); + + return ftell( fh ); +} + +void Bu::SFile::seek( long offset ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, offset, SEEK_CUR ); +} + +void Bu::SFile::setPos( long pos ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, pos, SEEK_SET ); +} + +void Bu::SFile::setPosEnd( long pos ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, pos, SEEK_END ); +} + +bool Bu::SFile::isEOS() +{ + return feof( fh ); +} + +bool Bu::SFile::canRead() +{ + return true; +} + +bool Bu::SFile::canWrite() +{ + return true; +} + +bool Bu::SFile::canSeek() +{ + return true; +} + diff --git a/src/sfile.h b/src/sfile.h new file mode 100644 index 0000000..304f6b7 --- /dev/null +++ b/src/sfile.h @@ -0,0 +1,36 @@ +#ifndef SFILE_H +#define SFILE_H + +#include + +#include "stream.h" + +namespace Bu +{ + class SFile : public Bu::Stream + { + public: + SFile( const char *sName, const char *sFlags ); + virtual ~SFile(); + + virtual void close(); + virtual size_t read( char *pBuf, size_t nBytes ); + virtual size_t write( const char *pBuf, size_t nBytes ); + + virtual long tell(); + virtual void seek( long offset ); + virtual void setPos( long pos ); + virtual void setPosEnd( long pos ); + virtual bool isEOS(); + + virtual bool canRead(); + virtual bool canWrite(); + virtual bool canSeek(); + + private: + FILE *fh; + + }; +} + +#endif diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp index fb0d97c..5b7e285 100644 --- a/src/tests/archive.cpp +++ b/src/tests/archive.cpp @@ -1,7 +1,16 @@ #include "archive.h" +#include "sfile.h" + +using namespace Bu; int main() { - //Archive + SFile f("test.dat", "wb"); + Archive ar( f, Archive::save ); + + std::string s("Hello there"); + ar << s; + + return 0; } -- cgit v1.2.3 From 5a0d7856dc265580cebaa833e0367d03ef21bbc3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 10 Apr 2007 13:53:18 +0000 Subject: Woo, changed the name of Achable to Archival, I dig that, and added the ground- work for the SSocket, that should be cool. --- src/archable.cpp | 10 ---------- src/archable.h | 35 ----------------------------------- src/archival.cpp | 10 ++++++++++ src/archival.h | 38 ++++++++++++++++++++++++++++++++++++++ src/archive.cpp | 6 +++--- src/archive.h | 8 ++++---- src/fstring.h | 4 ++-- src/hash.h | 2 +- src/ssocket.cpp | 9 +++++++++ src/ssocket.h | 24 ++++++++++++++++++++++++ 10 files changed, 91 insertions(+), 55 deletions(-) delete mode 100644 src/archable.cpp delete mode 100644 src/archable.h create mode 100644 src/archival.cpp create mode 100644 src/archival.h create mode 100644 src/ssocket.cpp create mode 100644 src/ssocket.h (limited to 'src/archive.cpp') diff --git a/src/archable.cpp b/src/archable.cpp deleted file mode 100644 index 38fc31f..0000000 --- a/src/archable.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "archable.h" - -Bu::Archable::Archable() -{ -} - -Bu::Archable::~Archable() -{ -} - diff --git a/src/archable.h b/src/archable.h deleted file mode 100644 index ed05a78..0000000 --- a/src/archable.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef ARCHABLE_H -#define ARCHABLE_H - -namespace Bu -{ - /** - * The base class for any class you want to archive. Simply include this as - * a base class, implement the purely virtual archive function and you've - * got an easily archiveable class. - */ - class Archable - { - public: - /** - * Does nothing, here for completeness. - */ - Archable(); - - /** - * Here to ensure the deconstructor is virtual. - */ - virtual ~Archable(); - - /** - * This is the main workhorse of the archive system, just override and - * you've got a archiveable class. A reference to the Archive - * used is passed in as your only parameter, query it to discover if - * you are loading or saving. - * @param ar A reference to the Archive object to use. - */ - virtual void archive( class Archive &ar )=0; - }; -} - -#endif diff --git a/src/archival.cpp b/src/archival.cpp new file mode 100644 index 0000000..79c28f1 --- /dev/null +++ b/src/archival.cpp @@ -0,0 +1,10 @@ +#include "archival.h" + +Bu::Archival::Archival() +{ +} + +Bu::Archival::~Archival() +{ +} + diff --git a/src/archival.h b/src/archival.h new file mode 100644 index 0000000..e2c803c --- /dev/null +++ b/src/archival.h @@ -0,0 +1,38 @@ +#ifndef ARCHIVAL_H +#define ARCHIVAL_H + +namespace Bu +{ + /** + * The base class for any class you want to archive. Simply include this as + * a base class, implement the purely virtual archive function and you've + * got an easily archiveable class. + * + * Archival: "of or pertaining to archives or valuable records; contained + * in or comprising such archives or records." + */ + class Archival + { + public: + /** + * Does nothing, here for completeness. + */ + Archival(); + + /** + * Here to ensure the deconstructor is virtual. + */ + virtual ~Archival(); + + /** + * This is the main workhorse of the archive system, just override and + * you've got a archiveable class. A reference to the Archive + * used is passed in as your only parameter, query it to discover if + * you are loading or saving. + * @param ar A reference to the Archive object to use. + */ + virtual void archive( class Archive &ar )=0; + }; +} + +#endif diff --git a/src/archive.cpp b/src/archive.cpp index be06c0e..edc8625 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -312,20 +312,20 @@ Bu::Archive &Bu::Archive::operator&&(long double &p) } -Bu::Archive &Bu::operator<<(Bu::Archive &s, Bu::Archable &p) +Bu::Archive &Bu::operator<<(Bu::Archive &s, Bu::Archival &p) { p.archive( s ); return s; } -Bu::Archive &Bu::operator>>(Bu::Archive &s, Bu::Archable &p) +Bu::Archive &Bu::operator>>(Bu::Archive &s, Bu::Archival &p) { p.archive( s ); return s; } /* -Bu::Archive &Bu::operator&&(Bu::Archive &s, Bu::Archable &p) +Bu::Archive &Bu::operator&&(Bu::Archive &s, Bu::Archival &p) { if (s.isLoading()) { diff --git a/src/archive.h b/src/archive.h index 26e430b..a8ce53e 100644 --- a/src/archive.h +++ b/src/archive.h @@ -3,7 +3,7 @@ #include #include -#include "archable.h" +#include "archival.h" #include "stream.h" namespace Bu @@ -73,9 +73,9 @@ namespace Bu Stream &rStream; }; - Archive &operator<<(Archive &, class Bu::Archable &); - Archive &operator>>(Archive &, class Bu::Archable &); - //Archive &operator&&(Archive &s, class Bu::Archable &p); + Archive &operator<<(Archive &, class Bu::Archival &); + Archive &operator>>(Archive &, class Bu::Archival &); + //Archive &operator&&(Archive &s, class Bu::Archival &p); Archive &operator<<(Archive &, std::string &); Archive &operator>>(Archive &, std::string &); diff --git a/src/fstring.h b/src/fstring.h index 717068f..0184301 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -3,7 +3,7 @@ #include #include -#include "archable.h" +#include "archival.h" #include "archive.h" #include "hash.h" @@ -29,7 +29,7 @@ namespace Bu * FBasicString into a ref-counting container class. */ template< typename chr, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > - class FBasicString : public Archable + class FBasicString : public Archival { #ifndef VALTEST #define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) ) diff --git a/src/hash.h b/src/hash.h index 9e498f1..dc097df 100644 --- a/src/hash.h +++ b/src/hash.h @@ -8,7 +8,7 @@ #include #include #include "exceptionbase.h" -#include "archable.h" +#include "archival.h" #include "archive.h" #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) diff --git a/src/ssocket.cpp b/src/ssocket.cpp new file mode 100644 index 0000000..bdaac24 --- /dev/null +++ b/src/ssocket.cpp @@ -0,0 +1,9 @@ +#include "ssocket.h" + +SSocket::SSocket() +{ +} + +SSocket::~SSocket() +{ +} diff --git a/src/ssocket.h b/src/ssocket.h new file mode 100644 index 0000000..ce02091 --- /dev/null +++ b/src/ssocket.h @@ -0,0 +1,24 @@ +#ifndef S_SOCKET_H +#define S_SOCKET_H + +#include + +#include "stream.h" + +namespace Bu +{ + /** + * + */ + class SSocket : public Stream + { + public: + SSocket(); + virtual ~SSocket(); + + private: + + }; +} + +#endif -- cgit v1.2.3 From e7ab7cb1604c04763bbdcece5885e6ce5aa100b4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 10 Apr 2007 17:37:46 +0000 Subject: Fixed a warning in the SFile test, and added std::list support to the archive. I guess I should write a test for it too... I'm also thinking of removing the S from the front of the stream children. --- src/archive.cpp | 13 ------------- src/archive.h | 29 +++++++++++++++++++++++++++++ src/unit/sfile.cpp | 2 +- 3 files changed, 30 insertions(+), 14 deletions(-) (limited to 'src/archive.cpp') diff --git a/src/archive.cpp b/src/archive.cpp index edc8625..c759477 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -324,19 +324,6 @@ Bu::Archive &Bu::operator>>(Bu::Archive &s, Bu::Archival &p) return s; } -/* -Bu::Archive &Bu::operator&&(Bu::Archive &s, Bu::Archival &p) -{ - if (s.isLoading()) - { - return s >> p; - } - else - { - return s << p; - } -}*/ - Bu::Archive &Bu::operator<<( Bu::Archive &ar, std::string &s ) { ar << (uint32_t)s.length(); diff --git a/src/archive.h b/src/archive.h index a8ce53e..9ac3303 100644 --- a/src/archive.h +++ b/src/archive.h @@ -5,6 +5,7 @@ #include #include "archival.h" #include "stream.h" +#include namespace Bu { @@ -92,6 +93,34 @@ namespace Bu return ar << dat; } } + + template Archive &operator<<( Archive &ar, std::list &l ) + { + typename std::list::size_type num = l.size(); + ar << num; + for( typename std::list::const_iterator i = l.begin(); i != l.end(); + i++ ) + { + ar << *i; + } + + return ar; + } + + template Archive &operator>>( Archive &ar, std::list &l ) + { + typename std::list::size_type num; + ar >> num; + + l.resize( num ); + for( typename std::list::const_iterator i = l.begin(); + i != l.end(); i++ ) + { + ar >> *i; + } + + return ar; + } } #endif diff --git a/src/unit/sfile.cpp b/src/unit/sfile.cpp index 3f52272..2aff312 100644 --- a/src/unit/sfile.cpp +++ b/src/unit/sfile.cpp @@ -87,7 +87,7 @@ public: unitTest( sf.isEOS() == false ); try { - int r = sf.read( buf, 5 ); + sf.read( buf, 5 ); unitFailed("No exception thrown"); } catch( Bu::FileException &e ) -- cgit v1.2.3 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(-) (limited to 'src/archive.cpp') 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