Update formatting for SharedPtr.h

pull/7815/head
Donatien Garnier 2018-08-20 12:04:53 +01:00
parent 6f3c07f6eb
commit d9c1748c14
1 changed files with 59 additions and 39 deletions

View File

@ -68,16 +68,18 @@ public:
* @brief Create empty SharedPtr not pointing to anything. * @brief Create empty SharedPtr not pointing to anything.
* @details Used for variable declaration. * @details Used for variable declaration.
*/ */
SharedPtr(): _ptr(NULL), _counter(NULL) { SharedPtr(): _ptr(NULL), _counter(NULL)
{
} }
/** /**
* @brief Create new SharedPtr * @brief Create new SharedPtr
* @param ptr Pointer to take control over * @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 // allocate counter on the heap so it can be shared
if(_ptr != NULL) { if (_ptr != NULL) {
_counter = new uint32_t; _counter = new uint32_t;
*_counter = 1; *_counter = 1;
} }
@ -87,7 +89,8 @@ public:
* @brief Destructor. * @brief Destructor.
* @details Decrement reference counter and delete object if no longer pointed to. * @details Decrement reference counter and delete object if no longer pointed to.
*/ */
~SharedPtr() { ~SharedPtr()
{
decrement_counter(); decrement_counter();
} }
@ -97,7 +100,8 @@ public:
* copying pointer to original object and pointer to counter. * copying pointer to original object and pointer to counter.
* @param source Object being copied from. * @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 // increment reference counter
if (_ptr != NULL) { if (_ptr != NULL) {
core_util_atomic_incr_u32(_counter, 1); core_util_atomic_incr_u32(_counter, 1);
@ -110,7 +114,8 @@ public:
* @param source Object being assigned from. * @param source Object being assigned from.
* @return Object being assigned. * @return Object being assigned.
*/ */
SharedPtr operator=(const SharedPtr& source) { SharedPtr operator=(const SharedPtr &source)
{
if (this != &source) { if (this != &source) {
// clean up by decrementing counter // clean up by decrementing counter
decrement_counter(); decrement_counter();
@ -132,11 +137,12 @@ public:
* @brief Replaces the managed pointer with a new unmanaged pointer. * @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 // clean up by decrementing counter
decrement_counter(); decrement_counter();
if(ptr != NULL) { if (ptr != NULL) {
// allocate counter on the heap so it can be shared // allocate counter on the heap so it can be shared
_counter = new uint32_t; _counter = new uint32_t;
*_counter = 1; *_counter = 1;
@ -146,7 +152,8 @@ public:
/** /**
* @brief Replace the managed pointer with a NULL pointer. * @brief Replace the managed pointer with a NULL pointer.
*/ */
void reset() { void reset()
{
reset(NULL); reset(NULL);
} }
@ -155,7 +162,8 @@ public:
* @details Get raw pointer to object pointed to. * @details Get raw pointer to object pointed to.
* @return Pointer. * @return Pointer.
*/ */
T* get() const { T *get() const
{
return _ptr; return _ptr;
} }
@ -163,7 +171,8 @@ public:
* @brief Reference count accessor. * @brief Reference count accessor.
* @return Reference count. * @return Reference count.
*/ */
uint32_t use_count() const { uint32_t use_count() const
{
if (_ptr != NULL) { if (_ptr != NULL) {
core_util_critical_section_enter(); core_util_critical_section_enter();
return *_counter; return *_counter;
@ -177,7 +186,8 @@ public:
* @brief Dereference object operator. * @brief Dereference object operator.
* @details Override to return the object pointed to. * @details Override to return the object pointed to.
*/ */
T& operator*() const { T &operator*() const
{
return *_ptr; return *_ptr;
} }
@ -185,7 +195,8 @@ public:
* @brief Dereference object member operator. * @brief Dereference object member operator.
* @details Override to return return member in object pointed to. * @details Override to return return member in object pointed to.
*/ */
T* operator->() const { T *operator->() const
{
return _ptr; return _ptr;
} }
@ -193,7 +204,8 @@ public:
* @brief Boolean conversion operator. * @brief Boolean conversion operator.
* @return Whether or not the pointer is NULL. * @return Whether or not the pointer is NULL.
*/ */
operator bool() const { operator bool() const
{
return (_ptr != NULL); return (_ptr != NULL);
} }
@ -202,7 +214,8 @@ private:
* @brief Get pointer to reference counter. * @brief Get pointer to reference counter.
* @return Pointer to reference counter. * @return Pointer to reference counter.
*/ */
uint32_t* get_counter() const { uint32_t *get_counter() const
{
return _counter; return _counter;
} }
@ -210,7 +223,8 @@ private:
* @brief Decrement reference counter. * @brief Decrement reference counter.
* @details If count reaches zero, free counter and delete object pointed to. * @details If count reaches zero, free counter and delete object pointed to.
*/ */
void decrement_counter() { void decrement_counter()
{
if (_ptr != NULL) { if (_ptr != NULL) {
uint32_t new_value = core_util_atomic_decr_u32(_counter, 1); uint32_t new_value = core_util_atomic_decr_u32(_counter, 1);
if (new_value == 0) { if (new_value == 0) {
@ -224,44 +238,50 @@ private:
private: private:
// pointer to shared object // pointer to shared object
T* _ptr; T *_ptr;
// pointer to shared reference counter // pointer to shared reference counter
uint32_t* _counter; uint32_t *_counter;
}; };
/** Non-member relational operators. /** Non-member relational operators.
*/ */
template <class T, class U> template <class T, class U>
bool operator== (const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) { bool operator== (const SharedPtr<T> &lhs, const SharedPtr<U> &rhs)
{
return (lhs.get() == rhs.get()); return (lhs.get() == rhs.get());
} }
template <class T, typename U> template <class T, typename U>
bool operator== (const SharedPtr<T>& lhs, U rhs) { bool operator== (const SharedPtr<T> &lhs, U rhs)
return (lhs.get() == (T*) rhs); {
return (lhs.get() == (T *) rhs);
} }
template <class T, typename U> template <class T, typename U>
bool operator== (U lhs, const SharedPtr<T>& rhs) { bool operator== (U lhs, const SharedPtr<T> &rhs)
return ((T*) lhs == rhs.get()); {
return ((T *) lhs == rhs.get());
} }
/** Non-member relational operators. /** Non-member relational operators.
*/ */
template <class T, class U> template <class T, class U>
bool operator!= (const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) { bool operator!= (const SharedPtr<T> &lhs, const SharedPtr<U> &rhs)
{
return (lhs.get() != rhs.get()); return (lhs.get() != rhs.get());
} }
template <class T, typename U> template <class T, typename U>
bool operator!= (const SharedPtr<T>& lhs, U rhs) { bool operator!= (const SharedPtr<T> &lhs, U rhs)
return (lhs.get() != (T*) rhs); {
return (lhs.get() != (T *) rhs);
} }
template <class T, typename U> template <class T, typename U>
bool operator!= (U lhs, const SharedPtr<T>& rhs) { bool operator!= (U lhs, const SharedPtr<T> &rhs)
return ((T*) lhs != rhs.get()); {
return ((T *) lhs != rhs.get());
} }
#endif // __SHAREDPTR_H__ #endif // __SHAREDPTR_H__