1 #ifndef DALI_INTRUSIVE_PTR_H
2 #define DALI_INTRUSIVE_PTR_H
5 * Copyright (c) 2022 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> // for std::nullptr_t
25 #include <dali/public-api/common/dali-common.h>
30 * @addtogroup dali_core_common
35 * @brief Templated intrusive pointer class.
37 * Uses the Dali:RefObject type with actual reference counting.
38 * The object is responsible for destroying itself.
46 * @brief Standard constructor to unassigned object.
55 * @brief Constructor to attach existing object.
58 * @param[in] p Pointer to object
70 * @brief Copy constructor.
73 * @param[in] rhs Const reference to an IntrusivePtr
74 * @tparam U Reference counter object type
77 IntrusivePtr(IntrusivePtr<U> const& rhs)
87 * @brief Copy constructor.
89 * @param[in] rhs Const reference to an IntrusivePtr
91 IntrusivePtr(IntrusivePtr const& rhs)
101 * @brief Move constructor.
103 * @param[in] rhs Reference to an IntrusivePtr
106 IntrusivePtr(IntrusivePtr<U>&& rhs)
112 * @brief Move constructor.
114 * @param[in] rhs Reference to an IntrusivePtr
116 IntrusivePtr(IntrusivePtr&& rhs)
124 * Object will self-destruct if reference count is zero.
136 * @brief Gets pointer to reference counted object.
139 * @return Pointer to reference counted object
147 * @brief Pointer operator override.
150 * @return Pointer to reference counted object
152 T* operator->() const
158 * @brief Dereference operator override.
161 * @return Reference to reference counted object
169 * @brief Assignment operator.
172 * @param rhs Const reference to intrusive pointer
173 * @return Reference to reference counted object
175 IntrusivePtr& operator=(IntrusivePtr const& rhs)
177 IntrusivePtr(rhs).Swap(*this);
182 * @brief Assignment operator.
185 * @param rhs Pointer to object to wrap
186 * @return A Reference to this object
188 IntrusivePtr& operator=(T* rhs)
190 IntrusivePtr(rhs).Swap(*this);
195 * @brief Move assignment operator.
198 * @param rhs Reference to intrusive pointer
199 * @return Reference to moved intrusive pointer
201 IntrusivePtr& operator=(IntrusivePtr&& rhs)
216 * @brief Move assignment operator.
219 * @param rhs Reference to intrusive pointer
220 * @return Reference to moved intrusive pointer
223 IntrusivePtr& operator=(IntrusivePtr<U>&& rhs)
225 if(this != reinterpret_cast<IntrusivePtr<T>*>(&rhs))
238 * @brief Reset intrusive pointer.
243 IntrusivePtr().Swap(*this);
247 * @brief Reset intrusive pointer with reference counted object.
250 * @param[in] rhs Pointer to object
254 IntrusivePtr(rhs).Swap(*this);
257 // IntrusivePtr comparisons
260 * @brief Converts an object handle to a bool.
262 * This is useful for checking whether the handle is NULL.
265 explicit operator bool() const
267 return mPtr != nullptr;
271 * @brief Detaches pointer from intrusive ptr counting.
275 * @return Pointer to reference counted object
286 * @brief Internal swap function.
289 void Swap(IntrusivePtr& rhs)
296 T* mPtr; ///< pointer to RefObject
300 * @brief Comparison overrides of objects wrapped by intrusive pointers.
303 * @param[in] lhs Intrusive pointer to compare with
304 * @param[in] rhs Intrusive pointer to compare against
305 * @return True if the pointers point at the same object
307 template<typename T, typename U>
308 inline bool operator==(IntrusivePtr<T> const& lhs, IntrusivePtr<U> const& rhs)
310 return lhs.Get() == rhs.Get();
314 * @brief Comparison overrides of objects wrapped by intrusive pointers.
317 * @param[in] lhs Intrusive pointer to compare with
318 * @param[in] rhs Intrusive pointer to compare against
319 * @return True if the pointers point at different objects
321 template<typename T, typename U>
322 inline bool operator!=(IntrusivePtr<T> const& lhs, IntrusivePtr<U> const& rhs)
324 return lhs.Get() != rhs.Get();
328 * @brief Comparison overrides of objects wrapped by intrusive pointers.
331 * @param[in] lhs Intrusive pointer to compare with
332 * @param[in] rhs Object to compare against
333 * @return True if the intrusive pointer points at the specified object
335 template<typename T, typename U>
336 inline bool operator==(IntrusivePtr<T> const& lhs, U* rhs)
338 return lhs.Get() == rhs;
342 * @brief Comparison overrides of objects wrapped by intrusive pointers.
345 * @param[in] lhs Intrusive pointer to compare with
346 * @param[in] rhs Intrusive pointer to compare against
347 * @return True if the intrusive pointer doesn't point at the specified object
349 template<typename T, typename U>
350 inline bool operator!=(IntrusivePtr<T> const& lhs, U* rhs)
352 return lhs.Get() != rhs;
356 * @brief Comparison overrides of objects wrapped by intrusive pointers.
359 * @param[in] lhs Object to compare with
360 * @param[in] rhs Intrusive pointer to compare against
361 * @return True if the intrusive pointer points at the specified object
363 template<typename T, typename U>
364 inline bool operator==(T* lhs, IntrusivePtr<U> const& rhs)
366 return lhs == rhs.Get();
370 * @brief Comparison overrides of objects wrapped by intrusive pointers.
373 * @param[in] lhs Object to compare with
374 * @param[in] rhs Intrusive pointer to compare against
375 * @return True if the intrusive pointer doesn't point at the specified object
377 template<typename T, typename U>
378 inline bool operator!=(T* lhs, IntrusivePtr<U> const& rhs)
380 return lhs != rhs.Get();
384 * @brief Comparison overrides of objects with nullptr_t.
387 * @param[in] lhs Intrusive pointer to compare with
388 * @param[in] rhs nullptr
389 * @return True if the pointers is nullptr
392 inline bool operator==(IntrusivePtr<T> const& lhs, std::nullptr_t rhs)
394 return lhs.Get() == nullptr;
398 * @brief Comparison overrides of objects with nullptr_t.
401 * @param[in] lhs Intrusive pointer to compare with
402 * @param[in] rhs nullptr
403 * @return True if the pointers is not nullptr
406 inline bool operator!=(IntrusivePtr<T> const& lhs, std::nullptr_t rhs)
408 return lhs.Get() != nullptr;
412 * @brief Comparison overrides of objects with nullptr_t.
415 * @param[in] lhs nullptr
416 * @param[in] rhs Intrusive pointer to compare against
417 * @return True if the pointers is nullptr
420 inline bool operator==(std::nullptr_t lhs, IntrusivePtr<T> const& rhs)
422 return nullptr == rhs.Get();
426 * @brief Comparison overrides of objects with nullptr_t.
429 * @param[in] lhs nullptr
430 * @param[in] rhs Intrusive pointer to compare against
431 * @return True if the pointers is not nullptr
434 inline bool operator!=(std::nullptr_t lhs, IntrusivePtr<T> const& rhs)
436 return nullptr != rhs.Get();
444 #endif // DALI_INTRUSIVE_PTR_H