aboutsummaryrefslogtreecommitdiff
path: root/src/sptr.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/sptr.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/sptr.h')
-rw-r--r--src/sptr.h229
1 files changed, 0 insertions, 229 deletions
diff --git a/src/sptr.h b/src/sptr.h
deleted file mode 100644
index 4eb5a52..0000000
--- a/src/sptr.h
+++ /dev/null
@@ -1,229 +0,0 @@
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_SPTR_H
9#define BU_SPTR_H
10
11#include <stdint.h>
12#include <stdio.h>
13
14namespace Bu
15{
16 template<typename T> class SPtr;
17 template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src );
18
19 /**
20 *@ingroup Containers
21 */
22 template<typename T>
23 class SPtr
24 {
25 template<typename Tb, typename Ta>
26 friend SPtr<Tb> SPtrCast( SPtr<Ta> pt );
27 public:
28 SPtr() :
29 pRefCnt( NULL ),
30 pData( NULL )
31 {
32 }
33
34 ~SPtr()
35 {
36 decCount();
37 }
38
39 SPtr( const SPtr<T> &src ) :
40 pRefCnt( src.pRefCnt ),
41 pData( src.pData )
42 {
43 if( pRefCnt )
44 (*pRefCnt) += 1;
45 }
46
47 SPtr( T *pSrc ) :
48 pRefCnt( NULL ),
49 pData( pSrc )
50 {
51 if( pData )
52 {
53 pRefCnt = new int32_t;
54 (*pRefCnt) = 1;
55 }
56 }
57
58 /**
59 * Get the number of references to this pointer.
60 *@returns (int32_t) The number of references to this pointer.
61 */
62 int32_t getRefCount() const
63 {
64 return *pRefCnt;
65 }
66
67 void clear()
68 {
69 decCount();
70 pRefCnt = NULL;
71 pData = NULL;
72 }
73
74 /**
75 * Pointer access operator.
76 *@returns (const T *)
77 */
78 const T *operator->() const
79 {
80 return pData;
81 }
82
83 /**
84 * Dereference operator.
85 *@returns (const T &) The value at the end of the pointer.
86 */
87 const T &operator*() const
88 {
89 return *pData;
90 }
91
92 /**
93 * Pointer access operator.
94 *@returns (T *)
95 */
96 T *operator->()
97 {
98 return pData;
99 }
100
101 /**
102 * Dereference operator.
103 *@returns (T &) The value at the end of the pointer.
104 */
105 T &operator*()
106 {
107 return *pData;
108 }
109
110 /**
111 * Assignment operator.
112 *@param src (const SPtr<T> &)
113 */
114 SPtr<T> operator=( const SPtr<T> &src )
115 {
116 decCount();
117 pRefCnt = src.pRefCnt;
118 pData = src.pData;
119 if( pRefCnt )
120 (*pRefCnt) += 1;
121
122 return *this;
123 }
124
125 /**
126 * Assignment operator.
127 *@param src (const SPtr<T> &)
128 */
129 const SPtr<T> operator=( const SPtr<T> &src ) const
130 {
131 decCount();
132 pRefCnt = src.pRefCnt;
133 pData = src.pData;
134 if( pRefCnt )
135 (*pRefCnt) += 1;
136
137 return *this;
138 }
139
140 /**
141 * Equals comparison operator.
142 *@param src (const SPtr<T> &) The SPtr to compare to.
143 *@returns (bool) Are the equal?
144 */
145 bool operator==( const SPtr<T> &src ) const
146 {
147 return pData == src.pData;
148 }
149
150 /**
151 * Equals comparison operator.
152 *@param src (const T *) The pointer to compare to.
153 *@returns (bool) Are the equal?
154 */
155 bool operator==( const T *src ) const
156 {
157 return pData == src;
158 }
159
160 /**
161 * Not equals comparison operator.
162 *@param src (const SPtr<T> &) The SPtr to compare to.
163 *@returns (bool) Are the equal?
164 */
165 bool operator!=( const SPtr<T> &src ) const
166 {
167 return !(pData == src.pData);
168 }
169
170 /**
171 * Not equals comparison operator.
172 *@param src (const T *) The pointer to compare to.
173 *@returns (bool) Are the equal?
174 */
175 bool operator!=( const T *src ) const
176 {
177 return !(pData == src);
178 }
179
180 /**
181 * Boolean cast operator. Do we have a pointer?
182 */
183 operator bool() const
184 {
185 return pRefCnt != NULL;
186 }
187
188 /**
189 * Do we have a pointer?
190 *@returns (bool) Do we have a pointer?
191 */
192 bool isSet() const
193 {
194 return pRefCnt != NULL;
195 }
196
197 private:
198 void decCount() const
199 {
200 if( pRefCnt )
201 {
202 (*pRefCnt) -= 1;
203 //printf("Decrementing ref-count to %d\n", *pRefCnt );
204 if( (*pRefCnt) == 0 )
205 {
206 delete pRefCnt;
207 delete pData;
208 pRefCnt = NULL;
209 pData = NULL;
210 }
211 }
212 }
213
214 mutable int32_t *pRefCnt;
215 mutable T *pData;
216 };
217
218 template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src )
219 {
220 SPtr<Tb> ret;
221 ret.pRefCnt = src.pRefCnt;
222 ret.pData = dynamic_cast<Tb *>(src.pData);
223 if( ret.pRefCnt )
224 (*(ret.pRefCnt)) += 1;
225 return ret;
226 }
227}
228
229#endif