X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Fcommon%2Fowner-pointer.h;h=165747a945efcc0ae5a77555b57dcf4e54fa2c6a;hb=61260bb3612098fca7124f400637237cb3f181d6;hp=7b929788a4da5150cc26f1f7b32e46b390326d32;hpb=111e4590a7f0b90fbf9683e29d6d39d9da15ef60;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/common/owner-pointer.h b/dali/internal/common/owner-pointer.h index 7b92978..165747a 100644 --- a/dali/internal/common/owner-pointer.h +++ b/dali/internal/common/owner-pointer.h @@ -2,7 +2,7 @@ #define __DALI_INTERNAL_OWNER_POINTER_H__ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,9 +18,6 @@ * */ -// EXTERNAL INCLUDES -#include // NULL - // INTERNAL INCLUDES #include @@ -30,16 +27,20 @@ namespace Dali namespace Internal { -template < typename T > +template< typename T > class OwnerPointer { public: + /** * Default constructor. Creates an OwnerPointer that does not own any object. + * @note This does not protect against two different OwnerPointers pointing to the same object. + * Both OwnerPointers will try to release the memory of the same object in that case which + * could lead to a crash. */ OwnerPointer() + : mObject( nullptr ) { - mObject = NULL; } /** @@ -47,17 +48,29 @@ public: * @param[in] object A pointer to a heap allocated object. */ OwnerPointer( T* object ) + : mObject( object ) { - mObject = object; } /** * Copy constructor. Passes the ownership of a pointer to another. * @param[in] other The pointer that gives away the ownership. */ - OwnerPointer( OwnerPointer& other ) + OwnerPointer( const OwnerPointer& other ) + : OwnerPointer( static_cast< OwnerPointer&& >( const_cast( other ) ) ) // Remove constness & cast to rvalue to use the move constructor { - Init( other ); + // other needs to be const for compiler to pick up this as copy constructor; + // though we are using this as move as there can only be one owner + } + + /** + * Move constructor. Passes the ownership of a pointer to another. + * @param[in] other The pointer that gives away the ownership. + */ + OwnerPointer( OwnerPointer&& other ) + : mObject( nullptr ) + { + Swap( other ); } /** @@ -68,8 +81,9 @@ public: { if( this != &other ) // no self-assignment { - Reset(); - Init( other ); + delete mObject; + mObject = other.mObject; + other.mObject = nullptr; } // return self @@ -77,6 +91,16 @@ public: } /** + * Move assignment operator. Passes the ownership of a pointer to another. + * @param[in] other The pointer that gives away the ownership. + */ + OwnerPointer& operator=( OwnerPointer&& other ) + { + // Reuse operator= + return operator=( other ); + } + + /** * Assignment operator. Takes the ownership of the object. * If it owns an object already, it will be deleted. * @param[in] pointer A pointer to a heap allocated object. @@ -106,7 +130,7 @@ public: */ T& operator*() { - DALI_ASSERT_DEBUG( mObject != NULL ); + DALI_ASSERT_DEBUG( mObject ); return *mObject; } @@ -117,7 +141,7 @@ public: */ T& operator*() const { - DALI_ASSERT_DEBUG( mObject != NULL ); + DALI_ASSERT_DEBUG( mObject ); // Pointer semantics: A const pointer does not mean const data. return const_cast< T& >( *mObject ); @@ -156,11 +180,8 @@ public: */ void Reset() { - if ( mObject != NULL ) - { - delete mObject; - mObject = NULL; - } + delete mObject; + mObject = nullptr; } /** @@ -170,7 +191,7 @@ public: T* Release() { T* tmp = mObject; - mObject = NULL; + mObject = nullptr; return tmp; } @@ -183,6 +204,20 @@ public: return mObject; } + /** + * Swap owned objects + * @param[in] other The pointer to swap the owned objects with. + */ + void Swap( OwnerPointer& other ) + { + if( this != &other ) + { + T* tmp = mObject; + mObject = other.mObject; + other.mObject = tmp; + } + } + // Handle comparisons - This is a variation of the safe bool idiom /** @@ -196,7 +231,7 @@ public: */ operator BooleanType() const { - return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL; + return ( mObject != nullptr ) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : nullptr; } private: @@ -206,19 +241,6 @@ private: */ void ThisIsSaferThanReturningVoidStar() const {} -private: - - /** - * Initialise this pointer from another one. - * ownerPointer parameter looses ownership. - * @param ownerPointer owner pointer - */ - void Init( OwnerPointer& ownerPointer ) - { - mObject = ownerPointer.mObject; - ownerPointer.mObject = NULL; - } - // data T* mObject; ///< Raw pointer to the object };