diff --git a/platform/SharedPtr.h b/platform/SharedPtr.h index e35d41c18a..d26194d089 100644 --- a/platform/SharedPtr.h +++ b/platform/SharedPtr.h @@ -28,27 +28,27 @@ * * A shared pointer is a "smart" pointer that retains ownership of an object using * reference counting accross all smart pointers referencing that object. - * + * * @code * #include "platform/SharedPtr.h" - * + * * void test() { * struct MyStruct { int a; }; - * + * * // Create shared pointer * SharedPtr ptr( new MyStruct ); - * + * * // Increase reference count * SharedPtr ptr2( ptr ); - * + * * ptr = NULL; // Reference to the struct instance is still held by ptr2 - * + * * ptr2 = NULL; // The raw pointer is freed * } * @endcode - * - * - * It is similar to the std::shared_ptr class introduced in C++11, + * + * + * It is similar to the std::shared_ptr class introduced in C++11, * however this is not a compatible implementation (no weak pointer, no make_shared, no custom deleters, etc.) * * Usage: SharedPtr ptr(new Class()) @@ -68,16 +68,18 @@ public: * @brief Create empty SharedPtr not pointing to anything. * @details Used for variable declaration. */ - SharedPtr(): _ptr(NULL), _counter(NULL) { + SharedPtr(): _ptr(NULL), _counter(NULL) + { } /** * @brief Create new SharedPtr * @param ptr Pointer to take control over */ - SharedPtr(T* ptr): _ptr(ptr), _counter(NULL) { + SharedPtr(T *ptr): _ptr(ptr), _counter(NULL) + { // allocate counter on the heap so it can be shared - if(_ptr != NULL) { + if (_ptr != NULL) { _counter = new uint32_t; *_counter = 1; } @@ -87,7 +89,8 @@ public: * @brief Destructor. * @details Decrement reference counter and delete object if no longer pointed to. */ - ~SharedPtr() { + ~SharedPtr() + { decrement_counter(); } @@ -97,7 +100,8 @@ public: * copying pointer to original object and pointer to counter. * @param source Object being copied from. */ - SharedPtr(const SharedPtr& source): _ptr(source._ptr), _counter(source._counter) { + SharedPtr(const SharedPtr &source): _ptr(source._ptr), _counter(source._counter) + { // increment reference counter if (_ptr != NULL) { core_util_atomic_incr_u32(_counter, 1); @@ -110,7 +114,8 @@ public: * @param source Object being assigned from. * @return Object being assigned. */ - SharedPtr operator=(const SharedPtr& source) { + SharedPtr operator=(const SharedPtr &source) + { if (this != &source) { // clean up by decrementing counter decrement_counter(); @@ -130,13 +135,14 @@ public: /** * @brief Replaces the managed pointer with a new unmanaged pointer. - * @param[in] ptr the new raw pointer to manage. + * @param[in] ptr the new raw pointer to manage. */ - void reset(T* ptr) { + void reset(T *ptr) + { // clean up by decrementing counter decrement_counter(); - if(ptr != NULL) { + if (ptr != NULL) { // allocate counter on the heap so it can be shared _counter = new uint32_t; *_counter = 1; @@ -145,8 +151,9 @@ public: /** * @brief Replace the managed pointer with a NULL pointer. - */ - void reset() { + */ + void reset() + { reset(NULL); } @@ -155,7 +162,8 @@ public: * @details Get raw pointer to object pointed to. * @return Pointer. */ - T* get() const { + T *get() const + { return _ptr; } @@ -163,7 +171,8 @@ public: * @brief Reference count accessor. * @return Reference count. */ - uint32_t use_count() const { + uint32_t use_count() const + { if (_ptr != NULL) { core_util_critical_section_enter(); return *_counter; @@ -177,7 +186,8 @@ public: * @brief Dereference object operator. * @details Override to return the object pointed to. */ - T& operator*() const { + T &operator*() const + { return *_ptr; } @@ -185,7 +195,8 @@ public: * @brief Dereference object member operator. * @details Override to return return member in object pointed to. */ - T* operator->() const { + T *operator->() const + { return _ptr; } @@ -193,7 +204,8 @@ public: * @brief Boolean conversion operator. * @return Whether or not the pointer is NULL. */ - operator bool() const { + operator bool() const + { return (_ptr != NULL); } @@ -202,7 +214,8 @@ private: * @brief Get pointer to reference counter. * @return Pointer to reference counter. */ - uint32_t* get_counter() const { + uint32_t *get_counter() const + { return _counter; } @@ -210,7 +223,8 @@ private: * @brief Decrement reference counter. * @details If count reaches zero, free counter and delete object pointed to. */ - void decrement_counter() { + void decrement_counter() + { if (_ptr != NULL) { uint32_t new_value = core_util_atomic_decr_u32(_counter, 1); if (new_value == 0) { @@ -224,44 +238,50 @@ private: private: // pointer to shared object - T* _ptr; + T *_ptr; // pointer to shared reference counter - uint32_t* _counter; + uint32_t *_counter; }; /** Non-member relational operators. */ template -bool operator== (const SharedPtr& lhs, const SharedPtr& rhs) { +bool operator== (const SharedPtr &lhs, const SharedPtr &rhs) +{ return (lhs.get() == rhs.get()); } template -bool operator== (const SharedPtr& lhs, U rhs) { - return (lhs.get() == (T*) rhs); +bool operator== (const SharedPtr &lhs, U rhs) +{ + return (lhs.get() == (T *) rhs); } template -bool operator== (U lhs, const SharedPtr& rhs) { - return ((T*) lhs == rhs.get()); +bool operator== (U lhs, const SharedPtr &rhs) +{ + return ((T *) lhs == rhs.get()); } /** Non-member relational operators. */ template -bool operator!= (const SharedPtr& lhs, const SharedPtr& rhs) { +bool operator!= (const SharedPtr &lhs, const SharedPtr &rhs) +{ return (lhs.get() != rhs.get()); } template -bool operator!= (const SharedPtr& lhs, U rhs) { - return (lhs.get() != (T*) rhs); +bool operator!= (const SharedPtr &lhs, U rhs) +{ + return (lhs.get() != (T *) rhs); } template -bool operator!= (U lhs, const SharedPtr& rhs) { - return ((T*) lhs != rhs.get()); +bool operator!= (U lhs, const SharedPtr &rhs) +{ + return ((T *) lhs != rhs.get()); } #endif // __SHAREDPTR_H__