1 #ifndef __DALI_INTERNAL_OWNER_POINTER_H__
2 #define __DALI_INTERNAL_OWNER_POINTER_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://floralicense.org/license/
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
21 #include <dali/public-api/common/dali-common.h>
29 template < typename T >
34 * Default constructor. Creates an OwnerPointer that does not own any object.
42 * Constructor. Creates an OwnerPointer that owns the object.
43 * @param[in] object A pointer to a heap allocated object.
45 OwnerPointer( T* object )
51 * Copy constructor. Passes the ownership of a pointer to another.
52 * @param[in] other The pointer that gives away the ownership.
54 OwnerPointer( OwnerPointer& other )
60 * Assignment operator. Passes the ownership of a pointer to another.
61 * @param[in] other The pointer that gives away the ownership.
63 OwnerPointer& operator=( OwnerPointer& other )
65 if( this != &other ) // no self-assignment
76 * Assignment operator. Takes the ownership of the object.
77 * If it owns an object already, it will be deleted.
78 * @param[in] pointer A pointer to a heap allocated object.
80 OwnerPointer& operator=( T* pointer )
82 if( mObject != pointer )
100 * Indirection operator.
101 * @return a reference to the object.
105 DALI_ASSERT_DEBUG( mObject != NULL );
111 * Const indirection operator.
112 * @return a reference to the object from const OwnerPointer.
116 DALI_ASSERT_DEBUG( mObject != NULL );
118 // Pointer semantics: A const pointer does not mean const data.
119 return const_cast< T& >( *mObject );
124 * @return a pointer to the object.
132 * Const pointer operator.
133 * @return a pointer to the object referenced by a const OwnerPointer.
135 T* operator->() const
137 // Pointer semantics: A const pointer does not mean const data.
138 return const_cast< T* >( mObject );
142 * Compare with a raw pointer.
143 * @return true if the raw pointer matches the one owned by this object.
145 bool operator==( const T* pointer )
147 return ( mObject == pointer );
151 * Reset the pointer, deleting any owned object.
155 if ( mObject != NULL )
163 * Release the ownership, it does not delete the object.
164 * @return a pointer to the object.
174 * Returns a const pointer to the object owned.
175 * @return a const pointer to the object.
182 // Handle comparisons - This is a variation of the safe bool idiom
185 * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
187 typedef void (OwnerPointer::*BooleanType)() const;
190 * Converts an object handle to a BooleanType.
191 * This is useful for checking whether the handle is NULL.
193 operator BooleanType() const
195 return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
201 * Used by the safe bool idiom.
203 void ThisIsSaferThanReturningVoidStar() const {}
208 * Initialise this pointer from another one.
209 * ownerPointer parameter looses ownership.
210 * @param ownerPointer owner pointer
212 void Init( OwnerPointer& ownerPointer )
214 mObject = ownerPointer.mObject;
215 ownerPointer.mObject = NULL;
219 T* mObject; ///< Raw pointer to the object
222 } // namespace Internal
226 #endif //__DALI_INTERNAL_OWNER_POINTER_H__