28
28
*
29
29
* A shared pointer is a "smart" pointer that retains ownership of an object using
30
30
* reference counting accross all smart pointers referencing that object.
31
- *
31
+ *
32
32
* @code
33
33
* #include "platform/SharedPtr.h"
34
- *
34
+ *
35
35
* void test() {
36
36
* struct MyStruct { int a; };
37
- *
37
+ *
38
38
* // Create shared pointer
39
39
* SharedPtr<MyStruct> ptr( new MyStruct );
40
- *
40
+ *
41
41
* // Increase reference count
42
42
* SharedPtr<MyStruct> ptr2( ptr );
43
- *
43
+ *
44
44
* ptr = NULL; // Reference to the struct instance is still held by ptr2
45
- *
45
+ *
46
46
* ptr2 = NULL; // The raw pointer is freed
47
47
* }
48
48
* @endcode
49
- *
50
- *
51
- * It is similar to the std::shared_ptr class introduced in C++11,
49
+ *
50
+ *
51
+ * It is similar to the std::shared_ptr class introduced in C++11,
52
52
* however this is not a compatible implementation (no weak pointer, no make_shared, no custom deleters, etc.)
53
53
*
54
54
* Usage: SharedPtr<Class> ptr(new Class())
@@ -68,16 +68,18 @@ class SharedPtr {
68
68
* @brief Create empty SharedPtr not pointing to anything.
69
69
* @details Used for variable declaration.
70
70
*/
71
- SharedPtr (): _ptr(NULL ), _counter(NULL ) {
71
+ SharedPtr (): _ptr(NULL ), _counter(NULL )
72
+ {
72
73
}
73
74
74
75
/* *
75
76
* @brief Create new SharedPtr
76
77
* @param ptr Pointer to take control over
77
78
*/
78
- SharedPtr (T* ptr): _ptr(ptr), _counter(NULL ) {
79
+ SharedPtr (T *ptr): _ptr(ptr), _counter(NULL )
80
+ {
79
81
// allocate counter on the heap so it can be shared
80
- if (_ptr != NULL ) {
82
+ if (_ptr != NULL ) {
81
83
_counter = new uint32_t ;
82
84
*_counter = 1 ;
83
85
}
@@ -87,7 +89,8 @@ class SharedPtr {
87
89
* @brief Destructor.
88
90
* @details Decrement reference counter and delete object if no longer pointed to.
89
91
*/
90
- ~SharedPtr () {
92
+ ~SharedPtr ()
93
+ {
91
94
decrement_counter ();
92
95
}
93
96
@@ -97,7 +100,8 @@ class SharedPtr {
97
100
* copying pointer to original object and pointer to counter.
98
101
* @param source Object being copied from.
99
102
*/
100
- SharedPtr (const SharedPtr& source): _ptr(source._ptr), _counter(source._counter) {
103
+ SharedPtr (const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
104
+ {
101
105
// increment reference counter
102
106
if (_ptr != NULL ) {
103
107
core_util_atomic_incr_u32 (_counter, 1 );
@@ -110,7 +114,8 @@ class SharedPtr {
110
114
* @param source Object being assigned from.
111
115
* @return Object being assigned.
112
116
*/
113
- SharedPtr operator =(const SharedPtr& source) {
117
+ SharedPtr operator =(const SharedPtr &source)
118
+ {
114
119
if (this != &source) {
115
120
// clean up by decrementing counter
116
121
decrement_counter ();
@@ -130,13 +135,14 @@ class SharedPtr {
130
135
131
136
/* *
132
137
* @brief Replaces the managed pointer with a new unmanaged pointer.
133
- * @param[in] ptr the new raw pointer to manage.
138
+ * @param[in] ptr the new raw pointer to manage.
134
139
*/
135
- void reset (T* ptr) {
140
+ void reset (T *ptr)
141
+ {
136
142
// clean up by decrementing counter
137
143
decrement_counter ();
138
144
139
- if (ptr != NULL ) {
145
+ if (ptr != NULL ) {
140
146
// allocate counter on the heap so it can be shared
141
147
_counter = new uint32_t ;
142
148
*_counter = 1 ;
@@ -145,8 +151,9 @@ class SharedPtr {
145
151
146
152
/* *
147
153
* @brief Replace the managed pointer with a NULL pointer.
148
- */
149
- void reset () {
154
+ */
155
+ void reset ()
156
+ {
150
157
reset (NULL );
151
158
}
152
159
@@ -155,15 +162,17 @@ class SharedPtr {
155
162
* @details Get raw pointer to object pointed to.
156
163
* @return Pointer.
157
164
*/
158
- T* get () const {
165
+ T *get () const
166
+ {
159
167
return _ptr;
160
168
}
161
169
162
170
/* *
163
171
* @brief Reference count accessor.
164
172
* @return Reference count.
165
173
*/
166
- uint32_t use_count () const {
174
+ uint32_t use_count () const
175
+ {
167
176
if (_ptr != NULL ) {
168
177
core_util_critical_section_enter ();
169
178
return *_counter;
@@ -177,23 +186,26 @@ class SharedPtr {
177
186
* @brief Dereference object operator.
178
187
* @details Override to return the object pointed to.
179
188
*/
180
- T& operator *() const {
189
+ T &operator *() const
190
+ {
181
191
return *_ptr;
182
192
}
183
193
184
194
/* *
185
195
* @brief Dereference object member operator.
186
196
* @details Override to return return member in object pointed to.
187
197
*/
188
- T* operator ->() const {
198
+ T *operator ->() const
199
+ {
189
200
return _ptr;
190
201
}
191
202
192
203
/* *
193
204
* @brief Boolean conversion operator.
194
205
* @return Whether or not the pointer is NULL.
195
206
*/
196
- operator bool () const {
207
+ operator bool () const
208
+ {
197
209
return (_ptr != NULL );
198
210
}
199
211
@@ -202,15 +214,17 @@ class SharedPtr {
202
214
* @brief Get pointer to reference counter.
203
215
* @return Pointer to reference counter.
204
216
*/
205
- uint32_t * get_counter () const {
217
+ uint32_t *get_counter () const
218
+ {
206
219
return _counter;
207
220
}
208
221
209
222
/* *
210
223
* @brief Decrement reference counter.
211
224
* @details If count reaches zero, free counter and delete object pointed to.
212
225
*/
213
- void decrement_counter () {
226
+ void decrement_counter ()
227
+ {
214
228
if (_ptr != NULL ) {
215
229
uint32_t new_value = core_util_atomic_decr_u32 (_counter, 1 );
216
230
if (new_value == 0 ) {
@@ -224,44 +238,50 @@ class SharedPtr {
224
238
225
239
private:
226
240
// pointer to shared object
227
- T* _ptr;
241
+ T * _ptr;
228
242
229
243
// pointer to shared reference counter
230
- uint32_t * _counter;
244
+ uint32_t * _counter;
231
245
};
232
246
233
247
/* * Non-member relational operators.
234
248
*/
235
249
template <class T , class U >
236
- bool operator == (const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
250
+ bool operator == (const SharedPtr<T> &lhs, const SharedPtr<U> &rhs)
251
+ {
237
252
return (lhs.get () == rhs.get ());
238
253
}
239
254
240
255
template <class T , typename U>
241
- bool operator == (const SharedPtr<T>& lhs, U rhs) {
242
- return (lhs.get () == (T*) rhs);
256
+ bool operator == (const SharedPtr<T> &lhs, U rhs)
257
+ {
258
+ return (lhs.get () == (T *) rhs);
243
259
}
244
260
245
261
template <class T , typename U>
246
- bool operator == (U lhs, const SharedPtr<T>& rhs) {
247
- return ((T*) lhs == rhs.get ());
262
+ bool operator == (U lhs, const SharedPtr<T> &rhs)
263
+ {
264
+ return ((T *) lhs == rhs.get ());
248
265
}
249
266
250
267
/* * Non-member relational operators.
251
268
*/
252
269
template <class T , class U >
253
- bool operator != (const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
270
+ bool operator != (const SharedPtr<T> &lhs, const SharedPtr<U> &rhs)
271
+ {
254
272
return (lhs.get () != rhs.get ());
255
273
}
256
274
257
275
template <class T , typename U>
258
- bool operator != (const SharedPtr<T>& lhs, U rhs) {
259
- return (lhs.get () != (T*) rhs);
276
+ bool operator != (const SharedPtr<T> &lhs, U rhs)
277
+ {
278
+ return (lhs.get () != (T *) rhs);
260
279
}
261
280
262
281
template <class T , typename U>
263
- bool operator != (U lhs, const SharedPtr<T>& rhs) {
264
- return ((T*) lhs != rhs.get ());
282
+ bool operator != (U lhs, const SharedPtr<T> &rhs)
283
+ {
284
+ return ((T *) lhs != rhs.get ());
265
285
}
266
286
267
287
#endif // __SHAREDPTR_H__
0 commit comments