From f58a0b3a1f657124076b96ba092e1f69e88af263 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 18 Jun 2007 20:25:50 +0000 Subject: Added the atom class and did some more client work, it will close the socket now when the end has been reached. --- src/atom.cpp | 1 + src/atom.h | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/client.cpp | 5 +++ src/tests/atom.cpp | 17 ++++++++++ 4 files changed, 116 insertions(+) create mode 100644 src/atom.cpp create mode 100644 src/atom.h create mode 100644 src/tests/atom.cpp (limited to 'src') diff --git a/src/atom.cpp b/src/atom.cpp new file mode 100644 index 0000000..f966bfc --- /dev/null +++ b/src/atom.cpp @@ -0,0 +1 @@ +#include "bu/atom.h" diff --git a/src/atom.h b/src/atom.h new file mode 100644 index 0000000..731e08b --- /dev/null +++ b/src/atom.h @@ -0,0 +1,93 @@ +#ifndef ATOM_H +#define ATOM_H + +#include +#include +#include "bu/exceptions.h" + +namespace Bu +{ + /** + * + */ + template > + class Atom + { + private: + typedef struct Atom MyType; + + public: + Atom() : + pData( NULL ) + { + } + + virtual ~Atom() + { + clear(); + } + + bool isSet() const + { + return (pData != NULL); + } + + void set( const t &val ) + { + clear(); + pData = ta.allocate( 1 ); + ta.construct( pData, val ); + } + + t &get() + { + if( !pData ) + throw Bu::ExceptionBase("Not set"); + return *pData; + } + + const t &get() const + { + if( !pData ) + throw Bu::ExceptionBase("Not set"); + return *pData; + } + + void clear() + { + if( pData ) + { + ta.destroy( pData ); + ta.deallocate( pData, 1 ); + pData = NULL; + } + } + + operator const t &() const + { + if( !pData ) + throw Bu::ExceptionBase("Not set"); + return *pData; + } + + operator t &() + { + if( !pData ) + throw Bu::ExceptionBase("Not set"); + return *pData; + } + + MyType &operator =( const t &oth ) + { + set( oth ); + + return *this; + } + + private: + t *pData; + talloc ta; + }; +} + +#endif diff --git a/src/client.cpp b/src/client.cpp index cf96424..6d7d81c 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -47,6 +47,11 @@ void Bu::Client::processInput() } } + if( nTotal == 0 ) + { + pSocket->close(); + } + if( pProto && nTotal ) { pProto->onNewData( this ); diff --git a/src/tests/atom.cpp b/src/tests/atom.cpp new file mode 100644 index 0000000..cf076b1 --- /dev/null +++ b/src/tests/atom.cpp @@ -0,0 +1,17 @@ +#include "bu/atom.h" +#include +#include + +int main() +{ + Bu::Atom aInt; + Bu::Atom aStr; + + aStr.set("Hey there, dude"); + aInt.set( 55 ); + int me = aInt; + aInt = 12; + printf("%d, %d\n", aInt.get(), me ); + printf("%s\n", aStr.get() ); +} + -- cgit v1.2.3