2 * Copyright (c) 2015 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 quaternionValue( new Quaternion( angleAxisValue.angle, angleAxisValue.axis ) )
108 Impl( const Quaternion& quaternionValue )
109 : type( Property::ROTATION ),
110 quaternionValue( new Quaternion( quaternionValue ) )
114 Impl(const std::string& stringValue)
115 : type( Property::STRING ),
116 stringValue( new std::string( stringValue ) )
120 Impl( const Rect<int>& rectValue )
121 : type( Property::RECTANGLE ),
122 rectValue( new Rect<int>( rectValue ) )
126 Impl( const Property::Array& arrayValue )
127 : type( Property::ARRAY ),
128 arrayValue( new Property::Array( arrayValue ) )
132 Impl( const Property::Map& mapValue )
133 : type( Property::MAP ),
134 mapValue( new Property::Map( mapValue ) )
139 * Destructor, takes care of releasing the dynamically allocated types
145 case Property::NONE : // FALLTHROUGH
146 case Property::BOOLEAN : // FALLTHROUGH
147 case Property::FLOAT : // FALLTHROUGH
148 case Property::INTEGER :
150 break; // nothing to do
152 case Property::VECTOR2 :
157 case Property::VECTOR3:
162 case Property::VECTOR4:
167 case Property::MATRIX3:
172 case Property::MATRIX:
177 case Property::RECTANGLE:
182 case Property::ROTATION:
184 delete quaternionValue;
187 case Property::STRING:
192 case Property::ARRAY:
212 // must use pointers for any class value pre c++ 11
213 Vector2* vector2Value;
214 Vector3* vector3Value;
215 Vector4* vector4Value;
216 Matrix3* matrix3Value;
218 Quaternion* quaternionValue;
219 std::string* stringValue;
220 Rect<int>* rectValue;
221 Property::Array* arrayValue;
222 Property::Map* mapValue;
226 Property::Value::Value()
231 Property::Value::Value( bool booleanValue )
232 : mImpl( new Impl( booleanValue ) )
236 Property::Value::Value( float floatValue )
237 : mImpl( new Impl( floatValue ) )
241 Property::Value::Value( int integerValue )
242 : mImpl( new Impl( integerValue ) )
246 Property::Value::Value( const Vector2& vectorValue )
247 : mImpl( new Impl( vectorValue ) )
251 Property::Value::Value( const Vector3& vectorValue )
252 : mImpl( new Impl( vectorValue ) )
256 Property::Value::Value( const Vector4& vectorValue )
257 : mImpl( new Impl( vectorValue ) )
261 Property::Value::Value( const Matrix3& matrixValue )
262 : mImpl( new Impl( matrixValue ) )
266 Property::Value::Value( const Matrix& matrixValue )
267 : mImpl( new Impl( matrixValue ) )
271 Property::Value::Value( const Rect<int>& rectValue )
272 : mImpl( new Impl( rectValue ) )
276 Property::Value::Value( const AngleAxis& angleAxisValue )
277 : mImpl( new Impl( angleAxisValue ) )
281 Property::Value::Value( const Quaternion& quaternionValue )
282 : mImpl( new Impl( quaternionValue ) )
286 Property::Value::Value( const std::string& stringValue )
287 : mImpl( new Impl( stringValue ) )
291 Property::Value::Value( const char* stringValue )
294 if( stringValue ) // string constructor is undefined with NULL pointer
296 mImpl = new Impl( std::string(stringValue) );
300 mImpl = new Impl( std::string() );
304 Property::Value::Value( Property::Array& arrayValue )
305 : mImpl( new Impl( arrayValue ) )
309 Property::Value::Value( Property::Map& mapValue )
310 : mImpl( new Impl( mapValue ) )
314 Property::Value::Value( Type type )
318 case Property::BOOLEAN:
320 mImpl = new Impl( false );
323 case Property::FLOAT:
325 mImpl = new Impl( 0.f );
328 case Property::INTEGER:
330 mImpl = new Impl( 0 );
333 case Property::VECTOR2:
335 mImpl = new Impl( Vector2::ZERO );
338 case Property::VECTOR3:
340 mImpl = new Impl( Vector3::ZERO );
343 case Property::VECTOR4:
345 mImpl = new Impl( Vector4::ZERO );
348 case Property::RECTANGLE:
350 mImpl = new Impl( Rect<int>(0,0,0,0) );
353 case Property::ROTATION:
355 mImpl = new Impl( Quaternion() );
358 case Property::STRING:
360 mImpl = new Impl( std::string() );
363 case Property::MATRIX:
365 mImpl = new Impl( Matrix() );
368 case Property::MATRIX3:
370 mImpl = new Impl( Matrix3() );
373 case Property::ARRAY:
375 mImpl = new Impl( Property::Array() );
380 mImpl = new Impl( Property::Map() );
390 Property::Value::Value( const Property::Value& value )
393 // reuse assignment operator
397 Property::Value& Property::Value::operator=( const Property::Value& value )
399 if ( this == &value )
401 // skip self assignment
404 // if we are assigned an empty value, just drop impl
411 // first check if the type is the same, no need to change impl, just assign
412 if( mImpl && ( mImpl->type == value.mImpl->type ) )
414 switch( mImpl->type )
416 case Property::BOOLEAN:
418 mImpl->integerValue = value.mImpl->integerValue;
421 case Property::FLOAT:
423 mImpl->floatValue = value.mImpl->floatValue;
426 case Property::INTEGER:
428 mImpl->integerValue = value.mImpl->integerValue;
431 case Property::VECTOR2:
433 *mImpl->vector2Value = *value.mImpl->vector2Value; // type cannot change in mImpl so vector is allocated
436 case Property::VECTOR3:
438 *mImpl->vector3Value = *value.mImpl->vector3Value; // type cannot change in mImpl so vector is allocated
441 case Property::VECTOR4:
443 *mImpl->vector4Value = *value.mImpl->vector4Value; // type cannot change in mImpl so vector is allocated
446 case Property::RECTANGLE:
448 *mImpl->rectValue = *value.mImpl->rectValue; // type cannot change in mImpl so rect is allocated
451 case Property::ROTATION:
453 *mImpl->quaternionValue = *value.mImpl->quaternionValue; // type cannot change in mImpl so quaternion is allocated
456 case Property::STRING:
458 *mImpl->stringValue = *value.mImpl->stringValue; // type cannot change in mImpl so string is allocated
461 case Property::MATRIX:
463 *mImpl->matrixValue = *value.mImpl->matrixValue; // type cannot change in mImpl so matrix is allocated
466 case Property::MATRIX3:
468 *mImpl->matrix3Value = *value.mImpl->matrix3Value; // type cannot change in mImpl so matrix is allocated
471 case Property::ARRAY:
473 *mImpl->arrayValue = *value.mImpl->arrayValue; // type cannot change in mImpl so array is allocated
478 *mImpl->mapValue = *value.mImpl->mapValue; // type cannot change in mImpl so map is allocated
482 { // mImpl will be NULL, there's no way to get to this case
488 // different type, release old impl and create new
489 Impl* newImpl( NULL );
490 switch ( value.mImpl->type )
492 case Property::BOOLEAN:
494 newImpl = new Impl( bool( value.mImpl->integerValue ) );
497 case Property::FLOAT:
499 newImpl = new Impl( value.mImpl->floatValue );
502 case Property::INTEGER:
504 newImpl = new Impl( value.mImpl->integerValue );
507 case Property::VECTOR2:
509 newImpl = new Impl( *value.mImpl->vector2Value ); // type cannot change in mImpl so vector is allocated
512 case Property::VECTOR3:
514 newImpl = new Impl( *value.mImpl->vector3Value ); // type cannot change in mImpl so vector is allocated
517 case Property::VECTOR4:
519 newImpl = new Impl( *value.mImpl->vector4Value ); // type cannot change in mImpl so vector is allocated
522 case Property::RECTANGLE:
524 newImpl = new Impl( *value.mImpl->rectValue ); // type cannot change in mImpl so rect is allocated
527 case Property::ROTATION:
529 newImpl = new Impl( *value.mImpl->quaternionValue ); // type cannot change in mImpl so quaternion is allocated
532 case Property::MATRIX3:
534 newImpl = new Impl( *value.mImpl->matrix3Value ); // type cannot change in mImpl so matrix is allocated
537 case Property::MATRIX:
539 newImpl = new Impl( *value.mImpl->matrixValue ); // type cannot change in mImpl so matrix is allocated
542 case Property::STRING:
544 newImpl = new Impl( *value.mImpl->stringValue ); // type cannot change in mImpl so string is allocated
547 case Property::ARRAY:
549 newImpl = new Impl( *value.mImpl->arrayValue ); // type cannot change in mImpl so array is allocated
554 newImpl = new Impl( *value.mImpl->mapValue ); // type cannot change in mImpl so map is allocated
558 { // NULL value will be used for "empty" value
568 Property::Value::~Value()
573 Property::Type Property::Value::GetType() const
575 Property::Type type( Property::NONE );
583 bool Property::Value::Get( bool& booleanValue ) const
585 bool converted = false;
586 if( mImpl && IsIntegerType( mImpl->type ) )
588 booleanValue = mImpl->integerValue;
594 bool Property::Value::Get( float& floatValue ) const
596 bool converted = false;
599 if( mImpl->type == FLOAT )
601 floatValue = mImpl->floatValue;
604 else if( IsIntegerType( mImpl->type ) )
606 floatValue = static_cast< float >( mImpl->integerValue );
613 bool Property::Value::Get( int& integerValue ) const
615 bool converted = false;
618 if( IsIntegerType( mImpl->type ) )
620 integerValue = mImpl->integerValue;
623 else if( mImpl->type == FLOAT )
625 integerValue = static_cast< int >( mImpl->floatValue );
632 bool Property::Value::Get( Vector2& vectorValue ) const
634 bool converted = false;
635 if( mImpl && (mImpl->type == VECTOR2) ) // type cannot change in mImpl so vector is allocated
637 vectorValue = *(mImpl->vector2Value);
643 bool Property::Value::Get( Vector3& vectorValue ) const
645 bool converted = false;
646 if( mImpl && (mImpl->type == VECTOR3) ) // type cannot change in mImpl so vector is allocated
648 vectorValue = *(mImpl->vector3Value);
654 bool Property::Value::Get( Vector4& vectorValue ) const
656 bool converted = false;
657 if( mImpl && (mImpl->type == VECTOR4) ) // type cannot change in mImpl so vector is allocated
659 vectorValue = *(mImpl->vector4Value);
665 bool Property::Value::Get( Matrix3& matrixValue ) const
667 bool converted = false;
668 if( mImpl && (mImpl->type == MATRIX3) ) // type cannot change in mImpl so matrix is allocated
670 matrixValue = *(mImpl->matrix3Value);
676 bool Property::Value::Get( Matrix& matrixValue ) const
678 bool converted = false;
679 if( mImpl && (mImpl->type == MATRIX) ) // type cannot change in mImpl so matrix is allocated
681 matrixValue = *(mImpl->matrixValue);
687 bool Property::Value::Get( Rect<int>& rectValue ) const
689 bool converted = false;
690 if( mImpl && (mImpl->type == RECTANGLE) ) // type cannot change in mImpl so rect is allocated
692 rectValue = *(mImpl->rectValue);
698 bool Property::Value::Get( AngleAxis& angleAxisValue ) const
700 bool converted = false;
701 if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so quaternion is allocated
703 mImpl->quaternionValue->ToAxisAngle( angleAxisValue.axis, angleAxisValue.angle );
709 bool Property::Value::Get( Quaternion& quaternionValue ) const
711 bool converted = false;
712 if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so quaternion is allocated
714 quaternionValue = *(mImpl->quaternionValue);
720 bool Property::Value::Get( std::string& stringValue ) const
722 bool converted = false;
723 if( mImpl && (mImpl->type == STRING) ) // type cannot change in mImpl so string is allocated
725 stringValue.assign( *(mImpl->stringValue) );
731 bool Property::Value::Get( Property::Array& arrayValue ) const
733 bool converted = false;
734 if( mImpl && (mImpl->type == ARRAY) ) // type cannot change in mImpl so array is allocated
736 arrayValue = *(mImpl->arrayValue);
742 bool Property::Value::Get( Property::Map& mapValue ) const
744 bool converted = false;
745 if( mImpl && (mImpl->type == MAP) ) // type cannot change in mImpl so map is allocated
747 mapValue = *(mImpl->mapValue);
753 Property::Array* Property::Value::GetArray() const
755 Property::Array* array = NULL;
756 if( mImpl && (mImpl->type == ARRAY) ) // type cannot change in mImpl so array is allocated
758 array = mImpl->arrayValue;
763 Property::Map* Property::Value::GetMap() const
765 Property::Map* map = NULL;
766 if( mImpl && (mImpl->type == MAP) ) // type cannot change in mImpl so map is allocated
768 map = mImpl->mapValue;
773 std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
777 const Property::Value::Impl& impl( *value.mImpl );
781 case Dali::Property::BOOLEAN:
783 stream << impl.integerValue;
786 case Dali::Property::FLOAT:
788 stream << impl.floatValue;
791 case Dali::Property::INTEGER:
793 stream << impl.integerValue;
796 case Dali::Property::VECTOR2:
798 stream << *impl.vector2Value;
801 case Dali::Property::VECTOR3:
803 stream << *impl.vector3Value;
806 case Dali::Property::VECTOR4:
808 stream << *impl.vector4Value;
811 case Dali::Property::MATRIX3:
813 stream << *impl.matrix3Value;
816 case Dali::Property::MATRIX:
818 stream << *impl.matrixValue;
821 case Dali::Property::RECTANGLE:
823 stream << *impl.rectValue;
826 case Dali::Property::ROTATION:
828 stream << *impl.quaternionValue;
831 case Dali::Property::STRING:
833 stream << *impl.stringValue;
836 case Dali::Property::ARRAY:
838 stream << "Array containing" << impl.arrayValue->Count() << " elements"; // TODO add ostream<< operator in array
841 case Dali::Property::MAP:
843 stream << "Map containing " << impl.mapValue->Count() << " elements"; // TODO add ostream<< operator in map
846 case Dali::Property::NONE:
848 stream << "undefined type";
855 stream << "empty type";