1 #ifndef DALI_INTERNAL_OWNER_POINTER_H
2 #define DALI_INTERNAL_OWNER_POINTER_H
5 * Copyright (c) 2021 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>
33 * Default constructor. Creates an OwnerPointer that does not own any object.
34 * @note This does not protect against two different OwnerPointers pointing to the same object.
35 * Both OwnerPointers will try to release the memory of the same object in that case which
36 * could lead to a crash.
44 * Constructor. Creates an OwnerPointer that owns the object.
45 * @param[in] object A pointer to a heap allocated object.
47 OwnerPointer(T* object)
53 * Copy constructor. Passes the ownership of a pointer to another.
54 * @param[in] other The pointer that gives away the ownership.
56 OwnerPointer(const OwnerPointer& other)
57 : OwnerPointer(static_cast<OwnerPointer&&>(const_cast<OwnerPointer&>(other))) // Remove constness & cast to rvalue to use the move constructor
59 // other needs to be const for compiler to pick up this as copy constructor;
60 // though we are using this as move as there can only be one owner
64 * Move constructor. Passes the ownership of a pointer to another.
65 * @param[in] other The pointer that gives away the ownership.
67 OwnerPointer(OwnerPointer&& other)
74 * Assignment operator. Passes the ownership of a pointer to another.
75 * @param[in] other The pointer that gives away the ownership.
77 OwnerPointer& operator=(OwnerPointer& other)
79 if(this != &other) // no self-assignment
82 mObject = other.mObject;
83 other.mObject = nullptr;
91 * Move assignment operator. Passes the ownership of a pointer to another.
92 * @param[in] other The pointer that gives away the ownership.
94 OwnerPointer& operator=(OwnerPointer&& other)
97 return operator=(other);
101 * Assignment operator. Takes the ownership of the object.
102 * If it owns an object already, it will be deleted.
103 * @param[in] pointer A pointer to a heap allocated object.
105 OwnerPointer& operator=(T* pointer)
107 if(mObject != pointer)
125 * Indirection operator.
126 * @return a reference to the object.
130 DALI_ASSERT_DEBUG(mObject);
136 * Const indirection operator.
137 * @return a reference to the object from const OwnerPointer.
141 DALI_ASSERT_DEBUG(mObject);
143 // Pointer semantics: A const pointer does not mean const data.
144 return const_cast<T&>(*mObject);
149 * @return a pointer to the object.
157 * Const pointer operator.
158 * @return a pointer to the object referenced by a const OwnerPointer.
160 T* operator->() const
162 // Pointer semantics: A const pointer does not mean const data.
163 return const_cast<T*>(mObject);
167 * Compare with a raw pointer.
168 * @return true if the raw pointer matches the one owned by this object.
170 bool operator==(const T* pointer)
172 return (mObject == pointer);
176 * Reset the pointer, deleting any owned object.
185 * Release the ownership, it does not delete the object.
186 * @return a pointer to the object.
196 * Returns a const pointer to the object owned.
197 * @return a const pointer to the object.
206 * @param[in] other The pointer to swap the owned objects with.
208 void Swap(OwnerPointer& other)
213 mObject = other.mObject;
218 // Handle comparisons - This is a variation of the safe bool idiom
221 * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
223 using BooleanType = void (OwnerPointer<T>::*)() const;
226 * Converts an object handle to a BooleanType.
227 * This is useful for checking whether the handle is NULL.
229 operator BooleanType() const
231 return (mObject != nullptr) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : nullptr;
236 * Used by the safe bool idiom.
238 void ThisIsSaferThanReturningVoidStar() const
243 T* mObject; ///< Raw pointer to the object
246 } // namespace Internal
250 #endif // DALI_INTERNAL_OWNER_POINTER_H