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.
83 * @param[in] rhs Const reference to an IntrusivePtr
85 IntrusivePtr( IntrusivePtr const& rhs ) : mPtr( rhs.mPtr )
96 * Object will self-destruct if reference count is zero
108 * @brief Get pointer to reference counted object.
111 * @return Pointer to reference counted object
119 * @brief Pointer operator override.
122 * @return Pointer to reference counted object
124 T* operator->() const
130 * @brief Dereference operator override.
133 * @return Reference to reference counted object
141 * @brief Assignment operator.
144 * @param rhs Const reference to intrusive pointer
145 * @return Reference to reference counted object
147 IntrusivePtr& operator=( IntrusivePtr const& rhs )
149 IntrusivePtr( rhs ).Swap( *this );
154 * @brief Assignment operator.
157 * @param rhs Pointer to object to wrap
158 * @return A Reference to this object
160 IntrusivePtr& operator=( T* rhs )
162 IntrusivePtr( rhs ).Swap( *this );
167 * @brief Reset intrusive pointer.
172 IntrusivePtr().Swap( *this );
176 * @brief Reset intrusive pointer with reference counted object.
179 * @param[in] rhs Pointer to object
183 IntrusivePtr( rhs ).Swap( *this );
186 // IntrusivePtr comparisons - This is a variation of the safe bool idiom
189 * @brief Pointer-to-member type.
191 * Objects can be implicitly converted to this for validity checks.
193 typedef void (IntrusivePtr::*BooleanType)() const;
196 * @brief Converts an object handle to a BooleanType.
198 * This is useful for checking whether the handle is NULL.
201 operator BooleanType() const
203 return mPtr ? &IntrusivePtr::ThisIsSaferThanReturningVoidStar : 0;
207 * @brief Detach pointer from intrusive ptr counting.
211 * @return Pointer to reference counted object
223 * @brief Used by the safe bool idiom.
226 void ThisIsSaferThanReturningVoidStar() const {}
229 * @brief Internal swap function
232 void Swap( IntrusivePtr& rhs )
239 T* mPtr; ///< pointer to RefObject
243 * @brief Comparison overrides of objects wrapped by intrusive pointers.
246 * @param[in] lhs Intrusive pointer to compare with
247 * @param[in] rhs Intrusive pointer to compare against
248 * @return True if the pointers point at the same object
250 template<typename T, typename U>
251 inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
253 return lhs.Get() == rhs.Get();
257 * @brief Comparison overrides of objects wrapped by intrusive pointers.
260 * @param[in] lhs Intrusive pointer to compare with
261 * @param[in] rhs Intrusive pointer to compare against
262 * @return True if the pointers point at different objects
264 template<typename T, typename U>
265 inline bool operator!=( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const &rhs)
267 return lhs.Get() != rhs.Get();
271 * @brief Comparison overrides of objects wrapped by intrusive pointers
274 * @param[in] lhs Intrusive pointer to compare with
275 * @param[in] rhs Object to compare against
276 * @return True if the intrusive pointer points at the specified object
278 template<typename T, typename U>
279 inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
281 return lhs.Get() == rhs;
285 * @brief Comparison overrides of objects wrapped by intrusive pointers.
288 * @param[in] lhs Intrusive pointer to compare with
289 * @param[in] rhs Intrusive pointer to compare against
290 * @return True if the intrusive pointer doesn't point at the specified object
292 template<typename T, typename U>
293 inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
295 return lhs.Get() != rhs;
299 * @brief Comparison overrides of objects wrapped by intrusive pointers
302 * @param[in] lhs Object to compare with
303 * @param[in] rhs Intrusive pointer to compare against
304 * @return True if the intrusive pointer points at the specified object
306 template<typename T, typename U>
307 inline bool operator==( T* lhs, IntrusivePtr<U>const& rhs )
309 return lhs == rhs.Get();
313 * @brief Comparison overrides of objects wrapped by intrusive pointers
316 * @param[in] lhs Object to compare with
317 * @param[in] rhs Intrusive pointer to compare against
318 * @return True if the intrusive pointer doesn't point at the specified object
320 template<typename T, typename U>
321 inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
323 return lhs != rhs.Get();
331 #endif /* __DALI_INTRUSIVE_PTR_H__ */