1 #ifndef __DALI_BASE_HANDLE_H__
2 #define __DALI_BASE_HANDLE_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
34 * @addtogroup dali_core_object
39 class ConnectionTrackerInterface;
43 * @brief Dali::BaseHandle is a handle to an internal Dali resource.
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.
48 * Dali handles have implicit smart-pointer semantics.
49 * This avoids the need to match resource allocation methods like new/delete (the RAII idiom).
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.
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.
59 class DALI_IMPORT_API BaseHandle
64 * @brief This constructor is used by Dali New() methods.
67 * @param [in] handle A pointer to a newly allocated Dali resource
69 BaseHandle(Dali::BaseObject* handle);
72 * @brief This constructor provides an uninitialized Dali::BaseHandle.
74 * This should be initialized with a Dali New() method before use.
75 * Methods called on an uninitialized Dali::BaseHandle will assert.
77 * BaseHandle handle; // uninitialized
78 * handle.SomeMethod(); // unsafe! This will assert
80 * handle = SomeClass::New(); // now initialized
81 * handle.SomeMethod(); // safe
88 * @brief Dali::BaseHandle is intended as a base class
90 * This is non-virtual since derived BaseHandle types must not contain data.
96 * @brief This copy constructor is required for (smart) pointer semantics.
99 * @param [in] handle A reference to the copied handle
101 BaseHandle(const BaseHandle& handle);
104 * @brief This assignment operator is required for (smart) pointer semantics.
106 * It makes this handle use the same BaseObject as the copied handle
108 * @param [in] rhs A reference to the copied handle
109 * @return A reference to this handle
111 BaseHandle& operator=(const BaseHandle& rhs);
114 * @brief Connects a void() functor to a specified signal.
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.
124 bool ConnectSignal( ConnectionTrackerInterface* connectionTracker, const std::string& signalName, const T& functor )
126 return DoConnectSignal( connectionTracker, signalName, FunctorDelegate::New( functor ) );
130 * @brief Perform action on this object with the given action name and attributes.
133 * @param [in] actionName The command for the action.
134 * @param [in] attributes The list of attributes for the action.
135 * @return The action is performed by the object or not.
137 bool DoAction(const std::string& actionName, const Property::Map& attributes);
140 * @brief Returns the type name for the Handle.
142 * Will return an empty string if the typename does not exist. This will happen for types that
143 * have not registered with type-registry.
146 * @return The type name. Empty string if the typename does not exist.
148 const std::string& GetTypeName() const;
151 * @brief Returns the type info for the Handle.
154 * @return The type info.
156 bool GetTypeInfo(Dali::TypeInfo& info) const;
160 // BaseHandle accessors
163 * @brief Retrieve the internal Dali resource.
165 * This is useful for checking the reference count of the internal resource.
166 * This method will not check the validity of the handle so the caller is expected to check it by using if (handle)
168 * @return The BaseObject which is referenced by the BaseHandle.
170 BaseObject& GetBaseObject();
173 * @brief Retrieve the internal Dali resource.
175 * This is useful for checking the reference count of the internal resource.
176 * This method will not check the validity of the handle so the caller is expected to check it by using if (handle)
178 * @return The BaseObject which is referenced by the BaseHandle.
180 const BaseObject& GetBaseObject() const;
183 * @brief Resets the handle.
185 * If no other handle copies exist, the internal Dali resouce will be deleted.
186 * Calling this is not required i.e. it will happen automatically when a Dali::BaseHandle is destroyed.
191 // BaseHandle comparisons - This is a variation of the safe bool idiom
194 * @brief Pointer-to-member type.
195 * Objects can be implicitly converted to this for validity checks.
197 typedef void (BaseHandle::*BooleanType)() const;
200 * @brief Converts an handle to a BooleanType.
202 * This is useful for checking whether the handle is empty.
205 operator BooleanType() const;
208 * @brief Equality operator overload.
211 * @param [in] rhs A reference to the compared handle.
212 * @return true if the handle handles point to the same Dali resource, or if both are NULL.
214 bool operator==(const BaseHandle& rhs) const;
217 * @brief Inequality operator overload.
220 * @param [in] rhs A reference to the compared handle.
221 * @return true if the handle handles point to the different Dali resources.
223 bool operator!=(const BaseHandle& rhs) const;
226 * @brief Get the reference counted object pointer.
229 * @return A pointer to the reference counted object.
231 Dali::RefObject* GetObjectPtr() const;
236 * @brief Not intended for application developers.
239 * @param [in] connectionTracker A connection tracker which can be used to disconnect.
240 * @param [in] signalName Name of the signal to connect to.
241 * @param [in] functorDelegate A newly allocatated functor delegate (takes ownership).
242 * @return True if the signal was available.
244 bool DoConnectSignal( ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functorDelegate );
249 * @brief Used by the safe bool idiom.
253 void ThisIsSaferThanReturningVoidStar() const {}
257 IntrusivePtr<Dali::RefObject> mObjectHandle; ///< Object this handle points at.
262 * @brief Template wrapper to downcast a base object handle to derived class handle.
265 * @param handle to a base object
266 * @return handle pointer to either a valid deriving handle or an uninitialized handle
267 * @pre The BaseHandle has been initialized.
270 inline T DownCast( BaseHandle handle )
272 return T::DownCast( handle );
275 // See also BaseHandle::BooleanType() conversion
278 * @brief Equality operator
281 template <typename T>
282 inline bool operator==(const BaseHandle& lhs, const T& rhs)
284 // We depart from the safe bool idiom to allow Dali::BaseHandle derived classes to be compared
285 return lhs == static_cast<const BaseHandle&>(rhs);
289 * @brief Equality operator
292 template <typename T>
293 inline bool operator!=(const BaseHandle& lhs, const T& rhs)
295 // We depart from the safe bool idiom to allow Dali::BaseHandle derived classes to be compared
296 return lhs != static_cast<const BaseHandle&>(rhs);
300 * @brief Less than operator
303 inline bool operator<(const BaseHandle& lhs, const BaseHandle& rhs)
305 return lhs.GetObjectPtr() < rhs.GetObjectPtr();
313 #endif // __DALI_BASE_HANDLE_H__