Ensure we use TransformManager const methods to retrieve values from const getter...
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / property-accessor.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_PROPERTY_ACCESSOR_H
2 #define DALI_INTERNAL_SCENE_GRAPH_PROPERTY_ACCESSOR_H
3
4 /*
5  * Copyright (c) 2020 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/common/dali-common.h>
23 #include <dali/internal/update/common/animatable-property.h>
24 #include <dali/internal/update/manager/transform-manager-property.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 /**
33  * A wrapper class for getting/setting a property.
34  * Animators use this instead of accessing properties directly.
35  */
36 template < typename PropertyType >
37 class PropertyAccessor
38 {
39 public:
40
41   /**
42    * Create a property component.
43    * @param [in] property The property to access.
44    */
45   PropertyAccessor( SceneGraph::PropertyBase* property )
46   : mProperty( static_cast< SceneGraph::AnimatableProperty<PropertyType>* >(property) ) // we know the type
47   {
48   }
49
50   /**
51    * Non-virtual destructor; PropertyAccessor is not suitable as a base class.
52    */
53   ~PropertyAccessor() = default;
54
55   /**
56    * Query whether the accessor is set.
57    * @return True if set.
58    */
59   bool IsSet() const
60   {
61     return mProperty != nullptr;
62   }
63
64   /**
65    * Reset the property accessor
66    * @post Calling any other PropertyAccessor is invalid.
67    */
68   void Reset()
69   {
70     mProperty = nullptr;
71   }
72
73   /**
74    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
75    */
76   bool IsClean() const
77   {
78     return mProperty->IsClean();
79   }
80
81   /**
82    * Read access to the property.
83    * @param [in] bufferIndex The current update buffer index.
84    */
85   const PropertyType& Get( BufferIndex bufferIndex ) const
86   {
87     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Get() mProperty was nullptr" );
88     const SceneGraph::AnimatableProperty<PropertyType>* property = mProperty;
89     return property->Get( bufferIndex );
90   }
91
92   /**
93    * @copydoc AnimatableProperty<float>::Set()
94    */
95   void Set( BufferIndex bufferIndex, const PropertyType& value ) const
96   {
97     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Set() mProperty was nullptr" );
98     mProperty->Set( bufferIndex, value );
99   }
100
101   /**
102    * @copydoc AnimatableProperty<float>::Bake()
103    */
104   void Bake( BufferIndex bufferIndex, const PropertyType& value ) const
105   {
106     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Bake() mProperty was nullptr" );
107     mProperty->Bake( bufferIndex, value );
108   }
109
110 private:
111
112   // Undefined
113   PropertyAccessor() = delete;
114   PropertyAccessor(const PropertyAccessor& property) = delete;
115   PropertyAccessor& operator=(const PropertyAccessor& rhs) = delete;
116
117 private:
118
119   SceneGraph::AnimatableProperty<PropertyType>* mProperty; ///< The real property
120
121 };
122
123 /**
124  * A wrapper class for getting/setting a transform manager property
125  * Animators use this instead of accessing properties directly.
126  */
127 template <typename T>
128 class TransformManagerPropertyAccessor
129 {
130 public:
131
132   /**
133    * Create a property component.
134    * @param [in] property The property to access.
135    */
136   TransformManagerPropertyAccessor( SceneGraph::PropertyBase* property )
137   : mProperty( static_cast< SceneGraph::TransformManagerPropertyHandler<T>* >(property) ) // we know the type
138   {
139   }
140
141   /**
142    * Non-virtual destructor; PropertyAccessor is not suitable as a base class.
143    */
144   ~TransformManagerPropertyAccessor() = default;
145
146   /**
147    * Query whether the accessor is set.
148    * @return True if set.
149    */
150   bool IsSet() const
151   {
152     return mProperty != nullptr;
153   }
154
155   /**
156    * Reset the property accessor
157    * @post Calling any other PropertyAccessor is invalid.
158    */
159   void Reset()
160   {
161     mProperty = nullptr;
162   }
163
164   /**
165    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
166    */
167   bool IsClean() const
168   {
169     return mProperty->IsClean();
170   }
171
172   /**
173    * Read access to the property.
174    * @param [in] bufferIndex The current update buffer index.
175    * @return The value of the property
176    */
177   const T& Get( BufferIndex bufferIndex ) const
178   {
179     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Get() mProperty was nullptr" );
180     const SceneGraph::TransformManagerPropertyHandler<T>* property = mProperty;
181     return property->Get( bufferIndex );
182   }
183
184   /**
185    * @copydoc AnimatableProperty<float>::Set()
186    */
187   void Set( BufferIndex bufferIndex, const T& value ) const
188   {
189     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Set() mProperty was nullptr" );
190     mProperty->Set( bufferIndex, value );
191   }
192
193   /**
194    * @copydoc AnimatableProperty<float>::Bake()
195    */
196   void Bake( BufferIndex bufferIndex, const T& value ) const
197   {
198     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Bake() mProperty was nullptr" );
199     mProperty->Bake( bufferIndex, value );
200   }
201
202 private:
203
204   // Undefined
205   TransformManagerPropertyAccessor() = delete;
206   TransformManagerPropertyAccessor(const TransformManagerPropertyAccessor& property) = delete;
207   TransformManagerPropertyAccessor& operator=(const TransformManagerPropertyAccessor& rhs) = delete;
208
209 private:
210
211   SceneGraph::TransformManagerPropertyHandler<T>* mProperty; ///< The real property
212
213 };
214
215 /**
216  * A wrapper class for getting/setting a transform manager property component
217  * Animators use this instead of accessing properties directly.
218  */
219 template <typename T, uint32_t COMPONENT>
220 class TransformManagerPropertyComponentAccessor
221 {
222 public:
223
224   /**
225    * Create a property component.
226    * @param [in] property The property to access.
227    */
228   TransformManagerPropertyComponentAccessor( SceneGraph::PropertyBase* property )
229   : mProperty( static_cast< SceneGraph::TransformManagerPropertyHandler<T>* >(property) ) // we know the type
230   {
231   }
232
233   /**
234    * Non-virtual destructor; PropertyAccessor is not suitable as a base class.
235    */
236   ~TransformManagerPropertyComponentAccessor() = default;
237
238   /**
239    * Query whether the accessor is set.
240    * @return True if set.
241    */
242   bool IsSet() const
243   {
244     return mProperty != nullptr;
245   }
246
247   /**
248    * Reset the property accessor
249    * @post Calling any other PropertyAccessor is invalid.
250    */
251   void Reset()
252   {
253     mProperty = nullptr;
254   }
255
256   /**
257    * @copydoc Dali::Internal::SceneGraph::PropertyBase::IsClean()
258    */
259   bool IsClean() const
260   {
261     return mProperty->IsClean();
262   }
263
264   /**
265    * Read access to the property.
266    * @param [in] bufferIndex The current update buffer index.
267    * @return The value of the component of the property
268    */
269   float Get( BufferIndex bufferIndex ) const
270   {
271     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Get() mProperty was nullptr" );
272     const SceneGraph::TransformManagerPropertyHandler<T>* property = mProperty;
273     return property->GetFloatComponent( COMPONENT );
274   }
275
276   /**
277    * @copydoc AnimatableProperty<float>::Set()
278    */
279   void Set( BufferIndex bufferIndex, float value ) const
280   {
281     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Set() mProperty was nullptr" );
282     mProperty->SetFloatComponent( value, COMPONENT );
283   }
284
285   /**
286    * @copydoc AnimatableProperty<float>::Bake()
287    */
288   void Bake( BufferIndex bufferIndex, float value ) const
289   {
290     DALI_ASSERT_DEBUG( nullptr != mProperty && "PropertyAccessor::Bake() mProperty was nullptr" );
291     mProperty->BakeFloatComponent( value, COMPONENT );
292   }
293
294 private:
295
296   // Undefined
297   TransformManagerPropertyComponentAccessor() = delete;
298   TransformManagerPropertyComponentAccessor(const TransformManagerPropertyComponentAccessor& property) = delete;
299   TransformManagerPropertyComponentAccessor& operator=(const TransformManagerPropertyComponentAccessor& rhs) = delete;
300
301 private:
302
303   SceneGraph::TransformManagerPropertyHandler<T>* mProperty; ///< The real property
304
305 };
306
307 } // namespace Internal
308
309 } // namespace Dali
310
311 #endif // DALI_INTERNAL_SCENE_GRAPH_PROPERTY_ACCESSOR_H