Merge "(Partial Update) Mark as not rendered if the node is transparent or culled...
[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) 2021 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 namespace Internal
27 {
28 template<typename T>
29 class OwnerPointer
30 {
31 public:
32   /**
33    * Default constructor. Creates an OwnerPointer that does not own any object.
34    * @note This does not protect against two different OwnerPointers pointing to the same object.
35    *       Both OwnerPointers will try to release the memory of the same object in that case which
36    *       could lead to a crash.
37    */
38   OwnerPointer()
39   : mObject(nullptr)
40   {
41   }
42
43   /**
44    * Constructor. Creates an OwnerPointer that owns the object.
45    * @param[in] object A pointer to a heap allocated object.
46    */
47   OwnerPointer(T* object)
48   : mObject(object)
49   {
50   }
51
52   /**
53    * Copy constructor. Passes the ownership of a pointer to another.
54    * @param[in] other The pointer that gives away the ownership.
55    */
56   OwnerPointer(const OwnerPointer& other)
57   : OwnerPointer(static_cast<OwnerPointer&&>(const_cast<OwnerPointer&>(other))) // Remove constness & cast to rvalue to use the move constructor
58   {
59     // other needs to be const for compiler to pick up this as copy constructor;
60     // though we are using this as move as there can only be one owner
61   }
62
63   /**
64    * Move constructor. Passes the ownership of a pointer to another.
65    * @param[in] other The pointer that gives away the ownership.
66    */
67   OwnerPointer(OwnerPointer&& other)
68   : mObject(nullptr)
69   {
70     Swap(other);
71   }
72
73   /**
74    * Assignment operator. Passes the ownership of a pointer to another.
75    * @param[in] other The pointer that gives away the ownership.
76    */
77   OwnerPointer& operator=(OwnerPointer& other)
78   {
79     if(this != &other) // no self-assignment
80     {
81       delete mObject;
82       mObject       = other.mObject;
83       other.mObject = nullptr;
84     }
85
86     // return self
87     return *this;
88   }
89
90   /**
91    * Move assignment operator. Passes the ownership of a pointer to another.
92    * @param[in] other The pointer that gives away the ownership.
93    */
94   OwnerPointer& operator=(OwnerPointer&& other)
95   {
96     // Reuse operator=
97     return operator=(other);
98   }
99
100   /**
101    * Assignment operator. Takes the ownership of the object.
102    * If it owns an object already, it will be deleted.
103    * @param[in] pointer A pointer to a heap allocated object.
104    */
105   OwnerPointer& operator=(T* pointer)
106   {
107     if(mObject != pointer)
108     {
109       Reset();
110       mObject = pointer;
111     }
112
113     return *this;
114   }
115
116   /**
117    * Destructor.
118    */
119   ~OwnerPointer()
120   {
121     Reset();
122   }
123
124   /**
125    * Indirection operator.
126    * @return a reference to the object.
127    */
128   T& operator*()
129   {
130     DALI_ASSERT_DEBUG(mObject);
131
132     return *mObject;
133   }
134
135   /**
136    * Const indirection operator.
137    * @return a reference to the object from const OwnerPointer.
138    */
139   T& operator*() const
140   {
141     DALI_ASSERT_DEBUG(mObject);
142
143     // Pointer semantics: A const pointer does not mean const data.
144     return const_cast<T&>(*mObject);
145   }
146
147   /**
148    * Pointer operator.
149    * @return a pointer to the object.
150    */
151   T* operator->()
152   {
153     return mObject;
154   }
155
156   /**
157    * Const pointer operator.
158    * @return a pointer to the object referenced by a const OwnerPointer.
159    */
160   T* operator->() const
161   {
162     // Pointer semantics: A const pointer does not mean const data.
163     return const_cast<T*>(mObject);
164   }
165
166   /**
167    * Compare with a raw pointer.
168    * @return true if the raw pointer matches the one owned by this object.
169    */
170   bool operator==(const T* pointer)
171   {
172     return (mObject == pointer);
173   }
174
175   /**
176    * Reset the pointer, deleting any owned object.
177    */
178   void Reset()
179   {
180     delete mObject;
181     mObject = nullptr;
182   }
183
184   /**
185    * Release the ownership, it does not delete the object.
186    * @return a pointer to the object.
187    */
188   T* Release()
189   {
190     T* tmp  = mObject;
191     mObject = nullptr;
192     return tmp;
193   }
194
195   /**
196    * Returns a const pointer to the object owned.
197    * @return a const pointer to the object.
198    */
199   const T* Get() const
200   {
201     return mObject;
202   }
203
204   /**
205    * Swap owned objects
206    * @param[in] other The pointer to swap the owned objects with.
207    */
208   void Swap(OwnerPointer& other)
209   {
210     if(this != &other)
211     {
212       T* tmp        = mObject;
213       mObject       = other.mObject;
214       other.mObject = tmp;
215     }
216   }
217
218   // Handle comparisons
219
220   /**
221    * Converts an object handle to a bool.
222    * This is useful for checking whether the handle is NULL.
223    */
224   explicit operator bool() const
225   {
226     return mObject != nullptr;
227   }
228
229 private:
230   // data
231   T* mObject; ///< Raw pointer to the object
232 };
233
234 } // namespace Internal
235
236 } // namespace Dali
237
238 #endif // DALI_INTERNAL_OWNER_POINTER_H