From 2b8255fabce194b35f7b2a350fd08f43d1e698a6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 19 Mar 2007 18:44:25 +0000 Subject: Fixed some bugs and added some new goo. You can serialize FStrings and Heshes now. --- src/fstring.h | 61 ++++++++++++++++++++++++++------- src/hash.h | 47 +++++++++++++++++++++++++- src/serializer.cpp | 94 ++++++++++++++++++++++++++++----------------------- src/serializer.h | 29 ++++++++-------- src/tests/fstring.cpp | 1 + 5 files changed, 162 insertions(+), 70 deletions(-) diff --git a/src/fstring.h b/src/fstring.h index 585073f..2f508b7 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -3,8 +3,10 @@ #include #include +#include "serializable.h" +#include "serializer.h" -template< typename chr=char > +template< typename chr > struct FStringChunk { long nLength; @@ -23,9 +25,12 @@ struct FStringChunk * data is actually copied. This also means that you never need to put any * FBasicString into a ref-counting container class. */ -template< typename chr=char, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > -class FBasicString +template< typename chr, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > +class FBasicString : public Serializable { +#ifndef VALTEST +#define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) ) +#endif private: typedef struct FStringChunk Chunk; typedef struct FBasicString MyType; @@ -57,16 +62,6 @@ public: append( pData, nLength ); } - /* - FBasicString( MyType &rSrc ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - joinShare( rSrc ); - }*/ - FBasicString( const MyType &rSrc ) : nLength( 0 ), pnRefs( NULL ), @@ -131,6 +126,23 @@ public: realClear(); } + void resize( long nNewSize ) + { + if( nLength == nNewSize ) + return; + + flatten(); + + Chunk *pNew = newChunk( nNewSize ); + long nNewLen = (nNewSizepData, pFirst->pData, nNewLen ); + pNew->pData[nNewLen] = (chr)0; + aChr.deallocate( pFirst->pData, pFirst->nLength+1 ); + aChunk.deallocate( pFirst, 1 ); + pFirst = pLast = pNew; + nLength = nNewSize; + } + chr *c_str() { if( pFirst == NULL ) @@ -243,6 +255,27 @@ public: return pFirst->pData[nIndex]; } + void serialize( class Serializer &ar ) + { + if( ar.isLoading() ) + { + clear(); + long nLen; + ar >> nLen; + + Chunk *pNew = newChunk( nLen ); + ar.read( pNew->pData, nLen*sizeof(chr) ); + appendChunk( pNew ); + } + else + { + flatten(); + + ar << nLength; + ar.write( pFirst->pData, nLength*sizeof(chr) ); + } + } + private: void flatten() const { @@ -471,6 +504,7 @@ private: realClear(); } +#ifdef VALTEST void cpy( chr *dest, const chr *src, long count ) const { for( int j = 0; j < count; j++ ) @@ -480,6 +514,7 @@ private: src++; } } +#endif void initCount() const { diff --git a/src/hash.h b/src/hash.h index 6671ae6..c37cfba 100644 --- a/src/hash.h +++ b/src/hash.h @@ -7,6 +7,8 @@ #include #include "exceptionbase.h" #include "hashable.h" +#include "serializable.h" +#include "serializer.h" #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) @@ -241,7 +243,7 @@ public: uint32_t size() { - return nFilled; + return nFilled-nDeleted; } uint32_t getDeleted() @@ -667,4 +669,47 @@ template<> bool __cmpHashKeys( const std::string &a, const std::str template<> uint32_t __calcHashCode( const Hashable &k ); template<> bool __cmpHashKeys( const Hashable &a, const Hashable &b ); +template +Serializer &operator<<( Serializer &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 +Serializer &operator>>( Serializer &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; +} + +template +Serializer &operator&&( Serializer &ar, Hash &h ) +{ + if( ar.isLoading() ) + { + return ar >> h; + } + else + { + return ar << h; + } +} + #endif diff --git a/src/serializer.cpp b/src/serializer.cpp index 407aab2..636224e 100644 --- a/src/serializer.cpp +++ b/src/serializer.cpp @@ -59,6 +59,11 @@ Serializer &Serializer::operator<<(uint64_t p) write( &p, sizeof(p) ); return *this; } +Serializer &Serializer::operator<<(long p) +{ + write( &p, sizeof(p) ); + return *this; +} Serializer &Serializer::operator<<(float p) { write( &p, sizeof(p) ); @@ -120,6 +125,11 @@ Serializer &Serializer::operator>>(uint64_t &p) read( &p, sizeof(p) ); return *this; } +Serializer &Serializer::operator>>(long &p) +{ + read( &p, sizeof(p) ); + return *this; +} Serializer &Serializer::operator>>(float &p) { read( &p, sizeof(p) ); @@ -136,149 +146,149 @@ Serializer &Serializer::operator>>(long double &p) return *this; } -/*Serializer::Serializer &operator&(bool &p) +Serializer &Serializer::operator&&(bool &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(int8_t &p) +Serializer &Serializer::operator&&(int8_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(int16_t &p) +Serializer &Serializer::operator&&(int16_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(int32_t &p) +Serializer &Serializer::operator&&(int32_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(int64_t &p) +Serializer &Serializer::operator&&(int64_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(uint8_t &p) +Serializer &Serializer::operator&&(uint8_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(uint16_t &p) +Serializer &Serializer::operator&&(uint16_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(uint32_t &p) +Serializer &Serializer::operator&&(uint32_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(uint64_t &p) +Serializer &Serializer::operator&&(uint64_t &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(float &p) +Serializer &Serializer::operator&&(float &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(double &p) +Serializer &Serializer::operator&&(double &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } } -Serializer::Serializer &operator&(long double &p) +Serializer &Serializer::operator&&(long double &p) { if (bLoading) { - return *this << p; + return *this >> p; } else { - return *this >> p; + return *this << p; } -}*/ +} Serializer &operator<<(Serializer &s, Serializable &p) @@ -293,17 +303,17 @@ Serializer &operator>>(Serializer &s, Serializable &p) return s; } -/*Serializer::Serializer &operator&(Serializable &p) +Serializer &operator&&(Serializer &s, Serializable &p) { - if (bLoading) + if (s.isLoading()) { - return *this << p; + return s >> p; } else { - return *this >> p; + return s << p; } -}*/ +} Serializer &operator<<( Serializer &ar, std::string &s ) { diff --git a/src/serializer.h b/src/serializer.h index e79c810..3af489c 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -35,6 +35,7 @@ public: virtual Serializer &operator<<(uint16_t); virtual Serializer &operator<<(uint32_t); virtual Serializer &operator<<(uint64_t); + virtual Serializer &operator<<(long); virtual Serializer &operator<<(float); virtual Serializer &operator<<(double); virtual Serializer &operator<<(long double); @@ -48,30 +49,30 @@ public: virtual Serializer &operator>>(uint16_t &); virtual Serializer &operator>>(uint32_t &); virtual Serializer &operator>>(uint64_t &); + virtual Serializer &operator>>(long &); virtual Serializer &operator>>(float &); virtual Serializer &operator>>(double &); virtual Serializer &operator>>(long double &); - /* - virtual Serializer &operator&(bool &); - virtual Serializer &operator&(int8_t &); - virtual Serializer &operator&(int16_t &); - virtual Serializer &operator&(int32_t &); - virtual Serializer &operator&(int64_t &); - virtual Serializer &operator&(uint8_t &); - virtual Serializer &operator&(uint16_t &); - virtual Serializer &operator&(uint32_t &); - virtual Serializer &operator&(uint64_t &); - virtual Serializer &operator&(float &); - virtual Serializer &operator&(double &); - virtual Serializer &operator&(long double &); - */ + virtual Serializer &operator&&(bool &); + virtual Serializer &operator&&(int8_t &); + virtual Serializer &operator&&(int16_t &); + virtual Serializer &operator&&(int32_t &); + virtual Serializer &operator&&(int64_t &); + virtual Serializer &operator&&(uint8_t &); + virtual Serializer &operator&&(uint16_t &); + virtual Serializer &operator&&(uint32_t &); + virtual Serializer &operator&&(uint64_t &); + virtual Serializer &operator&&(float &); + virtual Serializer &operator&&(double &); + virtual Serializer &operator&&(long double &); //virtual Serializer &operator&(Serializable &); }; Serializer &operator<<(Serializer &, class Serializable &); Serializer &operator>>(Serializer &, class Serializable &); +Serializer &operator&&(Serializer &s, class Serializable &p); Serializer &operator<<(Serializer &, std::string &); Serializer &operator>>(Serializer &, std::string &); diff --git a/src/tests/fstring.cpp b/src/tests/fstring.cpp index 33e24b4..271738c 100644 --- a/src/tests/fstring.cpp +++ b/src/tests/fstring.cpp @@ -41,6 +41,7 @@ int main( int argc, char *argv ) pem; thing( str2 ); + thing("test."); printf("%d == %d\n", __calcHashCode( str ), __calcHashCode( str.c_str() ) ); } -- cgit v1.2.3