2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/public-api/object/property-value.h>
25 #include <dali/public-api/math/angle-axis.h>
26 #include <dali/public-api/math/radian.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/math/vector3.h>
29 #include <dali/public-api/math/vector4.h>
30 #include <dali/public-api/math/matrix3.h>
31 #include <dali/public-api/math/matrix.h>
32 #include <dali/public-api/math/rect.h>
33 #include <dali/public-api/math/quaternion.h>
34 #include <dali/public-api/object/property-map.h>
35 #include <dali/public-api/object/property-array.h>
36 #include <dali/public-api/object/property-types.h>
37 #include <dali/integration-api/debug.h>
45 * Helper to check if the property value can be read as int/bool
47 inline bool IsIntegerType( Property::Type type )
49 return ( Property::BOOLEAN == type )||( Property::INTEGER == type );
53 struct Property::Value::Impl
56 : type( Property::NONE ),
60 Impl( bool booleanValue )
61 : type( Property::BOOLEAN ),
62 integerValue( booleanValue )
65 Impl( float floatValue )
66 : type( Property::FLOAT ),
67 floatValue( floatValue )
70 Impl( int integerValue )
71 : type( Property::INTEGER ),
72 integerValue( integerValue )
75 Impl( const Vector2& vectorValue )
76 : type( Property::VECTOR2 ),
77 vector2Value( new Vector2( vectorValue ) )
80 Impl( const Vector3& vectorValue )
81 : type( Property::VECTOR3 ),
82 vector3Value( new Vector3( vectorValue ) )
85 Impl( const Vector4& vectorValue )
86 : type( Property::VECTOR4 ),
87 vector4Value( new Vector4( vectorValue ) )
90 Impl( const Matrix3& matrixValue )
91 : type( Property::MATRIX3 ),
92 matrix3Value( new Matrix3( matrixValue ) )
96 Impl( const Matrix& matrixValue )
97 : type( Property::MATRIX ),
98 matrixValue( new Matrix( matrixValue ) )
102 Impl( const AngleAxis& angleAxisValue )
103 : type( Property::ROTATION ),
104 angleAxisValue( new AngleAxis(angleAxisValue) )
108 Impl( const Quaternion& quaternionValue )
109 : type( Property::ROTATION ),
110 angleAxisValue( new AngleAxis() )
112 quaternionValue.ToAxisAngle( angleAxisValue->axis, angleAxisValue->angle );
115 Impl(const std::string& stringValue)
116 : type( Property::STRING ),
117 stringValue( new std::string( stringValue ) )
121 Impl( const Rect<int>& rectValue )
122 : type( Property::RECTANGLE ),
123 rectValue( new Rect<int>( rectValue ) )
127 Impl( const Property::Array& arrayValue )
128 : type( Property::ARRAY ),
129 arrayValue( new Property::Array( arrayValue ) )
133 Impl( const Property::Map& mapValue )
134 : type( Property::MAP ),
135 mapValue( new Property::Map( mapValue ) )
140 * Destructor, takes care of releasing the dynamically allocated types
146 case Property::NONE : // FALLTHROUGH
147 case Property::BOOLEAN : // FALLTHROUGH
148 case Property::FLOAT : // FALLTHROUGH
149 case Property::INTEGER :
151 break; // nothing to do
153 case Property::VECTOR2 :
158 case Property::VECTOR3:
163 case Property::VECTOR4:
168 case Property::MATRIX3:
173 case Property::MATRIX:
178 case Property::RECTANGLE:
183 case Property::ROTATION:
185 delete angleAxisValue;
188 case Property::STRING:
193 case Property::ARRAY:
213 // must use pointers for any class value pre c++ 11
214 Vector2* vector2Value;
215 Vector3* vector3Value;
216 Vector4* vector4Value;
217 Matrix3* matrix3Value;
219 AngleAxis* angleAxisValue;
220 std::string* stringValue;
221 Rect<int>* rectValue;
222 Property::Array* arrayValue;
223 Property::Map* mapValue;
227 Property::Value::Value()
232 Property::Value::Value( bool booleanValue )
233 : mImpl( new Impl( booleanValue ) )
237 Property::Value::Value( float floatValue )
238 : mImpl( new Impl( floatValue ) )
242 Property::Value::Value( int integerValue )
243 : mImpl( new Impl( integerValue ) )
247 Property::Value::Value( const Vector2& vectorValue )
248 : mImpl( new Impl( vectorValue ) )
252 Property::Value::Value( const Vector3& vectorValue )
253 : mImpl( new Impl( vectorValue ) )
257 Property::Value::Value( const Vector4& vectorValue )
258 : mImpl( new Impl( vectorValue ) )
262 Property::Value::Value( const Matrix3& matrixValue )
263 : mImpl( new Impl( matrixValue ) )
267 Property::Value::Value( const Matrix& matrixValue )
268 : mImpl( new Impl( matrixValue ) )
272 Property::Value::Value( const Rect<int>& rectValue )
273 : mImpl( new Impl( rectValue ) )
277 Property::Value::Value( const AngleAxis& angleAxisValue )
278 : mImpl( new Impl( angleAxisValue ) )
282 Property::Value::Value( const Quaternion& quaternionValue )
283 : mImpl( new Impl( quaternionValue ) )
287 Property::Value::Value( const std::string& stringValue )
288 : mImpl( new Impl( stringValue ) )
292 Property::Value::Value( const char* stringValue )
295 if( stringValue ) // string constructor is undefined with NULL pointer
297 mImpl = new Impl( std::string(stringValue) );
301 mImpl = new Impl( std::string() );
305 Property::Value::Value( Property::Array& arrayValue )
306 : mImpl( new Impl( arrayValue ) )
310 Property::Value::Value( Property::Map& mapValue )
311 : mImpl( new Impl( mapValue ) )
315 Property::Value::Value( Type type )
319 case Property::BOOLEAN:
321 mImpl = new Impl( false );
324 case Property::FLOAT:
326 mImpl = new Impl( 0.f );
329 case Property::INTEGER:
331 mImpl = new Impl( 0 );
334 case Property::VECTOR2:
336 mImpl = new Impl( Vector2::ZERO );
339 case Property::VECTOR3:
341 mImpl = new Impl( Vector3::ZERO );
344 case Property::VECTOR4:
346 mImpl = new Impl( Vector4::ZERO );
349 case Property::RECTANGLE:
351 mImpl = new Impl( Rect<int>(0,0,0,0) );
354 case Property::ROTATION:
356 mImpl = new Impl( AngleAxis() );
359 case Property::STRING:
361 mImpl = new Impl( std::string() );
364 case Property::MATRIX:
366 mImpl = new Impl( Matrix() );
369 case Property::MATRIX3:
371 mImpl = new Impl( Matrix3() );
374 case Property::ARRAY:
376 mImpl = new Impl( Property::Array() );
381 mImpl = new Impl( Property::Map() );
392 Property::Value::Value( const Property::Value& value )
395 // reuse assignment operator
399 Property::Value& Property::Value::operator=( const Property::Value& value )
401 if ( this == &value )
403 // skip self assignment
406 // if we are assigned an empty value, just drop impl
413 // first check if the type is the same, no need to change impl, just assign
414 if( mImpl && ( mImpl->type == value.mImpl->type ) )
416 switch( mImpl->type )
418 case Property::BOOLEAN:
420 mImpl->integerValue = value.mImpl->integerValue;
423 case Property::FLOAT:
425 mImpl->floatValue = value.mImpl->floatValue;
428 case Property::INTEGER:
430 mImpl->integerValue = value.mImpl->integerValue;
433 case Property::VECTOR2:
435 *mImpl->vector2Value = *value.mImpl->vector2Value; // type cannot change in mImpl so vector is allocated
438 case Property::VECTOR3:
440 *mImpl->vector3Value = *value.mImpl->vector3Value; // type cannot change in mImpl so vector is allocated
443 case Property::VECTOR4:
445 *mImpl->vector4Value = *value.mImpl->vector4Value; // type cannot change in mImpl so vector is allocated
448 case Property::RECTANGLE:
450 *mImpl->rectValue = *value.mImpl->rectValue; // type cannot change in mImpl so rect is allocated
453 case Property::ROTATION:
455 *mImpl->angleAxisValue = *value.mImpl->angleAxisValue; // type cannot change in mImpl so quaternion is allocated
458 case Property::STRING:
460 *mImpl->stringValue = *value.mImpl->stringValue; // type cannot change in mImpl so string is allocated
463 case Property::MATRIX:
465 *mImpl->matrixValue = *value.mImpl->matrixValue; // type cannot change in mImpl so matrix is allocated
468 case Property::MATRIX3:
470 *mImpl->matrix3Value = *value.mImpl->matrix3Value; // type cannot change in mImpl so matrix is allocated
473 case Property::ARRAY:
475 *mImpl->arrayValue = *value.mImpl->arrayValue; // type cannot change in mImpl so array is allocated
480 *mImpl->mapValue = *value.mImpl->mapValue; // type cannot change in mImpl so map is allocated
484 { // mImpl will be NULL, there's no way to get to this case
490 // different type, release old impl and create new
491 Impl* newImpl( NULL );
492 switch ( value.mImpl->type )
494 case Property::BOOLEAN:
496 newImpl = new Impl( bool( value.mImpl->integerValue ) );
499 case Property::FLOAT:
501 newImpl = new Impl( value.mImpl->floatValue );
504 case Property::INTEGER:
506 newImpl = new Impl( value.mImpl->integerValue );
509 case Property::VECTOR2:
511 newImpl = new Impl( *value.mImpl->vector2Value ); // type cannot change in mImpl so vector is allocated
514 case Property::VECTOR3:
516 newImpl = new Impl( *value.mImpl->vector3Value ); // type cannot change in mImpl so vector is allocated
519 case Property::VECTOR4:
521 newImpl = new Impl( *value.mImpl->vector4Value ); // type cannot change in mImpl so vector is allocated
524 case Property::RECTANGLE:
526 newImpl = new Impl( *value.mImpl->rectValue ); // type cannot change in mImpl so rect is allocated
529 case Property::ROTATION:
531 newImpl = new Impl( *value.mImpl->angleAxisValue ); // type cannot change in mImpl so quaternion is allocated
534 case Property::MATRIX3:
536 newImpl = new Impl( *value.mImpl->matrix3Value ); // type cannot change in mImpl so matrix is allocated
539 case Property::MATRIX:
541 newImpl = new Impl( *value.mImpl->matrixValue ); // type cannot change in mImpl so matrix is allocated
544 case Property::STRING:
546 newImpl = new Impl( *value.mImpl->stringValue ); // type cannot change in mImpl so string is allocated
549 case Property::ARRAY:
551 newImpl = new Impl( *value.mImpl->arrayValue ); // type cannot change in mImpl so array is allocated
556 newImpl = new Impl( *value.mImpl->mapValue ); // type cannot change in mImpl so map is allocated
560 { // NULL value will be used for "empty" value
570 Property::Value::~Value()
575 Property::Type Property::Value::GetType() const
577 Property::Type type( Property::NONE );
585 bool Property::Value::Get( bool& booleanValue ) const
587 bool converted = false;
588 if( mImpl && IsIntegerType( mImpl->type ) )
590 booleanValue = mImpl->integerValue;
596 bool Property::Value::Get( float& floatValue ) const
598 bool converted = false;
601 if( mImpl->type == FLOAT )
603 floatValue = mImpl->floatValue;
606 else if( IsIntegerType( mImpl->type ) )
608 floatValue = static_cast< float >( mImpl->integerValue );
615 bool Property::Value::Get( int& integerValue ) const
617 bool converted = false;
620 if( IsIntegerType( mImpl->type ) )
622 integerValue = mImpl->integerValue;
625 else if( mImpl->type == FLOAT )
627 integerValue = static_cast< int >( mImpl->floatValue );
634 bool Property::Value::Get( Vector2& vectorValue ) const
636 bool converted = false;
639 // type cannot change in mImpl so vector is allocated
640 if( mImpl->type == VECTOR2 || mImpl->type == VECTOR3 || mImpl->type == VECTOR4 )
642 vectorValue = *(mImpl->vector2Value); // if Vector3 or 4 only x and y are assigned
649 bool Property::Value::Get( Vector3& vectorValue ) const
651 bool converted = false;
654 // type cannot change in mImpl so vector is allocated
655 if ( mImpl->type == VECTOR3 || mImpl->type == VECTOR4 )
657 vectorValue = *(mImpl->vector3Value); // if Vector4 only x,y,z are assigned
660 else if( mImpl->type == VECTOR2 )
662 vectorValue = *(mImpl->vector2Value);
669 bool Property::Value::Get( Vector4& vectorValue ) const
671 bool converted = false;
674 if( mImpl->type == VECTOR4 ) // type cannot change in mImpl so vector is allocated
676 vectorValue = *(mImpl->vector4Value);
679 else if( mImpl->type == VECTOR2 )
681 vectorValue = *(mImpl->vector2Value);
684 else if( mImpl->type == VECTOR3 )
686 vectorValue = *(mImpl->vector3Value);
693 bool Property::Value::Get( Matrix3& matrixValue ) const
695 bool converted = false;
696 if( mImpl && (mImpl->type == MATRIX3) ) // type cannot change in mImpl so matrix is allocated
698 matrixValue = *(mImpl->matrix3Value);
704 bool Property::Value::Get( Matrix& matrixValue ) const
706 bool converted = false;
707 if( mImpl && (mImpl->type == MATRIX) ) // type cannot change in mImpl so matrix is allocated
709 matrixValue = *(mImpl->matrixValue);
715 bool Property::Value::Get( Rect<int>& rectValue ) const
717 bool converted = false;
718 if( mImpl && (mImpl->type == RECTANGLE) ) // type cannot change in mImpl so rect is allocated
720 rectValue = *(mImpl->rectValue);
726 bool Property::Value::Get( AngleAxis& angleAxisValue ) const
728 bool converted = false;
729 if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so angleAxis is allocated
731 angleAxisValue = *(mImpl->angleAxisValue);
737 bool Property::Value::Get( Quaternion& quaternionValue ) const
739 bool converted = false;
740 if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so angleAxis is allocated
742 quaternionValue = Quaternion(mImpl->angleAxisValue->angle, mImpl->angleAxisValue->axis );
748 bool Property::Value::Get( std::string& stringValue ) const
750 bool converted = false;
751 if( mImpl && (mImpl->type == STRING) ) // type cannot change in mImpl so string is allocated
753 stringValue.assign( *(mImpl->stringValue) );
759 bool Property::Value::Get( Property::Array& arrayValue ) const
761 bool converted = false;
762 if( mImpl && (mImpl->type == ARRAY) ) // type cannot change in mImpl so array is allocated
764 arrayValue = *(mImpl->arrayValue);
770 bool Property::Value::Get( Property::Map& mapValue ) const
772 bool converted = false;
773 if( mImpl && (mImpl->type == MAP) ) // type cannot change in mImpl so map is allocated
775 mapValue = *(mImpl->mapValue);
781 Property::Array* Property::Value::GetArray() const
783 Property::Array* array = NULL;
784 if( mImpl && (mImpl->type == ARRAY) ) // type cannot change in mImpl so array is allocated
786 array = mImpl->arrayValue;
791 Property::Map* Property::Value::GetMap() const
793 Property::Map* map = NULL;
794 if( mImpl && (mImpl->type == MAP) ) // type cannot change in mImpl so map is allocated
796 map = mImpl->mapValue;
801 std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
805 const Property::Value::Impl& impl( *value.mImpl );
809 case Dali::Property::BOOLEAN:
811 stream << impl.integerValue;
814 case Dali::Property::FLOAT:
816 stream << impl.floatValue;
819 case Dali::Property::INTEGER:
821 stream << impl.integerValue;
824 case Dali::Property::VECTOR2:
826 stream << *impl.vector2Value;
829 case Dali::Property::VECTOR3:
831 stream << *impl.vector3Value;
834 case Dali::Property::VECTOR4:
836 stream << *impl.vector4Value;
839 case Dali::Property::MATRIX3:
841 stream << *impl.matrix3Value;
844 case Dali::Property::MATRIX:
846 stream << *impl.matrixValue;
849 case Dali::Property::RECTANGLE:
851 stream << *impl.rectValue;
854 case Dali::Property::ROTATION:
856 stream << *impl.angleAxisValue;
859 case Dali::Property::STRING:
861 stream << *impl.stringValue;
864 case Dali::Property::ARRAY:
866 stream << *(value.GetArray());
869 case Dali::Property::MAP:
871 stream << *(value.GetMap());
874 case Dali::Property::NONE:
876 stream << "undefined type";
883 stream << "empty type";