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