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 )
64 * Assignment operator. Passes the ownership of a pointer to another.
65 * @param[in] other The pointer that gives away the ownership.
67 OwnerPointer& operator=( OwnerPointer& other )
69 if( this != &other ) // no self-assignment
80 * Assignment operator. Takes the ownership of the object.
81 * If it owns an object already, it will be deleted.
82 * @param[in] pointer A pointer to a heap allocated object.
84 OwnerPointer& operator=( T* pointer )
86 if( mObject != pointer )
104 * Indirection operator.
105 * @return a reference to the object.
109 DALI_ASSERT_DEBUG( mObject != NULL );
115 * Const indirection operator.
116 * @return a reference to the object from const OwnerPointer.
120 DALI_ASSERT_DEBUG( mObject != NULL );
122 // Pointer semantics: A const pointer does not mean const data.
123 return const_cast< T& >( *mObject );
128 * @return a pointer to the object.
136 * Const pointer operator.
137 * @return a pointer to the object referenced by a const OwnerPointer.
139 T* operator->() const
141 // Pointer semantics: A const pointer does not mean const data.
142 return const_cast< T* >( mObject );
146 * Compare with a raw pointer.
147 * @return true if the raw pointer matches the one owned by this object.
149 bool operator==( const T* pointer )
151 return ( mObject == pointer );
155 * Reset the pointer, deleting any owned object.
159 if ( mObject != NULL )
167 * Release the ownership, it does not delete the object.
168 * @return a pointer to the object.
178 * Returns a const pointer to the object owned.
179 * @return a const pointer to the object.
186 // Handle comparisons - This is a variation of the safe bool idiom
189 * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
191 typedef void (OwnerPointer::*BooleanType)() const;
194 * Converts an object handle to a BooleanType.
195 * This is useful for checking whether the handle is NULL.
197 operator BooleanType() const
199 return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
205 * Used by the safe bool idiom.
207 void ThisIsSaferThanReturningVoidStar() const {}
212 * Initialise this pointer from another one.
213 * ownerPointer parameter looses ownership.
214 * @param ownerPointer owner pointer
216 void Init( OwnerPointer& ownerPointer )
218 mObject = ownerPointer.mObject;
219 ownerPointer.mObject = NULL;
223 T* mObject; ///< Raw pointer to the object
226 } // namespace Internal
230 #endif //__DALI_INTERNAL_OWNER_POINTER_H__