[3.0] Add descriptions and example codes
[platform/core/uifw/dali-core.git] / dali / public-api / object / base-handle.h
1 #ifndef __DALI_BASE_HANDLE_H__
2 #define __DALI_BASE_HANDLE_H__
3
4 /*
5  * Copyright (c) 2015 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 <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/property-types.h>
27 #include <dali/public-api/object/property-value.h>
28 #include <dali/public-api/object/ref-object.h>
29 #include <dali/public-api/signals/functor-delegate.h>
30
31 namespace Dali
32 {
33 /**
34  * @addtogroup dali_core_object
35  * @{
36  */
37
38 class BaseObject;
39 class ConnectionTrackerInterface;
40 class TypeInfo;
41
42 /**
43  * @brief Dali::BaseHandle is a handle to an internal Dali resource.
44  *
45  * Each Dali handle consists of a single private pointer, and a set of non-virtual forwarding functions.
46  * This hides the internal implementation, so it may be modified without affecting the public interface.
47  *
48  * Dali handles have implicit smart-pointer semantics.
49  * This avoids the need to match resource allocation methods like new/delete (the RAII idiom).
50  *
51  * Dali handles can be copied by value.
52  * When a Dali handle is copied, both the copy and original will point to the same Dali resource.
53  *
54  * The internal Dali resources are reference counted. copying a Dali handle will increase the reference count.
55  * A resource will not be deleted until all its Dali::BaseHandle handles are destroyed, or reset.
56  *
57  * @SINCE_1_0.0
58  */
59 class DALI_IMPORT_API BaseHandle
60 {
61 public:
62
63   /**
64    * @brief This constructor is used by Dali New() methods.
65    *
66    * @SINCE_1_0.0
67    * @param [in] handle A pointer to a newly allocated Dali resource
68    */
69   BaseHandle(Dali::BaseObject* handle);
70
71   /**
72    * @brief This constructor provides an uninitialized Dali::BaseHandle.
73    *
74    * This should be initialized with a Dali New() method before use.
75    * Methods called on an uninitialized Dali::BaseHandle will assert.
76    * @code
77    * BaseHandle handle; // uninitialized
78    * handle.SomeMethod(); // unsafe! This will assert
79    *
80    * handle = SomeClass::New(); // now initialized
81    * handle.SomeMethod(); // safe
82    * @endcode
83    * @SINCE_1_0.0
84    */
85   BaseHandle();
86
87   /**
88    * @brief Dali::BaseHandle is intended as a base class
89    *
90    * This is non-virtual since derived BaseHandle types must not contain data.
91    * @SINCE_1_0.0
92    */
93   ~BaseHandle();
94
95   /**
96    * @brief This copy constructor is required for (smart) pointer semantics.
97    *
98    * @SINCE_1_0.0
99    * @param [in] handle A reference to the copied handle
100    */
101   BaseHandle(const BaseHandle& handle);
102
103   /**
104    * @brief This assignment operator is required for (smart) pointer semantics.
105    *
106    * It makes this handle use the same BaseObject as the copied handle
107    * @SINCE_1_0.0
108    * @param [in] rhs  A reference to the copied handle
109    * @return A reference to this handle
110    */
111   BaseHandle& operator=(const BaseHandle& rhs);
112
113   /**
114    * @brief Connects a void() functor to a specified signal.
115    *
116    * @SINCE_1_0.0
117    * @param [in] connectionTracker A connection tracker which can be used to disconnect.
118    * @param [in] signalName Name of the signal to connect to.
119    * @param [in] functor The functor to copy.
120    * @return True if the signal was available.
121    * @pre The signal must be available in this object.
122    */
123   template <class T>
124   bool ConnectSignal( ConnectionTrackerInterface* connectionTracker, const std::string& signalName, const T& functor )
125   {
126     return DoConnectSignal( connectionTracker, signalName, FunctorDelegate::New( functor ) );
127   }
128
129   /**
130    * @brief Perform action on this object with the given action name and attributes.
131    *
132    * Usage example: -
133    * @code
134    * BaseHandle handle = SomeClass::New(); // Initialized with New() method
135    *
136    * Property::Map attributes; // Use empty Property::Map because "show" action doesn't need parameter
137    * handle.DoAction("show", attributes);
138    * @endcode
139    * @SINCE_1_0.0
140    * @param [in] actionName The command for the action.
141    * @param [in] attributes The list of attributes for the action.
142    * @return The action is performed by the object or not.
143    */
144   bool DoAction(const std::string& actionName, const Property::Map& attributes);
145
146   /**
147    * @brief Returns the type name for the Handle.
148    *
149    * Will return an empty string if the typename does not exist. This will happen for types that
150    * have not registered with type-registry.
151    *
152    * @SINCE_1_0.0
153    * @return The type name. Empty string if the typename does not exist.
154    */
155   const std::string& GetTypeName() const;
156
157   /**
158    * @brief Returns the type info for the Handle.
159    *
160    * @SINCE_1_0.0
161    * @return The type info.
162    */
163   bool GetTypeInfo(Dali::TypeInfo& info) const;
164
165 public:
166
167   // BaseHandle accessors
168
169   /**
170    * @brief Retrieve the internal Dali resource.
171    *
172    * This is useful for checking the reference count of the internal resource.
173    * This method will not check the validity of the handle so the caller is expected to check it by using if (handle)
174    * @SINCE_1_0.0
175    * @return The BaseObject which is referenced by the BaseHandle.
176    */
177   BaseObject& GetBaseObject();
178
179   /**
180    * @brief Retrieve the internal Dali resource.
181    *
182    * This is useful for checking the reference count of the internal resource.
183    * This method will not check the validity of the handle so the caller is expected to check it by using if (handle)
184    * @SINCE_1_0.0
185    * @return The BaseObject which is referenced by the BaseHandle.
186    */
187   const BaseObject& GetBaseObject() const;
188
189   /**
190    * @brief Resets the handle.
191    *
192    * If no other handle copies exist, the internal Dali resouce will be deleted.
193    * Calling this is not required i.e. it will happen automatically when a Dali::BaseHandle is destroyed.
194    * @SINCE_1_0.0
195    */
196   void Reset();
197
198   // BaseHandle comparisons - This is a variation of the safe bool idiom
199
200   /**
201    * @brief Pointer-to-member type.
202    * Objects can be implicitly converted to this for validity checks.
203    */
204   typedef void (BaseHandle::*BooleanType)() const;
205
206   /**
207    * @brief Converts an handle to a BooleanType.
208    *
209    * This is useful for checking whether the handle is empty.
210    * @SINCE_1_0.0
211    */
212   operator BooleanType() const;
213
214   /**
215    * @brief Equality operator overload.
216    *
217    * @SINCE_1_0.0
218    * @param [in] rhs A reference to the compared handle.
219    * @return true if the handle handles point to the same Dali resource, or if both are NULL.
220    */
221   bool operator==(const BaseHandle& rhs) const;
222
223   /**
224    * @brief Inequality operator overload.
225    *
226    * @SINCE_1_0.0
227    * @param [in] rhs A reference to the compared handle.
228    * @return true if the handle handles point to the different Dali resources.
229    */
230   bool operator!=(const BaseHandle& rhs) const;
231
232   /**
233    * @brief Get the reference counted object pointer.
234    *
235    * @SINCE_1_0.0
236    * @return A pointer to the reference counted object.
237    */
238   Dali::RefObject* GetObjectPtr() const;
239
240 private:
241
242   /**
243    * @brief Not intended for application developers.
244    *
245    * @SINCE_1_0.0
246    * @param [in] connectionTracker A connection tracker which can be used to disconnect.
247    * @param [in] signalName Name of the signal to connect to.
248    * @param [in] functorDelegate A newly allocatated functor delegate (takes ownership).
249    * @return True if the signal was available.
250    */
251   bool DoConnectSignal( ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functorDelegate );
252
253 protected:
254
255   /**
256    * @brief Used by the safe bool idiom.
257    *
258    * The safe bool idiom basically provides a Boolean test for classes. It validates objects
259    * in a boolean context without the usual harmful side effects.
260    * @SINCE_1_0.0
261    */
262   void ThisIsSaferThanReturningVoidStar() const {}
263
264 private:
265
266   IntrusivePtr<Dali::RefObject> mObjectHandle; ///< Object this handle points at.
267
268 };
269
270 /**
271  * @brief Template wrapper to downcast a base object handle to derived class handle.
272  *
273  * @SINCE_1_0.0
274  * @param handle to a base object
275  * @return handle pointer to either a valid deriving handle or an uninitialized handle
276  * @pre The BaseHandle has been initialized.
277  */
278 template< class T >
279 inline T DownCast( BaseHandle handle )
280 {
281   return T::DownCast( handle );
282 }
283
284 // See also BaseHandle::BooleanType() conversion
285
286 /**
287  * @brief Equality operator
288  * @SINCE_1_0.0
289  */
290 template <typename T>
291 inline bool operator==(const BaseHandle& lhs, const T& rhs)
292 {
293   // We depart from the safe bool idiom to allow Dali::BaseHandle derived classes to be compared
294   return lhs == static_cast<const BaseHandle&>(rhs);
295 }
296
297 /**
298  * @brief Equality operator
299  * @SINCE_1_0.0
300  */
301 template <typename T>
302 inline bool operator!=(const BaseHandle& lhs, const T& rhs)
303 {
304   // We depart from the safe bool idiom to allow Dali::BaseHandle derived classes to be compared
305   return lhs != static_cast<const BaseHandle&>(rhs);
306 }
307
308 /**
309  * @brief Less than operator
310  * @SINCE_1_0.0
311  */
312 inline bool operator<(const BaseHandle& lhs, const BaseHandle& rhs)
313 {
314   return lhs.GetObjectPtr() < rhs.GetObjectPtr();
315 }
316
317 /**
318  * @}
319  */
320 } // namespace Dali
321
322 #endif // __DALI_BASE_HANDLE_H__