aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/atom.cpp1
-rw-r--r--src/atom.h93
-rw-r--r--src/client.cpp5
-rw-r--r--src/tests/atom.cpp17
4 files changed, 116 insertions, 0 deletions
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 @@
1#ifndef ATOM_H
2#define ATOM_H
3
4#include <stdint.h>
5#include <memory>
6#include "bu/exceptions.h"
7
8namespace Bu
9{
10 /**
11 *
12 */
13 template <typename t, typename talloc=std::allocator<t> >
14 class Atom
15 {
16 private:
17 typedef struct Atom<t, talloc> MyType;
18
19 public:
20 Atom() :
21 pData( NULL )
22 {
23 }
24
25 virtual ~Atom()
26 {
27 clear();
28 }
29
30 bool isSet() const
31 {
32 return (pData != NULL);
33 }
34
35 void set( const t &val )
36 {
37 clear();
38 pData = ta.allocate( 1 );
39 ta.construct( pData, val );
40 }
41
42 t &get()
43 {
44 if( !pData )
45 throw Bu::ExceptionBase("Not set");
46 return *pData;
47 }
48
49 const t &get() const
50 {
51 if( !pData )
52 throw Bu::ExceptionBase("Not set");
53 return *pData;
54 }
55
56 void clear()
57 {
58 if( pData )
59 {
60 ta.destroy( pData );
61 ta.deallocate( pData, 1 );
62 pData = NULL;
63 }
64 }
65
66 operator const t &() const
67 {
68 if( !pData )
69 throw Bu::ExceptionBase("Not set");
70 return *pData;
71 }
72
73 operator t &()
74 {
75 if( !pData )
76 throw Bu::ExceptionBase("Not set");
77 return *pData;
78 }
79
80 MyType &operator =( const t &oth )
81 {
82 set( oth );
83
84 return *this;
85 }
86
87 private:
88 t *pData;
89 talloc ta;
90 };
91}
92
93#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()
47 } 47 }
48 } 48 }
49 49
50 if( nTotal == 0 )
51 {
52 pSocket->close();
53 }
54
50 if( pProto && nTotal ) 55 if( pProto && nTotal )
51 { 56 {
52 pProto->onNewData( this ); 57 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 @@
1#include "bu/atom.h"
2#include <stdio.h>
3#include <stdlib.h>
4
5int main()
6{
7 Bu::Atom<int> aInt;
8 Bu::Atom<char *> aStr;
9
10 aStr.set("Hey there, dude");
11 aInt.set( 55 );
12 int me = aInt;
13 aInt = 12;
14 printf("%d, %d\n", aInt.get(), me );
15 printf("%s\n", aStr.get() );
16}
17