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 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 <cstddef> // NULL
25 #include <dali/public-api/common/dali-common.h>
33 template < typename T >
38 * Default constructor. Creates an OwnerPointer that does not own any object.
46 * Constructor. Creates an OwnerPointer that owns the object.
47 * @param[in] object A pointer to a heap allocated object.
49 OwnerPointer( T* object )
55 * Copy constructor. Passes the ownership of a pointer to another.
56 * @param[in] other The pointer that gives away the ownership.
58 OwnerPointer( OwnerPointer& other )
65 * Assignment operator. Passes the ownership of a pointer to another.
66 * @param[in] other The pointer that gives away the ownership.
68 OwnerPointer& operator=( OwnerPointer& other )
70 if( this != &other ) // no self-assignment
72 // Creation of temportaty object to prevent premature deletion of object
73 OwnerPointer(other).Swap(*this);
81 * Assignment operator. Takes the ownership of the object.
82 * If it owns an object already, it will be deleted.
83 * @param[in] pointer A pointer to a heap allocated object.
85 OwnerPointer& operator=( T* pointer )
87 if( mObject != pointer )
105 * Indirection operator.
106 * @return a reference to the object.
110 DALI_ASSERT_DEBUG( mObject != NULL );
116 * Const indirection operator.
117 * @return a reference to the object from const OwnerPointer.
121 DALI_ASSERT_DEBUG( mObject != NULL );
123 // Pointer semantics: A const pointer does not mean const data.
124 return const_cast< T& >( *mObject );
129 * @return a pointer to the object.
137 * Const pointer operator.
138 * @return a pointer to the object referenced by a const OwnerPointer.
140 T* operator->() const
142 // Pointer semantics: A const pointer does not mean const data.
143 return const_cast< T* >( mObject );
147 * Compare with a raw pointer.
148 * @return true if the raw pointer matches the one owned by this object.
150 bool operator==( const T* pointer )
152 return ( mObject == pointer );
156 * Reset the pointer, deleting any owned object.
160 if ( mObject != NULL )
168 * Release the ownership, it does not delete the object.
169 * @return a pointer to the object.
179 * Returns a const pointer to the object owned.
180 * @return a const pointer to the object.
190 void Swap( OwnerPointer& other )
193 mObject = other.mObject;
197 // Handle comparisons - This is a variation of the safe bool idiom
200 * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
202 typedef void (OwnerPointer::*BooleanType)() const;
205 * Converts an object handle to a BooleanType.
206 * This is useful for checking whether the handle is NULL.
208 operator BooleanType() const
210 return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
216 * Used by the safe bool idiom.
218 void ThisIsSaferThanReturningVoidStar() const {}
221 T* mObject; ///< Raw pointer to the object
224 } // namespace Internal
228 #endif //__DALI_INTERNAL_OWNER_POINTER_H__