Skip to content

Commit d9c1748

Browse files
author
Donatien Garnier
committed
Update formatting for SharedPtr.h
1 parent 6f3c07f commit d9c1748

File tree

1 file changed

+59
-39
lines changed

1 file changed

+59
-39
lines changed

platform/SharedPtr.h

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@
2828
*
2929
* A shared pointer is a "smart" pointer that retains ownership of an object using
3030
* reference counting accross all smart pointers referencing that object.
31-
*
31+
*
3232
* @code
3333
* #include "platform/SharedPtr.h"
34-
*
34+
*
3535
* void test() {
3636
* struct MyStruct { int a; };
37-
*
37+
*
3838
* // Create shared pointer
3939
* SharedPtr<MyStruct> ptr( new MyStruct );
40-
*
40+
*
4141
* // Increase reference count
4242
* SharedPtr<MyStruct> ptr2( ptr );
43-
*
43+
*
4444
* ptr = NULL; // Reference to the struct instance is still held by ptr2
45-
*
45+
*
4646
* ptr2 = NULL; // The raw pointer is freed
4747
* }
4848
* @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,
5252
* however this is not a compatible implementation (no weak pointer, no make_shared, no custom deleters, etc.)
5353
*
5454
* Usage: SharedPtr<Class> ptr(new Class())
@@ -68,16 +68,18 @@ class SharedPtr {
6868
* @brief Create empty SharedPtr not pointing to anything.
6969
* @details Used for variable declaration.
7070
*/
71-
SharedPtr(): _ptr(NULL), _counter(NULL) {
71+
SharedPtr(): _ptr(NULL), _counter(NULL)
72+
{
7273
}
7374

7475
/**
7576
* @brief Create new SharedPtr
7677
* @param ptr Pointer to take control over
7778
*/
78-
SharedPtr(T* ptr): _ptr(ptr), _counter(NULL) {
79+
SharedPtr(T *ptr): _ptr(ptr), _counter(NULL)
80+
{
7981
// allocate counter on the heap so it can be shared
80-
if(_ptr != NULL) {
82+
if (_ptr != NULL) {
8183
_counter = new uint32_t;
8284
*_counter = 1;
8385
}
@@ -87,7 +89,8 @@ class SharedPtr {
8789
* @brief Destructor.
8890
* @details Decrement reference counter and delete object if no longer pointed to.
8991
*/
90-
~SharedPtr() {
92+
~SharedPtr()
93+
{
9194
decrement_counter();
9295
}
9396

@@ -97,7 +100,8 @@ class SharedPtr {
97100
* copying pointer to original object and pointer to counter.
98101
* @param source Object being copied from.
99102
*/
100-
SharedPtr(const SharedPtr& source): _ptr(source._ptr), _counter(source._counter) {
103+
SharedPtr(const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
104+
{
101105
// increment reference counter
102106
if (_ptr != NULL) {
103107
core_util_atomic_incr_u32(_counter, 1);
@@ -110,7 +114,8 @@ class SharedPtr {
110114
* @param source Object being assigned from.
111115
* @return Object being assigned.
112116
*/
113-
SharedPtr operator=(const SharedPtr& source) {
117+
SharedPtr operator=(const SharedPtr &source)
118+
{
114119
if (this != &source) {
115120
// clean up by decrementing counter
116121
decrement_counter();
@@ -130,13 +135,14 @@ class SharedPtr {
130135

131136
/**
132137
* @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.
134139
*/
135-
void reset(T* ptr) {
140+
void reset(T *ptr)
141+
{
136142
// clean up by decrementing counter
137143
decrement_counter();
138144

139-
if(ptr != NULL) {
145+
if (ptr != NULL) {
140146
// allocate counter on the heap so it can be shared
141147
_counter = new uint32_t;
142148
*_counter = 1;
@@ -145,8 +151,9 @@ class SharedPtr {
145151

146152
/**
147153
* @brief Replace the managed pointer with a NULL pointer.
148-
*/
149-
void reset() {
154+
*/
155+
void reset()
156+
{
150157
reset(NULL);
151158
}
152159

@@ -155,15 +162,17 @@ class SharedPtr {
155162
* @details Get raw pointer to object pointed to.
156163
* @return Pointer.
157164
*/
158-
T* get() const {
165+
T *get() const
166+
{
159167
return _ptr;
160168
}
161169

162170
/**
163171
* @brief Reference count accessor.
164172
* @return Reference count.
165173
*/
166-
uint32_t use_count() const {
174+
uint32_t use_count() const
175+
{
167176
if (_ptr != NULL) {
168177
core_util_critical_section_enter();
169178
return *_counter;
@@ -177,23 +186,26 @@ class SharedPtr {
177186
* @brief Dereference object operator.
178187
* @details Override to return the object pointed to.
179188
*/
180-
T& operator*() const {
189+
T &operator*() const
190+
{
181191
return *_ptr;
182192
}
183193

184194
/**
185195
* @brief Dereference object member operator.
186196
* @details Override to return return member in object pointed to.
187197
*/
188-
T* operator->() const {
198+
T *operator->() const
199+
{
189200
return _ptr;
190201
}
191202

192203
/**
193204
* @brief Boolean conversion operator.
194205
* @return Whether or not the pointer is NULL.
195206
*/
196-
operator bool() const {
207+
operator bool() const
208+
{
197209
return (_ptr != NULL);
198210
}
199211

@@ -202,15 +214,17 @@ class SharedPtr {
202214
* @brief Get pointer to reference counter.
203215
* @return Pointer to reference counter.
204216
*/
205-
uint32_t* get_counter() const {
217+
uint32_t *get_counter() const
218+
{
206219
return _counter;
207220
}
208221

209222
/**
210223
* @brief Decrement reference counter.
211224
* @details If count reaches zero, free counter and delete object pointed to.
212225
*/
213-
void decrement_counter() {
226+
void decrement_counter()
227+
{
214228
if (_ptr != NULL) {
215229
uint32_t new_value = core_util_atomic_decr_u32(_counter, 1);
216230
if (new_value == 0) {
@@ -224,44 +238,50 @@ class SharedPtr {
224238

225239
private:
226240
// pointer to shared object
227-
T* _ptr;
241+
T *_ptr;
228242

229243
// pointer to shared reference counter
230-
uint32_t* _counter;
244+
uint32_t *_counter;
231245
};
232246

233247
/** Non-member relational operators.
234248
*/
235249
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+
{
237252
return (lhs.get() == rhs.get());
238253
}
239254

240255
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);
243259
}
244260

245261
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());
248265
}
249266

250267
/** Non-member relational operators.
251268
*/
252269
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+
{
254272
return (lhs.get() != rhs.get());
255273
}
256274

257275
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);
260279
}
261280

262281
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());
265285
}
266286

267287
#endif // __SHAREDPTR_H__

0 commit comments

Comments
 (0)