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