Merge "Valgrind detected TextureSet leak and invalid access" into devel/master
[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   : mObject(NULL)
42   {
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   : mObject(object)
51   {
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   : mObject(NULL)
60   {
61     Swap( other );
62   }
63
64   /**
65    * Assignment operator. Passes the ownership of a pointer to another.
66    * @param[in] other The pointer that gives away the ownership.
67    */
68   OwnerPointer& operator=( OwnerPointer& other )
69   {
70     if( this != &other )    // no self-assignment
71     {
72       // Creation of temportaty object to prevent premature deletion of object
73       OwnerPointer(other).Swap(*this);
74     }
75
76     // return self
77     return *this;
78   }
79
80   /**
81    * Assignment operator. Takes the ownership of the object.
82    * If it owns an object already, it will be deleted.
83    * @param[in] pointer A pointer to a heap allocated object.
84    */
85   OwnerPointer& operator=( T* pointer )
86   {
87     if( mObject != pointer )
88     {
89       Reset();
90       mObject = pointer;
91     }
92
93     return *this;
94   }
95
96   /**
97    * Destructor.
98    */
99   ~OwnerPointer()
100   {
101     Reset();
102   }
103
104   /**
105    * Indirection operator.
106    * @return a reference to the object.
107    */
108   T& operator*()
109   {
110     DALI_ASSERT_DEBUG( mObject != NULL );
111
112     return *mObject;
113   }
114
115   /**
116    * Const indirection operator.
117    * @return a reference to the object from const OwnerPointer.
118    */
119   T& operator*() const
120   {
121     DALI_ASSERT_DEBUG( mObject != NULL );
122
123     // Pointer semantics: A const pointer does not mean const data.
124     return const_cast< T& >( *mObject );
125   }
126
127   /**
128    * Pointer operator.
129    * @return a pointer to the object.
130    */
131   T* operator->()
132   {
133     return mObject;
134   }
135
136   /**
137    * Const pointer operator.
138    * @return a pointer to the object referenced by a const OwnerPointer.
139    */
140   T* operator->() const
141   {
142     // Pointer semantics: A const pointer does not mean const data.
143     return const_cast< T* >( mObject );
144   }
145
146   /**
147    * Compare with a raw pointer.
148    * @return true if the raw pointer matches the one owned by this object.
149    */
150   bool operator==( const T* pointer )
151   {
152     return ( mObject == pointer );
153   }
154
155   /**
156    * Reset the pointer, deleting any owned object.
157    */
158   void Reset()
159   {
160     if ( mObject != NULL )
161     {
162       delete mObject;
163       mObject = NULL;
164     }
165   }
166
167   /**
168    * Release the ownership, it does not delete the object.
169    * @return a pointer to the object.
170    */
171   T* Release()
172   {
173     T* tmp = mObject;
174     mObject = NULL;
175     return tmp;
176   }
177
178   /**
179    * Returns a const pointer to the object owned.
180    * @return a const pointer to the object.
181    */
182   const T* Get() const
183   {
184     return mObject;
185   }
186
187   /**
188    * Swap owned objects
189    */
190   void Swap( OwnerPointer& other )
191   {
192     T* tmp = mObject;
193     mObject = other.mObject;
194     other.mObject = tmp;
195   }
196
197   // Handle comparisons - This is a variation of the safe bool idiom
198
199   /**
200    * Pointer-to-member type. Objects can be implicitly converted to this for validity checks.
201    */
202   typedef void (OwnerPointer::*BooleanType)() const;
203
204   /**
205    * Converts an object handle to a BooleanType.
206    * This is useful for checking whether the handle is NULL.
207    */
208   operator BooleanType() const
209   {
210     return (mObject != NULL) ? &OwnerPointer::ThisIsSaferThanReturningVoidStar : NULL;
211   }
212
213 private:
214
215   /**
216    * Used by the safe bool idiom.
217    */
218   void ThisIsSaferThanReturningVoidStar() const {}
219
220   // data
221   T* mObject; ///< Raw pointer to the object
222 };
223
224 } // namespace Internal
225
226 } // namespace Dali
227
228 #endif //__DALI_INTERNAL_OWNER_POINTER_H__