387de484ed23a09d70d2531bd6a4fc47df8f9121
[platform/core/uifw/dali-core.git] / capi / dali / public-api / object / handle.h
1 #ifndef __DALI_HANDLE_H__
2 #define __DALI_HANDLE_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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  * @addtogroup CAPI_DALI_FRAMEWORK
22  * @{
23  */
24
25 // EXTERNAL INCLUDES
26 #include <string>
27
28 // INTERNAL INCLUDES
29 #include <dali/public-api/common/dali-common.h>
30 #include <dali/public-api/object/base-handle.h>
31 #include <dali/public-api/object/property-types.h>
32 #include <dali/public-api/object/property-value.h>
33 #include <dali/public-api/object/property-notification-declarations.h>
34 #include <dali/public-api/object/ref-object.h>
35
36 namespace Dali DALI_IMPORT_API
37 {
38
39 class PropertyNotification;
40 class PropertyCondition;
41
42 namespace Internal DALI_INTERNAL
43 {
44 class Object;
45 }
46
47 /**
48  * Dali::Handle is a handle to an internal property owning Dali object.
49  *
50  */
51 class Handle : public BaseHandle
52 {
53 public:
54
55   /**
56    * An Handle's capabilities can be queried using Handle::Supports()
57    */
58   enum Capability
59   {
60     /**
61      * Some objects support dynamic property creation at run-time.
62      * New properties are registered by calling RegisterProperty() with an unused property name.
63      */
64     DYNAMIC_PROPERTIES = 0x01,
65   };
66
67 public:
68
69   /**
70    * This constructor is used by Dali New() methods.
71    * @param [in] handle A pointer to a newly allocated Dali resource
72    */
73   Handle(Dali::Internal::Object* handle);
74
75   /**
76    * This constructor provides an uninitialized Dali::Handle.
77    * This should be initialized with a Dali New() method before use.
78    * Methods called on an uninitialized Dali::Handle will assert.
79    * @code
80    * Handle handle; // uninitialized
81    * handle.SomeMethod(); // unsafe! This will assert
82    *
83    * handle = SomeClass::New(); // now initialized
84    * handle.SomeMethod(); // safe
85    * @endcode
86    */
87   Handle();
88
89   /**
90    * Dali::Handle is intended as a base class
91    */
92   virtual ~Handle();
93
94   /**
95    * This copy constructor is required for (smart) pointer semantics
96    * @param [in] handle A reference to the copied handle
97    */
98   Handle(const Handle& handle);
99
100   /**
101    * This assignment operator is required for (smart) pointer semantics
102    * @param [in] rhs  A reference to the copied handle
103    */
104   Handle& operator=(const Handle& rhs);
105
106   /**
107    * @copydoc Dali::BaseHandle::operator=
108    */
109   using BaseHandle::operator=;
110
111   /**
112    * Downcast to a handle. If not the returned handle is left uninitialized.
113    * @param[in] handle to An object
114    * @return handle or an uninitialized handle
115    */
116   static Handle DownCast( BaseHandle handle );
117
118 public:
119
120   /**
121    * Query whether an handle supports a given capability.
122    * @param[in] capability The queried capability.
123    * @return True if the capability is supported.
124    */
125   bool Supports(Capability capability) const;
126
127   /**
128    * Query how many properties are provided by an handle.
129    * This may vary between instances of a class, if dynamic properties are supported.
130    * @return The number of properties.
131    */
132   unsigned int GetPropertyCount() const;
133
134   /**
135    * Query the name of a property.
136    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
137    * @param [in] index The index of the property.
138    * @return The name of the property.
139    */
140   const std::string& GetPropertyName(Property::Index index) const;
141
142   /**
143    * Query the index of a property.
144    * @param [in] name The name of the property.
145    * @return The index of the property, or Property::INVALID_INDEX if no property exists with the given name.
146    */
147   Property::Index GetPropertyIndex(std::string name) const;
148
149   /**
150    * Query whether a property can be set using SetProperty().
151    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
152    * @param [in] index The index of the property.
153    * @return True if the property is writable.
154    */
155   bool IsPropertyWritable(Property::Index index) const;
156
157   /**
158    * Query whether a writable property can be the target of an animation or constraint.
159    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
160    * @param [in] index The index of the property.
161    * @return True if the property is animatable.
162    */
163   bool IsPropertyAnimatable(Property::Index index) const;
164
165   /**
166    * Query the type of a property.
167    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
168    * @param [in] index The index of the property.
169    * @return The type of the property.
170    */
171   Property::Type GetPropertyType(Property::Index index) const;
172
173   /**
174    * Set the value of an existing property.
175    * @pre Handle::IsPropertyWritable(index) returns true.
176    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
177    * @pre The property types match i.e. propertyValue.GetType() is equal to GetPropertyType(index).
178    * @param [in] index The index of the property.
179    * @param [in] propertyValue The new value of the property.
180    */
181   void SetProperty(Property::Index index, Property::Value propertyValue);
182
183   /**
184    * Register a new property.
185    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
186    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
187    * @param [in] name The name of the property.
188    * @param [in] propertyValue The new value of the property.
189    */
190   Property::Index RegisterProperty(std::string name, Property::Value propertyValue);
191
192   /**
193    * Register a new property.
194    * Properties can be set as non animatable using property attributes.
195    * @pre The handle supports dynamic properties i.e. Supports(Handle::DYNAMIC_PROPERTIES) returns true.
196    * @pre name is unused i.e. GetPropertyIndex(name) returns PropertyIndex::INVALID.
197    * @param [in] name The name of the property.
198    * @param [in] propertyValue The new value of the property.
199    * @param [in] accessMode The property access mode (writable, animatable etc).
200    */
201   Property::Index RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode);
202
203   /**
204    * Retrieve a property value.
205    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
206    * @param [in] index The index of the property.
207    * @return The property value.
208    */
209   Property::Value GetProperty(Property::Index index) const;
210
211   /**
212    * Convenience function for obtaining a property of a known type.
213    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
214    * @pre The property types match i.e. PropertyTypes::Get<T>() is equal to GetPropertyType(index).
215    * @param [in] index The index of the property.
216    * @return The property value.
217    */
218   template <typename T>
219   T GetProperty(Property::Index index) const
220   {
221     Property::Value value = GetProperty(index);
222
223     return T( value.Get<T>() );
224   }
225
226   /**
227    * Add a property notification to this object.
228    * @pre Property::INVALID_INDEX < index < GetPropertyCount().
229    * @param [in] index The index of the property.
230    * @param [in] condition The notification will be triggered when this condition is satisfied.
231    */
232   PropertyNotification AddPropertyNotification(Property::Index index,
233                                                const PropertyCondition& condition);
234
235   /**
236    * Remove a property notification from this object.
237    * @param [in] propertyNotification The propertyNotification to be removed.
238    */
239   void RemovePropertyNotification(Dali::PropertyNotification propertyNotification);
240
241   /**
242    * Remove all property notifications from this object.
243    */
244   void RemovePropertyNotifications();
245
246 };
247
248 } // namespace Dali
249
250 /**
251  * @}
252  */
253 #endif // __DALI_HANDLE_H__