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