91a07952832b3d1c1be31e15aa0969e103f80aea
[platform/core/uifw/dali-core.git] / dali / public-api / object / weak-handle.h
1 #ifndef DALI_WEAK_HANDLE_H
2 #define DALI_WEAK_HANDLE_H
3
4 /*
5  * Copyright (c) 2020 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/actors/custom-actor.h>
23 #include <dali/public-api/object/base-handle.h>
24
25 namespace Dali
26 {
27 /**
28  * @addtogroup dali_core_object
29  * @{
30  */
31
32 /**
33  * @brief Base class to store a weak pointer to an internal DALi object.
34  *
35  * The handle to the object can be accessed if the object exists, and such access is not reference counted.
36  * When the object is deleted, the weak pointer will be set to NULL, and any further attempt to access to a
37  * deleted object will return an empty handle.
38  * @SINCE_1_2.60
39  */
40 class DALI_CORE_API WeakHandleBase
41 {
42 public:
43   /**
44    * @brief Default constructor which provides an uninitialized Dali::WeakHandleBase.
45    * @SINCE_1_2.60
46    */
47   WeakHandleBase();
48
49   /**
50    * @brief This constructor creates a weak handle of the DALi object.
51    *
52    * @SINCE_1_2.60
53    * @param [in] handle A reference to the handle of the DALi object
54    */
55   WeakHandleBase(BaseHandle& handle);
56
57   /**
58    * @brief Destructor to free resources.
59    * @SINCE_1_2.60
60    */
61   ~WeakHandleBase();
62
63   /**
64    * @brief Copy constructor.
65    *
66    * @SINCE_1_2.60
67    * @param [in] handle A reference to the copied WeakHandleBase
68    */
69   WeakHandleBase(const WeakHandleBase& handle);
70
71   /**
72    * @brief Assignment operator.
73    *
74    * It makes this WeakHandleBase point to the same internal DALi object as the copied WeakHandleBase
75    * @SINCE_1_2.60
76    * @param [in] rhs  A reference to the copied WeakHandleBase
77    * @return A reference to this WeakHandleBase
78    */
79   WeakHandleBase& operator=(const WeakHandleBase& rhs);
80
81   /**
82    * @brief Move constructor.
83    *
84    * @SINCE_1_9.22
85    * @param[in] rhs A reference to the moved handle
86    */
87   WeakHandleBase(WeakHandleBase&& rhs);
88
89   /**
90    * @brief Move assignment operator.
91    *
92    * @SINCE_1_9.22
93    * @param[in] rhs A reference to the moved handle
94    * @return A reference to this handle
95    */
96   WeakHandleBase& operator=(WeakHandleBase&& rhs);
97
98   /**
99    * @brief Equality operator overload.
100    *
101    * @SINCE_1_2.60
102    * @param [in] rhs A reference to the compared WeakHandleBase
103    * @return true if the handle points to the same DALi resource, or if both are uninitialized
104    */
105   bool operator==(const WeakHandleBase& rhs) const;
106
107   /**
108    * @brief Inequality operator overload.
109    *
110    * @SINCE_1_2.60
111    * @param [in] rhs A reference to the compared WeakHandleBase
112    * @return true if the handle points to the different DALi resources
113    */
114   bool operator!=(const WeakHandleBase& rhs) const;
115
116   /**
117    * @brief Gets the handle to the DALi object.
118    *
119    * @SINCE_1_2.60
120    * @return The handle of the DALi object pointed by this WeakHandleBase or an empty handle if the DALi object doesn't exist
121    */
122   BaseHandle GetBaseHandle() const;
123
124   /**
125    * @brief Resets this weak handle to not point to any DALi object
126    * @SINCE_1_2.60
127    */
128   void Reset();
129
130 protected:
131   /// @cond internal
132   struct Impl;
133   Impl* mImpl;
134   /// @endcond
135 };
136
137 /**
138  * @brief Weak handle for the given type of DALi object.
139  * @SINCE_1_2.60
140  * @see WeakHandleBase
141  */
142 template<class T>
143 class WeakHandle : public WeakHandleBase
144 {
145 public:
146   /**
147    * @copydoc Dali::WeakHandleBase::WeakHandleBase()
148    */
149   WeakHandle()
150   : WeakHandleBase()
151   {
152   }
153
154   /**
155    * @copydoc Dali::WeakHandleBase::WeakHandleBase( BaseHandle& )
156    */
157   WeakHandle(T& handle)
158   : WeakHandleBase(handle)
159   {
160   }
161
162   /**
163    * @copydoc Dali::WeakHandleBase::~WeakHandleBase()
164    */
165   ~WeakHandle()
166   {
167   }
168
169   /**
170    * @copydoc Dali::WeakHandleBase::WeakHandleBase(const WeakHandleBase&)
171    */
172   WeakHandle(const WeakHandle& handle)
173   : WeakHandleBase(handle)
174   {
175   }
176
177   /**
178    * @copydoc Dali::WeakHandleBase::operator=(const WeakHandleBase& rhs)
179    */
180   WeakHandle& operator=(const WeakHandle& rhs)
181   {
182     WeakHandleBase::operator=(rhs);
183     return *this;
184   }
185
186   /**
187    * @copydoc Dali::WeakHandleBase::WeakHandleBase(WeakHandleBase&& rhs)
188    */
189   WeakHandle(WeakHandle&& rhs) = default;
190
191   /**
192    * @copydoc Dali::WeakHandleBase::operator=(WeakHandleBase&& rhs)
193    */
194   WeakHandle& operator=(WeakHandle&& rhs) = default;
195
196   /**
197    * @copydoc Dali::WeakHandleBase::operator==()
198    */
199   bool operator==(const WeakHandle& rhs) const
200   {
201     return WeakHandleBase::operator==(rhs);
202   }
203
204   /**
205    * @copydoc Dali::WeakHandleBase::operator!=()
206    */
207   bool operator!=(const WeakHandle& rhs) const
208   {
209     return WeakHandleBase::operator!=(rhs);
210   }
211
212   /**
213    * @copydoc Dali::WeakHandleBase::GetBaseHandle()
214    */
215   T GetHandle() const
216   {
217     BaseHandle handle(WeakHandleBase::GetBaseHandle());
218     if(handle)
219     {
220       return DownCast<T>(handle);
221     }
222     else
223     {
224       return T();
225     }
226   }
227 };
228
229 /**
230  * @}
231  */
232 } // namespace Dali
233
234 #endif // DALI_WEAK_HANDLE_H