Seperate the API macros
[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) 2018 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/object/handle.h>
23 #include <dali/public-api/actors/custom-actor.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
43 public:
44
45   /**
46    * @brief Default constructor which provides an uninitialized Dali::WeakHandleBase.
47    * @SINCE_1_2.60
48    */
49   WeakHandleBase();
50
51   /**
52    * @brief This constructor creates a weak handle of the DALi object.
53    *
54    * @SINCE_1_2.60
55    * @param [in] handle A reference to the handle of the DALi object
56    */
57   WeakHandleBase( Handle& handle );
58
59   /**
60    * @brief Destructor to free resources.
61    * @SINCE_1_2.60
62    */
63   ~WeakHandleBase();
64
65   /**
66    * @brief Copy constructor.
67    *
68    * @SINCE_1_2.60
69    * @param [in] handle A reference to the copied WeakHandleBase
70    */
71   WeakHandleBase(const WeakHandleBase& handle);
72
73   /**
74    * @brief Assignment operator.
75    *
76    * It makes this WeakHandleBase point to the same internal DALi object as the copied WeakHandleBase
77    * @SINCE_1_2.60
78    * @param [in] rhs  A reference to the copied WeakHandleBase
79    * @return A reference to this WeakHandleBase
80    */
81   WeakHandleBase& operator=( const WeakHandleBase& rhs );
82
83   /**
84    * @brief Equality operator overload.
85    *
86    * @SINCE_1_2.60
87    * @param [in] rhs A reference to the compared WeakHandleBase
88    * @return true if the handle points to the same DALi resource, or if both are uninitialized
89    */
90   bool operator==(const WeakHandleBase& rhs) const;
91
92   /**
93    * @brief Inequality operator overload.
94    *
95    * @SINCE_1_2.60
96    * @param [in] rhs A reference to the compared WeakHandleBase
97    * @return true if the handle points to the different DALi resources
98    */
99   bool operator!=(const WeakHandleBase& rhs) const;
100
101   /**
102    * @brief Gets the handle to the DALi object.
103    *
104    * @SINCE_1_2.60
105    * @return The handle of the DALi object pointed by this WeakHandleBase or an empty handle if the DALi object doesn't exist
106    */
107   Handle GetBaseHandle() const;
108
109   /**
110    * @brief Resets this weak handle to not point to any DALi object
111    * @SINCE_1_2.60
112    */
113   void Reset();
114
115
116 protected:
117
118   /// @cond internal
119   struct Impl;
120   Impl* mImpl;
121   /// @endcond
122 };
123
124
125 /**
126  * @brief Weak handle for the given type of DALi object.
127  * @SINCE_1_2.60
128  * @see WeakHandleBase
129  */
130 template < class T >
131 class WeakHandle : public WeakHandleBase
132 {
133 public:
134
135   /**
136    * @copydoc Dali::WeakHandleBase::WeakHandleBase()
137    */
138   WeakHandle()
139   : WeakHandleBase()
140   {
141   }
142
143   /**
144    * @copydoc Dali::WeakHandleBase::WeakHandleBase(Handle&)
145    */
146   WeakHandle( T& handle )
147   : WeakHandleBase( handle )
148   {
149   }
150
151   /**
152    * @copydoc Dali::WeakHandleBase::~WeakHandleBase()
153    */
154   ~WeakHandle() {}
155
156   /**
157    * @copydoc Dali::WeakHandleBase::WeakHandleBase(const WeakHandleBase&)
158    */
159   WeakHandle(const WeakHandle& handle)
160   : WeakHandleBase( handle )
161   {
162   }
163
164   /**
165    * @copydoc Dali::WeakHandleBase::operator=()
166    */
167   WeakHandle& operator=( const WeakHandle& rhs )
168   {
169     WeakHandleBase::operator=(rhs);
170     return *this;
171   }
172
173   /**
174    * @copydoc Dali::WeakHandleBase::operator==()
175    */
176   bool operator==(const WeakHandle& rhs) const
177   {
178     return WeakHandleBase::operator==(rhs);
179   }
180
181   /**
182    * @copydoc Dali::WeakHandleBase::operator!=()
183    */
184   bool operator!=(const WeakHandle& rhs) const
185   {
186     return WeakHandleBase::operator!=(rhs);
187   }
188
189   /**
190    * @copydoc Dali::WeakHandleBase::GetBaseHandle()
191    */
192   T GetHandle() const
193   {
194     Handle handle( GetBaseHandle() );
195     if( handle )
196     {
197       return DownCast< T >( handle );
198     }
199     else
200     {
201       return T();
202     }
203   }
204 };
205
206 /**
207  * @}
208  */
209 } // namespace Dali
210
211 #endif // DALI_WEAK_HANDLE_H