1 #ifndef __DALI_INTRUSIVE_PTR_H__
2 #define __DALI_INTRUSIVE_PTR_H__
5 * Copyright (c) 2014 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 * @addtogroup CAPI_DALI_OBJECT_MODULE
27 #include <dali/public-api/common/dali-common.h>
33 * @brief Templated class to emulate a sub-set of functions served by deprecating boost::intrusive_ptr.
35 * Uses the Dali:Refobject type supply actual reference counting
36 * The object is responsible for destroying itself
45 * @brief Standard constructor to unassigned object.
47 IntrusivePtr() : mPtr( 0 ) {}
50 * @brief Constructor to attach existing object.
52 * @param p pointer to object,
54 IntrusivePtr( T* p ) : mPtr( p )
63 * @brief Copy constructor.
65 * @param rhs const reference to an IntrusivePtr
66 * @tparam U reference counter object type
69 IntrusivePtr( IntrusivePtr<U> const& rhs ) : mPtr( rhs.Get() )
78 * @brief Copy constructor.
80 IntrusivePtr( IntrusivePtr const& rhs ) : mPtr( rhs.mPtr )
91 * Object will self-destruct if reference count is zero
102 * @brief Get pointer to reference counted object.
104 * @return pointer to reference counted object
112 * @brief Pointer operator ovveride.
114 * @return pointer to reference counted object
116 T* operator->() const
122 * @brief Dereference operator override.
124 * @return reference to reference counted object
132 * @brief Assignment operator.
134 * @param rhs const reference to intrusive pointer
135 * @return reference to reference counted object
137 IntrusivePtr& operator=( IntrusivePtr const& rhs )
139 IntrusivePtr( rhs ).Swap( *this );
144 * @brief Assignment operator.
146 * @param rhs pointer to object to wrap
147 * @return A reference to this object
149 IntrusivePtr& operator=( T* rhs )
151 IntrusivePtr( rhs ).Swap( *this );
156 * @brief Reset intrusive pointer.
160 IntrusivePtr().Swap( *this );
164 * @brief Reset intrusive pointer with reference counted object.
166 * @param rhs pointer to object
170 IntrusivePtr( rhs ).Swap( *this );
173 // IntrusivePtr comparisons - This is a variation of the safe bool idiom
176 * @brief Pointer-to-member type.
178 * Objects can be implicitly converted to this for validity checks.
180 typedef void (IntrusivePtr::*BooleanType)() const;
183 * @brief Converts an object handle to a BooleanType.
185 * This is useful for checking whether the handle is NULL.
187 operator BooleanType() const
189 return mPtr ? &IntrusivePtr::ThisIsSaferThanReturningVoidStar : 0;
195 * @brief Used by the safe bool idiom.
197 void ThisIsSaferThanReturningVoidStar() const {}
200 * @brief Internal swap function
202 void Swap( IntrusivePtr& rhs )
209 T* mPtr; ///< pointer to RefObject
213 * @brief Comparison overrides of objects wrapped by intrusive pointers.
215 * @param lhs intrusive pointer to compare with
216 * @param rhs intrusive pointer to compare against
217 * @return true if the pointers point at the same object
219 template<typename T, typename U> inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
221 return lhs.Get() == rhs.Get();
225 * @brief Comparison overrides of objects wrapped by intrusive pointers.
227 * @param lhs intrusive pointer to compare with
228 * @param rhs intrusive pointer to compare against
229 * @return true if the pointers point at different objects
231 template<typename T, typename U> 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 object to compare against
241 * @return true if the intrusive pointer points at the specified object
243 template<typename T, typename U> inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
245 return lhs.Get() == rhs;
249 * @brief Comparison overrides of objects wrapped by intrusive pointers.
251 * @param lhs intrusive pointer to compare with
252 * @param rhs intrusive pointer to compare against
253 * @return true if the intrusive pointer doesn't point at the specified object
255 template<typename T, typename U> inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
257 return lhs.Get() != rhs;
261 * @brief Comparison overrides of objects wrapped by intrusive pointers
263 * @param lhs object to compare with
264 * @param rhs intrusive pointer to compare against
265 * @return true if the intrusive pointer points at the specified object
267 template<typename T, typename U> inline bool operator==( T* lhs, IntrusivePtr<U>const& rhs )
269 return lhs == rhs.Get();
273 * @brief Comparison overrides of objects wrapped by intrusive pointers
275 * @param lhs object to compare with
276 * @param rhs intrusive pointer to compare against
277 * @return true if the intrusive pointer doesn't point at the specified object
279 template<typename T, typename U> inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
281 return lhs != rhs.Get();
285 * @brief Get pointer to reference counted object (Dali camel case variant)
287 * @param rhs intrusive pointer wrapping object
288 * @return pointer to object
290 template<typename T>inline T* GetPointer(IntrusivePtr<T> const& rhs)
296 * @brief Get pointer to reference counted object (boost:: naming convention)
298 * @param rhs intrusive pointer wrapping object
299 * @return pointer to object
301 template<typename T>inline T* get_pointer(IntrusivePtr<T> const& rhs)
311 #endif /* __DALI_INTRUSIVE_PTR_H__ */