1 #ifndef DALI_INTRUSIVE_PTR_H
2 #define DALI_INTRUSIVE_PTR_H
5 * Copyright (c) 2020 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>
27 * @addtogroup dali_core_common
32 * @brief Templated intrusive pointer class.
34 * Uses the Dali:RefObject type with actual reference counting.
35 * The object is responsible for destroying itself.
43 * @brief Standard constructor to unassigned object.
52 * @brief Constructor to attach existing object.
55 * @param[in] p Pointer to object
67 * @brief Copy constructor.
70 * @param[in] rhs Const reference to an IntrusivePtr
71 * @tparam U Reference counter object type
74 IntrusivePtr(IntrusivePtr<U> const& rhs)
84 * @brief Copy constructor.
86 * @param[in] rhs Const reference to an IntrusivePtr
88 IntrusivePtr(IntrusivePtr const& rhs)
98 * @brief Move constructor.
100 * @param[in] rhs Reference to an IntrusivePtr
103 IntrusivePtr(IntrusivePtr<U>&& rhs)
109 * @brief Move constructor.
111 * @param[in] rhs Reference to an IntrusivePtr
113 IntrusivePtr(IntrusivePtr&& rhs)
121 * Object will self-destruct if reference count is zero.
133 * @brief Gets pointer to reference counted object.
136 * @return Pointer to reference counted object
144 * @brief Pointer operator override.
147 * @return Pointer to reference counted object
149 T* operator->() const
155 * @brief Dereference operator override.
158 * @return Reference to reference counted object
166 * @brief Assignment operator.
169 * @param rhs Const reference to intrusive pointer
170 * @return Reference to reference counted object
172 IntrusivePtr& operator=(IntrusivePtr const& rhs)
174 IntrusivePtr(rhs).Swap(*this);
179 * @brief Assignment operator.
182 * @param rhs Pointer to object to wrap
183 * @return A Reference to this object
185 IntrusivePtr& operator=(T* rhs)
187 IntrusivePtr(rhs).Swap(*this);
192 * @brief Move assignment operator.
195 * @param rhs Reference to intrusive pointer
196 * @return Reference to moved intrusive pointer
198 IntrusivePtr& operator=(IntrusivePtr&& rhs)
213 * @brief Move assignment operator.
216 * @param rhs Reference to intrusive pointer
217 * @return Reference to moved intrusive pointer
220 IntrusivePtr& operator=(IntrusivePtr<U>&& rhs)
222 if(this != reinterpret_cast<IntrusivePtr<T>*>(&rhs))
235 * @brief Reset intrusive pointer.
240 IntrusivePtr().Swap(*this);
244 * @brief Reset intrusive pointer with reference counted object.
247 * @param[in] rhs Pointer to object
251 IntrusivePtr(rhs).Swap(*this);
254 // IntrusivePtr comparisons
257 * @brief Converts an object handle to a bool.
259 * This is useful for checking whether the handle is NULL.
262 explicit operator bool() const
264 return mPtr != nullptr;
268 * @brief Detaches pointer from intrusive ptr counting.
272 * @return Pointer to reference counted object
283 * @brief Internal swap function.
286 void Swap(IntrusivePtr& rhs)
293 T* mPtr; ///< pointer to RefObject
297 * @brief Comparison overrides of objects wrapped by intrusive pointers.
300 * @param[in] lhs Intrusive pointer to compare with
301 * @param[in] rhs Intrusive pointer to compare against
302 * @return True if the pointers point at the same object
304 template<typename T, typename U>
305 inline bool operator==(IntrusivePtr<T> const& lhs, IntrusivePtr<U> const& rhs)
307 return lhs.Get() == rhs.Get();
311 * @brief Comparison overrides of objects wrapped by intrusive pointers.
314 * @param[in] lhs Intrusive pointer to compare with
315 * @param[in] rhs Intrusive pointer to compare against
316 * @return True if the pointers point at different objects
318 template<typename T, typename U>
319 inline bool operator!=(IntrusivePtr<T> const& lhs, IntrusivePtr<U> const& rhs)
321 return lhs.Get() != rhs.Get();
325 * @brief Comparison overrides of objects wrapped by intrusive pointers.
328 * @param[in] lhs Intrusive pointer to compare with
329 * @param[in] rhs Object to compare against
330 * @return True if the intrusive pointer points at the specified object
332 template<typename T, typename U>
333 inline bool operator==(IntrusivePtr<T> const& lhs, U* rhs)
335 return lhs.Get() == rhs;
339 * @brief Comparison overrides of objects wrapped by intrusive pointers.
342 * @param[in] lhs Intrusive pointer to compare with
343 * @param[in] rhs Intrusive pointer to compare against
344 * @return True if the intrusive pointer doesn't point at the specified object
346 template<typename T, typename U>
347 inline bool operator!=(IntrusivePtr<T> const& lhs, U* rhs)
349 return lhs.Get() != rhs;
353 * @brief Comparison overrides of objects wrapped by intrusive pointers.
356 * @param[in] lhs Object to compare with
357 * @param[in] rhs Intrusive pointer to compare against
358 * @return True if the intrusive pointer points at the specified object
360 template<typename T, typename U>
361 inline bool operator==(T* lhs, IntrusivePtr<U> const& rhs)
363 return lhs == rhs.Get();
367 * @brief Comparison overrides of objects wrapped by intrusive pointers.
370 * @param[in] lhs Object to compare with
371 * @param[in] rhs Intrusive pointer to compare against
372 * @return True if the intrusive pointer doesn't point at the specified object
374 template<typename T, typename U>
375 inline bool operator!=(T* lhs, IntrusivePtr<U> const& rhs)
377 return lhs != rhs.Get();
385 #endif // DALI_INTRUSIVE_PTR_H