[dali_1.9.14] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-value.cpp
index 85f407f..7132bfb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
 #include <ostream>
 
 // INTERNAL INCLUDES
+#include <dali/public-api/common/extents.h>
 #include <dali/public-api/math/angle-axis.h>
 #include <dali/public-api/math/radian.h>
 #include <dali/public-api/math/vector2.h>
@@ -52,11 +53,6 @@ inline bool IsIntegerType( Property::Type type )
 
 struct Property::Value::Impl
 {
-  Impl()
-  : type( Property::NONE ),
-    integerValue( 0 )
-  { }
-
   Impl( bool booleanValue )
   : type( Property::BOOLEAN ),
     integerValue( booleanValue )
@@ -67,7 +63,7 @@ struct Property::Value::Impl
     floatValue( floatValue )
   { }
 
-  Impl( int integerValue )
+  Impl( int32_t integerValue )
   : type( Property::INTEGER ),
     integerValue( integerValue )
   { }
@@ -101,40 +97,71 @@ struct Property::Value::Impl
 
   Impl( const AngleAxis& angleAxisValue )
   : type( Property::ROTATION ),
-    quaternionValue( new Quaternion( angleAxisValue.angle, angleAxisValue.axis ) )
+    angleAxisValue( new AngleAxis(angleAxisValue) )
   {
   }
 
   Impl( const Quaternion& quaternionValue )
   : type( Property::ROTATION ),
-    quaternionValue( new Quaternion( quaternionValue ) )
+    angleAxisValue( new AngleAxis() )
   {
+    quaternionValue.ToAxisAngle( angleAxisValue->axis, angleAxisValue->angle );
   }
 
-  Impl(const std::string& stringValue)
+  Impl( const std::string& stringValue )
   : type( Property::STRING ),
     stringValue( new std::string( stringValue ) )
   {
   }
 
-  Impl( const Rect<int>& rectValue )
+  Impl( const Rect<int32_t>& rectValue )
   : type( Property::RECTANGLE ),
     rectValue( new Rect<int>( rectValue ) )
   {
   }
 
+  Impl( const Rect<float>& rectValue )
+  : type( Property::VECTOR4 ),
+    vector4Value( new Vector4( rectValue.x, rectValue.y, rectValue.width, rectValue.height ) )
+  {
+  }
+
   Impl( const Property::Array& arrayValue )
   : type( Property::ARRAY ),
     arrayValue( new Property::Array( arrayValue ) )
   {
   }
 
+  Impl( Property::Array&& arrayValue )
+  : type( Property::ARRAY ),
+    arrayValue( new Property::Array( std::move( arrayValue ) ) )
+  {
+  }
+
   Impl( const Property::Map& mapValue )
   : type( Property::MAP ),
     mapValue( new Property::Map( mapValue ) )
   {
   }
 
+  Impl( Property::Map&& mapValue )
+  : type( Property::MAP ),
+    mapValue( new Property::Map( std::move( mapValue ) ) )
+  {
+  }
+
+  Impl( const Extents& extentsValue )
+  : type( Property::EXTENTS ),
+    extentsValue( new Extents( extentsValue ) )
+  {
+  }
+
+  Impl( const std::initializer_list< KeyValuePair >& values )
+  : type( Property::MAP ),
+    mapValue( new Property::Map( values ) )
+  {
+  }
+
   /**
    * Destructor, takes care of releasing the dynamically allocated types
    */
@@ -181,7 +208,7 @@ struct Property::Value::Impl
       }
       case Property::ROTATION:
       {
-        delete quaternionValue;
+        delete angleAxisValue;
         break;
       }
       case Property::STRING:
@@ -199,6 +226,11 @@ struct Property::Value::Impl
         delete mapValue;
         break;
       }
+      case Property::EXTENTS:
+      {
+        delete extentsValue;
+        break;
+      }
     }
   }
 
@@ -207,7 +239,7 @@ public: // Data
   Type type;
   union
   {
-    int integerValue;
+    int32_t integerValue;
     float floatValue;
     // must use pointers for any class value pre c++ 11
     Vector2* vector2Value;
@@ -215,16 +247,24 @@ public: // Data
     Vector4* vector4Value;
     Matrix3* matrix3Value;
     Matrix* matrixValue;
-    Quaternion* quaternionValue;
+    AngleAxis* angleAxisValue;
     std::string* stringValue;
-    Rect<int>* rectValue;
+    Rect<int32_t>* rectValue;
     Property::Array* arrayValue;
     Property::Map* mapValue;
+    Extents* extentsValue;
   };
