1 #ifndef __DALI_INTRUSIVE_PTR_H__
2 #define __DALI_INTRUSIVE_PTR_H__
5 * Copyright (c) 2015 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.
45 * @brief Standard constructor to unassigned object.
48 IntrusivePtr() : mPtr( 0 ) {}
51 * @brief Constructor to attach existing object.
54 * @param[in] p Pointer to object,
56 IntrusivePtr( T* p ) : mPtr( p )
65 * @brief Copy constructor.
68 * @param[in] rhs Const reference to an IntrusivePtr
69 * @tparam U Reference counter object type
72 IntrusivePtr( IntrusivePtr<U> const& rhs ) : mPtr( rhs.Get() )
81 * @brief Copy constructor.
84 IntrusivePtr( IntrusivePtr const& rhs ) : mPtr( rhs.mPtr )
95 * Object will self-destruct if reference count is zero
107 * @brief Get pointer to reference counted object.
110 * @return Pointer to reference counted object
118 * @brief Pointer operator override.
121 * @return Pointer to reference counted object
123 T* operator->() const
129 * @brief Dereference operator override.
132 * @return Reference to reference counted object
140 * @brief Assignment operator.
143 * @param rhs Const reference to intrusive pointer
144 * @return Reference to reference counted object
146 IntrusivePtr& operator=( IntrusivePtr const& rhs )
148 IntrusivePtr( rhs ).Swap( *this );
153 * @brief Assignment operator.
156 * @param rhs Pointer to object to wrap
157 * @return A Reference to this object
159 IntrusivePtr& operator=( T* rhs )
161 IntrusivePtr( rhs ).Swap( *this );
166 * @brief Reset intrusive pointer.
171 IntrusivePtr().Swap( *this );
175 * @brief Reset intrusive pointer with reference counted object.
178 * @param[in] rhs Pointer to object
182 IntrusivePtr( rhs ).Swap( *this );
185 // IntrusivePtr comparisons - This is a variation of the safe bool idiom
188 * @brief Pointer-to-member type.
190 * Objects can be implicitly converted to this for validity checks.
192 typedef void (IntrusivePtr::*BooleanType)() const;
195 * @brief Converts an object handle to a BooleanType.
197 * This is useful for checking whether the handle is NULL.
200 operator BooleanType() const
202 return mPtr ? &IntrusivePtr::ThisIsSaferThanReturningVoidStar : 0;
206 * @brief Detach pointer from intrusive ptr counting.
221 * @brief Used by the safe bool idiom.
224 void ThisIsSaferThanReturningVoidStar() const {}
227 * @brief Internal swap function
230 void Swap( IntrusivePtr& rhs )
237 T* mPtr; ///< pointer to RefObject
241 * @brief Comparison overrides of objects wrapped by intrusive pointers.
244 * @param[in] lhs Intrusive pointer to compare with
245 * @param[in] rhs Intrusive pointer to compare against
246 * @return True if the pointers point at the same object
248 template<typename T, typename U>
249 inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
251 return lhs.Get() == rhs.Get();
255 * @brief Comparison overrides of objects wrapped by intrusive pointers.
258 * @param[in] lhs Intrusive pointer to compare with
259 * @param[in] rhs Intrusive pointer to compare against
260 * @return True if the pointers point at different objects
262 template<typename T, typename U>
263 inline bool operator!=( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const &rhs)
265 return lhs.Get() != rhs.Get();
269 * @brief Comparison overrides of objects wrapped by intrusive pointers
272 * @param[in] lhs Intrusive pointer to compare with
273 * @param[in] rhs Object to compare against
274 * @return True if the intrusive pointer points at the specified object
276 template<typename T, typename U>
277 inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
279 return lhs.Get() == rhs;
283 * @brief Comparison overrides of objects wrapped by intrusive pointers.
286 * @param[in] lhs Intrusive pointer to compare with
287 * @param[in] rhs Intrusive pointer to compare against
288 * @return True if the intrusive pointer doesn't point at the specified object
290 template<typename T, typename U>
291 inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
293 return lhs.Get() != rhs;
297 * @brief Comparison overrides of objects wrapped by intrusive pointers
300 * @param[in] lhs Object to compare with
301 * @param[in] rhs Intrusive pointer to compare against
302 * @return True if the intrusive pointer points at the specified object
304 template<typename T, typename U>
305 inline bool operator==( T* lhs, IntrusivePtr<U>const& rhs )
307 return lhs == rhs.Get();
311 * @brief Comparison overrides of objects wrapped by intrusive pointers
314 * @param[in] lhs Object to compare with
315 * @param[in] rhs Intrusive pointer to compare against
316 * @return True if the intrusive pointer doesn't point at the specified object
318 template<typename T, typename U>
319 inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
321 return lhs != rhs.Get();
329 #endif /* __DALI_INTRUSIVE_PTR_H__ */