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 <dali/public-api/common/dali-common.h>
30 template < typename T >
35 * Default constructor. Creates an OwnerPointer that does not own any object.
43 * Constructor. Creates an OwnerPointer that owns the object.
44 * @param[in] object A pointer to a heap allocated object.
46 OwnerPointer( T* object )
52 * Copy constructor. Passes the ownership of a pointer to another.
53 * @param[in] other The pointer that gives away the ownership.
55 OwnerPointer( OwnerPointer& other )
61 * Assignment operator. Passes the ownership of a pointer to another.
62 * @param[in] other The pointer that gives away the ownership.
64 OwnerPointer& operator=( OwnerPointer& other )
66 if( this != &other ) // no self-assignment
77 * Assignment operator. Takes the ownership of the object.
78 * If it owns an object already, it will be deleted.
79 * @param[in] pointer A pointer to a heap allocated object.
81 OwnerPointer& operator=( T* pointer )
83 if( mObject != pointer )
101 * Indirection operator.
102 * @return a reference to the object.
106 DALI_ASSERT_DEBUG( mObject != NULL );
112 * Const indirection operator.
113 * @return a reference to the object from const OwnerPointer.
117 DALI_ASSERT_DEBUG( mObject != NULL );
119 // Pointer semantics: A const pointer does not mean const data.
120 return const_cast< T& >( *mObject );
125 * @return a pointer to the object.
133 * Const pointer operator.
134 * @return a pointer to the object referenced by a const OwnerPointer.
136 T* operator->() const
138 // Pointer semantics: A const pointer does not mean const data.
139 return const_cast< T* >( mObject );
143 * Compare with a raw pointer.
144 * @return true if the raw pointer matches the one owned by this object.
146 bool operator==( const T* pointer )
148 return ( mObject == pointer );
152 * Reset the pointer, deleting any owned object.
156 if ( mObject != NULL )
164 * Release the ownership, it does not delete the object.
165 * @return a pointer to the object.
175 * Returns a const pointer to the object owned.
176 * @return a const pointer to the object.
183 // Handle comparisons - This is a variation of the safe bool idiom
186 * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
188 typedef void (OwnerPointer::*BooleanType)() const;
191 * Converts an object handle to a BooleanType.
192 * This is useful for checking whether the handle is NULL.
194 operator BooleanType() const
196 return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
202 * Used by the safe bool idiom.
204 void ThisIsSaferThanReturningVoidStar() const {}
209 * Initialise this pointer from another one.
210 * ownerPointer parameter looses ownership.
211 * @param ownerPointer owner pointer
213 void Init( OwnerPointer& ownerPointer )
215 mObject = ownerPointer.mObject;
216 ownerPointer.mObject = NULL;
220 T* mObject; ///< Raw pointer to the object
223 } // namespace Internal
227 #endif //__DALI_INTERNAL_OWNER_POINTER_H__