+
+private:
+
+  // non-copyable
+  Impl( const Impl& ) = delete;
+  Impl& operator=( const Impl& ) = delete;
+
 };
 
 Property::Value::Value()
-: mImpl( NULL )
+: mImpl( nullptr )
 {
 }
 
@@ -238,7 +278,7 @@ Property::Value::Value( float floatValue )
 {
 }
 
-Property::Value::Value( int integerValue )
+Property::Value::Value( int32_t integerValue )
 : mImpl( new Impl( integerValue ) )
 {
 }
@@ -268,7 +308,12 @@ Property::Value::Value( const Matrix& matrixValue )
 {
 }
 
-Property::Value::Value( const Rect<int>& rectValue )
+Property::Value::Value( const Rect<int32_t>& rectValue )
+: mImpl( new Impl( rectValue ) )
+{
+}
+
+Property::Value::Value( const Rect<float>& rectValue )
 : mImpl( new Impl( rectValue ) )
 {
 }
@@ -289,9 +334,9 @@ Property::Value::Value( const std::string& stringValue )
 }
 
 Property::Value::Value( const char* stringValue )
-: mImpl( NULL )
+: mImpl( nullptr )
 {
-  if( stringValue ) // string constructor is undefined with NULL pointer
+  if( stringValue ) // string constructor is undefined with nullptr
   {
     mImpl = new Impl( std::string(stringValue) );
   }
@@ -306,12 +351,33 @@ Property::Value::Value( Property::Array& arrayValue )
 {
 }
 
+Property::Value::Value( Property::Array&& arrayValue )
+: mImpl( new Impl( std::move( arrayValue ) ) )
+{
+}
+
 Property::Value::Value( Property::Map& mapValue )
 : mImpl( new Impl( mapValue ) )
 {
 }
 
+Property::Value::Value( Property::Map&& mapValue )
+: mImpl( new Impl( std::move( mapValue ) ) )
+{
+}
+
+Property::Value::Value( const Extents& extentsValue )
+: mImpl( new Impl( extentsValue ) )
+{
+}
+
+Property::Value::Value( const std::initializer_list< KeyValuePair >& values )
+: mImpl( new Impl( values ) )
+{
+}
+
 Property::Value::Value( Type type )
+: mImpl( nullptr )
 {
   switch (type)
   {
@@ -347,12 +413,12 @@ Property::Value::Value( Type type )
     }
     case Property::RECTANGLE:
     {
-      mImpl = new Impl( Rect<int>(0,0,0,0) );
+      mImpl = new Impl( Rect<int32_t>(0,0,0,0) );
       break;
     }
     case Property::ROTATION:
     {
-      mImpl = new Impl( Quaternion() );
+      mImpl = new Impl( AngleAxis() );
       break;
     }
     case Property::STRING:
@@ -380,21 +446,32 @@ Property::Value::Value( Type type )
       mImpl = new Impl( Property::Map() );
       break;
     }
+    case Property::EXTENTS:
+    {
+      mImpl = new Impl( Extents() );
+      break;
+    }
     case Property::NONE:
     {
-      mImpl = new Impl();
+      // No need to create an Impl
       break;
     }
   }
 }
 
 Property::Value::Value( const Property::Value& value )
-: mImpl( NULL )
+: mImpl( nullptr )
 {
   // reuse assignment operator
   operator=( value );
 }
 
+Property::Value::Value( Property::Value&& value )
+: mImpl( value.mImpl )
+{
+  value.mImpl = nullptr;
+}
+
 Property::Value& Property::Value::operator=( const Property::Value& value )
 {
   if ( this == &value )
@@ -406,7 +483,7 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
   if( !value.mImpl )
   {
     delete mImpl;
-    mImpl = NULL;
+    mImpl = nullptr;
     return *this;
   }
   // first check if the type is the same, no need to change impl, just assign
@@ -451,7 +528,7 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
       }
       case Property::ROTATION:
       {
-        *mImpl->quaternionValue = *value.mImpl->quaternionValue; // type cannot change in mImpl so quaternion is allocated
+        *mImpl->angleAxisValue = *value.mImpl->angleAxisValue; // type cannot change in mImpl so quaternion is allocated
         break;
       }
       case Property::STRING:
@@ -479,15 +556,20 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
         *mImpl->mapValue = *value.mImpl->mapValue; // type cannot change in mImpl so map is allocated
         break;
       }
+      case Property::EXTENTS:
+      {
+        *mImpl->extentsValue = *value.mImpl->extentsValue; // type cannot change in mImpl so extents is allocated
+        break;
+      }
       case Property::NONE:
