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 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'src/fstring.h') 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 { -- cgit v1.2.3