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 supply actual reference counting
35 * The object is responsible for destroying itself
44 * @brief Standard constructor to unassigned object.
46 IntrusivePtr() : mPtr( 0 ) {}
49 * @brief Constructor to attach existing object.
51 * @param p pointer to object,
53 IntrusivePtr( T* p ) : mPtr( p )
62 * @brief Copy constructor.
64 * @param rhs const reference to an IntrusivePtr
65 * @tparam U reference counter object type
68 IntrusivePtr( IntrusivePtr<U> const& rhs ) : mPtr( rhs.Get() )
77 * @brief Copy constructor.
79 IntrusivePtr( IntrusivePtr const& rhs ) : mPtr( rhs.mPtr )
90 * Object will self-destruct if reference count is zero
101 * @brief Get pointer to reference counted object.
103 * @return pointer to reference counted object
111 * @brief Pointer operator override.
113 * @return pointer to reference counted object
115 T* operator->() const
121 * @brief Dereference operator override.
123 * @return reference to reference counted object
131 * @brief Assignment operator.
133 * @param rhs const reference to intrusive pointer
134 * @return reference to reference counted object
136 IntrusivePtr& operator=( IntrusivePtr const& rhs )
138 IntrusivePtr( rhs ).Swap( *this );
143 * @brief Assignment operator.
145 * @param rhs pointer to object to wrap
146 * @return A reference to this object
148 IntrusivePtr& operator=( T* rhs )
150 IntrusivePtr( rhs ).Swap( *this );
155 * @brief Reset intrusive pointer.
159 IntrusivePtr().Swap( *this );
163 * @brief Reset intrusive pointer with reference counted object.
165 * @param rhs pointer to object
169 IntrusivePtr( rhs ).Swap( *this );
172 // IntrusivePtr comparisons - This is a variation of the safe bool idiom
175 * @brief Pointer-to-member type.
177 * Objects can be implicitly converted to this for validity checks.
179 typedef void (IntrusivePtr::*BooleanType)() const;
182 * @brief Converts an object handle to a BooleanType.
184 * This is useful for checking whether the handle is NULL.
186 operator BooleanType() const
188 return mPtr ? &IntrusivePtr::ThisIsSaferThanReturningVoidStar : 0;
192 * @brief Detach pointer from intrusive ptr counting.
206 * @brief Used by the safe bool idiom.
208 void ThisIsSaferThanReturningVoidStar() const {}
211 * @brief Internal swap function
213 void Swap( IntrusivePtr& rhs )
220 T* mPtr; ///< pointer to RefObject
224 * @brief Comparison overrides of objects wrapped by intrusive pointers.
226 * @param lhs intrusive pointer to compare with
227 * @param rhs intrusive pointer to compare against
228 * @return true if the pointers point at the same object
230 template<typename T, typename U>
231 inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
233 return lhs.Get() == rhs.Get();
237 * @brief Comparison overrides of objects wrapped by intrusive pointers.
239 * @param lhs intrusive pointer to compare with
240 * @param rhs intrusive pointer to compare against
241 * @return true if the pointers point at different objects
243 template<typename T, typename U>
244 inline bool operator!=( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const &rhs)
246 return lhs.Get() != rhs.Get();
250 * @brief Comparison overrides of objects wrapped by intrusive pointers
252 * @param lhs intrusive pointer to compare with
253 * @param rhs object to compare against
254 * @return true if the intrusive pointer points at the specified object
256 template<typename T, typename U>
257 inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
259 return lhs.Get() == rhs;
263 * @brief Comparison overrides of objects wrapped by intrusive pointers.
265 * @param lhs intrusive pointer to compare with
266 * @param rhs intrusive pointer to compare against
267 * @return true if the intrusive pointer doesn't point at the specified object
269 template<typename T, typename U>
270 inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
272 return lhs.Get() != rhs;
276 * @brief Comparison overrides of objects wrapped by intrusive pointers
278 * @param lhs object to compare with
279 * @param rhs intrusive pointer to compare against
280 * @return true if the intrusive pointer points at the specified object
282 template<typename T, typename U>
283 inline bool operator==( T* lhs, IntrusivePtr<U>const& rhs )
285 return lhs == rhs.Get();
289 * @brief Comparison overrides of objects wrapped by intrusive pointers
291 * @param lhs object to compare with
292 * @param rhs intrusive pointer to compare against
293 * @return true if the intrusive pointer doesn't point at the specified object
295 template<typename T, typename U>
296 inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
298 return lhs != rhs.Get();
306 #endif /* __DALI_INTRUSIVE_PTR_H__ */