Only include forward declaring iostream header in public API
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-value.cpp
index d03b012..8bffbc3 100644 (file)
@@ -18,6 +18,9 @@
 // CLASS HEADER
 #include <dali/public-api/object/property-value.h>
 
+// EXTERNAL INCLUDES
+#include <ostream>
+
 // INTERNAL INCLUDES
 #include <dali/public-api/object/any.h>
 #include <dali/public-api/math/angle-axis.h>
@@ -29,6 +32,7 @@
 #include <dali/public-api/math/matrix.h>
 #include <dali/public-api/math/rect.h>
 #include <dali/public-api/math/quaternion.h>
+#include <dali/public-api/object/property-map.h>
 #include <dali/public-api/object/property-types.h>
 #include <dali/integration-api/debug.h>
 
@@ -619,7 +623,7 @@ void Property::Value::Get(AngleAxis& angleAxisValue) const
 {
   DALI_ASSERT_ALWAYS( Property::ROTATION == GetType() && "Property type invalid" );
 
-  // Rotations have two representations
+  // Orientations have two representations
   DALI_ASSERT_DEBUG( typeid(Quaternion) == mImpl->mValue.GetType() ||
                      typeid(AngleAxis)  == mImpl->mValue.GetType() );
 
@@ -641,7 +645,7 @@ void Property::Value::Get(Quaternion& quaternionValue) const
 {
   DALI_ASSERT_DEBUG( Property::ROTATION == GetType() && "Property type invalid" );
 
-  // Rotations have two representations
+  // Orientations have two representations
   DALI_ASSERT_DEBUG( typeid(Quaternion) == mImpl->mValue.GetType() ||
                typeid(AngleAxis)  == mImpl->mValue.GetType() );
 
@@ -688,21 +692,15 @@ Property::Value& Property::Value::GetValue(const std::string& key) const
 
   if(container)
   {
-    for(Property::Map::iterator iter = container->begin(); iter != container->end(); ++iter)
+    Property::Value* value = container->Find( key );
+    if ( value )
     {
-      if(iter->first == key)
-      {
-        return iter->second;
-      }
+      return *value;
     }
   }
 
   DALI_LOG_WARNING("Cannot find property map key %s", key.c_str());
   DALI_ASSERT_ALWAYS(!"Cannot find property map key");
-
-  // should never return this
-  static Property::Value null;
-  return null;
 }
 
 bool Property::Value::HasKey(const std::string& key) const
@@ -717,12 +715,10 @@ bool Property::Value::HasKey(const std::string& key) const
 
     if(container)
     {
-      for(Property::Map::iterator iter = container->begin(); iter != container->end(); ++iter)
+      Property::Value* value = container->Find( key );
+      if ( value )
       {
-        if(iter->first == key)
-        {
-          has = true;
-        }
+        has = true;
       }
     }
   }
@@ -737,20 +733,13 @@ const std::string& Property::Value::GetKey(const int index) const
   {
     case Property::MAP:
     {
-      int i = 0;
       Property::Map *container = AnyCast<Property::Map>(&(mImpl->mValue));
       DALI_ASSERT_DEBUG(container && "Property::Map has no container?");
       if(container)
       {
-        if(0 <= index && index < static_cast<int>(container->size()))
+        if(0 <= index && index < static_cast<int>(container->Count()))
         {
-          for(Property::Map::iterator iter = container->begin(); iter != container->end(); ++iter)
-          {
-            if(i++ == index)
-            {
-              return iter->first;
-            }
-          }
+          return container->GetKey( index );
         }
       }
     }
@@ -790,18 +779,7 @@ void Property::Value::SetValue(const std::string& key, const Property::Value &va
 
   if(container)
   {
-    for(Property::Map::iterator iter = container->begin(); iter != container->end(); ++iter)
-    {
-      if(iter->first == key)
-      {
-        iter->second = value;
-        return;
-      }
-    }
-
-    // if we get here its a new key
-    container->push_back(Property::StringValuePair(key, value));
-
+    (*container)[ key ] = value;
   }
 }
 
@@ -811,22 +789,15 @@ Property::Value& Property::Value::GetItem(const int index) const
   {
     case Property::MAP:
     {
-      int i = 0;
       Property::Map *container = AnyCast<Property::Map>(&(mImpl->mValue));
 
       DALI_ASSERT_DEBUG(container && "Property::Map has no container?");
       if(container)
       {
-        DALI_ASSERT_ALWAYS(index < static_cast<int>(container->size()) && "Property array index invalid");
+        DALI_ASSERT_ALWAYS(index < static_cast<int>(container->Count()) && "Property array index invalid");
         DALI_ASSERT_ALWAYS(index >= 0 && "Property array index invalid");
 
-        for(Property::Map::iterator iter = container->begin(); iter != container->end(); ++iter)
-        {
-          if(i++ == index)
-          {
-            return iter->second;
-          }
-        }
+        return container->GetValue( index );
       }
     }
     break;
@@ -876,10 +847,6 @@ Property::Value& Property::Value::GetItem(const int index) const
 
 
   DALI_ASSERT_ALWAYS(!"Property value index not valid");
-
-  // should never return this
-  static Property::Value null;
-  return null;
 }
 
 Property::Value& Property::Value::GetItem(const int index, std::string& key) const
@@ -889,10 +856,9 @@ Property::Value& Property::Value::GetItem(const int index, std::string& key) con
   if( Property::MAP == GetType() )
   {
     Property::Map *container = AnyCast<Property::Map>(&(mImpl->mValue));
-    if( index < static_cast<int>(container->size()) )
+    if( index < static_cast<int>(container->Count()) )
     {
-
-      key = (*container)[ index ].first;
+      key = container->GetKey( index );
     }
   }
 
@@ -906,17 +872,10 @@ void Property::Value::SetItem(const int index, const Property::Value &value)
     case Property::MAP:
     {
       Property::Map *container = AnyCast<Property::Map>(&(mImpl->mValue));
-      if( container && index < static_cast<int>(container->size()) )
+      if( container && index < static_cast<int>(container->Count()) )
       {
-        int i = 0;
-        for(Property::Map::iterator iter = container->begin(); iter != container->end(); ++iter)
-        {
-          if(i++ == index)
-          {
-            iter->second = value;
-            break;
-          }
-        }
+        Property::Value& indexValue = container->GetValue( index );
+        indexValue = value;
       }
     }
     break;
@@ -981,7 +940,7 @@ int Property::Value::GetSize() const
       Property::Map *container = AnyCast<Property::Map>(&(mImpl->mValue));
       if(container)
       {
-        ret = container->size();
+        ret = container->Count();
       }
     }
     break;
