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( const OwnerPointer& other )
61 // other needs to be const for compiler to pick up this as copy constructor;
62 // though we are using this as move as there can only be one owner
63 Swap( const_cast<OwnerPointer&>( other ) );
67 * Assignment operator. Passes the ownership of a pointer to another.
68 * @param[in] other The pointer that gives away the ownership.
70 OwnerPointer& operator=( OwnerPointer& other )
72 if( this != &other ) // no self-assignment
74 // Creation of temportaty object to prevent premature deletion of object
75 OwnerPointer(other).Swap(*this);
83 * Assignment operator. Takes the ownership of the object.
84 * If it owns an object already, it will be deleted.
85 * @param[in] pointer A pointer to a heap allocated object.
87 OwnerPointer& operator=( T* pointer )
89 if( mObject != pointer )
107 * Indirection operator.
108 * @return a reference to the object.
112 DALI_ASSERT_DEBUG( mObject != NULL );
118 * Const indirection operator.
119 * @return a reference to the object from const OwnerPointer.
123 DALI_ASSERT_DEBUG( mObject != NULL );
125 // Pointer semantics: A const pointer does not mean const data.
126 return const_cast< T& >( *mObject );
131 * @return a pointer to the object.
139 * Const pointer operator.
140 * @return a pointer to the object referenced by a const OwnerPointer.
142 T* operator->() const
144 // Pointer semantics: A const pointer does not mean const data.
145 return const_cast< T* >( mObject );
149 * Compare with a raw pointer.
150 * @return true if the raw pointer matches the one owned by this object.
152 bool operator==( const T* pointer )
154 return ( mObject == pointer );
158 * Reset the pointer, deleting any owned object.
162 if ( mObject != NULL )
170 * Release the ownership, it does not delete the object.
171 * @return a pointer to the object.
181 * Returns a const pointer to the object owned.
182 * @return a const pointer to the object.
192 void Swap( OwnerPointer& other )
195 mObject = other.mObject;
199 // Handle comparisons - This is a variation of the safe bool idiom
202 * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
204 typedef void (OwnerPointer::*BooleanType)() const;
207 * Converts an object handle to a BooleanType.
208 * This is useful for checking whether the handle is NULL.
210 operator BooleanType() const
212 return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
218 * Used by the safe bool idiom.
220 void ThisIsSaferThanReturningVoidStar() const {}
223 T* mObject; ///< Raw pointer to the object
226 } // namespace Internal
230 #endif //__DALI_INTERNAL_OWNER_POINTER_H__