1 #ifndef DALI_INTERNAL_OWNER_POINTER_H
2 #define DALI_INTERNAL_OWNER_POINTER_H
5 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/common/dali-common.h>
30 template< typename T >
36 * Default constructor. Creates an OwnerPointer that does not own any object.
37 * @note This does not protect against two different OwnerPointers pointing to the same object.
38 * Both OwnerPointers will try to release the memory of the same object in that case which
39 * could lead to a crash.
47 * Constructor. Creates an OwnerPointer that owns the object.
48 * @param[in] object A pointer to a heap allocated object.
50 OwnerPointer( T* object )
56 * Copy constructor. Passes the ownership of a pointer to another.
57 * @param[in] other The pointer that gives away the ownership.
59 OwnerPointer( const OwnerPointer& other )
60 : OwnerPointer( static_cast< OwnerPointer&& >( const_cast<OwnerPointer&>( other ) ) ) // Remove constness & cast to rvalue to use the move constructor
62 // other needs to be const for compiler to pick up this as copy constructor;
63 // though we are using this as move as there can only be one owner
67 * Move constructor. Passes the ownership of a pointer to another.
68 * @param[in] other The pointer that gives away the ownership.
70 OwnerPointer( OwnerPointer&& other )
77 * Assignment operator. Passes the ownership of a pointer to another.
78 * @param[in] other The pointer that gives away the ownership.
80 OwnerPointer& operator=( OwnerPointer& other )
82 if( this != &other ) // no self-assignment
85 mObject = other.mObject;
86 other.mObject = nullptr;
94 * Move assignment operator. Passes the ownership of a pointer to another.
95 * @param[in] other The pointer that gives away the ownership.
97 OwnerPointer& operator=( OwnerPointer&& other )
100 return operator=( other );
104 * Assignment operator. Takes the ownership of the object.
105 * If it owns an object already, it will be deleted.
106 * @param[in] pointer A pointer to a heap allocated object.
108 OwnerPointer& operator=( T* pointer )
110 if( mObject != pointer )
128 * Indirection operator.
129 * @return a reference to the object.
133 DALI_ASSERT_DEBUG( mObject );
139 * Const indirection operator.
140 * @return a reference to the object from const OwnerPointer.
144 DALI_ASSERT_DEBUG( mObject );
146 // Pointer semantics: A const pointer does not mean const data.
147 return const_cast< T& >( *mObject );
152 * @return a pointer to the object.
160 * Const pointer operator.
161 * @return a pointer to the object referenced by a const OwnerPointer.
163 T* operator->() const
165 // Pointer semantics: A const pointer does not mean const data.
166 return const_cast< T* >( mObject );
170 * Compare with a raw pointer.
171 * @return true if the raw pointer matches the one owned by this object.
173 bool operator==( const T* pointer )
175 return ( mObject == pointer );
179 * Reset the pointer, deleting any owned object.
188 * Release the ownership, it does not delete the object.
189 * @return a pointer to the object.
199 * Returns a const pointer to the object owned.
200 * @return a const pointer to the object.
209 * @param[in] other The pointer to swap the owned objects with.
211 void Swap( OwnerPointer& other )
216 mObject = other.mObject;
221 // Handle comparisons - This is a variation of the safe bool idiom
224 * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
226 typedef void (OwnerPointer::*BooleanType)() const;
229 * Converts an object handle to a BooleanType.
230 * This is useful for checking whether the handle is NULL.
232 operator BooleanType() const
234 return ( mObject != nullptr ) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : nullptr;
240 * Used by the safe bool idiom.
242 void ThisIsSaferThanReturningVoidStar() const {}
245 T* mObject; ///< Raw pointer to the object
248 } // namespace Internal
252 #endif // DALI_INTERNAL_OWNER_POINTER_H