summaryrefslogtreecommitdiff
path: root/src/atom.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/atom.h')
-rw-r--r--src/atom.h107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/atom.h b/src/atom.h
new file mode 100644
index 0000000..fad47eb
--- /dev/null
+++ b/src/atom.h
@@ -0,0 +1,107 @@
1#ifndef BU_ATOM_H
2#define BU_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 has() 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 t *operator ->()
88 {
89 if( !pData )
90 throw Bu::ExceptionBase("Not set");
91 return pData;
92 }
93
94 t &operator *()
95 {
96 if( !pData )
97 throw Bu::ExceptionBase("Not set");
98 return *pData;
99 }
100
101 private:
102 t *pData;
103 talloc ta;
104 };
105}
106
107#endif