@@ -1019,5 +978,108 @@ int Property::Value::GetSize() const
   return ret;
 }
 
+std::ostream& operator<< (std::ostream& stream, const Property::Value& value )
+{
+
+  const Property::Value::Impl& impl( *value.mImpl );
+
+  switch( impl.mType )
+  {
+    case Dali::Property::STRING:
+    {
+      stream <<  AnyCast<std::string>(impl.mValue).c_str();
+      break;
+    }
+    case Dali::Property::VECTOR2:
+    {
+      stream << AnyCast<Vector2>(impl.mValue);
+      break;
+    }
+    case Dali::Property::VECTOR3:
+    {
+      stream << AnyCast<Vector3>(impl.mValue);
+      break;
+    }
+    case Dali::Property::VECTOR4:
+    {
+      stream << AnyCast<Vector4>(impl.mValue);
+      break;
+    }
+    case Dali::Property::MATRIX:
+    {
+      stream << AnyCast<Matrix>(impl.mValue);
+      break;
+    }
+    case Dali::Property::BOOLEAN:
+    {
+      stream << AnyCast<bool>(impl.mValue);
+      break;
+    }
+    case Dali::Property::FLOAT:
+    {
+      stream << AnyCast<float>(impl.mValue);
+      break;
+    }
+    case Dali::Property::INTEGER:
+    {
+       stream << AnyCast<int>(impl.mValue);
+       break;
+    }
+    case Dali::Property::UNSIGNED_INTEGER:
+    {
+      stream << AnyCast<unsigned int>(impl.mValue);
+      break;
+    }
+    case Dali::Property::RECTANGLE:
+    {
+      Dali::Rect<int> rect; // Propery Value rectangles are currently integer based
+      value.Get( rect );
+      stream << rect;
+      break;
+    }
+    case Dali::Property::MATRIX3:
+    {
+      stream << AnyCast<Matrix3>(impl.mValue);
+      break;
+    }
+    case Dali::Property::ROTATION:
+    {
+      // @todo this may change to Quaternion
+      Dali::Quaternion q;
+      value.Get( q );
+      Dali::Vector4 v4 = q.EulerAngles();
+      stream << v4;
+      break;
+    }
+
+    case Dali::Property::ARRAY:
+    {
+      // @todo Need to think about the best way to support array
+      // E.g Do we want to create a JSON style array like:
+      // [ {"property-name-0":"property-value-0", "property-name-1":"property-value-1"} ]
+      stream << "ARRAY unsupported";
+      break;
+    }
+    case Dali::Property::MAP:
+    {
+      Dali::Property::Map map;
+      value.Get( map );
+      stream << "Map containing " << map.Count() << " elements";
+      break;
+    }
+    case Dali::Property::TYPE_COUNT:
+    {
+      stream << "unsupported TYPE_COUNT";
+      break;
+    }
+    default:
+    {
+      stream << "unsupported type = " << value.GetType();
+      break;
+    }
+  }
+  return stream;
+}
+
 
 } // namespace Dali