Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / common / owner-pointer.h
1 #ifndef __DALI_INTERNAL_OWNER_POINTER_H__
2 #define __DALI_INTERNAL_OWNER_POINTER_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/common/dali-common.h>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 template < typename T >
30 class OwnerPointer
31 {
32 public:
33   /**
34    * Default constructor. Creates an OwnerPointer that does not own any object.
35    */
36   OwnerPointer()
37   {
38     mObject = NULL;
39   }
40
41   /**
42    * Constructor. Creates an OwnerPointer that owns the object.
43    * @param[in] object A pointer to a heap allocated object.
44    */
45   OwnerPointer( T* object )
46   {
47     mObject = object;
48   }
49
50   /**
51    * Copy constructor. Passes the ownership of a pointer to another.
52    * @param[in] other The pointer that gives away the ownership.
53    */
54   OwnerPointer( OwnerPointer& other )
55   {
56     Init( other );
57   }
58
59   /**
60    * Assignment operator. Passes the ownership of a pointer to another.
61    * @param[in] other The pointer that gives away the ownership.
62    */
63   OwnerPointer& operator=( OwnerPointer& other )
64   {
65     if( this != &other )    // no self-assignment
66     {
67       Reset();
68       Init( other );
69     }
70
71     // return self
72     return *this;
73   }
74
75   /**
76    * Assignment operator. Takes the ownership of the object.
77    * If it owns an object already, it will be deleted.
78    * @param[in] pointer A pointer to a heap allocated object.
79    */
80   OwnerPointer& operator=( T* pointer )
81   {
82     if( mObject != pointer )
83     {
84       Reset();
85       mObject = pointer;
86     }
87
88     return *this;
89   }
90
91   /**
92    * Destructor.
93    */
94   ~OwnerPointer()
95   {
96     Reset();
97   }
98
99   /**
100    * Indirection operator.
101    * @return a reference to the object.
102    */
103   T& operator*()
104   {
105     DALI_ASSERT_DEBUG( mObject != NULL );
106
107     return *mObject;
108   }
109
110   /**
111    * Const indirection operator.
112    * @return a reference to the object from const OwnerPointer.
113    */
114   T& operator*() const
115   {
116     DALI_ASSERT_DEBUG( mObject != NULL );
117
118     // Pointer semantics: A const pointer does not mean const data.
119     return const_cast< T& >( *mObject );
120   }
121
122   /**
123    * Pointer operator.
124    * @return a pointer to the object.
125    */
126   T* operator->()
127   {
128     return mObject;
129   }
130
131   /**
132    * Const pointer operator.
133    * @return a pointer to the object referenced by a const OwnerPointer.
134    */
135   T* operator->() const
136   {
137     // Pointer semantics: A const pointer does not mean const data.
138     return const_cast< T* >( mObject );
139   }
140
141   /**
142    * Compare with a raw pointer.
143    * @return true if the raw pointer matches the one owned by this object.
144    */
145   bool operator==( const T* pointer )
146   {
147     return ( mObject == pointer );
148   }
149
150   /**
151    * Reset the pointer, deleting any owned object.
152    */
153   void Reset()
154   {
155     if ( mObject != NULL )
156     {
157       delete mObject;
158       mObject = NULL;
159     }
160   }
161
162   /**
163    * Release the ownership, it does not delete the object.
164    * @return a pointer to the object.
165    */
166   T* Release()
167   {
168     T* tmp = mObject;
169     mObject = NULL;
170     return tmp;
171   }
172
173   /**
174    * Returns a const pointer to the object owned.
175    * @return a const pointer to the object.
176    */
177   const T* Get()
178   {
179     return mObject;
180   }
181
182   // Handle comparisons - This is a variation of the safe bool idiom
183
184   /**
185    * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
186    */
187   typedef void (OwnerPointer::*BooleanType)() const;
188
189   /**
190    * Converts an object handle to a BooleanType.
191    * This is useful for checking whether the handle is NULL.
192    */
193   operator BooleanType() const
194   {
195     return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
196   }
197
198 private:
199
200   /**
201    * Used by the safe bool idiom.
202    */
203   void ThisIsSaferThanReturningVoidStar() const {}
204
205 private:
206
207   /**
208    * Initialise this pointer from another one.
209    * ownerPointer parameter looses ownership.
210    * @param ownerPointer owner pointer
211    */
212   void Init( OwnerPointer& ownerPointer )
213   {
214     mObject = ownerPointer.mObject;
215     ownerPointer.mObject = NULL;
216   }
217
218   // data
219   T* mObject; ///< Raw pointer to the object
220 };
221
222 } // namespace Internal
223
224 } // namespace Dali
225
226 #endif //__DALI_INTERNAL_OWNER_POINTER_H__