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