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>
28 * @brief Templated intrusive pointer class
30 * Uses the Dali:Refobject type supply actual reference counting
31 * The object is responsible for destroying itself
40 * @brief Standard constructor to unassigned object.
42 IntrusivePtr() : mPtr( 0 ) {}
45 * @brief Constructor to attach existing object.
47 * @param p pointer to object,
49 IntrusivePtr( T* p ) : mPtr( p )
58 * @brief Copy constructor.
60 * @param rhs const reference to an IntrusivePtr
61 * @tparam U reference counter object type
64 IntrusivePtr( IntrusivePtr<U> const& rhs ) : mPtr( rhs.Get() )
73 * @brief Copy constructor.
75 IntrusivePtr( IntrusivePtr const& rhs ) : mPtr( rhs.mPtr )
86 * Object will self-destruct if reference count is zero
97 * @brief Get pointer to reference counted object.
99 * @return pointer to reference counted object
107 * @brief Pointer operator override.
109 * @return pointer to reference counted object
111 T* operator->() const
117 * @brief Dereference operator override.
119 * @return reference to reference counted object
127 * @brief Assignment operator.
129 * @param rhs const reference to intrusive pointer
130 * @return reference to reference counted object
132 IntrusivePtr& operator=( IntrusivePtr const& rhs )
134 IntrusivePtr( rhs ).Swap( *this );
139 * @brief Assignment operator.
141 * @param rhs pointer to object to wrap
142 * @return A reference to this object
144 IntrusivePtr& operator=( T* rhs )
146 IntrusivePtr( rhs ).Swap( *this );
151 * @brief Reset intrusive pointer.
155 IntrusivePtr().Swap( *this );
159 * @brief Reset intrusive pointer with reference counted object.
161 * @param rhs pointer to object
165 IntrusivePtr( rhs ).Swap( *this );
168 // IntrusivePtr comparisons - This is a variation of the safe bool idiom
171 * @brief Pointer-to-member type.
173 * Objects can be implicitly converted to this for validity checks.
175 typedef void (IntrusivePtr::*BooleanType)() const;
178 * @brief Converts an object handle to a BooleanType.
180 * This is useful for checking whether the handle is NULL.
182 operator BooleanType() const
184 return mPtr ? &IntrusivePtr::ThisIsSaferThanReturningVoidStar : 0;
188 * @brief Detach pointer from intrusive ptr counting.
202 * @brief Used by the safe bool idiom.
204 void ThisIsSaferThanReturningVoidStar() const {}
207 * @brief Internal swap function
209 void Swap( IntrusivePtr& rhs )
216 T* mPtr; ///< pointer to RefObject
220 * @brief Comparison overrides of objects wrapped by intrusive pointers.
222 * @param lhs intrusive pointer to compare with
223 * @param rhs intrusive pointer to compare against
224 * @return true if the pointers point at the same object
226 template<typename T, typename U>
227 inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
229 return lhs.Get() == rhs.Get();
233 * @brief Comparison overrides of objects wrapped by intrusive pointers.
235 * @param lhs intrusive pointer to compare with
236 * @param rhs intrusive pointer to compare against
237 * @return true if the pointers point at different objects
239 template<typename T, typename U>
240 inline bool operator!=( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const &rhs)
242 return lhs.Get() != rhs.Get();
246 * @brief Comparison overrides of objects wrapped by intrusive pointers
248 * @param lhs intrusive pointer to compare with
249 * @param rhs object to compare against
250 * @return true if the intrusive pointer points at the specified object
252 template<typename T, typename U>
253 inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
255 return lhs.Get() == rhs;
259 * @brief Comparison overrides of objects wrapped by intrusive pointers.
261 * @param lhs intrusive pointer to compare with
262 * @param rhs intrusive pointer to compare against
263 * @return true if the intrusive pointer doesn't point at the specified object
265 template<typename T, typename U>
266 inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
268 return lhs.Get() != rhs;
272 * @brief Comparison overrides of objects wrapped by intrusive pointers
274 * @param lhs object to compare with
275 * @param rhs intrusive pointer 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==( T* lhs, IntrusivePtr<U>const& rhs )
281 return lhs == rhs.Get();
285 * @brief Comparison overrides of objects wrapped by intrusive pointers
287 * @param lhs object to compare with
288 * @param rhs intrusive pointer to compare against
289 * @return true if the intrusive pointer doesn't point at the specified object
291 template<typename T, typename U>
292 inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
294 return lhs != rhs.Get();
299 #endif /* __DALI_INTRUSIVE_PTR_H__ */