aboutsummaryrefslogtreecommitdiff
path: root/src/stable/atom.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/stable/atom.h
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/stable/atom.h')
-rw-r--r--src/stable/atom.h147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/stable/atom.h b/src/stable/atom.h
new file mode 100644
index 0000000..fd88f2d
--- /dev/null
+++ b/src/stable/atom.h
@@ -0,0 +1,147 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_ATOM_H
9#define BU_ATOM_H
10
11#include <stdint.h>
12#include <memory>
13#include "bu/config.h"
14#include "bu/exceptionbase.h"
15
16namespace Bu
17{
18 /**
19 *
20 *@ingroup Containers
21 */
22 template <typename t, typename talloc=std::allocator<t> >
23 class Atom
24 {
25 private:
26 typedef struct Atom<t, talloc> MyType;
27
28 public:
29 Atom() :
30 pData( NULL )
31 {
32 }
33
34 Atom( const MyType &oth ) :
35 pData( NULL )
36 {
37 if( oth.pData )
38 set( *oth.pData );
39 }
40
41 Atom( const t &oth ) :
42 pData( NULL )
43 {
44 set( oth );
45 }
46
47 virtual ~Atom()
48 {
49 clear();
50 }
51
52 bool has() const
53 {
54 return (pData != NULL);
55 }
56
57 void set( const t &val )
58 {
59 clear();
60 pData = ta.allocate( 1 );
61 ta.construct( pData, val );
62 }
63
64 t &get()
65 {
66 if( !pData )
67 throw Bu::ExceptionBase("Not set");
68 return *pData;
69 }
70
71 const t &get() const
72 {
73 if( !pData )
74 throw Bu::ExceptionBase("Not set");
75 return *pData;
76 }
77
78 void clear()
79 {
80 if( pData )
81 {
82 ta.destroy( pData );
83 ta.deallocate( pData, 1 );
84 pData = NULL;
85 }
86 }
87
88 operator const t &() const
89 {
90 if( !pData )
91 throw Bu::ExceptionBase("Not set");
92 return *pData;
93 }
94
95 operator t &()
96 {
97 if( !pData )
98 throw Bu::ExceptionBase("Not set");
99 return *pData;
100 }
101
102 MyType &operator =( const t &oth )
103 {
104 set( oth );
105
106 return *this;
107 }
108
109 MyType &operator =( const MyType &oth )
110 {
111 if( oth.pData )
112 set( *oth.pData );
113
114 return *this;
115 }
116
117 bool operator ==( const MyType &oth )
118 {
119 return (*pData) == (*oth.pData);
120 }
121
122 bool operator ==( const t &oth )
123 {
124 return (*pData) == oth;
125 }
126
127 t *operator ->()
128 {
129 if( !pData )
130 throw Bu::ExceptionBase("Not set");
131 return pData;
132 }
133
134 t &operator *()
135 {
136 if( !pData )
137 throw Bu::ExceptionBase("Not set");
138 return *pData;
139 }
140
141 private:
142 t *pData;
143 talloc ta;
144 };
145}
146
147#endif