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 #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;
190 * @brief Used by the safe bool idiom.
192 void ThisIsSaferThanReturningVoidStar() const {}
195 * @brief Internal swap function
197 void Swap( IntrusivePtr& rhs )
204 T* mPtr; ///< pointer to RefObject
208 * @brief Comparison overrides of objects wrapped by intrusive pointers.
210 * @param lhs intrusive pointer to compare with
211 * @param rhs intrusive pointer to compare against
212 * @return true if the pointers point at the same object
214 template<typename T, typename U>
215 inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
217 return lhs.Get() == rhs.Get();
221 * @brief Comparison overrides of objects wrapped by intrusive pointers.
223 * @param lhs intrusive pointer to compare with
224 * @param rhs intrusive pointer to compare against
225 * @return true if the pointers point at different objects
227 template<typename T, typename U>
228 inline bool operator!=( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const &rhs)
230 return lhs.Get() != rhs.Get();
234 * @brief Comparison overrides of objects wrapped by intrusive pointers
236 * @param lhs intrusive pointer to compare with
237 * @param rhs object to compare against
238 * @return true if the intrusive pointer points at the specified object
240 template<typename T, typename U>
241 inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
243 return lhs.Get() == rhs;
247 * @brief Comparison overrides of objects wrapped by intrusive pointers.
249 * @param lhs intrusive pointer to compare with
250 * @param rhs intrusive pointer to compare against
251 * @return true if the intrusive pointer doesn't point at the specified object
253 template<typename T, typename U>
254 inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
256 return lhs.Get() != rhs;
260 * @brief Comparison overrides of objects wrapped by intrusive pointers
262 * @param lhs object to compare with
263 * @param rhs intrusive pointer to compare against
264 * @return true if the intrusive pointer points at the specified object
266 template<typename T, typename U>
267 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>
280 inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
282 return lhs != rhs.Get();
287 #endif /* __DALI_INTRUSIVE_PTR_H__ */