aboutsummaryrefslogtreecommitdiff
path: root/src/atom.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/atom.h')
-rw-r--r--src/atom.h93
1 files changed, 93 insertions, 0 deletions
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