From 3d5c548d630f8b6c86a250e1b7358824557ef01f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 13 Aug 2009 16:14:53 +0000 Subject: Ok, shared core looks good, and I added a unit test for Bu::List to check a few basics. It works, so now I'm going to apply SharedCore to Bu::List and see how bad it is. Also, I got rid of all the warnings and things that showed up during compilation, they were all silly anyway. Finally, mkunit.sh is much cooler. Hard to believe it's a shell script, it now also adds proper #line directives to the cpp output so if there is an error or warning g++ will give you the right line number in your .unit file, not the resultant cpp file. --- src/buffer.cpp | 1 + src/fastcgi.cpp | 14 +++---- src/fstring.cpp | 6 +-- src/httpget.cpp | 5 ++- src/sharedcore.cpp | 9 +++++ src/sharedcore.h | 101 +++++++++++++++++++++++++++++++++++++++++++++++ src/tests/sharedcore.cpp | 82 ++++++++++++++++++++++++++++++++++++++ src/unit/list.unit | 68 +++++++++++++++++++++++++++++++ src/util.h | 4 ++ 9 files changed, 275 insertions(+), 15 deletions(-) create mode 100644 src/sharedcore.cpp create mode 100644 src/sharedcore.h create mode 100644 src/tests/sharedcore.cpp create mode 100644 src/unit/list.unit (limited to 'src') diff --git a/src/buffer.cpp b/src/buffer.cpp index d4eb8a6..c5a972a 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -34,6 +34,7 @@ void Bu::Buffer::start() size_t Bu::Buffer::stop() { + return 0; } void Bu::Buffer::fillReadBuf() diff --git a/src/fastcgi.cpp b/src/fastcgi.cpp index 9aecbfb..3cc3a10 100644 --- a/src/fastcgi.cpp +++ b/src/fastcgi.cpp @@ -278,7 +278,9 @@ void Bu::FastCgi::run() // sio << "Scary."; // ??? we shouldn't get these. break; - + + case typeGetValues: + break; } // sio << sio.nl; @@ -313,10 +315,7 @@ void Bu::FastCgi::run() iSize = 65528; rOut.uContentLength = iSize; write( s, rOut ); - int iAmnt = s.write( - sStdOut.getStr()+iPos, iSize ); -// sio << "Wrote " << iAmnt << -// " of " << iSize << sio.nl; + s.write( sStdOut.getStr()+iPos, iSize ); } } rOut.uContentLength = 0; @@ -333,10 +332,7 @@ void Bu::FastCgi::run() iSize = 65528; rOut.uContentLength = iSize; write( s, rOut ); - int iAmnt = s.write( - sStdErr.getStr()+iPos, iSize ); -// sio << "Wrote " << iAmnt << -// " of " << iSize << sio.nl; + s.write( sStdErr.getStr()+iPos, iSize ); } } rOut.uContentLength = 0; diff --git a/src/fstring.cpp b/src/fstring.cpp index ce5492b..f77e718 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -110,15 +110,13 @@ int64_t &Bu::operator<<( int64_t &dst, const Bu::FString &sIn ) float &Bu::operator<<( float &dst, const Bu::FString &sIn ) { - double tmp; - sscanf( sIn.getStr(), "%f", &tmp ); - dst = tmp; + sscanf( sIn.getStr(), "%f", &dst ); return dst; } double &Bu::operator<<( double &dst, const Bu::FString &sIn ) { - sscanf( sIn.getStr(), "%f", &dst ); + sscanf( sIn.getStr(), "%lf", &dst ); return dst; } diff --git a/src/httpget.cpp b/src/httpget.cpp index 0356874..369f27b 100644 --- a/src/httpget.cpp +++ b/src/httpget.cpp @@ -34,11 +34,12 @@ void Bu::HttpGet::get() // sSrv.read( } -size_t Bu::HttpGet::read( void *pBuf, size_t nBytes ) +size_t Bu::HttpGet::read( void * /*pBuf*/, size_t /*nBytes*/ ) { + return 0; } -size_t Bu::HttpGet::write( const void *pBuf, size_t nBytes ) +size_t Bu::HttpGet::write( const void * /*pBuf*/, size_t /*nBytes*/ ) { return 0; } diff --git a/src/sharedcore.cpp b/src/sharedcore.cpp new file mode 100644 index 0000000..6333335 --- /dev/null +++ b/src/sharedcore.cpp @@ -0,0 +1,9 @@ +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/sharedcore.h" + diff --git a/src/sharedcore.h b/src/sharedcore.h new file mode 100644 index 0000000..9f42345 --- /dev/null +++ b/src/sharedcore.h @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_SHARED_CORE_H +#define BU_SHARED_CORE_H + +#include "bu/util.h" +#include "bu/sio.h" + +namespace Bu +{ + template + class SharedCore + { + typedef class SharedCore _SharedType; + public: + SharedCore() : + data( new Core ), + iRefCount( new int(1) ) + { + } + + SharedCore( const _SharedType &rSrc ) : + data( NULL ), + iRefCount( NULL ) + { + _softCopy( rSrc ); + } + + virtual ~SharedCore() + { + _deref(); + } + + SharedCore &operator=( const SharedCore &rhs ) + { + _softCopy( rhs ); + return *this; + } + + int getRefCount() const + { + return *iRefCount; + } + + protected: + Core *data; + void _hardCopy() + { + if( !data || !iRefCount ) + return; + sio << "_hardCopy()" << sio.nl; + Core *copy = new Core( *data ); + _deref(); + data = copy; + iRefCount = new int( 1 ); + } + + private: + void _deref() + { + sio << "_deref()" << sio.nl; + if( (--(*iRefCount)) == 0 ) + { + sio << " --> iRefCount == 0, cleaning up." << sio.nl; + delete data; + delete iRefCount; + } + else + sio << " --> iRefCount == " << *iRefCount << sio.nl; + data = NULL; + iRefCount = NULL; + } + + void _incRefCount() + { + sio << "_incRefCount()" << sio.nl; + if( iRefCount && data ) + ++(*iRefCount); + sio << " --> iRefCount == " << *iRefCount << sio.nl; + } + + void _softCopy( const _SharedType &rSrc ) + { + sio << "_softCopy()" << sio.nl; + if( data ) + _deref(); + data = rSrc.data; + iRefCount = rSrc.iRefCount; + _incRefCount(); + } + + int *iRefCount; + }; +}; + +#endif diff --git a/src/tests/sharedcore.cpp b/src/tests/sharedcore.cpp new file mode 100644 index 0000000..bdfde4c --- /dev/null +++ b/src/tests/sharedcore.cpp @@ -0,0 +1,82 @@ +#include "bu/sharedcore.h" +#include "bu/sio.h" + +using namespace Bu; + +struct ShintCore +{ + int val; +}; +class Shint : public Bu::SharedCore +{ +public: + Shint() + { + data->val = 0; + } + + Shint( int val ) + { + data->val = val; + } + + int getVal() + { + return data->val; + } + + void setValBad( int val ) + { + data->val = val; + } + + void setVal( int val ) + { + _hardCopy(); + data->val = val; + } + + bool operator==( const Shint &rhs ) + { + if( data == rhs.data ) + { + sio << "Same pointer (" << Fmt::ptr() << data << ")" << sio.nl; + return true; + } + if( data->val == rhs.data->val ) + { + sio << "Same value " << data->val << " (" + << Fmt::ptr() << data << " vs " + << Fmt::ptr() << rhs.data << ")" + << sio.nl; + return true; + } + sio << "Different" << sio.nl; + return false; + } +}; + +#define line( x ) sio << __FILE__ ": " << __LINE__ << ": " << #x << sio.nl; x + +int main() +{ + line( Shint a; ) + line( Shint b( 5 ); ) + + line( a == b; ) + + line( b = a; ) + line( a == b; ) + + line( b.setValBad( 12 ); ) + sio << a.getVal() << " != " << b.getVal() << sio.nl; + line( a == b; ) + + line( a.setVal( 3 ); ) + sio << a.getVal() << " != " << b.getVal() << sio.nl; + line( a == b; ) + + line( a.setVal( b.getVal() ); ) + line( a == b; ) +} + diff --git a/src/unit/list.unit b/src/unit/list.unit new file mode 100644 index 0000000..9da0342 --- /dev/null +++ b/src/unit/list.unit @@ -0,0 +1,68 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/fstring.h" +#include "bu/list.h" + +typedef Bu::List IntList; + +{=Init} + +{%append} +{ + IntList lst; + for( int j = 0; j < 50; j++ ) + { + lst.append( j ); + } + int j = 0; + for( IntList::iterator i = lst.begin(); i; i++, j++ ) + { + unitTest( *i == j ); + } +} + +{%prepend} +{ + IntList lst; + for( int j = 0; j < 50; j++ ) + { + lst.prepend( j ); + } + int j = 49; + for( IntList::iterator i = lst.begin(); i; i++, j-- ) + { + unitTest( *i == j ); + } +} + +{%copy} +{ + IntList lst; + int j; + for( j = 0; j < 50; j++ ) + { + lst.append( j ); + } + IntList lst2 = lst; + + j = 0; + for( IntList::iterator i = lst2.begin(); i; i++, j++ ) + { + unitTest( *i == j ); + } + lst2.clear(); + lst2 = lst; + + j = 0; + for( IntList::iterator i = lst2.begin(); i; i++, j++ ) + { + unitTest( *i == j ); + } +} + diff --git a/src/util.h b/src/util.h index ea107ee..0c2f0eb 100644 --- a/src/util.h +++ b/src/util.h @@ -8,6 +8,10 @@ #ifndef BU_UTIL_H #define BU_UTIL_H +#ifndef NULL +#define NULL 0 +#endif + /* I borrowed this from someone who borrowed it from glib who borrowed it * from... */ -- cgit v1.2.3