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 class to emulate a sub-set of functions served by deprecating boost::intrusive_ptr.
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 ovveride.
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> inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
216 return lhs.Get() == rhs.Get();
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 different objects
226 template<typename T, typename U> inline bool operator!=( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const &rhs)
228 return lhs.Get() != rhs.Get();
232 * @brief Comparison overrides of objects wrapped by intrusive pointers
234 * @param lhs intrusive pointer to compare with
235 * @param rhs object to compare against
236 * @return true if the intrusive pointer points at the specified object
238 template<typename T, typename U> inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
240 return lhs.Get() == rhs;
244 * @brief Comparison overrides of objects wrapped by intrusive pointers.
246 * @param lhs intrusive pointer to compare with
247 * @param rhs intrusive pointer to compare against
248 * @return true if the intrusive pointer doesn't point at the specified object
250 template<typename T, typename U> inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
252 return lhs.Get() != rhs;
256 * @brief Comparison overrides of objects wrapped by intrusive pointers
258 * @param lhs object to compare with
259 * @param rhs intrusive pointer to compare against
260 * @return true if the intrusive pointer points at the specified object
262 template<typename T, typename U> inline bool operator==( T* lhs, IntrusivePtr<U>const& rhs )
264 return lhs == rhs.Get();
268 * @brief Comparison overrides of objects wrapped by intrusive pointers
270 * @param lhs object to compare with
271 * @param rhs intrusive pointer to compare against
272 * @return true if the intrusive pointer doesn't point at the specified object
274 template<typename T, typename U> inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
276 return lhs != rhs.Get();
280 * @brief Get pointer to reference counted object (Dali camel case variant)
282 * @param rhs intrusive pointer wrapping object
283 * @return pointer to object
285 template<typename T>inline T* GetPointer(IntrusivePtr<T> const& rhs)
291 * @brief Get pointer to reference counted object (boost:: naming convention)
293 * @param rhs intrusive pointer wrapping object
294 * @return pointer to object
296 template<typename T>inline T* get_pointer(IntrusivePtr<T> const& rhs)
303 #endif /* __DALI_INTRUSIVE_PTR_H__ */