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/exceptions.h | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 src/exceptions.h (limited to 'src/exceptions.h') diff --git a/src/exceptions.h b/src/exceptions.h deleted file mode 100644 index 0ab2b15..0000000 --- a/src/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 -- 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/exceptions.h') 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 1fa3ca5f24c018126333ca2d6609730e1ae17386 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 10 Apr 2007 21:48:23 +0000 Subject: Added more comments, help, and socket actually reads and writes some, but it's not done. I need to decide how I want to do the buffering... --- src/archive.h | 47 +++++++++++ src/exceptions.cpp | 1 + src/exceptions.h | 2 + src/socket.cpp | 232 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/socket.h | 23 +++++- src/stream.h | 10 +++ 6 files changed, 312 insertions(+), 3 deletions(-) (limited to 'src/exceptions.h') diff --git a/src/archive.h b/src/archive.h index 9ac3303..2f782d3 100644 --- a/src/archive.h +++ b/src/archive.h @@ -9,6 +9,53 @@ namespace Bu { + /** + * Provides a framework for serialization of objects and primitives. The + * archive will handle any basic primitive, a few special types, like char * + * strings, as well as STL classes and anything that inherits from the + * Archival class. Each Archive operates on a Stream, so you can send the + * data using an Archive almost anywhere. + * + * In order to use an Archive to store something to a file, try something + * like: + *@code + * File sOut("output", "wb"); // This is a stream subclass + * Archive ar( sOut, Archive::save ); + * ar << myClass; + @endcode + * In this example myClass is any class that inherits from Archival. When + * the storage operator is called, the Archival::archive() function in the + * myClass object is called with a reference to the Archive. This can be + * handled in one of two ways: + *@code + * void MyClass::archive( Archive &ar ) + * { + * ar && sName && nAge && sJob; + * } + @endcode + * Here we don't worry about weather we're loading or saving by using the + * smart && operator. This allows us to write very consistent, very simple + * archive functions that really do a lot of work. If we wanted to do + * something different in the case of loading or saving we would do: + *@code + * void MyClass::archive( Archive &ar ) + * { + * if( ar.isLoading() ) + * { + * ar >> sName >> nAge >> sJob; + * } else + * { + * ar << sName << nAge << sJob; + * } + * } + @endcode + * Archive currently does not provide facility to make fully portable + * archives. For example, it will not convert between endianness for you, + * nor will it take into account differences between primitive sizes on + * different platforms. This, at the moment, is up to the user to ensure. + * One way of dealing with the latter problem is to make sure and use + * explicit primitive types from the stdint.h header, i.e. int32_t. + */ class Archive { private: diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 37f09a4..a512105 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -7,4 +7,5 @@ namespace Bu subExceptionDef( FileException ) subExceptionDef( ConnectionException ) subExceptionDef( PluginException ) + subExceptionDef( UnsupportedException ) } diff --git a/src/exceptions.h b/src/exceptions.h index b28d292..3efa19f 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -10,6 +10,7 @@ namespace Bu subExceptionDecl( FileException ) subExceptionDecl( ConnectionException ) subExceptionDecl( PluginException ) + subExceptionDecl( UnsupportedException ) enum eFileException { @@ -19,6 +20,7 @@ namespace Bu enum eConnectionException { excodeReadError, + excodeWriteError, excodeBadReadError, excodeConnectionClosed, excodeSocketTimeout diff --git a/src/socket.cpp b/src/socket.cpp index c5c592b..e206bb5 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -1,10 +1,240 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "socket.h" +#include "exceptions.h" -Bu::Socket::Socket() +#define RBS (1024*2) + +Bu::Socket::Socket( int nSocket ) : + nSocket( nSocket ), + bActive( true ) { } +Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout ) +{ + struct sockaddr_in xServerName; + bActive = false; + + /* Create the socket. */ + nSocket = socket( PF_INET, SOCK_STREAM, 0 ); + + if( nSocket < 0 ) + { + throw ExceptionBase("Couldn't create socket.\n"); + } + + // These lines set the socket to non-blocking, a good thing? + int flags; + flags = fcntl(nSocket, F_GETFL, 0); + flags |= O_NONBLOCK; + if (fcntl(nSocket, F_SETFL, flags) < 0) + { + throw ExceptionBase("Couldn't set socket options.\n"); + } + + /* Connect to the server. */ + //printf("Resolving hostname (%s)...\n", sAddr ); + { + struct hostent *hostinfo; + + xServerName.sin_family = AF_INET; + xServerName.sin_port = htons( nPort ); + hostinfo = gethostbyname( sAddr.getStr() ); + if (hostinfo == NULL) + { + throw ExceptionBase("Couldn't resolve hostname.\n"); + } + xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr; + } + + //printf("Making actual connection..."); + //fflush( stdout ); + connect( + nSocket, + (struct sockaddr *)&xServerName, + sizeof(xServerName) + ); + //printf("Connected.\n"); + + bActive = true; + + if( nTimeout > 0 ) + { + fd_set rfds, wfds, efds; + int retval; + + FD_ZERO(&rfds); + FD_SET(nSocket, &rfds); + FD_ZERO(&wfds); + FD_SET(nSocket, &wfds); + FD_ZERO(&efds); + FD_SET(nSocket, &efds); + + struct timeval tv; + tv.tv_sec = nTimeout; + tv.tv_usec = 0; + + retval = select( nSocket+1, &rfds, &wfds, &efds, &tv ); + + if( retval == 0 ) + { + close(); + throw ExceptionBase("Connection timeout.\n"); + } + + } +} + Bu::Socket::~Socket() { } +void Bu::Socket::close() +{ + if( bActive ) + { + fsync( nSocket ); + ::close( nSocket ); + } + bActive = false; + //xInputBuf.clearData(); + //xOutputBuf.clearData(); + //if( pProtocol != NULL ) + //{ + // delete pProtocol; + // pProtocol = NULL; + //} +} + +void Bu::Socket::read() +{ + char buffer[RBS]; + int nbytes; + int nTotalRead=0; + + for(;;) + { + //memset( buffer, 0, RBS ); + + nbytes = ::read( nSocket, buffer, RBS ); + if( nbytes < 0 && errno != 0 && errno != EAGAIN ) + { + //printf("errno: %d, %s\n", errno, strerror( errno ) ); + /* Read error. */ + //perror("readInput"); + throw ConnectionException( + excodeReadError, + "Read error: %s", + strerror( errno ) + ); + } + else + { + if( nbytes <= 0 ) + break; + nTotalRead += nbytes; + sReadBuf.append( buffer, nbytes ); + /* Data read. */ + if( nbytes < RBS ) + { + break; + } + + /* New test, if data is divisible by RBS bytes on some libs the + * read could block, this keeps it from happening. + */ + { + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(nSocket, &rfds); + struct timeval tv = { 0, 0 }; + int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); + if( retval == -1 ) + throw ConnectionException( + excodeBadReadError, + "Bad Read error" + ); + if( !FD_ISSET( nSocket, &rfds ) ) + break; + } + } + } + + /* + if( pProtocol != NULL && nTotalRead > 0 ) + { + pProtocol->onNewData(); + }*/ +} + +size_t Bu::Socket::read( void *pBuf, size_t nBytes ) +{ + read(); + + + + return sReadBuf.getSize(); +} + +size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) +{ + int nWrote = TEMP_FAILURE_RETRY( ::write( nSocket, pBuf, nBytes ) ); + if( nWrote < 0 ) + { + throw ConnectionException( excodeWriteError, strerror(errno) ); + } + return nWrote; +} + +long Bu::Socket::tell() +{ + throw UnsupportedException(); +} + +void Bu::Socket::seek( long offset ) +{ + throw UnsupportedException(); +} + +void Bu::Socket::setPos( long pos ) +{ + throw UnsupportedException(); +} + +void Bu::Socket::setPosEnd( long pos ) +{ + throw UnsupportedException(); +} + +bool Bu::Socket::isEOS() +{ + return !bActive; +} + +bool Bu::Socket::canRead() +{ + return true; +} + +bool Bu::Socket::canWrite() +{ + return true; +} + +bool Bu::Socket::canSeek() +{ + return false; +} + diff --git a/src/socket.h b/src/socket.h index 8ccde71..3d0125d 100644 --- a/src/socket.h +++ b/src/socket.h @@ -4,6 +4,7 @@ #include #include "stream.h" +#include "fstring.h" namespace Bu { @@ -13,11 +14,29 @@ namespace Bu class Socket : public Stream { public: - Socket(); + Socket( int nSocket ); + Socket( const FString &sAddr, int nPort, int nTimeout=30 ); virtual ~Socket(); + + virtual void close(); + virtual void read(); + virtual size_t read( void *pBuf, size_t nBytes ); + virtual size_t write( const void *pBuf, size_t nBytes ); - private: + virtual long tell(); + virtual void seek( long offset ); + virtual void setPos( long pos ); + virtual void setPosEnd( long pos ); + virtual bool isEOS(); + + virtual bool canRead(); + virtual bool canWrite(); + virtual bool canSeek(); + private: + int nSocket; + bool bActive; + FString sReadBuf; }; } diff --git a/src/stream.h b/src/stream.h index ae94234..e640959 100644 --- a/src/stream.h +++ b/src/stream.h @@ -6,6 +6,16 @@ namespace Bu { + /** + * The basis for a completely general data transport mechanism. Anything + * that inherits from this should provide at least the basic read and/or + * write functions, and very probably the close function. Any functions + * that aren't supported should throw an exception if called. + * + * The constructor of a child class should pretty much universally be used + * to open the stream. I can't think of anything that should require an + * exception. + */ class Stream { public: -- cgit v1.2.3 From 530014a3cce53e86dce8917e98a4e86d02f176aa Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 26 Apr 2007 15:06:49 +0000 Subject: Merged Ito and put it in the BU namespace. I should probably clean up the formatting on the comments, some of the lines wrap, but I'm not too worried about it right now. I also fixed up the doxygen config and build.conf files so that everything is building nice and smooth now. --- Doxyfile | 4 +- build.conf | 26 ++++-- mkincs.sh | 5 ++ src/exceptions.cpp | 1 + src/exceptions.h | 1 + src/ito.cpp | 40 +++++++++ src/ito.h | 98 ++++++++++++++++++++ src/itoatom.h | 56 ++++++++++++ src/itocondition.cpp | 42 +++++++++ src/itocondition.h | 81 +++++++++++++++++ src/itomutex.cpp | 27 ++++++ src/itomutex.h | 61 +++++++++++++ src/itoqueue.h | 231 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.dox | 14 +++ src/serversocket.cpp | 148 +++++++++++++++++++++++++++++++ src/serversocket.h | 31 +++++++ src/socket.cpp | 16 +--- src/socket.h | 4 +- src/tests/itoqueue1.cpp | 110 +++++++++++++++++++++++ src/tests/itoqueue2.cpp | 83 +++++++++++++++++ 20 files changed, 1059 insertions(+), 20 deletions(-) create mode 100755 mkincs.sh create mode 100644 src/ito.cpp create mode 100644 src/ito.h create mode 100644 src/itoatom.h create mode 100644 src/itocondition.cpp create mode 100644 src/itocondition.h create mode 100644 src/itomutex.cpp create mode 100644 src/itomutex.h create mode 100644 src/itoqueue.h create mode 100644 src/main.dox create mode 100644 src/serversocket.cpp create mode 100644 src/serversocket.h create mode 100644 src/tests/itoqueue1.cpp create mode 100644 src/tests/itoqueue2.cpp (limited to 'src/exceptions.h') diff --git a/Doxyfile b/Doxyfile index 6c7412e..c3168fa 100644 --- a/Doxyfile +++ b/Doxyfile @@ -74,9 +74,9 @@ QUIET = NO WARNINGS = YES WARN_IF_UNDOCUMENTED = YES WARN_IF_DOC_ERROR = YES -WARN_NO_PARAMDOC = NO +WARN_NO_PARAMDOC = YES WARN_FORMAT = "$file:$line: $text" -WARN_LOGFILE = +WARN_LOGFILE = Doxywarn #--------------------------------------------------------------------------- # configuration options related to the input files #--------------------------------------------------------------------------- diff --git a/build.conf b/build.conf index bc186f9..c289205 100644 --- a/build.conf +++ b/build.conf @@ -1,13 +1,17 @@ # This is a build file for libbu++ -default action: check "libbu++.a" -"clean" action: clean targets() -"tests" action: check targets() filter regexp("^tests/.*$") -"all" action: check targets() -"fstring" action: check "tests/fstring" +default action: check group "lnhdrs", check "libbu++.a" +"tests" action: check group "lnhdrs", check group "tests" +"all" action: check group "lnhdrs", check targets() set "CXXFLAGS" += "-ggdb -Wall" +filesIn("src") filter regexp("^src/(.*)\\.h$", "src/bu/{re:1}.h"): + rule "hln", + group "lnhdrs", + target file, + input "src/{re:1}.h" + "libbu++.a": rule "lib", target file, @@ -17,6 +21,7 @@ set "CXXFLAGS" += "-ggdb -Wall" directoriesIn("src/tests","tests/"): rule "exe", target file, + group "tests", requires "libbu++.a", set "CXXFLAGS" += "-Isrc", set "LDFLAGS" += "-L. -lbu++", @@ -25,14 +30,18 @@ directoriesIn("src/tests","tests/"): filesIn("src/tests") filter regexp("^src/tests/(.*)\\.cpp$", "tests/{re:1}"): rule "exe", target file, + group "tests", requires "libbu++.a", set "CXXFLAGS" += "-Isrc", set "LDFLAGS" += "-L. -lbu++", input "src/{target}.cpp" +["tests/itoqueue1", "tests/itoqueue2"]: set "LDFLAGS" += "-lpthread" + directoriesIn("src/unit","unit/"): rule "exe", target file, + group "tests", requires "libbu++.a", set "CXXFLAGS" += "-Isrc", set "LDFLAGS" += "-L. -lbu++", @@ -41,6 +50,7 @@ directoriesIn("src/unit","unit/"): filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"): rule "exe", target file, + group "tests", requires "libbu++.a", set "CXXFLAGS" += "-Isrc", set "LDFLAGS" += "-L. -lbu++", @@ -63,3 +73,9 @@ rule "cpp": produces "{re:1}.o", requires commandToList("g++ -M {CXXFLAGS} {match}", "make"), perform command("g++ {CXXFLAGS} -c -o {target} {match}") + +rule "hln": + matches regexp("src/(.*)\\.h"), + produces "src/bu/{re:1}.h", + perform command("ln -s ../{re:1}.h {target}") + diff --git a/mkincs.sh b/mkincs.sh new file mode 100755 index 0000000..6f72f89 --- /dev/null +++ b/mkincs.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd src/bu +rm * +for i in ../*.h; do ln -s $i; done diff --git a/src/exceptions.cpp b/src/exceptions.cpp index a512105..d9f4e70 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -5,6 +5,7 @@ namespace Bu { subExceptionDef( XmlException ) subExceptionDef( FileException ) + subExceptionDef( SocketException ) subExceptionDef( ConnectionException ) subExceptionDef( PluginException ) subExceptionDef( UnsupportedException ) diff --git a/src/exceptions.h b/src/exceptions.h index 3efa19f..f146b73 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -8,6 +8,7 @@ namespace Bu { subExceptionDecl( XmlException ) subExceptionDecl( FileException ) + subExceptionDecl( SocketException ) subExceptionDecl( ConnectionException ) subExceptionDecl( PluginException ) subExceptionDecl( UnsupportedException ) diff --git a/src/ito.cpp b/src/ito.cpp new file mode 100644 index 0000000..001ca06 --- /dev/null +++ b/src/ito.cpp @@ -0,0 +1,40 @@ +#include "ito.h" + +Bu::Ito::Ito() +{ +} + +Bu::Ito::~Ito() +{ +} + +bool Bu::Ito::start() +{ + nHandle = pthread_create( &ptHandle, NULL, threadRunner, this ); + + return true; +} + +bool Bu::Ito::stop() +{ + pthread_exit( &ptHandle ); + + return true; +} + +void *Bu::Ito::threadRunner( void *pThread ) +{ + return ((Ito *)pThread)->run(); +} + +bool Bu::Ito::join() +{ + pthread_join( ptHandle, NULL ); + return true; +} + +void Bu::Ito::yield() +{ + pthread_yield(); +} + diff --git a/src/ito.h b/src/ito.h new file mode 100644 index 0000000..01253f5 --- /dev/null +++ b/src/ito.h @@ -0,0 +1,98 @@ +#ifndef ITO_H +#define ITO_H + +#include + +namespace Bu +{ + /** + * Simple thread class. This wraps the basic pthread (posix threads) system in + * an object oriented sort of way. It allows you to create a class with + * standard member variables and callable functions that can be run in it's own + * thread, one per class instance. + *@author Mike Buland + */ + class Ito + { + public: + /** + * Construct an Ito thread. + */ + Ito(); + + /** + * Destroy an Ito thread. + */ + virtual ~Ito(); + + /** + * Begin thread execution. This will call the overridden run function, + * which will simply execute in it's own thread until the function exits, + * the thread is killed, or the thread is cancelled (optionally). The + * thread started in this manner has access to all of it's class variables, + * but be sure to protect possible multiple-access with ItoMutex objects. + *@returns True if starting the thread was successful. False if something + * went wrong and the thread has not started. + */ + bool start(); + + /** + * Forcibly kill a thread. This is not generally considered a good thing to + * do, but in those rare cases you need it, it's invaluable. The problem + * with stopping (or killing) a thread is that it stops it the moment you + * call stop, no matter what it's doing. The object oriented approach to + * this will help clean up any class variables that were used, but anything + * not managed as a member variable will probably create a memory leak type + * of situation. Instead of stop, consider using cancel, which can be + * handled by the running thread in a graceful manner. + *@returns True if the thread was stopped, false otherwise. When this + * function returns the thread may not have stopped, to ensure that the + * thread has really stopped, call join. + */ + bool stop(); + + /** + * The workhorse of the Ito class. This is the function that will run in + * the thread, when this function exits the thread dies and is cleaned up + * by the system. Make sure to read up on ItoMutex, ItoCondition, and + * cancel to see how to control and protect everything you do in a safe way + * within this function. + *@returns I'm not sure right now, but this is the posix standard form. + */ + virtual void *run()=0; + + /** + * Join the thread in action. This function performs what is commonly + * called a thread join. That is that it effectively makes the calling + * thread an the Ito thread contained in the called object one in the same, + * and pauses the calling thread until the called thread exits. That is, + * when called from, say, your main(), mythread.join() will not return until + * the thread mythread has exited. This is very handy at the end of + * programs to ensure all of your data was cleaned up. + *@returns True if the thread was joined, false if the thread couldn't be + * joined, usually because it isn't running to begin with. + */ + bool join(); + + private: + pthread_t ptHandle; /**< Internal handle to the posix thread. */ + int nHandle; /**< Numeric handle to the posix thread. */ + + protected: + /** + * This is the hidden-heard of the thread system. While run is what the + * user gets to override, and everything said about it is true, this is + * the function that actually makes up the thread, it simply calls the + * run member function in an OO-friendly way. This is what allows us to + * use member variables from within the thread itself. + *@param Should always be this. + *@returns This is specified by posix, I'm not sure yet. + */ + static void *threadRunner( void *pThread ); + + void yield(); + + }; +} + +#endif diff --git a/src/itoatom.h b/src/itoatom.h new file mode 100644 index 0000000..96090f2 --- /dev/null +++ b/src/itoatom.h @@ -0,0 +1,56 @@ +#ifndef ITO_QUEUE_H +#define ITO_QUEUE_H + +#include + +#include "itomutex.h" +#include "itocondition.h" + +/** + * A thread-safe wrapper class. + *@author Mike Buland + */ +template +class ItoAtom +{ +public: + /** + * Construct an empty queue. + */ + ItoAtom() + { + } + + ItoAtom( const T &src ) : + data( src ) + { + } + + ~ItoQueue() + { + } + + T get() + { + mOperate.lock(); + mOperate.unlock(); + return data; + } + + void set( const T &val ) + { + mOperate.lock(); + data = val; + cBlock.signal(); + mOperate.unlock(); + } + +private: + Item *pStart; /**< The start of the queue, the next element to dequeue. */ + Item *pEnd; /**< The end of the queue, the last element to dequeue. */ + + ItoMutex mOperate; /**< The master mutex, used on all operations. */ + ItoCondition cBlock; /**< The condition for blocking dequeues. */ +}; + +#endif diff --git a/src/itocondition.cpp b/src/itocondition.cpp new file mode 100644 index 0000000..d8f5375 --- /dev/null +++ b/src/itocondition.cpp @@ -0,0 +1,42 @@ +#include + +#include "itocondition.h" + +Bu::ItoCondition::ItoCondition() +{ + pthread_cond_init( &cond, NULL ); +} + +Bu::ItoCondition::~ItoCondition() +{ + pthread_cond_destroy( &cond ); +} + +int Bu::ItoCondition::wait() +{ + return pthread_cond_wait( &cond, &mutex ); +} + +int Bu::ItoCondition::wait( int nSec, int nUSec ) +{ + struct timeval now; + struct timespec timeout; + struct timezone tz; + + gettimeofday( &now, &tz ); + timeout.tv_sec = now.tv_sec + nSec + ((now.tv_usec + nUSec)/1000000); + timeout.tv_nsec = ((now.tv_usec + nUSec)%1000000)*1000; + + return pthread_cond_timedwait( &cond, &mutex, &timeout ); +} + +int Bu::ItoCondition::signal() +{ + return pthread_cond_signal( &cond ); +} + +int Bu::ItoCondition::broadcast() +{ + return pthread_cond_broadcast( &cond ); +} + diff --git a/src/itocondition.h b/src/itocondition.h new file mode 100644 index 0000000..4771b22 --- /dev/null +++ b/src/itocondition.h @@ -0,0 +1,81 @@ +#ifndef ITO_CONDITION_H +#define ITO_CONDITION_H + +#include + +#include "itomutex.h" + +namespace Bu +{ + /** + * Ito condition. This is a fairly simple condition mechanism. As you may + * notice this class inherits from the ItoMutex class, this is because all + * conditions must be within a locked block. The standard usage of a condition + * is to pause one thread, perhaps indefinately, until another thread signals + * that it is alright to procede. + *
+ * Standard usage for the thread that wants to wait is as follows: + *
+	 * ItoCondition cond;
+	 * ... // Perform setup and enter your run loop
+	 * cond.lock();
+	 * while( !isFinished() ) // Could be anything you're waiting for
+	 *     cond.wait();
+	 * ...  // Take care of what you have to.
+	 * cond.unlock();
+	 * 
+ * The usage for the triggering thread is much simpler, when it needs to tell + * the others that it's time to grab some data it calls either signal or + * broadcast. See both of those functions for the difference. + *@author Mike Buland + */ + class ItoCondition : public ItoMutex + { + public: + /** + * Create a condition. + */ + ItoCondition(); + + /** + * Destroy a condition. + */ + ~ItoCondition(); + + /** + * Wait forever, or until signalled. This has to be called from within a + * locked section, i.e. before calling this this object's lock function + * should be called. + */ + int wait(); + + /** + * Wait for a maximum of nSec seconds and nUSec micro-seconds or until + * signalled. This is a little more friendly function if you want to + * perform other operations in the thrad loop that calls this function. + * Like the other wait function, this must be inside a locked section. + *@param nSec The seconds to wait. + *@param nUSec the micro-seconds to wait. + */ + int wait( int nSec, int nUSec ); + + /** + * Notify the next thread waiting on this condition that they can go ahead. + * This only signals one thread, the next one in the condition queue, that + * it is safe to procede with whatever operation was being waited on. + */ + int signal(); + + /** + * Notify all threads waiting on this condition that they can go ahead now. + * This function is slower than signal, but more effective in certain + * situations where you may not know how many threads should be activated. + */ + int broadcast(); + + private: + pthread_cond_t cond; /**< Internal condition reference. */ + }; +} + +#endif diff --git a/src/itomutex.cpp b/src/itomutex.cpp new file mode 100644 index 0000000..dc51af9 --- /dev/null +++ b/src/itomutex.cpp @@ -0,0 +1,27 @@ +#include "itomutex.h" + +Bu::ItoMutex::ItoMutex() +{ + pthread_mutex_init( &mutex, NULL ); +} + +Bu::ItoMutex::~ItoMutex() +{ + pthread_mutex_destroy( &mutex ); +} + +int Bu::ItoMutex::lock() +{ + return pthread_mutex_lock( &mutex ); +} + +int Bu::ItoMutex::unlock() +{ + return pthread_mutex_unlock( &mutex ); +} + +int Bu::ItoMutex::trylock() +{ + return pthread_mutex_trylock( &mutex ); +} + diff --git a/src/itomutex.h b/src/itomutex.h new file mode 100644 index 0000000..80956b8 --- /dev/null +++ b/src/itomutex.h @@ -0,0 +1,61 @@ +#ifndef ITO_MUTEX_H +#define ITO_MUTEX_H + +#include + +namespace Bu +{ + /** + * Simple mutex wrapper. Currently this doesn't do anything extra for you + * except keep all of the functionality together in an OO sorta' way and keep + * you from having to worry about cleaning up your mutexes properly, or initing + * them. + *@author Mike Buland + */ + class ItoMutex + { + public: + /** + * Create an unlocked mutex. + */ + ItoMutex(); + + /** + * Destroy a mutex. This can only be done when a mutex is unlocked. + * Failure to unlock before destroying a mutex object could cause it to + * wait for the mutex to unlock, the odds of which are usually farily low + * at deconstruction time. + */ + ~ItoMutex(); + + /** + * Lock the mutex. This causes all future calls to lock on this instance + * of mutex to block until the first thread that called mutex unlocks it. + * At that point the next thread that called lock will get a chance to go + * to work. Because of the nature of a mutex lock it is a very bad idea to + * do any kind of serious or rather time consuming computation within a + * locked section. This can cause thread-deadlock and your program may + * hang. + */ + int lock(); + + /** + * Unlock the mutex. This allows the next thread that asked for a lock to + * lock the mutex and continue with execution. + */ + int unlock(); + + /** + * Try to lock the mutex. This is the option to go with if you cannot avoid + * putting lengthy operations within a locked section. trylock will attempt + * to lock the mutex, if the mutex is already locked this function returns + * immediately with an error code. + */ + int trylock(); + + protected: + pthread_mutex_t mutex; /**< The internal mutex reference. */ + }; +} + +#endif diff --git a/src/itoqueue.h b/src/itoqueue.h new file mode 100644 index 0000000..322698d --- /dev/null +++ b/src/itoqueue.h @@ -0,0 +1,231 @@ +#ifndef ITO_QUEUE_H +#define ITO_QUEUE_H + +#include + +#include "itomutex.h" +#include "itocondition.h" + +namespace Bu +{ + /** + * A thread-safe queue class. This class is a very simple queue with some cool + * extra functionality for use with the Ito system. The main extra that it + * provides is the option to either dequeue without blocking, with infinite + * blocking, or with timed blocking, which will return a value if something is + * enqueued within the specified time limit, or NULL if the time limit is + * exceded. + *@author Mike Buland + */ + template + class ItoQueue + { + private: + /** + * Helper struct. Keeps track of linked-list items for the queue data. + */ + typedef struct Item + { + T pData; + Item *pNext; + } Item; + + public: + /** + * Construct an empty queue. + */ + ItoQueue() : + pStart( NULL ), + pEnd( NULL ), + nSize( 0 ) + { + } + + /** + * Destroy the queue. This function will simply free all contained + * structures. If you stored pointers in the queue, this will lose the + * pointers without cleaning up the memory they pointed to. Make sure + * you're queue is empty before allowing it to be destroyed! + */ + ~ItoQueue() + { + Item *pCur = pStart; + while( pCur ) + { + Item *pTmp = pCur->pNext; + delete pCur; + pCur = pTmp; + } + } + + /** + * Enqueue a pieces of data. The new data will go at the end of the queue, + * and unless another piece of data is enqueued, will be the last piece of + * data to be dequeued. + *@param pData The data to enqueue. If this is not a primitive data type + * it's probably best to use a pointer type. + */ + void enqueue( T pData ) + { + mOperate.lock(); + + if( pStart == NULL ) + { + pStart = pEnd = new Item; + pStart->pData = pData; + pStart->pNext = NULL; + nSize++; + } + else + { + pEnd->pNext = new Item; + pEnd = pEnd->pNext; + pEnd->pData = pData; + pEnd->pNext = NULL; + nSize++; + } + + cBlock.signal(); + + mOperate.unlock(); + } + + /** + * Dequeue the first item from the queue. This function can operate in two + * different modes, blocking and non-blocking. In non-blocking mode it will + * return immediately weather there was data in the queue or not. If there + * was data it will remove it from the queue and return it to the caller. + * In blocking mode it will block forever wating for data to be enqueued. + * When data finally is enqueued this function will return immediately with + * the new data. The only way this function should ever return a null in + * blocking mode is if the calling thread was cancelled. It's probably a + * good idea to check for NULL return values even if you use blocking, just + * to be on the safe side. + *@param bBlock Set to true to enable blocking, leave as false to work in + * non-blocking mode. + *@returns The next piece of data in the queue, or NULL if no data was in + * the queue. + */ + T dequeue( bool bBlock=false ) + { + mOperate.lock(); + if( pStart == NULL ) + { + mOperate.unlock(); + + if( bBlock ) + { + cBlock.lock(); + + while( pStart == NULL ) + cBlock.wait(); + + T tmp = dequeue( false ); + + cBlock.unlock(); + return tmp; + + } + + return NULL; + } + else + { + T pTmp = pStart->pData; + Item *pDel = pStart; + pStart = pStart->pNext; + delete pDel; + nSize--; + + mOperate.unlock(); + return pTmp; + } + } + + /** + * Operates just like the other dequeue function in blocking mode with one + * twist. This function will block for at most nSec seconds and nUSec + * micro-seconds. If the timer is up and no data is available, this will + * just return NULL. If data is enqueued before the timeout expires, it + * will dequeue and exit immediately. + *@param nSec The number of seconds to wait, max. + *@param nUSec The number of micro-seconds to wait, max. + *@returns The next piece of data in the queue, or NULL if the timeout was + * exceeded. + */ + T dequeue( int nSec, int nUSec ) + { + mOperate.lock(); + if( pStart == NULL ) + { + mOperate.unlock(); + + cBlock.lock(); + + cBlock.wait( nSec, nUSec ); + + if( pStart == NULL ) + { + cBlock.unlock(); + return NULL; + } + + mOperate.lock(); + T pTmp = pStart->pData; + Item *pDel = pStart; + pStart = pStart->pNext; + delete pDel; + nSize--; + mOperate.unlock(); + + cBlock.unlock(); + return pTmp; + } + else + { + T pTmp = pStart->pData; + Item *pDel = pStart; + pStart = pStart->pNext; + delete pDel; + nSize--; + + mOperate.unlock(); + return pTmp; + } + } + + /** + * Checks to see if the queue has data in it or not. Note that there is no + * function to determine the length of the queue. This data isn't kept + * track of. If you really need to know, fix this. + *@returns True if the queue is empty, false if it has data in it. + */ + bool isEmpty() + { + mOperate.lock(); + bool bEmpty = (pStart == NULL ); + mOperate.unlock(); + + return bEmpty; + } + + long getSize() + { + mOperate.lock(); + long nRet = nSize; + mOperate.unlock(); + + return nRet; + } + + private: + Item *pStart; /**< The start of the queue, the next element to dequeue. */ + Item *pEnd; /**< The end of the queue, the last element to dequeue. */ + long nSize; /**< The number of items in the queue. */ + + ItoMutex mOperate; /**< The master mutex, used on all operations. */ + ItoCondition cBlock; /**< The condition for blocking dequeues. */ + }; +} + +#endif diff --git a/src/main.dox b/src/main.dox new file mode 100644 index 0000000..668d2e3 --- /dev/null +++ b/src/main.dox @@ -0,0 +1,14 @@ +/** + *@mainpage libbu++ utility library + * + *@section secIntro Introduction + * + * Libbu++ is a C++ library of general utility classes and functions. They + * cover a wide range of topics from streams and sockets to data structures to + * data serialization and xml handling to threading. + * + */ + +/** + *@namespace Bu The core libbu++ namespace, to ensure things don't get muddied. + */ diff --git a/src/serversocket.cpp b/src/serversocket.cpp new file mode 100644 index 0000000..c53c80d --- /dev/null +++ b/src/serversocket.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "serversocket.h" +#include "exceptions.h" + +Bu::ServerSocket::ServerSocket( int nPort, int nPoolSize ) : + nPort( nPort ) +{ + /* Create the socket and set it up to accept connections. */ + struct sockaddr_in name; + + /* Give the socket a name. */ + name.sin_family = AF_INET; + name.sin_port = htons( nPort ); + + // I think this specifies who we will accept connections from, + // a good thing to make configurable later on + name.sin_addr.s_addr = htonl( INADDR_ANY ); + + startServer( name, nPoolSize ); +} + +Bu::ServerSocket::ServerSocket(const FString &sAddr,int nPort, int nPoolSize) : + nPort( nPort ) +{ + /* Create the socket and set it up to accept connections. */ + struct sockaddr_in name; + + /* Give the socket a name. */ + name.sin_family = AF_INET; + name.sin_port = htons( nPort ); + + inet_aton( sAddr.getStr(), &name.sin_addr ); + + startServer( name, nPoolSize ); +} + +Bu::ServerSocket::~ServerSocket() +{ +} + +void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) +{ + /* Create the socket. */ + nServer = socket( PF_INET, SOCK_STREAM, 0 ); + if( nServer < 0 ) + { + throw Bu::SocketException("Couldn't create a listen socket."); + } + + int opt = 1; + setsockopt( + nServer, + SOL_SOCKET, + SO_REUSEADDR, + (char *)&opt, + sizeof( opt ) + ); + + if( bind( nServer, (struct sockaddr *) &name, sizeof(name) ) < 0 ) + { + throw Bu::SocketException("Couldn't bind to the listen socket."); + } + + if( listen( nServer, nPoolSize ) < 0 ) + { + throw Bu::SocketException( + "Couldn't begin listening to the server socket." + ); + } + + FD_ZERO( &fdActive ); + /* Initialize the set of active sockets. */ + FD_SET( nServer, &fdActive ); +} + +int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) +{ + fd_set fdRead = fdActive; + + struct timeval xT; + + xT.tv_sec = nTimeoutSec; + xT.tv_usec = nTimeoutUSec; + + if( TEMP_FAILURE_RETRY(select( nServer+1, &fdRead, NULL, NULL, &xT )) < 0 ) + { + throw SocketException( + "Error scanning for new connections: %s", strerror( errno ) + ); + } + + if( FD_ISSET( nServer, &fdRead ) ) + { + struct sockaddr_in clientname; + size_t size; + int nClient; + + size = sizeof( clientname ); +#ifdef __CYGWIN__ + nClient = ::accept( nServer, (struct sockaddr *)&clientname, + (int *)&size + ); +#else + nClient = ::accept( nServer, (struct sockaddr *)&clientname, &size ); +#endif + if( nClient < 0 ) + { + throw SocketException( + "Error accepting a new connection: %s", strerror( errno ) + ); + } + char tmpa[20]; + inet_ntop( AF_INET, (void *)&clientname.sin_addr, tmpa, 20 ); + //"New connection from host %s, port %hd.", + // tmpa, ntohs (clientname.sin_port) ); + + { + int flags; + + flags = fcntl( nClient, F_GETFL, 0 ); + flags |= O_NONBLOCK; + if( fcntl( nClient, F_SETFL, flags ) < 0) + { + throw SocketException( + "Error setting option on client socket: %s", + strerror( errno ) + ); + } + } + + return nClient; + } + + return -1; +} + diff --git a/src/serversocket.h b/src/serversocket.h new file mode 100644 index 0000000..9a26e2d --- /dev/null +++ b/src/serversocket.h @@ -0,0 +1,31 @@ +#ifndef SERVER_SOCKET_H +#define SERVER_SOCKET_H + +#include +#include "fstring.h" +#include "socket.h" + +namespace Bu +{ + /** + * + */ + class ServerSocket + { + public: + ServerSocket( int nPort, int nPoolSize=40 ); + ServerSocket( const FString &sAddr, int nPort, int nPoolSize=40 ); + virtual ~ServerSocket(); + + int accept( int nTimeoutSec, int nTimeoutUSec ); + + private: + void startServer( struct sockaddr_in &name, int nPoolSize ); + + fd_set fdActive; + int nServer; + int nPort; + }; +} + +#endif diff --git a/src/socket.cpp b/src/socket.cpp index c4f914b..455b5c8 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -118,6 +118,7 @@ void Bu::Socket::close() //} } +/* void Bu::Socket::read() { char buffer[RBS]; @@ -132,7 +133,6 @@ void Bu::Socket::read() if( nbytes < 0 && errno != 0 && errno != EAGAIN ) { //printf("errno: %d, %s\n", errno, strerror( errno ) ); - /* Read error. */ //perror("readInput"); throw ConnectionException( excodeReadError, @@ -146,15 +146,13 @@ void Bu::Socket::read() break; nTotalRead += nbytes; sReadBuf.append( buffer, nbytes ); - /* Data read. */ if( nbytes < RBS ) { break; } - /* New test, if data is divisible by RBS bytes on some libs the - * read could block, this keeps it from happening. - */ + // New test, if data is divisible by RBS bytes on some libs the + // read could block, this keeps it from happening. { fd_set rfds; FD_ZERO(&rfds); @@ -171,13 +169,7 @@ void Bu::Socket::read() } } } - - /* - if( pProtocol != NULL && nTotalRead > 0 ) - { - pProtocol->onNewData(); - }*/ -} +}*/ size_t Bu::Socket::read( void *pBuf, size_t nBytes ) { diff --git a/src/socket.h b/src/socket.h index 3d0125d..568cad6 100644 --- a/src/socket.h +++ b/src/socket.h @@ -19,7 +19,7 @@ namespace Bu virtual ~Socket(); virtual void close(); - virtual void read(); + //virtual void read(); virtual size_t read( void *pBuf, size_t nBytes ); virtual size_t write( const void *pBuf, size_t nBytes ); @@ -33,6 +33,8 @@ namespace Bu virtual bool canWrite(); virtual bool canSeek(); + + private: int nSocket; bool bActive; diff --git a/src/tests/itoqueue1.cpp b/src/tests/itoqueue1.cpp new file mode 100644 index 0000000..f73f4d3 --- /dev/null +++ b/src/tests/itoqueue1.cpp @@ -0,0 +1,110 @@ +#include +#include "bu/ito.h" +#include "bu/itoqueue.h" + +class Reader : public Bu::Ito +{ +public: + Reader( Bu::ItoQueue &q, int id ) : + q( q ), + id( id ) + { + } + + void *run() + { + for( int i = 0; i < 10; i++ ) + { + std::string *pStr = q.dequeue( true ); + if( pStr == NULL ) + { + printf("Null received...\n"); + } + else + { + printf("[%d] read: %s\n", id, pStr->c_str() ); + delete pStr; + } + usleep( (int)(((double)rand())/((double)RAND_MAX)*2000000.0) ); + } + + return NULL; + } + +private: + Bu::ItoQueue &q; + int id; +}; + +class Writer : public Bu::Ito +{ +public: + Writer( Bu::ItoQueue &q, int id, const char *strbase ) : + q( q ), + strbase( strbase ), + id( id ) + { + } + + void *run() + { + for( int i = 0; i < 11; i++ ) + { + usleep( (int)(((double)rand())/((double)RAND_MAX)*2000000.0) ); + q.enqueue( new std::string( strbase ) ); + printf("[%d] write: %s\n", id, strbase ); + } + + return NULL; + } + +private: + Bu::ItoQueue &q; + const char *strbase; + int id; +}; + +int main() +{ + Writer *wr[5]; + Reader *rd[5]; + const char bob[][7]={ + {"Test 1"}, + {"Test 2"}, + {"Test 3"}, + {"Test 4"}, + {"Test 5"} + }; + + Bu::ItoQueue q; + + for( int j = 0; j < 5; j++ ) + { + wr[j] = new Writer( q, j, bob[j] ); + rd[j] = new Reader( q, j ); + } + + for( int j = 0; j < 5; j++ ) + { + rd[j]->start(); + } + + for( int j = 0; j < 5; j++ ) + { + wr[j]->start(); + } + + for( int j = 0; j < 5; j++ ) + { + rd[j]->join(); + } + + for( int j = 0; j < 5; j++ ) + { + delete wr[j]; + delete rd[j]; + } + + return 0; +} + diff --git a/src/tests/itoqueue2.cpp b/src/tests/itoqueue2.cpp new file mode 100644 index 0000000..f4b5e19 --- /dev/null +++ b/src/tests/itoqueue2.cpp @@ -0,0 +1,83 @@ +#include +#include "bu/ito.h" +#include "bu/itoqueue.h" +#include + +class Reader : public Bu::Ito +{ +public: + Reader( Bu::ItoQueue &q, int id ) : + q( q ), + id( id ) + { + } + + void *run() + { + for( int i = 0; i < 10; i++ ) + { + std::string *pStr = q.dequeue( 0, 500000 ); + if( pStr == NULL ) + { + printf("Null received...\n"); + i--; + } + else + { + printf("[%d] read: %s\n", id, pStr->c_str() ); + delete pStr; + } + } + + return NULL; + } + +private: + Bu::ItoQueue &q; + int id; +}; + +class Writer : public Bu::Ito +{ +public: + Writer( Bu::ItoQueue &q, int id, const char *strbase ) : + q( q ), + strbase( strbase ), + id( id ) + { + } + + void *run() + { + for( int i = 0; i < 11; i++ ) + { + sleep( 2 ); + printf("[%d] write: %s\n", id, strbase ); + q.enqueue( new std::string( strbase ) ); + } + + return NULL; + } + +private: + Bu::ItoQueue &q; + const char *strbase; + int id; +}; + +int main() +{ + printf("ETIMEDOUT: %d\n", ETIMEDOUT ); + Bu::ItoQueue q; + Writer wr( q, 0, "writer" ); + Reader rd( q, 0 ); + + rd.start(); + wr.start(); + + rd.join(); + wr.join(); + + return 0; +} + -- cgit v1.2.3 From 3144bd7deb950de0cb80e2215c1545bdf8fc81e9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 6 Jun 2007 18:48:07 +0000 Subject: Except for storing the data somewhere, the TafReader is more or less done. Some more generalizations are in order, then we'll stuff the data somewhere. --- misc/taf | 4 +- src/exceptions.cpp | 1 + src/exceptions.h | 1 + src/tafreader.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/tafreader.h | 6 +++ src/tests/taf.cpp | 9 ++++ 6 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/tests/taf.cpp (limited to 'src/exceptions.h') diff --git a/misc/taf b/misc/taf index 5ffcdcf..045b042 100644 --- a/misc/taf +++ b/misc/taf @@ -20,7 +20,7 @@ {: key="," value="yell"} {: key="li" value="lightning"} } - description = """They appear to be rather average looking, not particularly + description = "They appear to be rather average looking, not particularly tall or short, with facial features that are difficult to remember even - seconds after witnessing them.""" + seconds after witnessing them." } diff --git a/src/exceptions.cpp b/src/exceptions.cpp index d9f4e70..50b95c3 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -4,6 +4,7 @@ namespace Bu { subExceptionDef( XmlException ) + subExceptionDef( TafException ) subExceptionDef( FileException ) subExceptionDef( SocketException ) subExceptionDef( ConnectionException ) diff --git a/src/exceptions.h b/src/exceptions.h index f146b73..d5a9d39 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -7,6 +7,7 @@ namespace Bu { subExceptionDecl( XmlException ) + subExceptionDecl( TafException ) subExceptionDecl( FileException ) subExceptionDecl( SocketException ) subExceptionDecl( ConnectionException ) diff --git a/src/tafreader.cpp b/src/tafreader.cpp index 4d10b8d..91aa9f2 100644 --- a/src/tafreader.cpp +++ b/src/tafreader.cpp @@ -1,14 +1,141 @@ -#include "tafreader.h" +#include "bu/tafreader.h" +#include "bu/exceptions.h" +#include "bu/fstring.h" Bu::TafReader::TafReader( Bu::Stream &sIn ) : sIn( sIn ) { + next(); + node(); } Bu::TafReader::~TafReader() { } +void Bu::TafReader::node() +{ + ws(); + if( c != '{' ) + throw Bu::TafException("Expected '{'"); + next(); + ws(); + Bu::FString sName; + for(;;) + { + if( c == ':' ) + break; + else + sName += c; + next(); + } + next(); + printf("Node[%s]:\n", sName.getStr() ); + + nodeContent(); + + if( c != '}' ) + throw TafException("Expected '}'"); + + next(); +} + +void Bu::TafReader::nodeContent() +{ + for(;;) + { + ws(); + if( c == '{' ) + node(); + else if( c == '}' ) + return; + else + nodeProperty(); + } +} + +void Bu::TafReader::nodeProperty() +{ + Bu::FString sName; + for(;;) + { + if( isws() || c == '=' ) + break; + else + sName += c; + next(); + } + ws(); + if( c != '=' ) + { + printf(" %s (true)\n", sName.getStr() ); + return; + } + next(); + ws(); + Bu::FString sValue; + if( c == '"' ) + { + next(); + for(;;) + { + if( c == '\\' ) + { + next(); + if( c == 'x' ) + { + char code[3]={'\0','\0','\0'}; + next(); + code[0] = c; + next(); + code[1] = c; + c = (unsigned char)strtol( code, NULL, 16 ); + } + else if( c == '"' ) + c = '"'; + else + throw TafException("Invalid escape sequence."); + } + else if( c == '"' ) + break; + sValue += c; + next(); + } + next(); + } + else + { + for(;;) + { + if( isws() || c == '}' || c == '{' ) + break; + sValue += c; + next(); + } + } + printf(" %s = %s\n", sName.getStr(), sValue.getStr() ); +} + +FString Bu::TafReader::readStr() +{ +} + +void Bu::TafReader::ws() +{ + for(;;) + { + if( !isws() ) + return; + + next(); + } +} + +bool Bu::TafReader::isws() +{ + return (c == ' ' || c == '\t' || c == '\n' || c == '\r'); +} + void Bu::TafReader::next() { sIn.read( &c, 1 ); diff --git a/src/tafreader.h b/src/tafreader.h index b552f5d..127b571 100644 --- a/src/tafreader.h +++ b/src/tafreader.h @@ -17,7 +17,13 @@ namespace Bu virtual ~TafReader(); private: + void node(); + void nodeContent(); + void nodeProperty(); + void ws(); + bool isws(); void next(); + FString readStr(); char c; Stream &sIn; diff --git a/src/tests/taf.cpp b/src/tests/taf.cpp new file mode 100644 index 0000000..12c653e --- /dev/null +++ b/src/tests/taf.cpp @@ -0,0 +1,9 @@ +#include "bu/tafreader.h" +#include "bu/file.h" + +int main() +{ + Bu::File f("test.taf", "rb"); + Bu::TafReader tr( f ); +} + -- cgit v1.2.3 From 932677862376e9642a496a15b7f09f1106f495a5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 Jun 2007 20:49:39 +0000 Subject: Many minor changes, hopefully the header fixes will keep future header macro conflicts from happening. And, from now on, other projects should do -Ilibbu++ not -Ilibbu++/src so we can get ready for an installed version of libbu++. --- bu | 1 + src/archival.h | 4 ++-- src/archive.h | 4 ++-- src/atom.h | 4 ++-- src/bzip2.h | 4 ++-- src/client.h | 4 ++-- src/exceptionbase.h | 4 ++-- src/exceptions.h | 4 ++-- src/file.h | 4 ++-- src/filter.h | 4 ++-- src/fstring.h | 4 ++-- src/hash.h | 4 ++-- src/ito.h | 4 ++-- src/itoatom.h | 4 ++-- src/itocondition.h | 4 ++-- src/itomutex.h | 4 ++-- src/itoqueue.h | 4 ++-- src/linkmessage.h | 4 ++-- src/list.h | 4 ++-- src/membuf.h | 4 ++-- src/paramproc.h | 4 ++-- src/plugger.h | 4 ++-- src/programchain.h | 4 ++-- src/programlink.h | 4 ++-- src/protocol.h | 4 ++-- src/ringbuffer.h | 4 ++-- src/server.h | 4 ++-- src/serversocket.h | 4 ++-- src/singleton.h | 4 ++-- src/socket.h | 4 ++-- src/sptr.h | 4 ++-- src/stream.h | 4 ++-- src/unitsuite.h | 4 ++-- 33 files changed, 65 insertions(+), 64 deletions(-) create mode 120000 bu (limited to 'src/exceptions.h') diff --git a/bu b/bu new file mode 120000 index 0000000..02b8847 --- /dev/null +++ b/bu @@ -0,0 +1 @@ +src/bu/ \ No newline at end of file diff --git a/src/archival.h b/src/archival.h index e2c803c..8be3308 100644 --- a/src/archival.h +++ b/src/archival.h @@ -1,5 +1,5 @@ -#ifndef ARCHIVAL_H -#define ARCHIVAL_H +#ifndef BU_ARCHIVAL_H +#define BU_ARCHIVAL_H namespace Bu { diff --git a/src/archive.h b/src/archive.h index 05c6f57..6c0c1df 100644 --- a/src/archive.h +++ b/src/archive.h @@ -1,5 +1,5 @@ -#ifndef ARCHIVE_H -#define ARCHIVE_H +#ifndef BU_ARCHIVE_H +#define BU_ARCHIVE_H #include #include diff --git a/src/atom.h b/src/atom.h index a0469b6..fad47eb 100644 --- a/src/atom.h +++ b/src/atom.h @@ -1,5 +1,5 @@ -#ifndef ATOM_H -#define ATOM_H +#ifndef BU_ATOM_H +#define BU_ATOM_H #include #include diff --git a/src/bzip2.h b/src/bzip2.h index 25f10c5..0a111e8 100644 --- a/src/bzip2.h +++ b/src/bzip2.h @@ -1,5 +1,5 @@ -#ifndef B_ZIP2_H -#define B_ZIP2_H +#ifndef BU_BZIP2_H +#define BU_BZIP2_H #include #include diff --git a/src/client.h b/src/client.h index 02ba077..cee558d 100644 --- a/src/client.h +++ b/src/client.h @@ -1,5 +1,5 @@ -#ifndef CLIENT_H -#define CLIENT_H +#ifndef BU_CLIENT_H +#define BU_CLIENT_H #include diff --git a/src/exceptionbase.h b/src/exceptionbase.h index fd78089..24e4bbf 100644 --- a/src/exceptionbase.h +++ b/src/exceptionbase.h @@ -1,5 +1,5 @@ -#ifndef EXCEPTION_BASE_H -#define EXCEPTION_BASE_H +#ifndef BU_EXCEPTION_BASE_H +#define BU_EXCEPTION_BASE_H #include #include diff --git a/src/exceptions.h b/src/exceptions.h index d5a9d39..b824b91 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -1,5 +1,5 @@ -#ifndef EXCEPTIONS_H -#define EXCEPTIONS_H +#ifndef BU_EXCEPTIONS_H +#define BU_EXCEPTIONS_H #include "exceptionbase.h" #include diff --git a/src/file.h b/src/file.h index 0f638a7..d4e43eb 100644 --- a/src/file.h +++ b/src/file.h @@ -1,5 +1,5 @@ -#ifndef FILE_H -#define FILE_H +#ifndef BU_FILE_H +#define BU_FILE_H #include diff --git a/src/filter.h b/src/filter.h index 7bb04bc..bd557b2 100644 --- a/src/filter.h +++ b/src/filter.h @@ -1,5 +1,5 @@ -#ifndef FILTER_H -#define FILTER_H +#ifndef BU_FILTER_H +#define BU_FILTER_H #include diff --git a/src/fstring.h b/src/fstring.h index 7180f5f..bf6518b 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -1,5 +1,5 @@ -#ifndef F_STRING_H -#define F_STRING_H +#ifndef BU_F_STRING_H +#define BU_F_STRING_H #include #include diff --git a/src/hash.h b/src/hash.h index 6c4a443..0fa3af7 100644 --- a/src/hash.h +++ b/src/hash.h @@ -1,5 +1,5 @@ -#ifndef HASH_H -#define HASH_H +#ifndef BU_HASH_H +#define BU_HASH_H #include #include diff --git a/src/ito.h b/src/ito.h index 01253f5..c062052 100644 --- a/src/ito.h +++ b/src/ito.h @@ -1,5 +1,5 @@ -#ifndef ITO_H -#define ITO_H +#ifndef BU_ITO_H +#define BU_ITO_H #include diff --git a/src/itoatom.h b/src/itoatom.h index 96090f2..7fc9090 100644 --- a/src/itoatom.h +++ b/src/itoatom.h @@ -1,5 +1,5 @@ -#ifndef ITO_QUEUE_H -#define ITO_QUEUE_H +#ifndef BU_ITO_QUEUE_H +#define BU_ITO_QUEUE_H #include diff --git a/src/itocondition.h b/src/itocondition.h index 4771b22..1793f81 100644 --- a/src/itocondition.h +++ b/src/itocondition.h @@ -1,5 +1,5 @@ -#ifndef ITO_CONDITION_H -#define ITO_CONDITION_H +#ifndef BU_ITO_CONDITION_H +#define BU_ITO_CONDITION_H #include diff --git a/src/itomutex.h b/src/itomutex.h index 80956b8..9c9d205 100644 --- a/src/itomutex.h +++ b/src/itomutex.h @@ -1,5 +1,5 @@ -#ifndef ITO_MUTEX_H -#define ITO_MUTEX_H +#ifndef BU_ITO_MUTEX_H +#define BU_ITO_MUTEX_H #include diff --git a/src/itoqueue.h b/src/itoqueue.h index 322698d..75a2f27 100644 --- a/src/itoqueue.h +++ b/src/itoqueue.h @@ -1,5 +1,5 @@ -#ifndef ITO_QUEUE_H -#define ITO_QUEUE_H +#ifndef BU_ITO_QUEUE_H +#define BU_ITO_QUEUE_H #include diff --git a/src/linkmessage.h b/src/linkmessage.h index 64b8bc2..baa22a6 100644 --- a/src/linkmessage.h +++ b/src/linkmessage.h @@ -1,8 +1,8 @@ /**\file linkmessage.h */ -#ifndef LINKMESSAGE_H -#define LINKMESSAGE_H +#ifndef BU_LINKMESSAGE_H +#define BU_LINKMESSAGE_H namespace Bu { diff --git a/src/list.h b/src/list.h index 314459e..6235619 100644 --- a/src/list.h +++ b/src/list.h @@ -1,5 +1,5 @@ -#ifndef LIST_H -#define LIST_H +#ifndef BU_LIST_H +#define BU_LIST_H #include #include "bu/exceptionbase.h" diff --git a/src/membuf.h b/src/membuf.h index 877b35e..b82f943 100644 --- a/src/membuf.h +++ b/src/membuf.h @@ -1,5 +1,5 @@ -#ifndef MEM_BUF_H -#define MEM_BUF_H +#ifndef BU_MEM_BUF_H +#define BU_MEM_BUF_H #include diff --git a/src/paramproc.h b/src/paramproc.h index beee4a0..2bca588 100644 --- a/src/paramproc.h +++ b/src/paramproc.h @@ -1,5 +1,5 @@ -#ifndef PARAM_PROC_H -#define PARAM_PROC_H +#ifndef BU_PARAM_PROC_H +#define BU_PARAM_PROC_H #include #include diff --git a/src/plugger.h b/src/plugger.h index 4d2e7af..79a3271 100644 --- a/src/plugger.h +++ b/src/plugger.h @@ -1,5 +1,5 @@ -#ifndef PLUGGER_H -#define PLUGGER_H +#ifndef BU_PLUGGER_H +#define BU_PLUGGER_H #include "bu/hash.h" diff --git a/src/programchain.h b/src/programchain.h index e73d659..336a9f1 100644 --- a/src/programchain.h +++ b/src/programchain.h @@ -1,5 +1,5 @@ -#ifndef PROGRAMCHAIN_H -#define PROGRAMCHAIN_H +#ifndef BU_PROGRAMCHAIN_H +#define BU_PROGRAMCHAIN_H #include "bu/list.h" #include "bu/linkmessage.h" diff --git a/src/programlink.h b/src/programlink.h index 7c31a18..07d7489 100644 --- a/src/programlink.h +++ b/src/programlink.h @@ -1,5 +1,5 @@ -#ifndef PROGRAMLINK_H -#define PROGRAMLINK_H +#ifndef BU_PROGRAMLINK_H +#define BU_PROGRAMLINK_H #include "bu/linkmessage.h" #include "bu/programchain.h" diff --git a/src/protocol.h b/src/protocol.h index 3accd99..c7a9275 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -1,5 +1,5 @@ -#ifndef PROTOCOL_H -#define PROTOCOL_H +#ifndef BU_PROTOCOL_H +#define BU_PROTOCOL_H #include diff --git a/src/ringbuffer.h b/src/ringbuffer.h index 625f65b..be80e2f 100644 --- a/src/ringbuffer.h +++ b/src/ringbuffer.h @@ -1,5 +1,5 @@ -#ifndef RING_BUFFER_H -#define RING_BUFFER_H +#ifndef BU_RING_BUFFER_H +#define BU_RING_BUFFER_H #include #include "bu/exceptionbase.h" diff --git a/src/server.h b/src/server.h index 07eef95..302b6e3 100644 --- a/src/server.h +++ b/src/server.h @@ -1,5 +1,5 @@ -#ifndef SERVER_H -#define SERVER_H +#ifndef BU_SERVER_H +#define BU_SERVER_H #include diff --git a/src/serversocket.h b/src/serversocket.h index cb86078..b4ee247 100644 --- a/src/serversocket.h +++ b/src/serversocket.h @@ -1,5 +1,5 @@ -#ifndef SERVER_SOCKET_H -#define SERVER_SOCKET_H +#ifndef BU_SERVER_SOCKET_H +#define BU_SERVER_SOCKET_H #include #include "bu/fstring.h" diff --git a/src/singleton.h b/src/singleton.h index 4976a61..c43d71b 100644 --- a/src/singleton.h +++ b/src/singleton.h @@ -1,5 +1,5 @@ -#ifndef SINGLETON_H -#define SINGLETON_H +#ifndef BU_SINGLETON_H +#define BU_SINGLETON_H #include diff --git a/src/socket.h b/src/socket.h index c9dbd8d..0ccee3b 100644 --- a/src/socket.h +++ b/src/socket.h @@ -1,5 +1,5 @@ -#ifndef SOCKET_H -#define SOCKET_H +#ifndef BU_SOCKET_H +#define BU_SOCKET_H #include diff --git a/src/sptr.h b/src/sptr.h index 4baa697..75851a6 100644 --- a/src/sptr.h +++ b/src/sptr.h @@ -1,5 +1,5 @@ -#ifndef SPTR_H -#define SPTR_H +#ifndef BU_SPTR_H +#define BU_SPTR_H #include #include diff --git a/src/stream.h b/src/stream.h index 056de0c..1e236a6 100644 --- a/src/stream.h +++ b/src/stream.h @@ -1,5 +1,5 @@ -#ifndef STREAM_H -#define STREAM_H +#ifndef BU_STREAM_H +#define BU_STREAM_H #include #include diff --git a/src/unitsuite.h b/src/unitsuite.h index 6e9270a..578b4cc 100644 --- a/src/unitsuite.h +++ b/src/unitsuite.h @@ -1,5 +1,5 @@ -#ifndef UNIT_SUITE_H -#define UNIT_SUITE_H +#ifndef BU_UNIT_SUITE_H +#define BU_UNIT_SUITE_H #include #include -- cgit v1.2.3