-      { // mImpl will be NULL, there's no way to get to this case
+      { // mImpl will be a nullptr, there's no way to get to this case
       }
     }
   }
   else
   {
     // different type, release old impl and create new
-    Impl* newImpl( NULL );
+    Impl* newImpl( nullptr );
     switch ( value.mImpl->type )
     {
       case Property::BOOLEAN:
@@ -527,7 +609,7 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
       }
       case Property::ROTATION:
       {
-        newImpl = new Impl( *value.mImpl->quaternionValue ); // type cannot change in mImpl so quaternion is allocated
+        newImpl = new Impl( *value.mImpl->angleAxisValue ); // type cannot change in mImpl so quaternion is allocated
         break;
       }
       case Property::MATRIX3:
@@ -555,8 +637,13 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
         newImpl = new Impl( *value.mImpl->mapValue ); // type cannot change in mImpl so map is allocated
         break;
       }
+      case Property::EXTENTS:
+      {
+        newImpl = new Impl( *value.mImpl->extentsValue ); // type cannot change in mImpl so extents is allocated
+        break;
+      }
       case Property::NONE:
-      { // NULL value will be used for "empty" value
+      { // nullptr value will be used for "empty" value
       }
     }
     delete mImpl;
@@ -566,6 +653,18 @@ Property::Value& Property::Value::operator=( const Property::Value& value )
   return *this;
 }
 
+Property::Value& Property::Value::operator=( Property::Value&& value )
+{
+  if( this != &value )
+  {
+    delete mImpl;
+    mImpl = value.mImpl;
+    value.mImpl = nullptr;
+  }
+
+  return *this;
+}
+
 Property::Value::~Value()
 {
   delete mImpl;
@@ -611,7 +710,7 @@ bool Property::Value::Get( float& floatValue ) const
   return converted;
 }
 
-bool Property::Value::Get( int& integerValue ) const
+bool Property::Value::Get( int32_t& integerValue ) const
 {
   bool converted = false;
   if( mImpl )
@@ -623,7 +722,7 @@ bool Property::Value::Get( int& integerValue ) const
     }
     else if( mImpl->type == FLOAT )
     {
-      integerValue = static_cast< int >( mImpl->floatValue );
+      integerValue = static_cast< int32_t >( mImpl->floatValue );
       converted = true;
     }
   }
@@ -633,10 +732,14 @@ bool Property::Value::Get( int& integerValue ) const
 bool Property::Value::Get( Vector2& vectorValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == VECTOR2) ) // type cannot change in mImpl so vector is allocated
+  if( mImpl )
   {
-    vectorValue = *(mImpl->vector2Value);
-    converted = true;
+    // type cannot change in mImpl so vector is allocated
+    if( mImpl->type == VECTOR2 || mImpl->type == VECTOR3 || mImpl->type == VECTOR4 )
+    {
+      vectorValue = *(mImpl->vector2Value); // if Vector3 or 4 only x and y are assigned
+      converted = true;
+    }
   }
   return converted;
 }
@@ -644,10 +747,19 @@ bool Property::Value::Get( Vector2& vectorValue ) const
 bool Property::Value::Get( Vector3& vectorValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == VECTOR3) ) // type cannot change in mImpl so vector is allocated
+  if( mImpl )
   {
-    vectorValue = *(mImpl->vector3Value);
-    converted = true;
+    // type cannot change in mImpl so vector is allocated
+    if ( mImpl->type == VECTOR3 || mImpl->type == VECTOR4 )
+    {
+      vectorValue = *(mImpl->vector3Value); // if Vector4 only x,y,z are assigned
+      converted = true;
+    }
+    else if( mImpl->type == VECTOR2 )
+    {
+      vectorValue = *(mImpl->vector2Value);
+      converted = true;
+    }
   }
   return converted;
 }
@@ -655,10 +767,23 @@ bool Property::Value::Get( Vector3& vectorValue ) const
 bool Property::Value::Get( Vector4& vectorValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == VECTOR4) ) // type cannot change in mImpl so vector is allocated
+  if( mImpl )
   {
-    vectorValue = *(mImpl->vector4Value);
-    converted = true;
+    if( mImpl->type == VECTOR4 ) // type cannot change in mImpl so vector is allocated
+    {
+      vectorValue = *(mImpl->vector4Value);
+      converted = true;
+    }
+    else if( mImpl->type == VECTOR2 )
+    {
+      vectorValue = *(mImpl->vector2Value);
+      converted = true;
+    }
+    else if( mImpl->type == VECTOR3 )
+    {
+      vectorValue = *(mImpl->vector3Value);
+      converted = true;
+    }
   }
   return converted;
 }
