From f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 03:49:53 +0000 Subject: Ok, no code is left in src, it's all in src/old. We'll gradually move code back into src as it's fixed and re-org'd. This includes tests, which, I may write a unit test system into libbu++ just to make my life easier. --- src/fstring.cpp | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 src/fstring.cpp (limited to 'src/fstring.cpp') diff --git a/src/fstring.cpp b/src/fstring.cpp deleted file mode 100644 index 82d024d..0000000 --- a/src/fstring.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "fstring.h" -#include "hash.h" - -template<> uint32_t __calcHashCode( const FString &k ) -{ - return __calcHashCode( k.c_str() ); -} - -template<> bool __cmpHashKeys( const FString &a, const FString &b ) -{ - return a == b; -} - -- cgit v1.2.3 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/archable.cpp | 10 + src/archable.h | 35 +++ src/archive.cpp | 337 +++++++++++++++++++++ src/archive.h | 93 ++++++ src/exceptionbase.cpp | 70 +++++ src/exceptionbase.h | 114 +++++++ src/exceptions.cpp | 10 + src/exceptions.h | 28 ++ src/fstring.cpp | 14 + src/fstring.h | 653 ++++++++++++++++++++++++++++++++++++++++ src/hash.cpp | 101 +++++++ src/hash.h | 745 ++++++++++++++++++++++++++++++++++++++++++++++ src/old/exceptionbase.cpp | 70 ----- src/old/exceptionbase.h | 105 ------- src/old/exceptions.cpp | 8 - src/old/exceptions.h | 25 -- src/old/fstring.cpp | 13 - src/old/fstring.h | 651 ---------------------------------------- src/old/hash.cpp | 113 ------- src/old/hash.h | 744 --------------------------------------------- src/old/hashable.cpp | 1 - src/old/hashable.h | 12 - src/old/serializable.cpp | 8 - src/old/serializable.h | 34 --- src/old/serializer.cpp | 338 --------------------- src/old/serializer.h | 80 ----- src/old/stream.cpp | 10 - src/old/stream.h | 27 -- src/stream.cpp | 10 + src/stream.h | 34 +++ src/tests/archive.cpp | 7 + tests/comments.xml | 12 - tests/guy.cpp | 22 -- tests/makeplugin.sh | 3 - 34 files changed, 2261 insertions(+), 2276 deletions(-) create mode 100644 src/archable.cpp create mode 100644 src/archable.h create mode 100644 src/archive.cpp create mode 100644 src/archive.h create mode 100644 src/exceptionbase.cpp create mode 100644 src/exceptionbase.h create mode 100644 src/exceptions.cpp create mode 100644 src/exceptions.h create mode 100644 src/fstring.cpp create mode 100644 src/fstring.h create mode 100644 src/hash.cpp create mode 100644 src/hash.h delete mode 100644 src/old/exceptionbase.cpp delete mode 100644 src/old/exceptionbase.h delete mode 100644 src/old/exceptions.cpp delete mode 100644 src/old/exceptions.h delete mode 100644 src/old/fstring.cpp delete mode 100644 src/old/fstring.h delete mode 100644 src/old/hash.cpp delete mode 100644 src/old/hash.h delete mode 100644 src/old/hashable.cpp delete mode 100644 src/old/hashable.h delete mode 100644 src/old/serializable.cpp delete mode 100644 src/old/serializable.h delete mode 100644 src/old/serializer.cpp delete mode 100644 src/old/serializer.h delete mode 100644 src/old/stream.cpp delete mode 100644 src/old/stream.h create mode 100644 src/stream.cpp create mode 100644 src/stream.h create mode 100644 src/tests/archive.cpp delete mode 100644 tests/comments.xml delete mode 100644 tests/guy.cpp delete mode 100755 tests/makeplugin.sh (limited to 'src/fstring.cpp') diff --git a/src/archable.cpp b/src/archable.cpp new file mode 100644 index 0000000..38fc31f --- /dev/null +++ b/src/archable.cpp @@ -0,0 +1,10 @@ +#include "archable.h" + +Bu::Archable::Archable() +{ +} + +Bu::Archable::~Archable() +{ +} + diff --git a/src/archable.h b/src/archable.h new file mode 100644 index 0000000..ed05a78 --- /dev/null +++ b/src/archable.h @@ -0,0 +1,35 @@ +#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/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; +} + diff --git a/src/archive.h b/src/archive.h new file mode 100644 index 0000000..7de9220 --- /dev/null +++ b/src/archive.h @@ -0,0 +1,93 @@ +#ifndef ARCHIVE_H +#define ARCHIVE_H + +#include +#include +#include "archable.h" + +namespace Bu +{ + class Archive + { + private: + bool bLoading; + public: + bool isLoading(); + + enum + { + load = true, + save = false + }; + + Archive( bool bLoading ); + virtual ~Archive(); + virtual void close()=0; + + virtual void write(const void *, int32_t)=0; + virtual void read(void *, int32_t)=0; + + virtual Archive &operator<<(bool); + virtual Archive &operator<<(int8_t); + virtual Archive &operator<<(int16_t); + virtual Archive &operator<<(int32_t); + virtual Archive &operator<<(int64_t); + virtual Archive &operator<<(uint8_t); + virtual Archive &operator<<(uint16_t); + virtual Archive &operator<<(uint32_t); + virtual Archive &operator<<(uint64_t); + virtual Archive &operator<<(long); + virtual Archive &operator<<(float); + virtual Archive &operator<<(double); + virtual Archive &operator<<(long double); + + virtual Archive &operator>>(bool &); + virtual Archive &operator>>(int8_t &); + virtual Archive &operator>>(int16_t &); + virtual Archive &operator>>(int32_t &); + virtual Archive &operator>>(int64_t &); + virtual Archive &operator>>(uint8_t &); + virtual Archive &operator>>(uint16_t &); + virtual Archive &operator>>(uint32_t &); + virtual Archive &operator>>(uint64_t &); + virtual Archive &operator>>(long &); + virtual Archive &operator>>(float &); + virtual Archive &operator>>(double &); + virtual Archive &operator>>(long double &); + + virtual Archive &operator&&(bool &); + virtual Archive &operator&&(int8_t &); + virtual Archive &operator&&(int16_t &); + virtual Archive &operator&&(int32_t &); + virtual Archive &operator&&(int64_t &); + virtual Archive &operator&&(uint8_t &); + virtual Archive &operator&&(uint16_t &); + virtual Archive &operator&&(uint32_t &); + virtual Archive &operator&&(uint64_t &); + virtual Archive &operator&&(float &); + virtual Archive &operator&&(double &); + virtual Archive &operator&&(long double &); + }; + + Archive &operator<<(Archive &, class Bu::Archable &); + Archive &operator>>(Archive &, class Bu::Archable &); + //Archive &operator&&(Archive &s, class Bu::Archable &p); + + Archive &operator<<(Archive &, std::string &); + Archive &operator>>(Archive &, std::string &); + //Archive &operator&&(Archive &, std::string &); + + template Archive &operator&&( Archive &ar, T &dat ) + { + if( ar.isLoading() ) + { + return ar >> dat; + } + else + { + return ar << dat; + } + } +} + +#endif diff --git a/src/exceptionbase.cpp b/src/exceptionbase.cpp new file mode 100644 index 0000000..f6ec625 --- /dev/null +++ b/src/exceptionbase.cpp @@ -0,0 +1,70 @@ +#include "exceptionbase.h" +#include + +Bu::ExceptionBase::ExceptionBase( const char *lpFormat, ... ) throw() : + nErrorCode( 0 ), + sWhat( NULL ) +{ + va_list ap; + + va_start(ap, lpFormat); + setWhat( lpFormat, ap ); + va_end(ap); +} + +Bu::ExceptionBase::ExceptionBase( int nCode, const char *lpFormat, ... ) throw() : + nErrorCode( nCode ), + sWhat( NULL ) +{ + va_list ap; + + va_start(ap, lpFormat); + setWhat( lpFormat, ap ); + va_end(ap); +} + +Bu::ExceptionBase::ExceptionBase( int nCode ) throw() : + nErrorCode( nCode ), + sWhat( NULL ) +{ +} + +Bu::ExceptionBase::~ExceptionBase() throw() +{ + if( sWhat ) + { + delete[] sWhat; + sWhat = NULL; + } +} + +void Bu::ExceptionBase::setWhat( const char *lpFormat, va_list &vargs ) +{ + if( sWhat ) delete[] sWhat; + int nSize; + + nSize = vsnprintf( NULL, 0, lpFormat, vargs ); + sWhat = new char[nSize+1]; + vsnprintf( sWhat, nSize+1, lpFormat, vargs ); +} + +void Bu::ExceptionBase::setWhat( const char *lpText ) +{ + if( sWhat ) delete[] sWhat; + int nSize; + + nSize = strlen( lpText ); + sWhat = new char[nSize+1]; + strcpy( sWhat, lpText ); +} + +const char *Bu::ExceptionBase::what() const throw() +{ + return sWhat; +} + +int Bu::ExceptionBase::getErrorCode() +{ + return nErrorCode; +} + diff --git a/src/exceptionbase.h b/src/exceptionbase.h new file mode 100644 index 0000000..fd78089 --- /dev/null +++ b/src/exceptionbase.h @@ -0,0 +1,114 @@ +#ifndef EXCEPTION_BASE_H +#define EXCEPTION_BASE_H + +#include +#include +#include + +namespace Bu +{ + /** + * A generalized Exception base class. This is nice for making general and + * flexible child classes that can create new error code classes. + * + * In order to create your own exception class use these two lines. + * + * in your header: subExceptionDecl( NewClassName ); + * + * in your source: subExcpetienDef( NewClassName ); + */ + class ExceptionBase : public std::exception + { + public: + /** + * Construct an exception with an error code of zero, but with a + * description. The use of this is not reccomended most of the time, + * it's generally best to include an error code with the exception so + * your program can handle the exception in a better way. + * @param sFormat The format of the text. See printf for more info. + */ + ExceptionBase( const char *sFormat, ... ) throw(); + + /** + * + * @param nCode + * @param sFormat + */ + ExceptionBase( int nCode, const char *sFormat, ... ) throw(); + + /** + * + * @param nCode + * @return + */ + ExceptionBase( int nCode=0 ) throw(); + + /** + * + * @return + */ + virtual ~ExceptionBase() throw(); + + /** + * + * @return + */ + virtual const char *what() const throw(); + + /** + * + * @return + */ + int getErrorCode(); + + /** + * + * @param lpFormat + * @param vargs + */ + void setWhat( const char *lpFormat, va_list &vargs ); + + /** + * + * @param lpText + */ + void setWhat( const char *lpText ); + + private: + int nErrorCode; /**< The code for the error that occured. */ + char *sWhat; /**< The text string telling people what went wrong. */ + }; +} + +#define subExceptionDecl( name ) \ +class name : public ExceptionBase \ +{ \ + public: \ + name( const char *sFormat, ... ) throw (); \ + name( int nCode, const char *sFormat, ... ) throw(); \ + name( int nCode=0 ) throw (); \ +}; + +#define subExceptionDef( name ) \ +name::name( const char *lpFormat, ... ) throw() : \ + ExceptionBase( 0 ) \ +{ \ + va_list ap; \ + va_start( ap, lpFormat ); \ + setWhat( lpFormat, ap ); \ + va_end( ap ); \ +} \ +name::name( int nCode, const char *lpFormat, ... ) throw() : \ + ExceptionBase( nCode ) \ +{ \ + va_list ap; \ + va_start( ap, lpFormat ); \ + setWhat( lpFormat, ap ); \ + va_end( ap ); \ +} \ +name::name( int nCode ) throw() : \ + ExceptionBase( nCode ) \ +{ \ +} + +#endif diff --git a/src/exceptions.cpp b/src/exceptions.cpp new file mode 100644 index 0000000..37f09a4 --- /dev/null +++ b/src/exceptions.cpp @@ -0,0 +1,10 @@ +#include "exceptions.h" +#include + +namespace Bu +{ + subExceptionDef( XmlException ) + subExceptionDef( FileException ) + subExceptionDef( ConnectionException ) + subExceptionDef( PluginException ) +} diff --git a/src/exceptions.h b/src/exceptions.h new file mode 100644 index 0000000..b28d292 --- /dev/null +++ b/src/exceptions.h @@ -0,0 +1,28 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +#include "exceptionbase.h" +#include + +namespace Bu +{ + subExceptionDecl( XmlException ) + subExceptionDecl( FileException ) + subExceptionDecl( ConnectionException ) + subExceptionDecl( PluginException ) + + enum eFileException + { + excodeEOF + }; + + enum eConnectionException + { + excodeReadError, + excodeBadReadError, + excodeConnectionClosed, + excodeSocketTimeout + }; +} + +#endif diff --git a/src/fstring.cpp b/src/fstring.cpp new file mode 100644 index 0000000..56d2173 --- /dev/null +++ b/src/fstring.cpp @@ -0,0 +1,14 @@ +#include "fstring.h" +#include "hash.h" + +template<> uint32_t Bu::__calcHashCode( const Bu::FString &k ) +{ + return __calcHashCode( k.c_str() ); +} + +template<> bool Bu::__cmpHashKeys( + const Bu::FString &a, const Bu::FString &b ) +{ + return a == b; +} + diff --git a/src/fstring.h b/src/fstring.h new file mode 100644 index 0000000..717068f --- /dev/null +++ b/src/fstring.h @@ -0,0 +1,653 @@ +#ifndef F_STRING_H +#define F_STRING_H + +#include +#include +#include "archable.h" +#include "archive.h" +#include "hash.h" + +namespace Bu +{ + template< typename chr > + struct FStringChunk + { + long nLength; + chr *pData; + FStringChunk *pNext; + }; + + /** + * Flexible String class. This class was designed with string passing and + * generation in mind. Like the standard string class you can specify what + * datatype to use for each character. Unlike the standard string class, + * collection of appended and prepended terms is done lazily, making long + * operations that involve many appends very inexpensive. In addition internal + * ref-counting means that if you pass strings around between functions there's + * almost no overhead in time or memory since a reference is created and no + * data is actually copied. This also means that you never need to put any + * FBasicString into a ref-counting container class. + */ + template< typename chr, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > + class FBasicString : public Archable + { +#ifndef VALTEST +#define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) ) +#endif + private: + typedef struct FStringChunk Chunk; + typedef struct FBasicString MyType; + + public: + FBasicString() : + nLength( 0 ), + pnRefs( NULL ), + pFirst( NULL ), + pLast( NULL ) + { + } + + FBasicString( const chr *pData ) : + nLength( 0 ), + pnRefs( NULL ), + pFirst( NULL ), + pLast( NULL ) + { + append( pData ); + } + + FBasicString( const chr *pData, long nLength ) : + nLength( 0 ), + pnRefs( NULL ), + pFirst( NULL ), + pLast( NULL ) + { + append( pData, nLength ); + } + + FBasicString( const MyType &rSrc ) : + nLength( 0 ), + pnRefs( NULL ), + pFirst( NULL ), + pLast( NULL ) + { + // Here we have no choice but to copy, since the other guy is a const. + // In the case that the source were flat, we could get a reference, it + // would make some things faster, but not matter in many other cases. + + joinShare( rSrc ); + //copyFrom( rSrc ); + } + + FBasicString( const MyType &rSrc, long nLength ) : + nLength( 0 ), + pnRefs( NULL ), + pFirst( NULL ), + pLast( NULL ) + { + append( rSrc.pFirst->pData, nLength ); + } + + FBasicString( const MyType &rSrc, long nStart, long nLength ) : + nLength( 0 ), + pnRefs( NULL ), + pFirst( NULL ), + pLast( NULL ) + { + append( rSrc.pFirst->pData+nStart, nLength ); + } + + FBasicString( long nSize ) : + nLength( nSize ), + pnRefs( NULL ), + pFirst( NULL ), + pLast( NULL ) + { + pFirst = pLast = newChunk( nSize ); + } + + virtual ~FBasicString() + { + clear(); + } + + void append( const chr *pData ) + { + long nLen; + for( nLen = 0; pData[nLen] != (chr)0; nLen++ ); + + Chunk *pNew = newChunk( nLen ); + cpy( pNew->pData, pData, nLen ); + + appendChunk( pNew ); + } + + void append( const chr *pData, long nLen ) + { + Chunk *pNew = newChunk( nLen ); + + cpy( pNew->pData, pData, nLen ); + + appendChunk( pNew ); + } + + void prepend( const chr *pData ) + { + long nLen; + for( nLen = 0; pData[nLen] != (chr)0; nLen++ ); + + Chunk *pNew = newChunk( nLen ); + cpy( pNew->pData, pData, nLen ); + + prependChunk( pNew ); + } + + void prepend( const chr *pData, long nLen ) + { + Chunk *pNew = newChunk( nLen ); + + cpy( pNew->pData, pData, nLen ); + + prependChunk( pNew ); + } + + void clear() + { + 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; + } + + long getSize() const + { + return nLength; + } + + chr *getStr() + { + if( pFirst == NULL ) + return NULL; + + flatten(); + return pFirst->pData; + } + + const chr *getStr() const + { + if( pFirst == NULL ) + return NULL; + + flatten(); + return pFirst->pData; + } + + chr *c_str() + { + if( pFirst == NULL ) + return NULL; + + flatten(); + return pFirst->pData; + } + + const chr *c_str() const + { + if( pFirst == NULL ) + return NULL; + + flatten(); + return pFirst->pData; + } + + MyType &operator +=( const chr *pData ) + { + append( pData ); + + return (*this); + } + + MyType &operator +=( const MyType &rSrc ) + { + rSrc.flatten(); + append( rSrc.pFirst->pData, rSrc.nLength ); + + return (*this); + } + + MyType &operator +=( const chr pData ) + { + chr tmp[2] = { pData, (chr)0 }; + append( tmp ); + + return (*this); + } + + MyType &operator =( const chr *pData ) + { + clear(); + append( pData ); + + return (*this); + } + + MyType &operator =( const MyType &rSrc ) + { + //if( rSrc.isFlat() ) + //{ + joinShare( rSrc ); + //} + //else + //{ + // copyFrom( rSrc ); + //} + // + + return (*this); + } + + bool operator ==( const chr *pData ) const + { + if( pFirst == NULL ) { + if( pData == NULL ) + return true; + return false; + } + + flatten(); + const chr *a = pData; + chr *b = pFirst->pData; + for( ; *a!=(chr)0; a++, b++ ) + { + if( *a != *b ) + return false; + } + + return true; + } + + bool operator ==( const MyType &pData ) const + { + if( pFirst == pData.pFirst ) + return true; + if( pFirst == NULL ) + return false; + + flatten(); + pData.flatten(); + const chr *a = pData.pFirst->pData; + chr *b = pFirst->pData; + for( ; *a!=(chr)0; a++, b++ ) + { + if( *a != *b ) + return false; + } + + return true; + } + + bool operator !=(const chr *pData ) const + { + return !(*this == pData); + } + + bool operator !=(const MyType &pData ) const + { + return !(*this == pData); + } + + chr &operator[]( long nIndex ) + { + flatten(); + + return pFirst->pData[nIndex]; + } + + const chr &operator[]( long nIndex ) const + { + flatten(); + + return pFirst->pData[nIndex]; + } + + bool isWS( long nIndex ) const + { + flatten(); + + return pFirst->pData[nIndex]==' ' || pFirst->pData[nIndex]=='\t' + || pFirst->pData[nIndex]=='\r' || pFirst->pData[nIndex]=='\n'; + } + + bool isAlpha( long nIndex ) const + { + flatten(); + + return (pFirst->pData[nIndex] >= 'a' && pFirst->pData[nIndex] <= 'z') + || (pFirst->pData[nIndex] >= 'A' && pFirst->pData[nIndex] <= 'Z'); + } + + void toLower() + { + flatten(); + unShare(); + + for( long j = 0; j < nLength; j++ ) + { + if( pFirst->pData[j] >= 'A' && pFirst->pData[j] <= 'Z' ) + pFirst->pData[j] -= 'A'-'a'; + } + } + + void toUpper() + { + flatten(); + unShare(); + + for( long j = 0; j < nLength; j++ ) + { + if( pFirst->pData[j] >= 'a' && pFirst->pData[j] <= 'z' ) + pFirst->pData[j] += 'A'-'a'; + } + } + + void archive( class Archive &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 + { + if( isFlat() ) + return; + + if( pFirst == NULL ) + return; + + unShare(); + + Chunk *pNew = newChunk( nLength ); + chr *pos = pNew->pData; + Chunk *i = pFirst; + for(;;) + { + cpy( pos, i->pData, i->nLength ); + pos += i->nLength; + i = i->pNext; + if( i == NULL ) + break; + } + realClear(); + + pLast = pFirst = pNew; + nLength = pNew->nLength; + } + + void realClear() const + { + if( pFirst == NULL ) + return; + + if( isShared() ) + { + decRefs(); + } + else + { + Chunk *i = pFirst; + for(;;) + { + Chunk *n = i->pNext; + aChr.deallocate( i->pData, i->nLength+1 ); + aChunk.deallocate( i, 1 ); + if( n == NULL ) + break; + i = n; + } + pFirst = pLast = NULL; + nLength = 0; + } + } + + void copyFrom( const FBasicString &rSrc ) + { + if( rSrc.pFirst == NULL ) + return; + + decRefs(); + + Chunk *pNew = newChunk( rSrc.nLength ); + chr *pos = pNew->pData; + Chunk *i = rSrc.pFirst; + for(;;) + { + cpy( pos, i->pData, i->nLength ); + pos += i->nLength; + i = i->pNext; + if( i == NULL ) + break; + } + clear(); + + appendChunk( pNew ); + } + + bool isFlat() const + { + return (pFirst == pLast); + } + + bool isShared() const + { + return (pnRefs != NULL); + } + + Chunk *newChunk() const + { + Chunk *pNew = aChunk.allocate( 1 ); + pNew->pNext = NULL; + return pNew; + } + + Chunk *newChunk( long nLen ) const + { + Chunk *pNew = aChunk.allocate( 1 ); + pNew->pNext = NULL; + pNew->nLength = nLen; + pNew->pData = aChr.allocate( nLen+1 ); + pNew->pData[nLen] = (chr)0; + return pNew; + } + + void appendChunk( Chunk *pNewChunk ) + { + unShare(); + + if( pFirst == NULL ) + pLast = pFirst = pNewChunk; + else + { + pLast->pNext = pNewChunk; + pLast = pNewChunk; + } + + nLength += pNewChunk->nLength; + } + + void prependChunk( Chunk *pNewChunk ) + { + unShare(); + + if( pFirst == NULL ) + pLast = pFirst = pNewChunk; + else + { + pNewChunk->pNext = pFirst; + pFirst = pNewChunk; + } + + nLength += pNewChunk->nLength; + } + + void joinShare( MyType &rSrc ) + { + clear(); + + if( !rSrc.isFlat() ) + rSrc.flatten(); + + rSrc.initCount(); + pnRefs = rSrc.pnRefs; + (*pnRefs)++; + nLength = rSrc.nLength; + pFirst = rSrc.pFirst; + pLast = rSrc.pLast; + } + + void joinShare( const MyType &rSrc ) + { + clear(); + + rSrc.flatten(); + + if( !rSrc.isShared() ) + { + rSrc.pnRefs = new uint32_t; + (*rSrc.pnRefs) = 1; + } + pnRefs = rSrc.pnRefs; + (*pnRefs)++; + nLength = rSrc.nLength; + pFirst = rSrc.pFirst; + pLast = rSrc.pLast; + } + + /** + * This takes an object that was shared and makes a copy of the base data + * that was being shared so that this copy can be changed. This should be + * added before any call that will change this object; + */ + void unShare() const + { + if( isShared() == false ) + return; + + Chunk *pNew = newChunk( nLength ); + chr *pos = pNew->pData; + Chunk *i = pFirst; + for(;;) + { + cpy( pos, i->pData, i->nLength ); + pos += i->nLength; + i = i->pNext; + if( i == NULL ) + break; + } + decRefs(); + pLast = pFirst = pNew; + nLength = pNew->nLength; + } + + /** + * This decrements our ref count and pulls us out of the share. If the ref + * count hits zero because of this, it destroys the share. This is not + * safe to call on it's own, it's much better to call unShare. + */ + void decRefs() const + { + if( isShared() ) + { + (*pnRefs)--; + if( (*pnRefs) == 0 ) + destroyShare(); + else + { + pnRefs = NULL; + pFirst = NULL; + pLast = NULL; + nLength = 0; + } + } + } + + /** + * While the unShare function removes an instance from a share, this + * function destroys the data that was in the share, removing the share + * itself. This should only be called when the refcount for the share has + * or is about to reach zero. + */ + void destroyShare() const + { + delete pnRefs; + pnRefs = NULL; + realClear(); + } + +#ifdef VALTEST + void cpy( chr *dest, const chr *src, long count ) const + { + for( int j = 0; j < count; j++ ) + { + *dest = *src; + dest++; + src++; + } + } +#endif + + void initCount() const + { + if( !isShared() ) + { + pnRefs = new uint32_t; + (*pnRefs) = 1; + } + } + + private: + mutable long nLength; + mutable uint32_t *pnRefs; + mutable Chunk *pFirst; + mutable Chunk *pLast; + + mutable chralloc aChr; + mutable chunkalloc aChunk; + }; + + typedef FBasicString FString; + + template<> uint32_t __calcHashCode( const FString &k ); + template<> bool __cmpHashKeys( const FString &a, const FString &b ); +} + +#endif diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 0000000..a207c29 --- /dev/null +++ b/src/hash.cpp @@ -0,0 +1,101 @@ +#include "hash.h" + +namespace Bu { subExceptionDef( HashException ) } + +template<> uint32_t Bu::__calcHashCode( const int &k ) +{ + return k; +} + +template<> bool Bu::__cmpHashKeys( const int &a, const int &b ) +{ + return a == b; +} + +template<> uint32_t Bu::__calcHashCode( const unsigned int &k ) +{ + return k; +} + +template<> bool Bu::__cmpHashKeys( const unsigned int &a, const unsigned int &b ) +{ + return a == b; +} + +template<> +uint32_t Bu::__calcHashCode( const char * const &k ) +{ + if (k == NULL) + { + return 0; + } + + unsigned long int nPos = 0; + for( const char *s = k; *s; s++ ) + { + nPos = *s + (nPos << 6) + (nPos << 16) - nPos; + } + + return nPos; +} + +template<> bool Bu::__cmpHashKeys( const char * const &a, const char * const &b ) +{ + if( a == b ) + return true; + + for(int j=0; a[j] == b[j]; j++ ) + if( a[j] == '\0' ) + return true; + + return false; +} + +template<> +uint32_t Bu::__calcHashCode( char * const &k ) +{ + if (k == NULL) + { + return 0; + } + + unsigned long int nPos = 0; + for( const char *s = k; *s; s++ ) + { + nPos = *s + (nPos << 6) + (nPos << 16) - nPos; + } + + return nPos; +} + +template<> bool Bu::__cmpHashKeys( char * const &a, char * const &b ) +{ + if( a == b ) + return true; + + for(int j=0; a[j] == b[j]; j++ ) + if( a[j] == '\0' ) + return true; + + return false; +} + +template<> uint32_t Bu::__calcHashCode( const std::string &k ) +{ + std::string::size_type j, sz = k.size(); + const char *s = k.c_str(); + + unsigned long int nPos = 0; + for( j = 0; j < sz; j++, s++ ) + { + nPos = *s + (nPos << 6) + (nPos << 16) - nPos; + } + + return nPos; +} + +template<> bool Bu::__cmpHashKeys( const std::string &a, const std::string &b ) +{ + return a == b; +} + diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..9e498f1 --- /dev/null +++ b/src/hash.h @@ -0,0 +1,745 @@ +#ifndef HASH_H +#define HASH_H + +#include +#include +#include +#include +#include +#include +#include "exceptionbase.h" +#include "archable.h" +#include "archive.h" + +#define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) + +namespace Bu +{ + subExceptionDecl( HashException ) + + enum eHashException + { + excodeNotFilled + }; + + template + uint32_t __calcHashCode( const T &k ); + + template + bool __cmpHashKeys( const T &a, const T &b ); + + struct __calcNextTSize_fast + { + uint32_t operator()( uint32_t nCapacity, uint32_t nFill, uint32_t nDeleted ) const + { + if( nDeleted >= nCapacity/2 ) + return nCapacity; + return nCapacity*2+1; + } + }; + + template, typename valuealloc = std::allocator, typename challoc = std::allocator > + class Hash; + + template< typename key, typename _value, typename sizecalc = __calcNextTSize_fast, typename keyalloc = std::allocator, typename valuealloc = std::allocator<_value>, typename challoc = std::allocator > + struct HashProxy + { + friend class Hash; + private: + HashProxy( Hash &h, key *k, uint32_t nPos, uint32_t hash ) : + hsh( h ), + pKey( k ), + nPos( nPos ), + hash( hash ), + bFilled( false ) + { + } + + HashProxy( Hash &h, uint32_t nPos, _value *pValue ) : + hsh( h ), + nPos( nPos ), + pValue( pValue ), + bFilled( true ) + { + } + + Hash &hsh; + key *pKey; + uint32_t nPos; + _value *pValue; + uint32_t hash; + bool bFilled; + + public: + operator _value &() + { + if( bFilled == false ) + throw HashException( + excodeNotFilled, + "No data assosiated with that key." + ); + return *pValue; + } + + _value &value() + { + if( bFilled == false ) + throw HashException( + excodeNotFilled, + "No data assosiated with that key." + ); + return *pValue; + } + + bool isFilled() + { + return bFilled; + } + + void erase() + { + if( bFilled ) + { + hsh._erase( nPos ); + hsh.onDelete(); + } + } + + _value operator=( _value nval ) + { + if( bFilled ) + { + hsh.va.destroy( pValue ); + hsh.va.construct( pValue, nval ); + hsh.onUpdate(); + } + else + { + hsh.fill( nPos, *pKey, nval, hash ); + hsh.onInsert(); + } + + return nval; + } + + _value *operator->() + { + if( bFilled == false ) + throw HashException( + excodeNotFilled, + "No data assosiated with that key." + ); + return pValue; + } + }; + + template + class Hash + { + friend struct HashProxy; + public: + Hash() : + nCapacity( 11 ), + nFilled( 0 ), + nDeleted( 0 ), + bFilled( NULL ), + bDeleted( NULL ), + aKeys( NULL ), + aValues( NULL ), + aHashCodes( NULL ) + { + nKeysSize = bitsToBytes( nCapacity ); + bFilled = ca.allocate( nKeysSize ); + bDeleted = ca.allocate( nKeysSize ); + clearBits(); + + aHashCodes = ca.allocate( nCapacity ); + aKeys = ka.allocate( nCapacity ); + aValues = va.allocate( nCapacity ); + } + + Hash( const Hash &src ) : + nCapacity( src.nCapacity ), + nFilled( 0 ), + nDeleted( 0 ), + bFilled( NULL ), + bDeleted( NULL ), + aKeys( NULL ), + aValues( NULL ), + aHashCodes( NULL ) + { + nKeysSize = bitsToBytes( nCapacity ); + bFilled = ca.allocate( nKeysSize ); + bDeleted = ca.allocate( nKeysSize ); + clearBits(); + + aHashCodes = ca.allocate( nCapacity ); + aKeys = ka.allocate( nCapacity ); + aValues = va.allocate( nCapacity ); + + for( uint32_t j = 0; j < src.nCapacity; j++ ) + { + if( src.isFilled( j ) ) + { + insert( src.aKeys[j], src.aValues[j] ); + } + } + } + + Hash &operator=( const Hash &src ) + { + for( uint32_t j = 0; j < nCapacity; j++ ) + { + if( isFilled( j ) ) + if( !isDeleted( j ) ) + { + va.destroy( &aValues[j] ); + ka.destroy( &aKeys[j] ); + } + } + va.deallocate( aValues, nCapacity ); + ka.deallocate( aKeys, nCapacity ); + ca.deallocate( bFilled, nKeysSize ); + ca.deallocate( bDeleted, nKeysSize ); + ca.deallocate( aHashCodes, nCapacity ); + + nFilled = 0; + nDeleted = 0; + nCapacity = src.nCapacity; + nKeysSize = bitsToBytes( nCapacity ); + bFilled = ca.allocate( nKeysSize ); + bDeleted = ca.allocate( nKeysSize ); + clearBits(); + + aHashCodes = ca.allocate( nCapacity ); + aKeys = ka.allocate( nCapacity ); + aValues = va.allocate( nCapacity ); + + for( uint32_t j = 0; j < src.nCapacity; j++ ) + { + if( src.isFilled( j ) ) + { + insert( src.aKeys[j], src.aValues[j] ); + } + } + + return *this; + } + + virtual ~Hash() + { + for( uint32_t j = 0; j < nCapacity; j++ ) + { + if( isFilled( j ) ) + if( !isDeleted( j ) ) + { + va.destroy( &aValues[j] ); + ka.destroy( &aKeys[j] ); + } + } + va.deallocate( aValues, nCapacity ); + ka.deallocate( aKeys, nCapacity ); + ca.deallocate( bFilled, nKeysSize ); + ca.deallocate( bDeleted, nKeysSize ); + ca.deallocate( aHashCodes, nCapacity ); + } + + uint32_t getCapacity() + { + return nCapacity; + } + + uint32_t getFill() + { + return nFilled; + } + + uint32_t size() + { + return nFilled-nDeleted; + } + + uint32_t getDeleted() + { + return nDeleted; + } + + virtual HashProxy operator[]( key k ) + { + uint32_t hash = __calcHashCode( k ); + bool bFill; + uint32_t nPos = probe( hash, k, bFill ); + + if( bFill ) + { + return HashProxy( *this, nPos, &aValues[nPos] ); + } + else + { + return HashProxy( *this, &k, nPos, hash ); + } + } + + virtual void insert( key k, value v ) + { + uint32_t hash = __calcHashCode( k ); + bool bFill; + uint32_t nPos = probe( hash, k, bFill ); + + if( bFill ) + { + va.destroy( &aValues[nPos] ); + va.construct( &aValues[nPos], v ); + onUpdate(); + } + else + { + fill( nPos, k, v, hash ); + onInsert(); + } + } + + virtual void erase( key k ) + { + uint32_t hash = __calcHashCode( k ); + bool bFill; + uint32_t nPos = probe( hash, k, bFill ); + + if( bFill ) + { + _erase( nPos ); + onDelete(); + } + } + + struct iterator; + virtual void erase( struct iterator &i ) + { + if( this != &i.hsh ) + throw HashException("This iterator didn't come from this Hash."); + if( isFilled( i.nPos ) && !isDeleted( i.nPos ) ) + { + _erase( i.nPos ); + onDelete(); + } + } + + virtual void clear() + { + for( uint32_t j = 0; j < nCapacity; j++ ) + { + if( isFilled( j ) ) + if( !isDeleted( j ) ) + { + va.destroy( &aValues[j] ); + ka.destroy( &aKeys[j] ); + onDelete(); + } + } + + clearBits(); + } + + virtual value &get( key k ) + { + uint32_t hash = __calcHashCode( k ); + bool bFill; + uint32_t nPos = probe( hash, k, bFill ); + + if( bFill ) + { + return aValues[nPos]; + } + else + { + throw HashException( + excodeNotFilled, + "No data assosiated with that key." + ); + } + } + + virtual bool has( key k ) + { + bool bFill; + probe( __calcHashCode( k ), k, bFill, false ); + + return bFill; + } + + typedef struct iterator + { + friend class Hash; + private: + iterator( Hash &hsh ) : + hsh( hsh ), + nPos( 0 ), + bFinished( false ) + { + nPos = hsh.getFirstPos( bFinished ); + } + + iterator( Hash &hsh, bool bDone ) : + hsh( hsh ), + nPos( 0 ), + bFinished( bDone ) + { + } + + Hash &hsh; + uint32_t nPos; + bool bFinished; + + public: + iterator operator++( int ) + { + if( bFinished == false ) + nPos = hsh.getNextPos( nPos, bFinished ); + + return *this; + } + + iterator operator++() + { + if( bFinished == false ) + nPos = hsh.getNextPos( nPos, bFinished ); + + return *this; + } + + bool operator==( const iterator &oth ) + { + if( bFinished != oth.bFinished ) + return false; + if( bFinished == true ) + { + return true; + } + else + { + if( oth.nPos == nPos ) + return true; + return false; + } + } + + bool operator!=( const iterator &oth ) + { + return !(*this == oth ); + } + + iterator operator=( const iterator &oth ) + { + if( &hsh != &oth.hsh ) + throw HashException( + "Cannot mix iterators from different hash objects."); + nPos = oth.nPos; + bFinished = oth.bFinished; + } + + std::pair operator *() + { + return hsh.getAtPos( nPos ); + } + + key &getKey() + { + return hsh.getKeyAtPos( nPos ); + } + + value &getValue() + { + return hsh.getValueAtPos( nPos ); + } + }; + + iterator begin() + { + return iterator( *this ); + } + + iterator end() + { + return iterator( *this, true ); + } + + std::list getKeys() + { + std::list lKeys; + + for( uint32_t j = 0; j < nCapacity; j++ ) + { + if( isFilled( j ) ) + { + if( !isDeleted( j ) ) + { + lKeys.push_back( aKeys[j] ); + } + } + } + + return lKeys; + } + + protected: + virtual void onInsert() {} + virtual void onUpdate() {} + virtual void onDelete() {} + virtual void onReHash() {} + + virtual void clearBits() + { + for( uint32_t j = 0; j < nKeysSize; j++ ) + { + bFilled[j] = bDeleted[j] = 0; + } + } + + virtual void fill( uint32_t loc, key &k, value &v, uint32_t hash ) + { + bFilled[loc/32] |= (1<<(loc%32)); + va.construct( &aValues[loc], v ); + ka.construct( &aKeys[loc], k ); + aHashCodes[loc] = hash; + nFilled++; + //printf("Filled: %d, Deleted: %d, Capacity: %d\n", + // nFilled, nDeleted, nCapacity ); + } + + virtual void _erase( uint32_t loc ) + { + bDeleted[loc/32] |= (1<<(loc%32)); + va.destroy( &aValues[loc] ); + ka.destroy( &aKeys[loc] ); + nDeleted++; + //printf("Filled: %d, Deleted: %d, Capacity: %d\n", + // nFilled, nDeleted, nCapacity ); + } + + virtual std::pair getAtPos( uint32_t nPos ) + { + return std::pair(aKeys[nPos],aValues[nPos]); + } + + virtual key &getKeyAtPos( uint32_t nPos ) + { + return aKeys[nPos]; + } + + virtual value &getValueAtPos( uint32_t nPos ) + { + return aValues[nPos]; + } + + virtual uint32_t getFirstPos( bool &bFinished ) + { + for( uint32_t j = 0; j < nCapacity; j++ ) + { + if( isFilled( j ) ) + if( !isDeleted( j ) ) + return j; + } + + bFinished = true; + return 0; + } + + virtual uint32_t getNextPos( uint32_t nPos, bool &bFinished ) + { + for( uint32_t j = nPos+1; j < nCapacity; j++ ) + { + if( isFilled( j ) ) + if( !isDeleted( j ) ) + return j; + } + + bFinished = true; + return 0; + } + + uint32_t probe( uint32_t hash, key k, bool &bFill, bool rehash=true ) + { + uint32_t nCur = hash%nCapacity; + + // First we scan to see if the key is already there, abort if we + // run out of probing room, or we find a non-filled entry + for( int8_t j = 0; + isFilled( nCur ) && j < 32; + nCur = (nCur + (1< uint32_t __calcHashCode( const int &k ); + template<> bool __cmpHashKeys( const int &a, const int &b ); + + template<> uint32_t __calcHashCode( const unsigned int &k ); + template<> bool __cmpHashKeys( const unsigned int &a, const unsigned int &b ); + + template<> uint32_t __calcHashCode( const char * const &k ); + template<> bool __cmpHashKeys( const char * const &a, const char * const &b ); + + template<> uint32_t __calcHashCode( char * const &k ); + template<> bool __cmpHashKeys( char * const &a, char * const &b ); + + 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 ) + { + 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; + } + + /* + template + Serializer &operator&&( Serializer &ar, Hash &h ) + { + if( ar.isLoading() ) + { + return ar >> h; + } + else + { + return ar << h; + } + }*/ +} + +#endif diff --git a/src/old/exceptionbase.cpp b/src/old/exceptionbase.cpp deleted file mode 100644 index f3d22da..0000000 --- a/src/old/exceptionbase.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "exceptionbase.h" -#include - -ExceptionBase::ExceptionBase( const char *lpFormat, ... ) throw() : - nErrorCode( 0 ), - sWhat( NULL ) -{ - va_list ap; - - va_start(ap, lpFormat); - setWhat( lpFormat, ap ); - va_end(ap); -} - -ExceptionBase::ExceptionBase( int nCode, const char *lpFormat, ... ) throw() : - nErrorCode( nCode ), - sWhat( NULL ) -{ - va_list ap; - - va_start(ap, lpFormat); - setWhat( lpFormat, ap ); - va_end(ap); -} - -ExceptionBase::ExceptionBase( int nCode ) throw() : - nErrorCode( nCode ), - sWhat( NULL ) -{ -} - -ExceptionBase::~ExceptionBase() throw() -{ - if( sWhat ) - { - delete[] sWhat; - sWhat = NULL; - } -} - -void ExceptionBase::setWhat( const char *lpFormat, va_list &vargs ) -{ - if( sWhat ) delete[] sWhat; - int nSize; - - nSize = vsnprintf( NULL, 0, lpFormat, vargs ); - sWhat = new char[nSize+1]; - vsnprintf( sWhat, nSize+1, lpFormat, vargs ); -} - -void ExceptionBase::setWhat( const char *lpText ) -{ - if( sWhat ) delete[] sWhat; - int nSize; - - nSize = strlen( lpText ); - sWhat = new char[nSize+1]; - strcpy( sWhat, lpText ); -} - -const char *ExceptionBase::what() const throw() -{ - return sWhat; -} - -int ExceptionBase::getErrorCode() -{ - return nErrorCode; -} - diff --git a/src/old/exceptionbase.h b/src/old/exceptionbase.h deleted file mode 100644 index 6f1eca7..0000000 --- a/src/old/exceptionbase.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef EXCEPTION_BASE_H -#define EXCEPTION_BASE_H - -#include -#include -#include - -/** - * A generalized Exception base class. This is nice for making general and - * flexible child classes that can create new error code classes. - */ -class ExceptionBase : public std::exception -{ -public: - /** - * Construct an exception with an error code of zero, but with a - * description. The use of this is not reccomended most of the time, it's - * generally best to include an error code with the exception so your - * program can handle the exception in a better way. - * @param sFormat The format of the text. See printf for more info. - */ - ExceptionBase( const char *sFormat, ... ) throw(); - - /** - * - * @param nCode - * @param sFormat - */ - ExceptionBase( int nCode, const char *sFormat, ... ) throw(); - - /** - * - * @param nCode - * @return - */ - ExceptionBase( int nCode=0 ) throw(); - - /** - * - * @return - */ - virtual ~ExceptionBase() throw(); - - /** - * - * @return - */ - virtual const char *what() const throw(); - - /** - * - * @return - */ - int getErrorCode(); - - /** - * - * @param lpFormat - * @param vargs - */ - void setWhat( const char *lpFormat, va_list &vargs ); - - /** - * - * @param lpText - */ - void setWhat( const char *lpText ); - -private: - int nErrorCode; /**< The code for the error that occured. */ - char *sWhat; /**< The text string telling people what went wrong. */ -}; - -#define subExceptionDecl( name ) \ -class name : public ExceptionBase \ -{ \ - public: \ - name( const char *sFormat, ... ) throw (); \ - name( int nCode, const char *sFormat, ... ) throw(); \ - name( int nCode=0 ) throw (); \ -}; - -#define subExceptionDef( name ) \ -name::name( const char *lpFormat, ... ) throw() : \ - ExceptionBase( 0 ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode, const char *lpFormat, ... ) throw() : \ - ExceptionBase( nCode ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode ) throw() : \ - ExceptionBase( nCode ) \ -{ \ -} - -#endif diff --git a/src/old/exceptions.cpp b/src/old/exceptions.cpp deleted file mode 100644 index ce79a5e..0000000 --- a/src/old/exceptions.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "exceptions.h" -#include - -subExceptionDef( XmlException ) -subExceptionDef( FileException ) -subExceptionDef( ConnectionException ) -subExceptionDef( PluginException ) - diff --git a/src/old/exceptions.h b/src/old/exceptions.h deleted file mode 100644 index 0ab2b15..0000000 --- a/src/old/exceptions.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef EXCEPTIONS_H -#define EXCEPTIONS_H - -#include "exceptionbase.h" -#include - -subExceptionDecl( XmlException ) -subExceptionDecl( FileException ) -subExceptionDecl( ConnectionException ) -subExceptionDecl( PluginException ) - -enum eFileException -{ - excodeEOF -}; - -enum eConnectionException -{ - excodeReadError, - excodeBadReadError, - excodeConnectionClosed, - excodeSocketTimeout -}; - -#endif diff --git a/src/old/fstring.cpp b/src/old/fstring.cpp deleted file mode 100644 index 82d024d..0000000 --- a/src/old/fstring.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "fstring.h" -#include "hash.h" - -template<> uint32_t __calcHashCode( const FString &k ) -{ - return __calcHashCode( k.c_str() ); -} - -template<> bool __cmpHashKeys( const FString &a, const FString &b ) -{ - return a == b; -} - diff --git a/src/old/fstring.h b/src/old/fstring.h deleted file mode 100644 index c5397cc..0000000 --- a/src/old/fstring.h +++ /dev/null @@ -1,651 +0,0 @@ -#ifndef F_STRING_H -#define F_STRING_H - -#include -#include -#include "serializable.h" -#include "serializer.h" - -template< typename chr > -struct FStringChunk -{ - long nLength; - chr *pData; - FStringChunk *pNext; -}; - -/** - * Flexible String class. This class was designed with string passing and - * generation in mind. Like the standard string class you can specify what - * datatype to use for each character. Unlike the standard string class, - * collection of appended and prepended terms is done lazily, making long - * operations that involve many appends very inexpensive. In addition internal - * ref-counting means that if you pass strings around between functions there's - * almost no overhead in time or memory since a reference is created and no - * data is actually copied. This also means that you never need to put any - * FBasicString into a ref-counting container class. - */ -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; - -public: - FBasicString() : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - } - - FBasicString( const chr *pData ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( pData ); - } - - FBasicString( const chr *pData, long nLength ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( pData, nLength ); - } - - FBasicString( const MyType &rSrc ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - // Here we have no choice but to copy, since the other guy is a const. - // In the case that the source were flat, we could get a reference, it - // would make some things faster, but not matter in many other cases. - - joinShare( rSrc ); - //copyFrom( rSrc ); - } - - FBasicString( const MyType &rSrc, long nLength ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( rSrc.pFirst->pData, nLength ); - } - - FBasicString( const MyType &rSrc, long nStart, long nLength ) : - nLength( 0 ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - append( rSrc.pFirst->pData+nStart, nLength ); - } - - FBasicString( long nSize ) : - nLength( nSize ), - pnRefs( NULL ), - pFirst( NULL ), - pLast( NULL ) - { - pFirst = pLast = newChunk( nSize ); - } - - virtual ~FBasicString() - { - clear(); - } - - void append( const chr *pData ) - { - long nLen; - for( nLen = 0; pData[nLen] != (chr)0; nLen++ ); - - Chunk *pNew = newChunk( nLen ); - cpy( pNew->pData, pData, nLen ); - - appendChunk( pNew ); - } - - void append( const chr *pData, long nLen ) - { - Chunk *pNew = newChunk( nLen ); - - cpy( pNew->pData, pData, nLen ); - - appendChunk( pNew ); - } - - void prepend( const chr *pData ) - { - long nLen; - for( nLen = 0; pData[nLen] != (chr)0; nLen++ ); - - Chunk *pNew = newChunk( nLen ); - cpy( pNew->pData, pData, nLen ); - - prependChunk( pNew ); - } - - void prepend( const chr *pData, long nLen ) - { - Chunk *pNew = newChunk( nLen ); - - cpy( pNew->pData, pData, nLen ); - - prependChunk( pNew ); - } - - void clear() - { - 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; - } - - long getSize() const - { - return nLength; - } - - chr *getStr() - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - const chr *getStr() const - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - chr *c_str() - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - const chr *c_str() const - { - if( pFirst == NULL ) - return NULL; - - flatten(); - return pFirst->pData; - } - - MyType &operator +=( const chr *pData ) - { - append( pData ); - - return (*this); - } - - MyType &operator +=( const MyType &rSrc ) - { - rSrc.flatten(); - append( rSrc.pFirst->pData, rSrc.nLength ); - - return (*this); - } - - MyType &operator +=( const chr pData ) - { - chr tmp[2] = { pData, (chr)0 }; - append( tmp ); - - return (*this); - } - - MyType &operator =( const chr *pData ) - { - clear(); - append( pData ); - - return (*this); - } - - MyType &operator =( const MyType &rSrc ) - { - //if( rSrc.isFlat() ) - //{ - joinShare( rSrc ); - //} - //else - //{ - // copyFrom( rSrc ); - //} - // - - return (*this); - } - - bool operator ==( const chr *pData ) const - { - if( pFirst == NULL ) { - if( pData == NULL ) - return true; - return false; - } - - flatten(); - const chr *a = pData; - chr *b = pFirst->pData; - for( ; *a!=(chr)0; a++, b++ ) - { - if( *a != *b ) - return false; - } - - return true; - } - - bool operator ==( const MyType &pData ) const - { - if( pFirst == pData.pFirst ) - return true; - if( pFirst == NULL ) - return false; - - flatten(); - pData.flatten(); - const chr *a = pData.pFirst->pData; - chr *b = pFirst->pData; - for( ; *a!=(chr)0; a++, b++ ) - { - if( *a != *b ) - return false; - } - - return true; - } - - bool operator !=(const chr *pData ) const - { - return !(*this == pData); - } - - bool operator !=(const MyType &pData ) const - { - return !(*this == pData); - } - - chr &operator[]( long nIndex ) - { - flatten(); - - return pFirst->pData[nIndex]; - } - - const chr &operator[]( long nIndex ) const - { - flatten(); - - return pFirst->pData[nIndex]; - } - - bool isWS( long nIndex ) const - { - flatten(); - - return pFirst->pData[nIndex]==' ' || pFirst->pData[nIndex]=='\t' - || pFirst->pData[nIndex]=='\r' || pFirst->pData[nIndex]=='\n'; - } - - bool isAlpha( long nIndex ) const - { - flatten(); - - return (pFirst->pData[nIndex] >= 'a' && pFirst->pData[nIndex] <= 'z') - || (pFirst->pData[nIndex] >= 'A' && pFirst->pData[nIndex] <= 'Z'); - } - - void toLower() - { - flatten(); - unShare(); - - for( long j = 0; j < nLength; j++ ) - { - if( pFirst->pData[j] >= 'A' && pFirst->pData[j] <= 'Z' ) - pFirst->pData[j] -= 'A'-'a'; - } - } - - void toUpper() - { - flatten(); - unShare(); - - for( long j = 0; j < nLength; j++ ) - { - if( pFirst->pData[j] >= 'a' && pFirst->pData[j] <= 'z' ) - pFirst->pData[j] += 'A'-'a'; - } - } - - 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 - { - if( isFlat() ) - return; - - if( pFirst == NULL ) - return; - - unShare(); - - Chunk *pNew = newChunk( nLength ); - chr *pos = pNew->pData; - Chunk *i = pFirst; - for(;;) - { - cpy( pos, i->pData, i->nLength ); - pos += i->nLength; - i = i->pNext; - if( i == NULL ) - break; - } - realClear(); - - pLast = pFirst = pNew; - nLength = pNew->nLength; - } - - void realClear() const - { - if( pFirst == NULL ) - return; - - if( isShared() ) - { - decRefs(); - } - else - { - Chunk *i = pFirst; - for(;;) - { - Chunk *n = i->pNext; - aChr.deallocate( i->pData, i->nLength+1 ); - aChunk.deallocate( i, 1 ); - if( n == NULL ) - break; - i = n; - } - pFirst = pLast = NULL; - nLength = 0; - } - } - - void copyFrom( const FBasicString &rSrc ) - { - if( rSrc.pFirst == NULL ) - return; - - decRefs(); - - Chunk *pNew = newChunk( rSrc.nLength ); - chr *pos = pNew->pData; - Chunk *i = rSrc.pFirst; - for(;;) - { - cpy( pos, i->pData, i->nLength ); - pos += i->nLength; - i = i->pNext; - if( i == NULL ) - break; - } - clear(); - - appendChunk( pNew ); - } - - bool isFlat() const - { - return (pFirst == pLast); - } - - bool isShared() const - { - return (pnRefs != NULL); - } - - Chunk *newChunk() const - { - Chunk *pNew = aChunk.allocate( 1 ); - pNew->pNext = NULL; - return pNew; - } - - Chunk *newChunk( long nLen ) const - { - Chunk *pNew = aChunk.allocate( 1 ); - pNew->pNext = NULL; - pNew->nLength = nLen; - pNew->pData = aChr.allocate( nLen+1 ); - pNew->pData[nLen] = (chr)0; - return pNew; - } - - void appendChunk( Chunk *pNewChunk ) - { - unShare(); - - if( pFirst == NULL ) - pLast = pFirst = pNewChunk; - else - { - pLast->pNext = pNewChunk; - pLast = pNewChunk; - } - - nLength += pNewChunk->nLength; - } - - void prependChunk( Chunk *pNewChunk ) - { - unShare(); - - if( pFirst == NULL ) - pLast = pFirst = pNewChunk; - else - { - pNewChunk->pNext = pFirst; - pFirst = pNewChunk; - } - - nLength += pNewChunk->nLength; - } - - void joinShare( MyType &rSrc ) - { - clear(); - - if( !rSrc.isFlat() ) - rSrc.flatten(); - - rSrc.initCount(); - pnRefs = rSrc.pnRefs; - (*pnRefs)++; - nLength = rSrc.nLength; - pFirst = rSrc.pFirst; - pLast = rSrc.pLast; - } - - void joinShare( const MyType &rSrc ) - { - clear(); - - rSrc.flatten(); - - if( !rSrc.isShared() ) - { - rSrc.pnRefs = new uint32_t; - (*rSrc.pnRefs) = 1; - } - pnRefs = rSrc.pnRefs; - (*pnRefs)++; - nLength = rSrc.nLength; - pFirst = rSrc.pFirst; - pLast = rSrc.pLast; - } - - /** - * This takes an object that was shared and makes a copy of the base data - * that was being shared so that this copy can be changed. This should be - * added before any call that will change this object; - */ - void unShare() const - { - if( isShared() == false ) - return; - - Chunk *pNew = newChunk( nLength ); - chr *pos = pNew->pData; - Chunk *i = pFirst; - for(;;) - { - cpy( pos, i->pData, i->nLength ); - pos += i->nLength; - i = i->pNext; - if( i == NULL ) - break; - } - decRefs(); - pLast = pFirst = pNew; - nLength = pNew->nLength; - } - - /** - * This decrements our ref count and pulls us out of the share. If the ref - * count hits zero because of this, it destroys the share. This is not - * safe to call on it's own, it's much better to call unShare. - */ - void decRefs() const - { - if( isShared() ) - { - (*pnRefs)--; - if( (*pnRefs) == 0 ) - destroyShare(); - else - { - pnRefs = NULL; - pFirst = NULL; - pLast = NULL; - nLength = 0; - } - } - } - - /** - * While the unShare function removes an instance from a share, this - * function destroys the data that was in the share, removing the share - * itself. This should only be called when the refcount for the share has - * or is about to reach zero. - */ - void destroyShare() const - { - delete pnRefs; - pnRefs = NULL; - realClear(); - } - -#ifdef VALTEST - void cpy( chr *dest, const chr *src, long count ) const - { - for( int j = 0; j < count; j++ ) - { - *dest = *src; - dest++; - src++; - } - } -#endif - - void initCount() const - { - if( !isShared() ) - { - pnRefs = new uint32_t; - (*pnRefs) = 1; - } - } - -private: - mutable long nLength; - mutable uint32_t *pnRefs; - mutable Chunk *pFirst; - mutable Chunk *pLast; - - mutable chralloc aChr; - mutable chunkalloc aChunk; -}; - -typedef FBasicString FString; - -#include "hash.h" -template<> uint32_t __calcHashCode( const FString &k ); -template<> bool __cmpHashKeys( const FString &a, const FString &b ); - - -#endif diff --git a/src/old/hash.cpp b/src/old/hash.cpp deleted file mode 100644 index c52e6b1..0000000 --- a/src/old/hash.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "hash.h" - -subExceptionDef( HashException ) - -template<> uint32_t __calcHashCode( const int &k ) -{ - return k; -} - -template<> bool __cmpHashKeys( const int &a, const int &b ) -{ - return a == b; -} - -template<> uint32_t __calcHashCode( const unsigned int &k ) -{ - return k; -} - -template<> bool __cmpHashKeys( const unsigned int &a, const unsigned int &b ) -{ - return a == b; -} - -template<> -uint32_t __calcHashCode( const char * const &k ) -{ - if (k == NULL) - { - return 0; - } - - unsigned long int nPos = 0; - for( const char *s = k; *s; s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool __cmpHashKeys( const char * const &a, const char * const &b ) -{ - if( a == b ) - return true; - - for(int j=0; a[j] == b[j]; j++ ) - if( a[j] == '\0' ) - return true; - - return false; -} - -template<> -uint32_t __calcHashCode( char * const &k ) -{ - if (k == NULL) - { - return 0; - } - - unsigned long int nPos = 0; - for( const char *s = k; *s; s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool __cmpHashKeys( char * const &a, char * const &b ) -{ - if( a == b ) - return true; - - for(int j=0; a[j] == b[j]; j++ ) - if( a[j] == '\0' ) - return true; - - return false; -} - -template<> uint32_t __calcHashCode( const std::string &k ) -{ - std::string::size_type j, sz = k.size(); - const char *s = k.c_str(); - - unsigned long int nPos = 0; - for( j = 0; j < sz; j++, s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool __cmpHashKeys( const std::string &a, const std::string &b ) -{ - return a == b; -} - -template<> uint32_t __calcHashCode( const Hashable &k ) -{ - return 0; - //return k.getHashCode(); -} - -template<> bool __cmpHashKeys( const Hashable &a, const Hashable &b ) -{ - return false; - //return a.compareForHash( b ); -} - diff --git a/src/old/hash.h b/src/old/hash.h deleted file mode 100644 index e819379..0000000 --- a/src/old/hash.h +++ /dev/null @@ -1,744 +0,0 @@ -#ifndef HASH_H -#define HASH_H - -#include -#include -#include -#include -#include -#include "exceptionbase.h" -#include "hashable.h" -#include "serializable.h" -#include "serializer.h" - -#define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) - -subExceptionDecl( HashException ) - -enum eHashException -{ - excodeNotFilled -}; - -template -uint32_t __calcHashCode( const T &k ); - -template -bool __cmpHashKeys( const T &a, const T &b ); - -struct __calcNextTSize_fast -{ - uint32_t operator()( uint32_t nCapacity, uint32_t nFill, uint32_t nDeleted ) const - { - if( nDeleted >= nCapacity/2 ) - return nCapacity; - return nCapacity*2+1; - } -}; - -template, typename valuealloc = std::allocator, typename challoc = std::allocator > -class Hash; - -template< typename key, typename _value, typename sizecalc = __calcNextTSize_fast, typename keyalloc = std::allocator, typename valuealloc = std::allocator<_value>, typename challoc = std::allocator > -struct HashProxy -{ - friend class Hash; -private: - HashProxy( Hash &h, key *k, uint32_t nPos, uint32_t hash ) : - hsh( h ), - pKey( k ), - nPos( nPos ), - hash( hash ), - bFilled( false ) - { - } - - HashProxy( Hash &h, uint32_t nPos, _value *pValue ) : - hsh( h ), - nPos( nPos ), - pValue( pValue ), - bFilled( true ) - { - } - - Hash &hsh; - key *pKey; - uint32_t nPos; - _value *pValue; - uint32_t hash; - bool bFilled; - -public: - operator _value &() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return *pValue; - } - - _value &value() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return *pValue; - } - - bool isFilled() - { - return bFilled; - } - - void erase() - { - if( bFilled ) - { - hsh._erase( nPos ); - hsh.onDelete(); - } - } - - _value operator=( _value nval ) - { - if( bFilled ) - { - hsh.va.destroy( pValue ); - hsh.va.construct( pValue, nval ); - hsh.onUpdate(); - } - else - { - hsh.fill( nPos, *pKey, nval, hash ); - hsh.onInsert(); - } - - return nval; - } - - _value *operator->() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return pValue; - } -}; - -template -class Hash -{ - friend struct HashProxy; -public: - Hash() : - nCapacity( 11 ), - nFilled( 0 ), - nDeleted( 0 ), - bFilled( NULL ), - bDeleted( NULL ), - aKeys( NULL ), - aValues( NULL ), - aHashCodes( NULL ) - { - nKeysSize = bitsToBytes( nCapacity ); - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - } - - Hash( const Hash &src ) : - nCapacity( src.nCapacity ), - nFilled( 0 ), - nDeleted( 0 ), - bFilled( NULL ), - bDeleted( NULL ), - aKeys( NULL ), - aValues( NULL ), - aHashCodes( NULL ) - { - nKeysSize = bitsToBytes( nCapacity ); - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - - for( uint32_t j = 0; j < src.nCapacity; j++ ) - { - if( src.isFilled( j ) ) - { - insert( src.aKeys[j], src.aValues[j] ); - } - } - } - - Hash &operator=( const Hash &src ) - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - { - va.destroy( &aValues[j] ); - ka.destroy( &aKeys[j] ); - } - } - va.deallocate( aValues, nCapacity ); - ka.deallocate( aKeys, nCapacity ); - ca.deallocate( bFilled, nKeysSize ); - ca.deallocate( bDeleted, nKeysSize ); - ca.deallocate( aHashCodes, nCapacity ); - - nFilled = 0; - nDeleted = 0; - nCapacity = src.nCapacity; - nKeysSize = bitsToBytes( nCapacity ); - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - - for( uint32_t j = 0; j < src.nCapacity; j++ ) - { - if( src.isFilled( j ) ) - { - insert( src.aKeys[j], src.aValues[j] ); - } - } - - return *this; - } - - virtual ~Hash() - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - { - va.destroy( &aValues[j] ); - ka.destroy( &aKeys[j] ); - } - } - va.deallocate( aValues, nCapacity ); - ka.deallocate( aKeys, nCapacity ); - ca.deallocate( bFilled, nKeysSize ); - ca.deallocate( bDeleted, nKeysSize ); - ca.deallocate( aHashCodes, nCapacity ); - } - - uint32_t getCapacity() - { - return nCapacity; - } - - uint32_t getFill() - { - return nFilled; - } - - uint32_t size() - { - return nFilled-nDeleted; - } - - uint32_t getDeleted() - { - return nDeleted; - } - - virtual HashProxy operator[]( key k ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - return HashProxy( *this, nPos, &aValues[nPos] ); - } - else - { - return HashProxy( *this, &k, nPos, hash ); - } - } - - virtual void insert( key k, value v ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - va.destroy( &aValues[nPos] ); - va.construct( &aValues[nPos], v ); - onUpdate(); - } - else - { - fill( nPos, k, v, hash ); - onInsert(); - } - } - - virtual void erase( key k ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - _erase( nPos ); - onDelete(); - } - } - - struct iterator; - virtual void erase( struct iterator &i ) - { - if( this != &i.hsh ) - throw HashException("This iterator didn't come from this Hash."); - if( isFilled( i.nPos ) && !isDeleted( i.nPos ) ) - { - _erase( i.nPos ); - onDelete(); - } - } - - virtual void clear() - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - { - va.destroy( &aValues[j] ); - ka.destroy( &aKeys[j] ); - onDelete(); - } - } - - clearBits(); - } - - virtual value &get( key k ) - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = probe( hash, k, bFill ); - - if( bFill ) - { - return aValues[nPos]; - } - else - { - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - } - } - - virtual bool has( key k ) - { - bool bFill; - probe( __calcHashCode( k ), k, bFill, false ); - - return bFill; - } - - typedef struct iterator - { - friend class Hash; - private: - iterator( Hash &hsh ) : - hsh( hsh ), - nPos( 0 ), - bFinished( false ) - { - nPos = hsh.getFirstPos( bFinished ); - } - - iterator( Hash &hsh, bool bDone ) : - hsh( hsh ), - nPos( 0 ), - bFinished( bDone ) - { - } - - Hash &hsh; - uint32_t nPos; - bool bFinished; - - public: - iterator operator++( int ) - { - if( bFinished == false ) - nPos = hsh.getNextPos( nPos, bFinished ); - - return *this; - } - - iterator operator++() - { - if( bFinished == false ) - nPos = hsh.getNextPos( nPos, bFinished ); - - return *this; - } - - bool operator==( const iterator &oth ) - { - if( bFinished != oth.bFinished ) - return false; - if( bFinished == true ) - { - return true; - } - else - { - if( oth.nPos == nPos ) - return true; - return false; - } - } - - bool operator!=( const iterator &oth ) - { - return !(*this == oth ); - } - - iterator operator=( const iterator &oth ) - { - if( &hsh != &oth.hsh ) - throw HashException( - "Cannot mix iterators from different hash objects."); - nPos = oth.nPos; - bFinished = oth.bFinished; - } - - std::pair operator *() - { - return hsh.getAtPos( nPos ); - } - - key &getKey() - { - return hsh.getKeyAtPos( nPos ); - } - - value &getValue() - { - return hsh.getValueAtPos( nPos ); - } - }; - - iterator begin() - { - return iterator( *this ); - } - - iterator end() - { - return iterator( *this, true ); - } - - std::list getKeys() - { - std::list lKeys; - - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - { - if( !isDeleted( j ) ) - { - lKeys.push_back( aKeys[j] ); - } - } - } - - return lKeys; - } - -protected: - virtual void onInsert() {} - virtual void onUpdate() {} - virtual void onDelete() {} - virtual void onReHash() {} - - virtual void clearBits() - { - for( uint32_t j = 0; j < nKeysSize; j++ ) - { - bFilled[j] = bDeleted[j] = 0; - } - } - - virtual void fill( uint32_t loc, key &k, value &v, uint32_t hash ) - { - bFilled[loc/32] |= (1<<(loc%32)); - va.construct( &aValues[loc], v ); - ka.construct( &aKeys[loc], k ); - aHashCodes[loc] = hash; - nFilled++; - //printf("Filled: %d, Deleted: %d, Capacity: %d\n", - // nFilled, nDeleted, nCapacity ); - } - - virtual void _erase( uint32_t loc ) - { - bDeleted[loc/32] |= (1<<(loc%32)); - va.destroy( &aValues[loc] ); - ka.destroy( &aKeys[loc] ); - nDeleted++; - //printf("Filled: %d, Deleted: %d, Capacity: %d\n", - // nFilled, nDeleted, nCapacity ); - } - - virtual std::pair getAtPos( uint32_t nPos ) - { - return std::pair(aKeys[nPos],aValues[nPos]); - } - - virtual key &getKeyAtPos( uint32_t nPos ) - { - return aKeys[nPos]; - } - - virtual value &getValueAtPos( uint32_t nPos ) - { - return aValues[nPos]; - } - - virtual uint32_t getFirstPos( bool &bFinished ) - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - return j; - } - - bFinished = true; - return 0; - } - - virtual uint32_t getNextPos( uint32_t nPos, bool &bFinished ) - { - for( uint32_t j = nPos+1; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - return j; - } - - bFinished = true; - return 0; - } - - uint32_t probe( uint32_t hash, key k, bool &bFill, bool rehash=true ) - { - uint32_t nCur = hash%nCapacity; - - // First we scan to see if the key is already there, abort if we - // run out of probing room, or we find a non-filled entry - for( int8_t j = 0; - isFilled( nCur ) && j < 32; - nCur = (nCur + (1< uint32_t __calcHashCode( const int &k ); -template<> bool __cmpHashKeys( const int &a, const int &b ); - -template<> uint32_t __calcHashCode( const unsigned int &k ); -template<> bool __cmpHashKeys( const unsigned int &a, const unsigned int &b ); - -template<> uint32_t __calcHashCode( const char * const &k ); -template<> bool __cmpHashKeys( const char * const &a, const char * const &b ); - -template<> uint32_t __calcHashCode( char * const &k ); -template<> bool __cmpHashKeys( char * const &a, char * const &b ); - -template<> uint32_t __calcHashCode( const std::string &k ); -template<> bool __cmpHashKeys( const std::string &a, const std::string &b ); - -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/old/hashable.cpp b/src/old/hashable.cpp deleted file mode 100644 index 8565956..0000000 --- a/src/old/hashable.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "hashable.h" diff --git a/src/old/hashable.h b/src/old/hashable.h deleted file mode 100644 index 98643d5..0000000 --- a/src/old/hashable.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef HASHABLE_H -#define HASHABLE_H - -class Hashable -{ -public: - virtual ~Hashable() {}; - virtual unsigned long int getHashCode() = 0; - virtual bool compareForHash( Hashable &other ) = 0; -}; - -#endif diff --git a/src/old/serializable.cpp b/src/old/serializable.cpp deleted file mode 100644 index fd50943..0000000 --- a/src/old/serializable.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "serializable.h" - -Serializable::Serializable() -{ -} -Serializable::~Serializable() -{ -} diff --git a/src/old/serializable.h b/src/old/serializable.h deleted file mode 100644 index 06def29..0000000 --- a/src/old/serializable.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef SERIALIZER_H -#define SERIALIZER_H - -//#include "serializer.h" - -/** - * The base class for any class you want to serialize. Simply include this as - * a base class, implement the purely virtual serialize function and you've got - * an easily serializable class. - */ -class Serializable -{ -public: - /** - * Does nothing, here for completeness. - */ - Serializable(); - - /** - * Here to ensure the deconstructor is virtual. - */ - virtual ~Serializable(); - - /** - * This is the main workhorse of the serialization system, just override and - * you've got a serializable class. A reference to the Serializer 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 Serializer object to use. - */ - virtual void serialize( class Serializer &ar )=0; -}; - -#endif diff --git a/src/old/serializer.cpp b/src/old/serializer.cpp deleted file mode 100644 index 636224e..0000000 --- a/src/old/serializer.cpp +++ /dev/null @@ -1,338 +0,0 @@ -#include "serializer.h" -#include "serializable.h" -#include - -Serializer::Serializer(bool bLoading): - bLoading(bLoading) -{ -} -Serializer::~Serializer() -{ -} - -bool Serializer::isLoading() -{ - return bLoading; -} -Serializer &Serializer::operator<<(bool p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int8_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int16_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int32_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(int64_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(uint8_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(uint16_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(uint32_t p) -{ - write( &p, sizeof(p) ); - return *this; -} -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) ); - return *this; -} -Serializer &Serializer::operator<<(double p) -{ - write( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator<<(long double p) -{ - write( &p, sizeof(p) ); - return *this; -} - -Serializer &Serializer::operator>>(bool &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int8_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int16_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int32_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(int64_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(uint8_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(uint16_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(uint32_t &p) -{ - read( &p, sizeof(p) ); - return *this; -} -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) ); - return *this; -} -Serializer &Serializer::operator>>(double &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Serializer &Serializer::operator>>(long double &p) -{ - read( &p, sizeof(p) ); - return *this; -} - -Serializer &Serializer::operator&&(bool &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int8_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int16_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int32_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(int64_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint8_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint16_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint32_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(uint64_t &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(float &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(double &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - -Serializer &Serializer::operator&&(long double &p) -{ - if (bLoading) - { - return *this >> p; - } - else - { - return *this << p; - } -} - - -Serializer &operator<<(Serializer &s, Serializable &p) -{ - p.serialize( s ); - return s; -} - -Serializer &operator>>(Serializer &s, Serializable &p) -{ - p.serialize( s ); - return s; -} - -Serializer &operator&&(Serializer &s, Serializable &p) -{ - if (s.isLoading()) - { - return s >> p; - } - else - { - return s << p; - } -} - -Serializer &operator<<( Serializer &ar, std::string &s ) -{ - ar << (uint32_t)s.length(); - ar.write( s.c_str(), s.length() ); - - return ar; -} - -Serializer &operator>>( Serializer &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; -} - diff --git a/src/old/serializer.h b/src/old/serializer.h deleted file mode 100644 index 3af489c..0000000 --- a/src/old/serializer.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef SERIALIZABLE_H -#define SERIALIZABLE_H - -#include -#include -#include -//#include "serializable.h" - -class Serializer -{ -private: - bool bLoading; -public: - bool isLoading(); - - enum - { - load = true, - save = false - }; - - Serializer(bool bLoading); - virtual ~Serializer(); - virtual void close()=0; - - virtual void write(const void *, int32_t)=0; - virtual void read(void *, int32_t)=0; - - 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<<(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>>(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&(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 &); - -#endif diff --git a/src/old/stream.cpp b/src/old/stream.cpp deleted file mode 100644 index 856a58d..0000000 --- a/src/old/stream.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "stream.h" - -Stream::Stream() -{ -} - -Stream::~Stream() -{ -} - diff --git a/src/old/stream.h b/src/old/stream.h deleted file mode 100644 index e086e28..0000000 --- a/src/old/stream.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef STREAM_H -#define STREAM_H - -#include -#include - -class Stream -{ -public: - Stream(); - virtual ~Stream(); - - virtual void close() = 0; - virtual size_t read( char *pBuf, size_t nBytes ) = 0; - virtual size_t write( const char *pBuf, size_t nBytes ) = 0; - - virtual long tell() = 0; - virtual void seek( long offset ) = 0; - virtual void setPos( long pos ) = 0; - virtual void setPosEnd( long pos ) = 0; - virtual bool isEOS() = 0; - -private: - -}; - -#endif diff --git a/src/stream.cpp b/src/stream.cpp new file mode 100644 index 0000000..267a7d1 --- /dev/null +++ b/src/stream.cpp @@ -0,0 +1,10 @@ +#include "stream.h" + +Bu::Stream::Stream() +{ +} + +Bu::Stream::~Stream() +{ +} + diff --git a/src/stream.h b/src/stream.h new file mode 100644 index 0000000..274f4fd --- /dev/null +++ b/src/stream.h @@ -0,0 +1,34 @@ +#ifndef STREAM_H +#define STREAM_H + +#include +#include + +namespace Bu +{ + class Stream + { + public: + Stream(); + virtual ~Stream(); + + virtual void close() = 0; + virtual size_t read( char *pBuf, size_t nBytes ) = 0; + virtual size_t write( const char *pBuf, size_t nBytes ) = 0; + + virtual long tell() = 0; + virtual void seek( long offset ) = 0; + virtual void setPos( long pos ) = 0; + virtual void setPosEnd( long pos ) = 0; + virtual bool isEOS() = 0; + + virtual bool canRead() = 0; + virtual bool canWrite() = 0; + virtual bool canSeek() = 0; + + private: + + }; +} + +#endif diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp new file mode 100644 index 0000000..fb0d97c --- /dev/null +++ b/src/tests/archive.cpp @@ -0,0 +1,7 @@ +#include "archive.h" + +int main() +{ + //Archive +} + diff --git a/tests/comments.xml b/tests/comments.xml deleted file mode 100644 index df05b3b..0000000 --- a/tests/comments.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - Aaaugh! - - diff --git a/tests/guy.cpp b/tests/guy.cpp deleted file mode 100644 index 6510771..0000000 --- a/tests/guy.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "stdio.h" -#include "plugin.h" -#include "plugger.h" - -class Guy : public Plugin -{ -public: - Guy() - { - printf("I'm guy!\n"); - } - - virtual ~Guy() - { - printf("Guy is dead...\n"); - } - -private: -}; - -PluginInterface( Guy, Plugin, "Mike", 0, 1 ) - diff --git a/tests/makeplugin.sh b/tests/makeplugin.sh deleted file mode 100755 index 086fefd..0000000 --- a/tests/makeplugin.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -g++ -fPIC -shared -Wl,-soname,guy.so -o guy.so -I../src -I../src/test/plugin guy.cpp ../src/test/plugin/plugin.cpp -- cgit v1.2.3 From afb50f535dd60b485a38f1f1f692b3303e28fecc Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 Jun 2007 21:58:47 +0000 Subject: The FString has more things that it can do...now. --- src/client.h | 1 + src/fstring.cpp | 6 ++++++ src/fstring.h | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) (limited to 'src/fstring.cpp') diff --git a/src/client.h b/src/client.h index cee558d..f228e96 100644 --- a/src/client.h +++ b/src/client.h @@ -25,6 +25,7 @@ namespace Bu Bu::FString &getInput(); Bu::FString &getOutput(); void write( const char *pData, int nBytes ); + void read( const char *pData, int nBytes ); void setProtocol( Protocol *pProto ); Bu::Protocol *getProtocol(); diff --git a/src/fstring.cpp b/src/fstring.cpp index 56d2173..0b5a970 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -12,3 +12,9 @@ template<> bool Bu::__cmpHashKeys( return a == b; } +std::ostream& operator<< (std::ostream &os, Bu::FString &val ) +{ + os.write( val.getStr(), val.getSize() ); + return os; +} + diff --git a/src/fstring.h b/src/fstring.h index bf6518b..1f21b5f 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -586,6 +586,20 @@ namespace Bu return -1; } + /** + * Remove nAmnt bytes from the front of the string. This function + * operates in O(n) time and should be used sparingly. + */ + void trimFront( long nAmnt ) + { + long nNewLen = nLength - nAmnt; + flatten(); + Chunk *pNew = newChunk( nNewLen ); + cpy( pNew->pData, pFirst->pData, nNewLen ); + clear(); + appendChunk( pNew ); + } + /** * Function the archiver calls to archive your FString. *@param ar (Archive) The archive which is archiving your FString. @@ -882,4 +896,7 @@ namespace Bu template<> bool __cmpHashKeys( const FString &a, const FString &b ); } +#include +std::ostream& operator<< (std::ostream &os, Bu::FString &val ); + #endif -- cgit v1.2.3 From ec8ed8b4b44c7b039e87faaa50bb4d503393d336 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 29 Jun 2007 00:48:32 +0000 Subject: A few changes here and there, mainly related to getting the new Server system working in optimal condition... --- src/client.cpp | 35 ++++++++++++++++++++++++++++++++--- src/client.h | 11 +++++++++-- src/fstring.cpp | 2 +- src/fstring.h | 2 +- src/membuf.cpp | 11 +++++++++++ src/membuf.h | 3 +++ 6 files changed, 57 insertions(+), 7 deletions(-) (limited to 'src/fstring.cpp') diff --git a/src/client.cpp b/src/client.cpp index 2f293b7..8077b3d 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -99,9 +99,9 @@ bool Bu::Client::isOpen() return pSocket->isOpen(); } -void Bu::Client::write( const char *pData, int nBytes ) +void Bu::Client::write( const void *pData, int nBytes ) { - sWriteBuf.append( pData, nBytes ); + sWriteBuf.append( (const char *)pData, nBytes ); } void Bu::Client::write( int8_t nData ) @@ -144,7 +144,7 @@ void Bu::Client::write( uint64_t nData ) sWriteBuf.append( (const char *)&nData, sizeof(nData) ); } -void Bu::Client::read( char *pData, int nBytes ) +void Bu::Client::read( void *pData, int nBytes ) { memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); nRBOffset += nBytes; @@ -165,6 +165,31 @@ void Bu::Client::read( char *pData, int nBytes ) } } +void Bu::Client::peek( void *pData, int nBytes ) +{ + memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); +} + +void Bu::Client::seek( int nBytes ) +{ + nRBOffset += nBytes; + if( sReadBuf.getSize()-nRBOffset == 0 ) + { + sReadBuf.clear(); + nRBOffset = 0; + } + // This is an experimental threshold, maybe I'll make this configurable + // later on. + else if( + (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || + (nRBOffset >= sReadBuf.getSize()/4) + ) + { + sReadBuf.trimFront( nRBOffset ); + nRBOffset = 0; + } +} + long Bu::Client::getInputSize() { return sReadBuf.getSize()-nRBOffset; @@ -175,3 +200,7 @@ const Bu::Socket *Bu::Client::getSocket() const return pSocket; } +void Bu::Client::disconnect() +{ +} + diff --git a/src/client.h b/src/client.h index 1253dcd..5947521 100644 --- a/src/client.h +++ b/src/client.h @@ -24,7 +24,7 @@ namespace Bu Bu::FString &getInput(); Bu::FString &getOutput(); - void write( const char *pData, int nBytes ); + void write( const void *pData, int nBytes ); void write( int8_t nData ); void write( int16_t nData ); void write( int32_t nData ); @@ -33,7 +33,9 @@ namespace Bu void write( uint16_t nData ); void write( uint32_t nData ); void write( uint64_t nData ); - void read( char *pData, int nBytes ); + void read( void *pData, int nBytes ); + void peek( void *pData, int nBytes ); + void seek( int nBytes ); long getInputSize(); void setProtocol( Protocol *pProto ); @@ -44,6 +46,11 @@ namespace Bu const Bu::Socket *getSocket() const; + /** + *@todo Make this not suck. + */ + void disconnect(); + private: Bu::Socket *pSocket; Bu::Protocol *pProto; diff --git a/src/fstring.cpp b/src/fstring.cpp index 0b5a970..f71d6c1 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -12,7 +12,7 @@ template<> bool Bu::__cmpHashKeys( return a == b; } -std::ostream& operator<< (std::ostream &os, Bu::FString &val ) +std::basic_ostream& operator<< (std::basic_ostream &os, const Bu::FString &val ) { os.write( val.getStr(), val.getSize() ); return os; diff --git a/src/fstring.h b/src/fstring.h index 1f21b5f..f06c362 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -897,6 +897,6 @@ namespace Bu } #include -std::ostream& operator<< (std::ostream &os, Bu::FString &val ); +std::basic_ostream& operator<< (std::basic_ostream &os, const Bu::FString &val ); #endif diff --git a/src/membuf.cpp b/src/membuf.cpp index 3c394b0..45ff5bd 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp @@ -7,6 +7,12 @@ Bu::MemBuf::MemBuf() : { } +Bu::MemBuf::MemBuf( const Bu::FString &str ) : + sBuf( str ), + nPos( 0 ) +{ +} + Bu::MemBuf::~MemBuf() { } @@ -107,3 +113,8 @@ void Bu::MemBuf::setBlocking( bool bBlocking ) { } +Bu::FString &Bu::MemBuf::getString() +{ + return sBuf; +} + diff --git a/src/membuf.h b/src/membuf.h index b82f943..8f53d4b 100644 --- a/src/membuf.h +++ b/src/membuf.h @@ -15,6 +15,7 @@ namespace Bu { public: MemBuf(); + MemBuf( const Bu::FString &str ); virtual ~MemBuf(); virtual void close(); @@ -41,6 +42,8 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + Bu::FString &getString(); + private: Bu::FString sBuf; long nPos; -- cgit v1.2.3