License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / capi / dali / public-api / common / intrusive-ptr.h
1 #ifndef __DALI_INTRUSIVE_PTR_H__
2 #define __DALI_INTRUSIVE_PTR_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 /**
22  * @addtogroup CAPI_DALI_OBJECT_MODULE
23  * @{
24  */
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/common/dali-common.h>
28
29 namespace Dali
30 {
31
32 /**
33  * @brief Templated class to emulate a sub-set of functions served by deprecating boost::intrusive_ptr.
34  *
35  * Uses the Dali:Refobject type supply actual reference counting
36  * The object is responsible for destroying itself
37  */
38 template<typename T>
39 class IntrusivePtr
40 {
41
42 public:
43
44   /**
45    * @brief Standard constructor to unassigned object.
46    */
47   IntrusivePtr() : mPtr( 0 ) {}
48
49   /**
50    * @brief Constructor to attach existing object.
51    *
52    * @param p pointer to object,
53    */
54   IntrusivePtr( T* p ) : mPtr( p )
55   {
56     if( mPtr )
57     {
58       mPtr->Reference();
59     }
60   }
61
62   /**
63    * @brief Copy constructor.
64    *
65    * @param rhs const reference to an IntrusivePtr
66    * @tparam U reference counter object type
67    */
68   template<typename U>
69   IntrusivePtr( IntrusivePtr<U> const& rhs ) : mPtr( rhs.Get() )
70   {
71     if( mPtr )
72     {
73       mPtr->Reference();
74     }
75   }
76
77   /**
78    * @brief Copy constructor.
79    */
80   IntrusivePtr( IntrusivePtr const& rhs ) : mPtr( rhs.mPtr )
81   {
82     if( mPtr )
83     {
84       mPtr->Reference();
85     }
86   }
87
88   /**
89    * @brief Destructor.
90    *
91    * Object will self-destruct if reference count is zero
92    */
93   ~IntrusivePtr()
94   {
95     if( mPtr )
96     {
97       mPtr->Unreference();
98     }
99   }
100
101   /**
102    * @brief Get pointer to reference counted object.
103    *
104    * @return pointer to reference counted object
105    */
106   T* Get() const
107   {
108     return mPtr;
109   }
110
111   /**
112    * @brief Pointer operator ovveride.
113    *
114    * @return pointer to reference counted object
115    */
116   T* operator->() const
117   {
118     return mPtr;
119   }
120
121   /**
122    * @brief Dereference operator override.
123    *
124    * @return reference to reference counted object
125    */
126   T& operator*() const
127   {
128     return *mPtr;
129   }
130
131   /**
132    * @brief Assignment operator.
133    *
134    * @param rhs const reference to intrusive pointer
135    * @return reference to reference counted object
136    */
137   IntrusivePtr& operator=( IntrusivePtr const& rhs )
138   {
139     IntrusivePtr( rhs ).Swap( *this );
140     return *this;
141   }
142
143   /**
144    * @brief Assignment operator.
145    *
146    * @param rhs pointer to object to wrap
147    * @return A reference to this object
148    */
149   IntrusivePtr& operator=( T* rhs )
150   {
151     IntrusivePtr( rhs ).Swap( *this );
152     return *this;
153   }
154
155   /**
156    * @brief Reset intrusive pointer.
157    */
158   void Reset()
159   {
160     IntrusivePtr().Swap( *this );
161   }
162
163   /**
164    * @brief Reset intrusive pointer with reference counted object.
165    *
166    * @param rhs pointer to object
167    */
168   void Reset( T* rhs )
169   {
170     IntrusivePtr( rhs ).Swap( *this );
171   }
172
173   // IntrusivePtr comparisons - This is a variation of the safe bool idiom
174
175   /**
176    * @brief Pointer-to-member type.
177    *
178    * Objects can be implicitly converted to this for validity checks.
179    */
180   typedef void (IntrusivePtr::*BooleanType)() const;
181
182   /**
183    * @brief Converts an object handle to a BooleanType.
184    *
185    * This is useful for checking whether the handle is NULL.
186    */
187   operator BooleanType() const
188   {
189     return mPtr ? &IntrusivePtr::ThisIsSaferThanReturningVoidStar : 0;
190   }
191
192 private:
193
194   /**
195    * @brief Used by the safe bool idiom.
196    */
197   void ThisIsSaferThanReturningVoidStar() const {}
198
199   /**
200    * @brief Internal swap function
201    */
202   void Swap( IntrusivePtr& rhs )
203   {
204     T* tmp = mPtr;
205     mPtr = rhs.mPtr;
206     rhs.mPtr = tmp;
207   }
208
209   T* mPtr;  ///< pointer to RefObject
210 };
211
212 /**
213  * @brief Comparison overrides of objects wrapped by intrusive pointers.
214  *
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
218  */
219 template<typename T, typename U> inline bool operator==( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const& rhs )
220 {
221   return lhs.Get() == rhs.Get();
222 }
223
224 /**
225  * @brief Comparison overrides of objects wrapped by intrusive pointers.
226  *
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
230  */
231 template<typename T, typename U> inline bool operator!=( IntrusivePtr<T>const& lhs, IntrusivePtr<U>const &rhs)
232 {
233   return lhs.Get() != rhs.Get();
234 }
235
236 /**
237  * @brief Comparison overrides of objects wrapped by intrusive pointers
238  *
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
242  */
243 template<typename T, typename U> inline bool operator==( IntrusivePtr<T>const& lhs, U* rhs )
244 {
245   return lhs.Get() == rhs;
246 }
247
248 /**
249  * @brief Comparison overrides of objects wrapped by intrusive pointers.
250  *
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
254  */
255 template<typename T, typename U> inline bool operator!=( IntrusivePtr<T>const& lhs, U* rhs )
256 {
257   return lhs.Get() != rhs;
258 }
259
260 /**
261  * @brief Comparison overrides of objects wrapped by intrusive pointers
262  *
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
266  */
267 template<typename T, typename U> inline bool operator==( T* lhs, IntrusivePtr<U>const& rhs )
268 {
269   return lhs == rhs.Get();
270 }
271
272 /**
273  * @brief Comparison overrides of objects wrapped by intrusive pointers
274  *
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
278  */
279 template<typename T, typename U> inline bool operator!=( T* lhs, IntrusivePtr<U>const& rhs )
280 {
281   return lhs != rhs.Get();
282 }
283
284 /**
285  * @brief Get pointer to reference counted object (Dali camel case variant)
286  *
287  * @param rhs intrusive pointer wrapping object
288  * @return pointer to object
289  */
290 template<typename T>inline T* GetPointer(IntrusivePtr<T> const& rhs)
291 {
292   return rhs.Get();
293 }
294
295 /**
296  * @brief Get pointer to reference counted object (boost:: naming convention)
297  *
298  * @param rhs intrusive pointer wrapping object
299  * @return pointer to object
300  */
301 template<typename T>inline T* get_pointer(IntrusivePtr<T> const& rhs)
302 {
303   return rhs.Get();
304 }
305
306 } // namespace Dali
307
308 /**
309  * @}
310  */
311 #endif /* __DALI_INTRUSIVE_PTR_H__ */