@@ -685,7 +810,7 @@ bool Property::Value::Get( Matrix& matrixValue ) const
   return converted;
 }
 
-bool Property::Value::Get( Rect<int>& rectValue ) const
+bool Property::Value::Get( Rect<int32_t>& rectValue ) const
 {
   bool converted = false;
   if( mImpl && (mImpl->type == RECTANGLE) ) // type cannot change in mImpl so rect is allocated
@@ -699,9 +824,9 @@ bool Property::Value::Get( Rect<int>& rectValue ) const
 bool Property::Value::Get( AngleAxis& angleAxisValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so quaternion is allocated
+  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so angleAxis is allocated
   {
-    mImpl->quaternionValue->ToAxisAngle( angleAxisValue.axis, angleAxisValue.angle );
+    angleAxisValue = *(mImpl->angleAxisValue);
     converted = true;
   }
   return converted;
@@ -710,9 +835,9 @@ bool Property::Value::Get( AngleAxis& angleAxisValue ) const
 bool Property::Value::Get( Quaternion& quaternionValue ) const
 {
   bool converted = false;
-  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so quaternion is allocated
+  if( mImpl && (mImpl->type == ROTATION) ) // type cannot change in mImpl so angleAxis is allocated
   {
-    quaternionValue = *(mImpl->quaternionValue);
+    quaternionValue = Quaternion(mImpl->angleAxisValue->angle, mImpl->angleAxisValue->axis );
     converted = true;
   }
   return converted;
@@ -753,7 +878,7 @@ bool Property::Value::Get( Property::Map& mapValue ) const
 
 Property::Array* Property::Value::GetArray() const
 {
-  Property::Array* array = NULL;
+  Property::Array* array = nullptr;
   if( mImpl && (mImpl->type == ARRAY) ) // type cannot change in mImpl so array is allocated
   {
     array = mImpl->arrayValue;
@@ -763,7 +888,7 @@ Property::Array* Property::Value::GetArray() const
 
 Property::Map* Property::Value::GetMap() const
 {
-  Property::Map* map = NULL;
+  Property::Map* map = nullptr;
   if( mImpl && (mImpl->type == MAP) ) // type cannot change in mImpl so map is allocated
   {
     map = mImpl->mapValue;
@@ -771,6 +896,28 @@ Property::Map* Property::Value::GetMap() const
   return map;
 }
 
+bool Property::Value::Get( Extents& extentsValue ) const
+{
+  bool converted = false;
+  if( mImpl )
+  {
+    if( mImpl->type == EXTENTS )
+    {
+      extentsValue = *(mImpl->extentsValue);
+      converted = true;
+    }
+    else if( mImpl->type == VECTOR4 )
+    {
+      extentsValue.start = static_cast< uint16_t >( mImpl->vector4Value->x );
+      extentsValue.end = static_cast< uint16_t >( mImpl->vector4Value->y );
+      extentsValue.top = static_cast< uint16_t >( mImpl->vector4Value->z );
+      extentsValue.bottom = static_cast< uint16_t >( mImpl->vector4Value->w );
+      converted = true;
+    }
+  }
+  return converted;
+}
+
 std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
 {
   if( value.mImpl )
@@ -826,7 +973,7 @@ std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
       }
       case Dali::Property::ROTATION:
       {
-        stream << *impl.quaternionValue;
+        stream << *impl.angleAxisValue;
         break;
       }
       case Dali::Property::STRING:
@@ -836,24 +983,27 @@ std::ostream& operator<<( std::ostream& stream, const Property::Value& value )
       }
       case Dali::Property::ARRAY:
       {
-        stream << "Array containing" << impl.arrayValue->Count() << " elements"; // TODO add ostream<< operator in array
+        stream << *(value.GetArray());
         break;
       }
       case Dali::Property::MAP:
       {
-        stream << "Map containing " << impl.mapValue->Count() << " elements"; // TODO add ostream<< operator in map
+        stream << *(value.GetMap());
         break;
       }
-      case Dali::Property::NONE:
+      case Dali::Property::EXTENTS:
       {
-        stream << "undefined type";
+        stream << *impl.extentsValue;
         break;
       }
+      case Dali::Property::NONE:
+      { // mImpl will be a nullptr, there's no way to get to this case
+      }
     }
   }
   else
   {
-    stream << "empty type";
+    stream << "undefined type";
   }
   return stream;
 }