From 509d136e9adb60c56369565b9545e613cac3678e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 12 Nov 2009 17:05:30 +0000 Subject: I've started my campaign to clean up all of the header files in libbu++ as far as includes go. This required a little bit of reworking as far as archive goes, but I've been planning on changing it aronud for a bit anyway. The final result here is that you may need to add some more includes in your own code, libbu++ doesn't include as many random things you didn't ask for anymore, most of these seem to be bu/hash.h, unistd.h, and time.h. Also, any Archive functions and operators should use ArchiveBase when they can instead of Archive, archivebase.h is a much lighterweight include that will be used everywhere in core that it can be, there are a few classes that actually want a specific archiver to be used, they will use it (such as the nids storage class). So far, except for adding header files, nothing has changed in functionality, and no other code changes should be required, although the above mentioned archive changeover is reccomended. --- src/archival.cpp | 18 ++ src/archival.h | 9 +- src/archive.cpp | 476 ---------------------------------------------- src/archive.h | 252 +----------------------- src/archivebase.cpp | 190 ++++++++++++++++++ src/archivebase.h | 67 +++++++ src/array.h | 33 ++++ src/cachestorenids.h | 1 + src/client.h | 1 + src/exceptionbase.cpp | 1 + src/exceptionbase.h | 1 - src/fastcgi.cpp | 1 + src/fbasicstring.h | 108 +++-------- src/fifo.cpp | 1 + src/formatter.cpp | 2 + src/fstring.cpp | 6 - src/fstring.h | 6 + src/hash.cpp | 19 -- src/hash.h | 56 ++---- src/heap.h | 81 +++++--- src/httpget.h | 1 + src/list.h | 127 +++++++++++-- src/logger.cpp | 1 + src/nidsstream.cpp | 2 + src/paramproc.cpp | 1 + src/protocolhttp.h | 1 + src/server.cpp | 1 + src/server.h | 1 + src/set.h | 30 +++ src/tests/archive.cpp | 3 +- src/tests/archive2.cpp | 6 +- src/tests/cache.cpp | 1 + src/tests/fastcgi.cpp | 2 + src/tests/heap.cpp | 53 ++++++ src/tests/itoheap.cpp | 1 + src/tests/listsort.cpp | 60 ++++++ src/tests/ringbuffer.cpp | 2 + src/tests/serverticks.cpp | 1 + src/tests/socketblock.cpp | 1 + src/tests/socketbreak.cpp | 1 + src/tests/uuid.cpp | 14 ++ src/unit/archive.unit | 89 +++++++-- src/unit/file.unit | 2 +- src/unit/fstring.unit | 21 ++ src/unit/list.unit | 80 ++++++++ src/util.cpp | 19 +- src/util.h | 10 +- 47 files changed, 931 insertions(+), 929 deletions(-) create mode 100644 src/archivebase.cpp create mode 100644 src/archivebase.h create mode 100644 src/tests/listsort.cpp create mode 100644 src/tests/uuid.cpp (limited to 'src') diff --git a/src/archival.cpp b/src/archival.cpp index daec60a..cae183a 100644 --- a/src/archival.cpp +++ b/src/archival.cpp @@ -15,3 +15,21 @@ Bu::Archival::~Archival() { } +Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, const Bu::Archival &p) +{ + const_cast(p).archive( s ); + return s; +} + +Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, Bu::Archival &p) +{ + p.archive( s ); + return s; +} + +Bu::ArchiveBase &Bu::operator>>(Bu::ArchiveBase &s, Bu::Archival &p) +{ + p.archive( s ); + return s; +} + diff --git a/src/archival.h b/src/archival.h index d758f3d..76f0f5f 100644 --- a/src/archival.h +++ b/src/archival.h @@ -8,6 +8,8 @@ #ifndef BU_ARCHIVAL_H #define BU_ARCHIVAL_H +#include "bu/archivebase.h" + namespace Bu { /** @@ -38,8 +40,13 @@ namespace Bu * you are loading or saving. * @param ar A reference to the Archive object to use. */ - virtual void archive( class Archive &ar )=0; + virtual void archive( class ArchiveBase &ar )=0; }; + + ArchiveBase &operator<<(ArchiveBase &, const class Bu::Archival &); + ArchiveBase &operator<<(ArchiveBase &, class Bu::Archival &); + ArchiveBase &operator>>(ArchiveBase &, class Bu::Archival &); + } #endif diff --git a/src/archive.cpp b/src/archive.cpp index 6bad44c..1b48ec8 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -46,482 +46,6 @@ bool Bu::Archive::isLoading() return bLoading; } -Bu::Archive &Bu::Archive::operator<<(bool p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(char p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(signed char p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(unsigned char p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(signed short p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(unsigned short p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(signed int p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(unsigned int p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(signed long p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(unsigned long p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(signed long long p) -{ - write( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator<<(unsigned long 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>>(char &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(signed char &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(unsigned char &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(signed short &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(unsigned short &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(signed int &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(unsigned int &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(signed long &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(unsigned long &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(signed long long &p) -{ - read( &p, sizeof(p) ); - return *this; -} -Bu::Archive &Bu::Archive::operator>>(unsigned long 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) -{ - 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::Archival &p) -{ - p.archive( s ); - return s; -} - -Bu::Archive &Bu::operator>>(Bu::Archive &s, Bu::Archival &p) -{ - p.archive( s ); - return s; -} - -Bu::Archive &Bu::operator<<(Bu::Archive &ar, class Bu::Archival *p ) -{ - ar << *p; - return ar; -} - -Bu::Archive &Bu::operator>>(Bu::Archive &ar, class Bu::Archival *p ) -{ - ar >> *p; - return ar; -} - -Bu::Archive &Bu::operator<<( Bu::Archive &ar, std::string &s ) -{ - // This should be defined as long anyway, this is just insurance - ar << (long)s.length(); - ar.write( s.c_str(), s.length() ); - - return ar; -} - -Bu::Archive &Bu::operator>>( Bu::Archive &ar, std::string &s ) -{ - long l; - ar >> l; - char *tmp = new char[l+1]; - tmp[l] = '\0'; - ar.read( tmp, l ); - s = tmp; - delete[] tmp; - - return ar; -} - uint32_t Bu::Archive::getID( const void *ptr ) { if( hPtrID.has( (ptrdiff_t)ptr ) ) diff --git a/src/archive.h b/src/archive.h index dcbd219..6eaf408 100644 --- a/src/archive.h +++ b/src/archive.h @@ -9,11 +9,8 @@ #define BU_ARCHIVE_H #include -#include -#include +#include "bu/archivebase.h" #include "bu/hash.h" -#include "bu/list.h" -//#include "bu/set.h" #include "bu/util.h" namespace Bu @@ -68,7 +65,7 @@ namespace Bu * 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 + class Archive : public ArchiveBase { private: bool bLoading; @@ -88,81 +85,6 @@ namespace Bu virtual void write(const void *, int32_t); virtual void read(void *, int32_t); - virtual Archive &operator<<(bool p); - virtual Archive &operator<<(char p); - virtual Archive &operator<<(signed char p); - virtual Archive &operator<<(unsigned char p); - virtual Archive &operator<<(signed short p); - virtual Archive &operator<<(unsigned short p); - virtual Archive &operator<<(signed int p); - virtual Archive &operator<<(unsigned int p); - virtual Archive &operator<<(signed long p); - virtual Archive &operator<<(unsigned long p); - virtual Archive &operator<<(signed long long p); - virtual Archive &operator<<(unsigned long long p); - virtual Archive &operator<<(float p); - virtual Archive &operator<<(double p); - virtual Archive &operator<<(long double p); - - virtual Archive &operator>>(bool &p); - virtual Archive &operator>>(char &p); - virtual Archive &operator>>(signed char &p); - virtual Archive &operator>>(unsigned char &p); - virtual Archive &operator>>(signed short &p); - virtual Archive &operator>>(unsigned short &p); - virtual Archive &operator>>(signed int &p); - virtual Archive &operator>>(unsigned int &p); - virtual Archive &operator>>(signed long &p); - virtual Archive &operator>>(unsigned long &p); - virtual Archive &operator>>(signed long long &p); - virtual Archive &operator>>(unsigned long long &p); - virtual Archive &operator>>(float &p); - virtual Archive &operator>>(double &p); - virtual Archive &operator>>(long double &p); - - /* - 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 &); - */ - /** * For storage, get an ID for the pointer to the object you're going to * write. @@ -193,176 +115,6 @@ namespace Bu Hash hPtrID; Hash > hPtrDest; }; - - Archive &operator<<(Archive &, class Bu::Archival &); - Archive &operator>>(Archive &, class Bu::Archival &); - //Archive &operator&&(Archive &s, class Bu::Archival &p); - - Archive &operator<<(Archive &, std::string &); - Archive &operator>>(Archive &, std::string &); - //Archive &operator&&(Archive &, std::string &); - - template Archive &operator&&( Archive &ar, T &dat ) - { - if( ar.isLoading() ) - { - return ar >> dat; - } - else - { - return ar << dat; - } - } - - template Archive &operator<<( Archive &ar, std::list &l ) - { - typename std::list::size_type num = l.getSize(); - ar << num; - for( typename std::list::const_iterator i = l.begin(); i != l.end(); - i++ ) - { - ar << *i; - } - - return ar; - } - - template Archive &operator>>( Archive &ar, std::list &l ) - { - typename std::list::size_type num; - ar >> num; - - l.resize( num ); - for( typename std::list::const_iterator i = l.begin(); - i != l.end(); i++ ) - { - ar >> *i; - } - - return ar; - } - - Archive &operator<<(Archive &, class Bu::Archival *p); - Archive &operator>>(Archive &, class Bu::Archival *p); - - template - Archive &operator<<( Archive &ar, Hash &h ) - { - ar << h.getSize(); - for( typename Hash::iterator i = h.begin(); i != h.end(); i++ ) - { - //std::pair p = *i; - ar << (i.getKey()) << (i.getValue()); - } - - return ar; - } - - template - Archive &operator>>( Archive &ar, Hash &h ) - { - h.clear(); - long nSize; - ar >> nSize; - - for( long j = 0; j < nSize; j++ ) - { - key k; value v; - ar >> k >> v; - h.insert( k, v ); - } - - return ar; - } - - template - Archive &operator<<( Archive &ar, List &h ) - { - ar << h.getSize(); - for( typename List::iterator i = h.begin(); i != h.end(); i++ ) - { - ar << (*i); - } - - return ar; - } - - template - Archive &operator>>( Archive &ar, List &h ) - { - h.clear(); - long nSize; - ar >> nSize; - - for( long j = 0; j < nSize; j++ ) - { - value v; - ar >> v; - h.append( v ); - } - - return ar; - } - - template class Array; - template - Archive &operator<<( Archive &ar, Array &h ) - { - ar << h.getSize(); - for( typename Array::iterator i = h.begin(); i != h.end(); i++ ) - { - ar << (*i); - } - - return ar; - } - - template - Archive &operator>>(Archive &ar, Array &h ) - { - h.clear(); - long nSize; - ar >> nSize; - - h.setCapacity( nSize ); - for( long j = 0; j < nSize; j++ ) - { - value v; - ar >> v; - h.append( v ); - } - return ar; - } - - template class Set; - template - Archive &operator<<( Archive &ar, Set &h ) - { - ar << h.getSize(); - for( typename Set::iterator i = h.begin(); i != h.end(); i++ ) - { - ar << (*i); - } - - return ar; - } - - template - Archive &operator>>( Archive &ar, Set &h ) - { - h.clear(); - long nSize; - ar >> nSize; - - for( long j = 0; j < nSize; j++ ) - { - key v; - ar >> v; - h.insert( v ); - } - - return ar; - } } #endif diff --git a/src/archivebase.cpp b/src/archivebase.cpp new file mode 100644 index 0000000..41b7ebe --- /dev/null +++ b/src/archivebase.cpp @@ -0,0 +1,190 @@ +#include "bu/archivebase.h" + +Bu::ArchiveBase::ArchiveBase() +{ +} + +Bu::ArchiveBase::~ArchiveBase() +{ +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, bool p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, char p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed char p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned char p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed short p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned short p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed int p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned int p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long long p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long long p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, float p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, double p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, long double p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, bool &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, char &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed char &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned char &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed short &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned short &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed int &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned int &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, float &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, double &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, long double &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + diff --git a/src/archivebase.h b/src/archivebase.h new file mode 100644 index 0000000..f421efb --- /dev/null +++ b/src/archivebase.h @@ -0,0 +1,67 @@ +#ifndef BU_ARCHIVE_BASE_H +#define BU_ARCHIVE_BASE_H + +#include + +namespace Bu +{ + class ArchiveBase + { + public: + ArchiveBase(); + virtual ~ArchiveBase(); + + virtual void close()=0; + virtual void write( const void *pData, int32_t iLength )=0; + virtual void read( void *pData, int32_t iLength )=0; + virtual bool isLoading()=0; + }; + + template ArchiveBase &operator&&( ArchiveBase &ar, T &dat ) + { + if( ar.isLoading() ) + { + return ar >> dat; + } + else + { + return ar << dat; + } + } + + ArchiveBase &operator<<( ArchiveBase &ar, bool p ); + ArchiveBase &operator<<( ArchiveBase &ar, char p ); + ArchiveBase &operator<<( ArchiveBase &ar, signed char p ); + ArchiveBase &operator<<( ArchiveBase &ar, unsigned char p ); + ArchiveBase &operator<<( ArchiveBase &ar, signed short p ); + ArchiveBase &operator<<( ArchiveBase &ar, unsigned short p ); + ArchiveBase &operator<<( ArchiveBase &ar, signed int p ); + ArchiveBase &operator<<( ArchiveBase &ar, unsigned int p ); + ArchiveBase &operator<<( ArchiveBase &ar, signed long p ); + ArchiveBase &operator<<( ArchiveBase &ar, unsigned long p ); + ArchiveBase &operator<<( ArchiveBase &ar, signed long long p ); + ArchiveBase &operator<<( ArchiveBase &ar, unsigned long long p ); + ArchiveBase &operator<<( ArchiveBase &ar, float p ); + ArchiveBase &operator<<( ArchiveBase &ar, double p ); + ArchiveBase &operator<<( ArchiveBase &ar, long double p ); + + ArchiveBase &operator>>( ArchiveBase &ar, bool &p ); + ArchiveBase &operator>>( ArchiveBase &ar, char &p ); + ArchiveBase &operator>>( ArchiveBase &ar, signed char &p ); + ArchiveBase &operator>>( ArchiveBase &ar, unsigned char &p ); + ArchiveBase &operator>>( ArchiveBase &ar, signed short &p ); + ArchiveBase &operator>>( ArchiveBase &ar, unsigned short &p ); + ArchiveBase &operator>>( ArchiveBase &ar, signed int &p ); + ArchiveBase &operator>>( ArchiveBase &ar, unsigned int &p ); + ArchiveBase &operator>>( ArchiveBase &ar, signed long &p ); + ArchiveBase &operator>>( ArchiveBase &ar, unsigned long &p ); + ArchiveBase &operator>>( ArchiveBase &ar, signed long long &p ); + ArchiveBase &operator>>( ArchiveBase &ar, unsigned long long &p ); + ArchiveBase &operator>>( ArchiveBase &ar, float &p ); + ArchiveBase &operator>>( ArchiveBase &ar, double &p ); + ArchiveBase &operator>>( ArchiveBase &ar, long double &p ); + + +}; + +#endif diff --git a/src/array.h b/src/array.h index ca4ec21..6279382 100644 --- a/src/array.h +++ b/src/array.h @@ -10,6 +10,7 @@ #include #include "bu/exceptionbase.h" +#include "bu/archivebase.h" namespace Bu { @@ -442,6 +443,38 @@ namespace Bu return f; } + + template + ArchiveBase &operator<<( ArchiveBase &ar, + const Array &h ) + { + ar << h.getSize(); + for( typename Array::const_iterator i = + h.begin(); i != h.end(); i++ ) + { + ar << (*i); + } + + return ar; + } + + template + ArchiveBase &operator>>(ArchiveBase &ar, Array &h ) + { + h.clear(); + long nSize; + ar >> nSize; + + h.setCapacity( nSize ); + for( long j = 0; j < nSize; j++ ) + { + value v; + ar >> v; + h.append( v ); + } + return ar; + } + } #endif diff --git a/src/cachestorenids.h b/src/cachestorenids.h index 3859900..ae2b9a2 100644 --- a/src/cachestorenids.h +++ b/src/cachestorenids.h @@ -15,6 +15,7 @@ #include "bu/cachestore.h" #include "bu/file.h" +#include "bu/archive.h" namespace Bu { diff --git a/src/client.h b/src/client.h index 72b1b05..e91dead 100644 --- a/src/client.h +++ b/src/client.h @@ -15,6 +15,7 @@ namespace Bu { class Protocol; + class Stream; class Socket; class ClientLinkFactory; diff --git a/src/exceptionbase.cpp b/src/exceptionbase.cpp index a3b10af..b3f06f4 100644 --- a/src/exceptionbase.cpp +++ b/src/exceptionbase.cpp @@ -8,6 +8,7 @@ #include "exceptionbase.h" #include #include +#include Bu::ExceptionBase::ExceptionBase( const char *lpFormat, ... ) throw() : nErrorCode( 0 ), diff --git a/src/exceptionbase.h b/src/exceptionbase.h index de63177..20e8ee1 100644 --- a/src/exceptionbase.h +++ b/src/exceptionbase.h @@ -8,7 +8,6 @@ #ifndef BU_EXCEPTION_BASE_H #define BU_EXCEPTION_BASE_H -#include #include #include diff --git a/src/fastcgi.cpp b/src/fastcgi.cpp index 3cc3a10..1662fe6 100644 --- a/src/fastcgi.cpp +++ b/src/fastcgi.cpp @@ -9,6 +9,7 @@ #include #include +#include #include "bu/membuf.h" diff --git a/src/fbasicstring.h b/src/fbasicstring.h index 225bc80..0e63efe 100644 --- a/src/fbasicstring.h +++ b/src/fbasicstring.h @@ -15,10 +15,13 @@ #include #endif -#include "bu/archival.h" -#include "bu/archive.h" #include "bu/util.h" #include "bu/sharedcore.h" +#include "bu/exceptionbase.h" +#include "bu/archivebase.h" +#include "bu/list.h" + +#include namespace Bu { @@ -30,21 +33,8 @@ namespace Bu FStringChunk *pNext; }; -#ifndef VALTEST -#define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) ) -#endif +#define cpy( dest, src, size ) Bu::memcpy( dest, src, size*sizeof(chr) ) -#ifdef VALTEST - void cpy( chr *dest, const chr *src, long count ) const - { - for( int j = 0; j < count; j++ ) - { - *dest = *src; - dest++; - src++; - } - } -#endif template struct FStringCore { @@ -185,7 +175,7 @@ namespace Bu *@param chunkalloc (typename) Memory Allocator for chr chunks */ template< typename chr, int nMinSize=256, typename chralloc=std::allocator, typename chunkalloc=std::allocator > > - class FBasicString : public SharedCore< FStringCore >, public Archival + class FBasicString : public SharedCore< FStringCore > { protected: typedef struct FStringChunk Chunk; @@ -211,8 +201,7 @@ namespace Bu } FBasicString( const MyType &rSrc ) : - SharedCore( rSrc ), - Archival() + SharedCore( rSrc ) { } @@ -1284,13 +1273,6 @@ namespace Bu return (*this); } - MyType &operator=( const std::basic_string &rData ) - { - set( rData.c_str(), rData.size() ); - - return (*this); - } - MyType operator+( const MyType &rRight ) const { MyType ret( *this ); @@ -1505,8 +1487,8 @@ namespace Bu flatten(); pData.flatten(); - const chr *a = pData.core->pFirst->pData; - chr *b = core->pFirst->pData; + const chr *a = core->pFirst->pData; + chr *b = pData.core->pFirst->pData; for( long j = 0; j < core->nLength; j++, a++, b++ ) { if( *a != *b ) @@ -1521,8 +1503,8 @@ namespace Bu flatten(); pData.flatten(); - const chr *a = pData.core->pFirst->pData; - chr *b = core->pFirst->pData; + const chr *a = core->pFirst->pData; + chr *b = pData.core->pFirst->pData; for( long j = 0; j < core->nLength; j++, a++, b++ ) { if( *a != *b ) @@ -1902,51 +1884,6 @@ namespace Bu va_end( ap ); } - /** - * Function the archiver calls to archive your FString. - *@param ar (Archive) The archive which is archiving your FString. - */ - void archive( class Archive &ar ) - { - if( ar.isLoading() ) - { - _hardCopy(); - core->clear(); - long nLen; - ar >> nLen; - - if( nLen > 0 ) - { - Chunk *pNew = core->newChunk( nLen ); - ar.read( pNew->pData, nLen*sizeof(chr) ); - core->appendChunk( pNew ); - } - } - else - { - flatten(); - - ar << core->nLength; - if( core->nLength ) - ar.write( core->pFirst->pData, core->nLength*sizeof(chr) ); - } - } - /* - void archive( class Archive &ar ) const - { - if( ar.isLoading() ) - { - } - else - { - flatten(); - - ar << core->nLength; - if( core->nLength ) - ar.write( core->pFirst->pData, core->nLength*sizeof(chr) ); - } - }*/ - iterator begin() { if( core->nLength == 0 ) @@ -2016,10 +1953,27 @@ namespace Bu ret.append( rRight ); return ret; } + + template + ArchiveBase &operator<<( ArchiveBase &ar, const FBasicString &s ) + { + long n = s.getSize(); + ar << n; + ar.write( s.getConstStr(), n ); + return ar; + } + + template + ArchiveBase &operator>>( ArchiveBase &ar, FBasicString &s ) + { + long n; + ar >> n; + s.setSize( n ); + ar.read( s.getStr(), n ); + return ar; + } } -#ifndef VALTEST #undef cpy -#endif #endif diff --git a/src/fifo.cpp b/src/fifo.cpp index f34bb91..f72e965 100644 --- a/src/fifo.cpp +++ b/src/fifo.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Bu { subExceptionDef( FifoException ) } diff --git a/src/formatter.cpp b/src/formatter.cpp index b841c5e..830e527 100644 --- a/src/formatter.cpp +++ b/src/formatter.cpp @@ -7,6 +7,8 @@ #include "formatter.h" +#include + Bu::Formatter::Formatter( Stream &rOut ) : rOut( rOut ), uIndent( 0 ), diff --git a/src/fstring.cpp b/src/fstring.cpp index f77e718..a036d72 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -33,12 +33,6 @@ template<> bool Bu::__cmpHashKeys( return a == b; } -std::basic_ostream& operator<< (std::basic_ostream &os, const Bu::FString &val ) -{ - os.write( val.getStr(), val.getSize() ); - return os; -} - template<> void Bu::__tracer_format( const Bu::FString &v ) { printf("(%ld)\"%s\"", v.getSize(), v.getStr() ); diff --git a/src/fstring.h b/src/fstring.h index 1fc02ca..0bffd3e 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -14,6 +14,12 @@ namespace Bu { typedef FBasicString FString; + template + uint32_t __calcHashCode( const T &k ); + + template + bool __cmpHashKeys( const T &a, const T &b ); + template<> uint32_t __calcHashCode( const FString &k ); template<> bool __cmpHashKeys( const FString &a, const FString &b ); diff --git a/src/hash.cpp b/src/hash.cpp index de925a7..dfd5d80 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -67,22 +67,3 @@ template<> bool Bu::__cmpHashKeys( char * const &a, char * const &b ) 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 index 10c661f..09025ba 100644 --- a/src/hash.h +++ b/src/hash.h @@ -8,17 +8,11 @@ #ifndef BU_HASH_H #define BU_HASH_H -#include -#include #include -#include -#include -#include #include "bu/exceptionbase.h" #include "bu/list.h" #include "bu/util.h" -//#include "archival.h" -//#include "archive.h" +#include "archivebase.h" #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) @@ -373,6 +367,11 @@ namespace Bu return nFilled-nDeleted; } + bool isEmpty() const + { + return (nFilled-nDeleted) == 0; + } + /** * Get the number of items which have been deleted, but not yet * cleaned up. @@ -1171,9 +1170,6 @@ namespace Bu 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 ); - class Formatter; Formatter &operator<<( Formatter &rOut, char *sStr ); Formatter &operator<<( Formatter &rOut, signed char c ); @@ -1190,30 +1186,30 @@ namespace Bu f << '}'; return f; - } + } - /* - template - Archive &operator<<( Archive &ar, Hash &h ) + template + ArchiveBase &operator<<( ArchiveBase &ar, const Hash &h ) { - ar << h.size(); - for( typename Hash::iterator i = h.begin(); i != h.end(); i++ ) + ar << h.getSize(); + for( typename Hash::const_iterator i = h.begin(); i != h.end(); i++ ) { - std::pair p = *i; - ar << p.first << p.second; + ar << (i.getKey()) << (i.getValue()); } return ar; } - template - Archive &operator>>( Archive &ar, Hash &h ) + template + ArchiveBase &operator>>( ArchiveBase &ar, Hash &h ) { h.clear(); - uint32_t nSize; + long nSize; ar >> nSize; - for( uint32_t j = 0; j < nSize; j++ ) + for( long j = 0; j < nSize; j++ ) { key k; value v; ar >> k >> v; @@ -1221,21 +1217,7 @@ namespace Bu } return ar; - }*/ - - /* - template - Serializer &operator&&( Serializer &ar, Hash &h ) - { - if( ar.isLoading() ) - { - return ar >> h; - } - else - { - return ar << h; - } - }*/ + } } #endif diff --git a/src/heap.h b/src/heap.h index 2741818..a8d904c 100644 --- a/src/heap.h +++ b/src/heap.h @@ -9,11 +9,10 @@ #define BU_HEAP_H #include -#include #include -#include #include "bu/exceptionbase.h" #include "bu/util.h" +// #include "bu/formatter.h" namespace Bu { @@ -29,6 +28,33 @@ namespace Bu aItem( ia.allocate( iSize ) ) { } + + Heap( cmpfunc cmpin ) : + iSize( 7 ), + iFill( 0 ), + aItem( ia.allocate( iSize ) ), + cmp( cmpin ) + { + } + + Heap( int iInitialCapacity ) : + iSize( 0 ), + iFill( 0 ), + aItem( NULL ) + { + for( iSize = 1; iSize < iInitialCapacity; iSize=iSize*2+1 ) { } + aItem = ia.allocate( iSize ); + } + + Heap( cmpfunc cmpin, int iInitialCapacity ) : + iSize( 0 ), + iFill( 0 ), + aItem( NULL ), + cmp( cmpin ) + { + for( iSize = 1; iSize < iInitialCapacity; iSize=iSize*2+1 ) { } + aItem = ia.allocate( iSize ); + } virtual ~Heap() { @@ -47,9 +73,11 @@ namespace Bu { if( cmp( i, aItem[j] ) ) { - swap( i, aItem[j] ); + Bu::swap( i, aItem[j] ); } + if( j*2+1 >= iFill ) + break; if( cmp( i, aItem[j*2+1] ) ) { j = j*2+1; @@ -68,7 +96,7 @@ namespace Bu if( cmp( aItem[k], aItem[j] ) ) break; - swap( aItem[k], aItem[j] ); + Bu::swap( aItem[k], aItem[j] ); j = k; } } @@ -90,20 +118,22 @@ namespace Bu int j; for( j = 0; j < iFill; ) { - if( cmp( aItem[j*2+2], aItem[j*2+1] ) && j*2+2 < iFill ) + int k = j*2+1; + if( k+1 < iFill && cmp( aItem[k+1], aItem[k] ) ) { - aItem[j] = aItem[j*2+2]; - j = j*2+2; + aItem[j] = aItem[k+1]; + j = k+1; } - else if( j*2+1 < iFill ) + else if( k < iFill ) { - aItem[j] = aItem[j*2+1]; - j = j*2+1; + aItem[j] = aItem[k]; + j = k; } else break; } - aItem[j] = aItem[iFill-1]; + if( j < iFill-1 ) + aItem[j] = aItem[iFill-1]; ia.destroy( &aItem[iFill-1] ); iFill--; @@ -119,35 +149,34 @@ namespace Bu { return iFill; } - - void print() +/* + void print( Formatter &f ) { - printf("graph G {\n"); + f << "graph G {" << f.nl; for( int j = 0; j < iFill; j++ ) { if( j*2+1 < iFill ) - printf(" %d -- %d;\n", - j, j*2+1 - ); + f << " " << j << " -- " << j*2+1 << ";" << f.nl; if( j*2+2 < iFill ) - printf(" %d -- %d;\n", - j, j*2+2 - ); + f << " " << j << " -- " << j*2+2 << ";" << f.nl; } for( int j = 0; j < iFill; j++ ) { - printf(" %d [label=\"%d\"];\n", - j, aItem[j] - ); + f << " " << j << " [label=\"" << aItem[j] << "\"];" << f.nl; } - printf("}\n"); - } + f << "}" << f.nl; + } */ private: void upSize() { item *aNewItems = ia.allocate( iSize*2+1 ); - memcpy( aNewItems, aItem, sizeof(item)*iFill ); +// memcpy( aNewItems, aItem, sizeof(item)*iFill ); + for( int j = 0; j < iFill; j++ ) + { + ia.construct( &aNewItems[j], aItem[j] ); + ia.destroy( &aItem[j] ); + } ia.deallocate( aItem, iSize ); aItem = aNewItems; iSize = iSize*2+1; diff --git a/src/httpget.h b/src/httpget.h index 7484566..840c893 100644 --- a/src/httpget.h +++ b/src/httpget.h @@ -12,6 +12,7 @@ #include "bu/fstring.h" #include "bu/url.h" #include "bu/socket.h" +#include "bu/hash.h" namespace Bu { diff --git a/src/list.h b/src/list.h index d8c5a4a..f76c505 100644 --- a/src/list.h +++ b/src/list.h @@ -11,7 +11,8 @@ #include #include "bu/exceptionbase.h" #include "bu/sharedcore.h" -//#include "bu/util.h" +#include "bu/archivebase.h" +#include "bu/heap.h" namespace Bu { @@ -23,7 +24,7 @@ namespace Bu ListLink *pPrev; }; - template struct ListCore { @@ -42,7 +43,6 @@ namespace Bu Link *pFirst; Link *pLast; long nSize; - cmpfunc cmp; linkalloc la; valuealloc va; @@ -189,16 +189,15 @@ namespace Bu *@param linkalloc (typename) Memory Allocator for the list links. *@ingroup Containers */ - template, - typename valuealloc=std::allocator, + template, typename linkalloc=std::allocator > > - class List : public SharedCore< struct ListCore > { private: typedef struct ListLink Link; - typedef class List MyType; - typedef struct ListCore Core; + typedef class List MyType; + typedef struct ListCore Core; protected: using SharedCore< Core >::core; @@ -241,6 +240,26 @@ namespace Bu return *this; } + bool operator==( const MyType &rhs ) + { + if( getSize() != rhs.getSize() ) + return false; + + for( typename MyType::const_iterator a = begin(), b = rhs.begin(); + a; a++, b++ ) + { + if( *a != *b ) + return false; + } + + return true; + } + + bool operator!=( const MyType &rhs ) + { + return !(*this == rhs); + } + /** * Clear the data from the list. */ @@ -350,6 +369,41 @@ namespace Bu return *this; } + template + void sort( cmptype cmp ) + { + Heap hSort( cmp, getSize() ); + for( typename MyType::iterator i = begin(); i; i++ ) + { + hSort.enqueue( *i ); + } + clear(); + while( !hSort.isEmpty() ) + { + append( hSort.dequeue() ); + } + } + + void sort() + { + sort<__basicLTCmp >(); + } + + template + void sort() + { + Heap hSort( getSize() ); + for( typename MyType::iterator i = begin(); i; i++ ) + { + hSort.enqueue( *i ); + } + clear(); + while( !hSort.isEmpty() ) + { + append( hSort.dequeue() ); + } + } + /** * Insert a new item in sort order by searching for the first item that * is larger and inserting this before it, or at the end if none are @@ -357,7 +411,8 @@ namespace Bu * List all items will be sorted. To use this, the value type must * support the > operator. */ - MyType &insertSorted( const value &v ) + template + MyType &insertSorted( cmptype cmp, const value &v ) { _hardCopy(); if( core->pFirst == NULL ) @@ -371,7 +426,7 @@ namespace Bu Link *pCur = core->pFirst; for(;;) { - if( !core->cmp( v, *(pCur->pValue)) ) + if( cmp( v, *(pCur->pValue)) ) { core->insert( pCur, v ); return *this; @@ -385,6 +440,18 @@ namespace Bu } } } + + MyType &insertSorted( const value &v ) + { + return insertSorted<__basicLTCmp >( v ); + } + + template + MyType &insertSorted( const value &v ) + { + cmptype cmp; + return insertSorted( cmp, v ); + } /** * An iterator to iterate through your list. @@ -392,7 +459,7 @@ namespace Bu typedef struct iterator { friend struct const_iterator; - friend class List; + friend class List; private: Link *pLink; @@ -559,7 +626,7 @@ namespace Bu */ typedef struct const_iterator { - friend class List; + friend class List; private: Link *pLink; @@ -844,11 +911,11 @@ namespace Bu class Formatter; Formatter &operator<<( Formatter &rOut, char *sStr ); Formatter &operator<<( Formatter &rOut, signed char c ); - template - Formatter &operator<<( Formatter &f, const Bu::List &l ) + template + Formatter &operator<<( Formatter &f, const Bu::List &l ) { f << '['; - for( typename Bu::List::const_iterator i = l.begin(); i; i++ ) + for( typename Bu::List::const_iterator i = l.begin(); i; i++ ) { if( i != l.begin() ) f << ", "; @@ -858,6 +925,36 @@ namespace Bu return f; } + + template + ArchiveBase &operator<<( ArchiveBase &ar, const List &h ) + { + ar << h.getSize(); + for( typename List::const_iterator i = h.begin(); i != h.end(); i++ ) + { + ar << (*i); + } + + return ar; + } + + template + ArchiveBase &operator>>( ArchiveBase &ar, List &h ) + { + h.clear(); + long nSize; + ar >> nSize; + + for( long j = 0; j < nSize; j++ ) + { + value v; + ar >> v; + h.append( v ); + } + + return ar; + } + } #endif diff --git a/src/logger.cpp b/src/logger.cpp index 8cba1b9..e3de2fb 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -10,6 +10,7 @@ #include #include #include +#include Bu::Logger::Logger() { diff --git a/src/nidsstream.cpp b/src/nidsstream.cpp index 9aee156..e8c3323 100644 --- a/src/nidsstream.cpp +++ b/src/nidsstream.cpp @@ -7,6 +7,8 @@ #include "bu/nidsstream.h" +#include + Bu::NidsStream::NidsStream( Nids &rNids, uint32_t uStream ) : rNids( rNids ), uStream( uStream ), diff --git a/src/paramproc.cpp b/src/paramproc.cpp index 819a4da..fb07894 100644 --- a/src/paramproc.cpp +++ b/src/paramproc.cpp @@ -8,6 +8,7 @@ #include "paramproc.h" #include #include +#include #define ptrtype( iitype, iiname ) \ Bu::ParamProc::ParamPtr::ParamPtr( iitype *iiname ) : \ diff --git a/src/protocolhttp.h b/src/protocolhttp.h index 6632a65..eae525a 100644 --- a/src/protocolhttp.h +++ b/src/protocolhttp.h @@ -14,6 +14,7 @@ #include "bu/protocol.h" #include "bu/client.h" #include "bu/fstring.h" +#include "bu/hash.h" namespace Bu { diff --git a/src/server.cpp b/src/server.cpp index bfa7880..804ec70 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -7,6 +7,7 @@ #include "bu/server.h" #include +#include #include "bu/serversocket.h" #include "bu/client.h" #include "bu/socket.h" diff --git a/src/server.h b/src/server.h index 315248b..1e317a4 100644 --- a/src/server.h +++ b/src/server.h @@ -16,6 +16,7 @@ #include "bu/clientlink.h" #include "bu/clientlinkfactory.h" +#include "bu/hash.h" namespace Bu { diff --git a/src/set.h b/src/set.h index aec5781..d5593a2 100644 --- a/src/set.h +++ b/src/set.h @@ -16,6 +16,7 @@ #include #include "bu/exceptionbase.h" #include "bu/list.h" +#include "bu/archive.h" #define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) @@ -763,6 +764,35 @@ namespace Bu challoc ca; sizecalc szCalc; }; + + template + Archive &operator<<( Archive &ar, const Set &h ) + { + ar << h.getSize(); + for( typename Set::const_iterator i = h.begin(); i != h.end(); i++ ) + { + ar << (*i); + } + + return ar; + } + + template + Archive &operator>>( Archive &ar, Set &h ) + { + h.clear(); + long nSize; + ar >> nSize; + + for( long j = 0; j < nSize; j++ ) + { + key v; + ar >> v; + h.insert( v ); + } + + return ar; + } } #endif diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp index 131d4de..8a5f6ee 100644 --- a/src/tests/archive.cpp +++ b/src/tests/archive.cpp @@ -7,6 +7,7 @@ #include "bu/archive.h" #include "bu/file.h" +#include "bu/fstring.h" using namespace Bu; @@ -15,7 +16,7 @@ int main() File f("test.dat", File::Write ); Archive ar( f, Archive::save ); - std::string s("Hello there"); + Bu::FString s("Hello there"); ar << s; return 0; diff --git a/src/tests/archive2.cpp b/src/tests/archive2.cpp index fddb91b..3d95c2f 100644 --- a/src/tests/archive2.cpp +++ b/src/tests/archive2.cpp @@ -23,7 +23,7 @@ public: { } - virtual void archive( Bu::Archive &ar ) + virtual void archive( Bu::ArchiveBase &ar ) { ar && iId; } @@ -47,7 +47,7 @@ public: delete a2; } - virtual void archive( Bu::Archive &ar ) + virtual void archive( Bu::ArchiveBase &ar ) { //ar && iId && a1 && a2; ar << iId << a1 << a2; @@ -73,7 +73,7 @@ public: delete b; } - virtual void archive( Bu::Archive &ar ) + virtual void archive( Bu::ArchiveBase &ar ) { //ar && iId && a && b; ar << iId; diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 3fcc34c..a098145 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "bu/cache.h" #include "bu/file.h" diff --git a/src/tests/fastcgi.cpp b/src/tests/fastcgi.cpp index cd6cdda..46a7d62 100644 --- a/src/tests/fastcgi.cpp +++ b/src/tests/fastcgi.cpp @@ -7,6 +7,8 @@ #include "bu/fastcgi.h" +#include + class Cgi : public Bu::FastCgi { public: diff --git a/src/tests/heap.cpp b/src/tests/heap.cpp index daa0356..ae130ec 100644 --- a/src/tests/heap.cpp +++ b/src/tests/heap.cpp @@ -8,7 +8,10 @@ #include #include +#include "bu/formatter.h" #include "bu/heap.h" +#include "bu/fstring.h" +#include "bu/file.h" typedef struct num { @@ -35,8 +38,18 @@ typedef struct num } } num; +void printHeap( Bu::Heap &h, int j ) +{ + Bu::FString sFName; + sFName.format("graph-step-%02d.dot", j ); + Bu::File fOut( sFName, Bu::File::WriteNew ); + Bu::Formatter f( Bu::File ); + //h.print( f ); +} + int main() { + /* Bu::Heap hNum; for( int j = 0; j < 30; j++ ) @@ -53,6 +66,46 @@ int main() hNum.dequeue(); } printf("\n"); +*/ + Bu::Heap hStr; + int j = 0; + + hStr.enqueue("George"); + printHeap( hStr, j++ ); + hStr.enqueue("Sam"); + printHeap( hStr, j++ ); + hStr.enqueue("Abby"); + printHeap( hStr, j++ ); + hStr.enqueue("Zorro"); + printHeap( hStr, j++ ); + hStr.enqueue("Brianna"); + printHeap( hStr, j++ ); + hStr.enqueue("Kate"); + printHeap( hStr, j++ ); + hStr.enqueue("Soggy"); + printHeap( hStr, j++ ); + + while( !hStr.isEmpty() ) + { + printf("\"%s\" ", hStr.dequeue().getStr() ); + printHeap( hStr, j++ ); + } + printf("\n"); + + Bu::List lStr; + + lStr.insertSorted("George"); + lStr.insertSorted("Sam"); + lStr.insertSorted("Abby"); + lStr.insertSorted("Zorro"); + lStr.insertSorted("Brianna"); + lStr.insertSorted("Kate"); + lStr.insertSorted("Soggy"); + for( Bu::List::iterator i = lStr.begin(); i; i++ ) + { + printf("\"%s\" ", (*i).getStr() ); + } + printf("\n"); return 0; } diff --git a/src/tests/itoheap.cpp b/src/tests/itoheap.cpp index 9016d86..c5816b2 100644 --- a/src/tests/itoheap.cpp +++ b/src/tests/itoheap.cpp @@ -7,6 +7,7 @@ #include #include +#include #include "bu/itoheap.h" #include "bu/ito.h" diff --git a/src/tests/listsort.cpp b/src/tests/listsort.cpp new file mode 100644 index 0000000..f9236e6 --- /dev/null +++ b/src/tests/listsort.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace Bu; + +int main() +{ + FString a("Soggy"), b("Sam"); + + if( a < b ) + { + sio << "Bad" << sio.nl; + } + else + { + sio << "Good" << sio.nl; + } + + typedef List StrList; + + StrList lNames; + + lNames.append("George"); + lNames.append("Sam"); + lNames.append("Abby"); + lNames.append("Zorro"); + lNames.append("Brianna"); + lNames.append("Kate"); + lNames.append("Soggy"); + + sio << "Names: " << lNames << sio.nl; + lNames.sort(); + + sio << "Names: " << lNames << sio.nl; + + StrList lNames2; + + lNames2.insertSorted("George"); + lNames2.insertSorted("Sam"); + lNames2.insertSorted("Abby"); + lNames2.insertSorted("Zorro"); + lNames2.insertSorted("Brianna"); + lNames2.insertSorted("Kate"); + lNames2.insertSorted("Soggy"); + + sio << "Names: " << lNames2 << sio.nl; + + if( lNames == lNames2 ) + { + sio << "They're the same." << sio.nl; + } + else + { + sio << "They're different." << sio.nl; + } + + return 0; +} + diff --git a/src/tests/ringbuffer.cpp b/src/tests/ringbuffer.cpp index 46c515b..9489b30 100644 --- a/src/tests/ringbuffer.cpp +++ b/src/tests/ringbuffer.cpp @@ -7,6 +7,8 @@ #include "bu/ringbuffer.h" #include +#include +#include int main() { diff --git a/src/tests/serverticks.cpp b/src/tests/serverticks.cpp index e599444..3aad746 100644 --- a/src/tests/serverticks.cpp +++ b/src/tests/serverticks.cpp @@ -8,6 +8,7 @@ #include "bu/server.h" #include "bu/client.h" #include "bu/protocol.h" +#include class TickProtocol : public Bu::Protocol { diff --git a/src/tests/socketblock.cpp b/src/tests/socketblock.cpp index 2a7dfdc..5dad46c 100644 --- a/src/tests/socketblock.cpp +++ b/src/tests/socketblock.cpp @@ -9,6 +9,7 @@ #include "bu/socket.h" #include "bu/serversocket.h" #include +#include class TstServer : public Bu::Ito { diff --git a/src/tests/socketbreak.cpp b/src/tests/socketbreak.cpp index e77ae4f..b873830 100644 --- a/src/tests/socketbreak.cpp +++ b/src/tests/socketbreak.cpp @@ -7,6 +7,7 @@ #include "bu/serversocket.h" #include "bu/socket.h" +#include int main() { diff --git a/src/tests/uuid.cpp b/src/tests/uuid.cpp new file mode 100644 index 0000000..4544543 --- /dev/null +++ b/src/tests/uuid.cpp @@ -0,0 +1,14 @@ +#include +#include + +using namespace Bu; + +int main() +{ + Uuid i = Uuid::gen(); + + sio << i.toString() << sio.nl; + + return 0; +} + diff --git a/src/unit/archive.unit b/src/unit/archive.unit index ecc589b..266784f 100644 --- a/src/unit/archive.unit +++ b/src/unit/archive.unit @@ -7,14 +7,18 @@ */ #include "bu/membuf.h" +#include "bu/array.h" +#include "bu/archive.h" + +using namespace Bu; {=Init} {%testPrimitives} { - Bu::MemBuf mb; + MemBuf mb; { - Bu::Archive ar( mb, Bu::Archive::save ); + Archive ar( mb, Archive::save ); ar << (int8_t)1; ar << (uint8_t)2; ar << (int16_t)3; @@ -37,7 +41,7 @@ } mb.setPos( 0 ); { - Bu::Archive ar( mb, Bu::Archive::load ); + Archive ar( mb, Archive::load ); int8_t p1; uint8_t p2; int16_t p3; @@ -96,13 +100,13 @@ } } -{%testContainers} +{%testContainers1} { - Bu::MemBuf mb; + MemBuf mb; { - Bu::Archive ar( mb, Bu::Archive::save ); - Bu::FString sStr("This is a test string."); - Bu::List lList; + Archive ar( mb, Archive::save ); + FString sStr("This is a test string."); + List lList; lList.append( 10 ); lList.append( 20 ); lList.append( 30 ); @@ -113,14 +117,47 @@ } mb.setPos( 0 ); { - Bu::Archive ar( mb, Bu::Archive::load ); - Bu::FString sStr; - Bu::List lList; + Archive ar( mb, Archive::load ); + FString sStr; + List lList; ar >> sStr; ar >> lList; unitTest( sStr == "This is a test string." ); unitTest( lList.getSize() == 4 ); - Bu::List::iterator i = lList.begin(); + List::iterator i = lList.begin(); + unitTest( *i == 10 ); i++; + unitTest( *i == 20 ); i++; + unitTest( *i == 30 ); i++; + unitTest( *i == 40 ); + ar.close(); + } +} + +{%testContainers2} +{ + MemBuf mb; + { + Archive ar( mb, Archive::save ); + FString sStr("This is a test string."); + Array lArray; + lArray.append( 10 ); + lArray.append( 20 ); + lArray.append( 30 ); + lArray.append( 40 ); + ar << sStr; + ar << lArray; + ar.close(); + } + mb.setPos( 0 ); + { + Archive ar( mb, Archive::load ); + FString sStr; + Array lArray; + ar >> sStr; + ar >> lArray; + unitTest( sStr == "This is a test string." ); + unitTest( lArray.getSize() == 4 ); + Array::iterator i = lArray.begin(); unitTest( *i == 10 ); i++; unitTest( *i == 20 ); i++; unitTest( *i == 30 ); i++; @@ -128,3 +165,31 @@ ar.close(); } } + +{%testContainers3} +{ + MemBuf mb; + { + Archive ar( mb, Archive::save ); + Array lArray; + lArray.append( "10" ); + lArray.append( "20" ); + lArray.append( "30" ); + lArray.append( "40" ); + ar << lArray; + ar.close(); + } + mb.setPos( 0 ); + { + Archive ar( mb, Archive::load ); + Array lArray; + ar >> lArray; + unitTest( lArray.getSize() == 4 ); + Array::iterator i = lArray.begin(); + unitTest( *i == "10" ); i++; + unitTest( *i == "20" ); i++; + unitTest( *i == "30" ); i++; + unitTest( *i == "40" ); + ar.close(); + } +} diff --git a/src/unit/file.unit b/src/unit/file.unit index 911d8f6..e0d2005 100644 --- a/src/unit/file.unit +++ b/src/unit/file.unit @@ -16,7 +16,7 @@ {%writeFull} { - Bu::File sf("testfile1", Bu::File::Write ); + Bu::File sf("testfile1", Bu::File::WriteNew ); for( int c = 0; c < 256; c++ ) { unsigned char ch = (unsigned char)c; diff --git a/src/unit/fstring.unit b/src/unit/fstring.unit index 3e4456d..d218a07 100644 --- a/src/unit/fstring.unit +++ b/src/unit/fstring.unit @@ -312,3 +312,24 @@ bob = ""; unitTest( bob.isSet() == false ); } + +{%swap1} +{ + Bu::FString a, b; + a = "Goodbye"; + b = "Hello"; + Bu::swap( a, b ); + unitTest( a == "Hello" ); + unitTest( b == "Goodbye" ); +} + +{%swap2} +{ + Bu::FString a, b; + a = "Goodbye"; + b = "Hello"; + std::swap( a, b ); + unitTest( a == "Hello" ); + unitTest( b == "Goodbye" ); +} + diff --git a/src/unit/list.unit b/src/unit/list.unit index 9da0342..9f66f54 100644 --- a/src/unit/list.unit +++ b/src/unit/list.unit @@ -66,3 +66,83 @@ typedef Bu::List IntList; } } +{%sort1} +{ + IntList lst; + + lst.insertSorted( 5 ); + lst.insertSorted( 1 ); + lst.insertSorted( 10 ); + lst.insertSorted( 3 ); + + unitTest( lst == IntList(1).append(3).append(5).append(10) ); +} + +{%sort2} +{ + IntList lst; + + lst.insertSorted >( 5 ); + lst.insertSorted >( 1 ); + lst.insertSorted >( 10 ); + lst.insertSorted >( 3 ); + + unitTest( lst == IntList(10).append(5).append(3).append(1) ); +} + +{%sort3} +{ + IntList lst; + Bu::__basicGTCmp cmp; + + lst.insertSorted( cmp, 5 ); + lst.insertSorted( cmp, 1 ); + lst.insertSorted( cmp, 10 ); + lst.insertSorted( cmp, 3 ); + + unitTest( lst == IntList(10).append(5).append(3).append(1) ); +} + +{%sort4} +{ + IntList lst; + + lst.append( 5 ); + lst.append( 1 ); + lst.append( 10 ); + lst.append( 3 ); + + lst.sort(); + + unitTest( lst == IntList(1).append(3).append(5).append(10) ); +} + +{%sort5} +{ + IntList lst; + + lst.append( 5 ); + lst.append( 1 ); + lst.append( 10 ); + lst.append( 3 ); + + lst.sort >(); + + unitTest( lst == IntList(10).append(5).append(3).append(1) ); +} + +{%sort6} +{ + IntList lst; + + lst.append( 5 ); + lst.append( 1 ); + lst.append( 10 ); + lst.append( 3 ); + + Bu::__basicGTCmp x; + lst.sort( x ); + + unitTest( lst == IntList(10).append(5).append(3).append(1) ); +} + diff --git a/src/util.cpp b/src/util.cpp index 3ca711d..6107bdb 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -35,7 +35,24 @@ int Bu::getDaysInMonth( int iMonth, int iYear ) return 29; return 28; break; - } + default: + return -1; + } +} +void Bu::memcpy( void *pDest, const void *pSrc, size_t iBytes ) +{ +#ifdef VALTEST + const char *src = (const char *)pSrc; + char *dest = (char *)pDest; + for( int j = 0; j < count; j++ ) + { + *dest = *src; + dest++; + src++; + } +#else + ::memcpy( pDest, pSrc, iBytes ); +#endif } diff --git a/src/util.h b/src/util.h index c284880..6f2f930 100644 --- a/src/util.h +++ b/src/util.h @@ -9,18 +9,20 @@ #define BU_UTIL_H #ifndef NULL -#define NULL 0 +# define NULL 0 #endif /* I borrowed this from someone who borrowed it from glib who borrowed it * from... */ #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) -#define DEPRECATED __attribute__((__deprecated__)) +# define DEPRECATED __attribute__((__deprecated__)) #else -#define DEPRECATED +# define DEPRECATED #endif /* __GNUC__ */ +#include + namespace Bu { /** @@ -173,6 +175,8 @@ namespace Bu * leap years into account. */ int getDaysInMonth( int iMonth, int iYear ); + + void memcpy( void *pDest, const void *pSrc, size_t iBytes ); }; #endif -- cgit v1.2.3