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