Add support to create Property::Map with initializer_list 98/108498/26
authorFrancisco Santos <f1.santos@samsung.com>
Wed, 4 Jan 2017 15:01:39 +0000 (15:01 +0000)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Wed, 24 Apr 2019 16:28:02 +0000 (17:28 +0100)
Change-Id: I2aba417e8dddb43b796b27d7290469e319b1832a

automated-tests/src/dali/utc-Dali-PropertyArray.cpp
automated-tests/src/dali/utc-Dali-PropertyMap.cpp
dali/public-api/object/property-array.cpp
dali/public-api/object/property-array.h
dali/public-api/object/property-key.cpp
dali/public-api/object/property-key.h
dali/public-api/object/property-map.cpp
dali/public-api/object/property-map.h
dali/public-api/object/property-value.cpp
dali/public-api/object/property-value.h

index b4e5577..7baa2f7 100644 (file)
@@ -375,3 +375,13 @@ int UtcDaliPropertyArrayMoveAssignmentOperator(void)
   END_TEST;
 }
 
+int UtcDaliPropertyArrayInitializerListConstructor(void)
+{
+  Property::Array array{ 1, 2, "hello" };
+  DALI_TEST_EQUALS( 3u, array.Size(), TEST_LOCATION );
+  DALI_TEST_EQUALS( Property::INTEGER, array.GetElementAt( 0 ).GetType(), TEST_LOCATION );
+  DALI_TEST_EQUALS( Property::INTEGER, array.GetElementAt( 1 ).GetType(), TEST_LOCATION );
+  DALI_TEST_EQUALS( Property::STRING,  array.GetElementAt( 2 ).GetType(), TEST_LOCATION );
+
+  END_TEST;
+}
index 46da84c..dbe8ec3 100644 (file)
@@ -774,3 +774,73 @@ int UtcDaliPropertyKeyOutputStream(void)
 
   END_TEST;
 }
+
+int UtcDaliPropertyMapInitializerListConstructor(void)
+{
+  auto map = Property::Map{
+      {"number mapped to string", 1},
+      {10, "string mapped to number"},
+      {"string mapped", "to string"},
+      {100, 3},
+  };
+
+  DALI_TEST_CHECK( !map.Empty() );    // Should not be empty
+  DALI_TEST_EQUALS( 4, map.Count(), TEST_LOCATION ); // Should have four items
+
+  DALI_TEST_EQUALS( 1, map[ "number mapped to string" ].Get< int >(), TEST_LOCATION );
+  DALI_TEST_EQUALS( "string mapped to number", map[ 10 ].Get< std::string >(), TEST_LOCATION );
+  DALI_TEST_EQUALS( "to string", map[ "string mapped" ].Get< std::string >(), TEST_LOCATION );
+  DALI_TEST_EQUALS( 3, map[ 100 ].Get< int >(), TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliPropertyMapNestedInitializerListConstructor(void)
+{
+  auto map = Property::Map
+  {
+    {1, 1},
+    {2, {{2, 2}}},
+    {3, {{3, {{3, 3}}}}}
+  };
+
+  DALI_TEST_CHECK( !map.Empty() );
+  DALI_TEST_EQUALS( 3, map.Count(), TEST_LOCATION );
+
+  // Check first item
+  {
+    DALI_TEST_EQUALS( 1, map[ 1 ].Get< int >(), TEST_LOCATION );
+  }
+
+  // Check second item
+  {
+    auto& value1 = map[ 2 ];
+    DALI_TEST_EQUALS( Property::MAP, value1.GetType(), TEST_LOCATION );
+
+    auto& map2 = *(value1.GetMap());
+    DALI_TEST_EQUALS( 1, map2.Count(), TEST_LOCATION );
+
+    // check the value
+    DALI_TEST_EQUALS( 2, map2[ 2 ].Get< int >(), TEST_LOCATION );
+  }
+
+  // Check the third item
+  {
+    auto& value1 = map[ 3 ];
+    DALI_TEST_EQUALS( Property::MAP, value1.GetType(), TEST_LOCATION );
+
+    auto& map2 = *(value1.GetMap());
+    DALI_TEST_EQUALS( 1, map2.Count(), TEST_LOCATION );
+
+    auto& value2 = map2[ 3 ];
+    DALI_TEST_EQUALS( Property::MAP, value2.GetType(), TEST_LOCATION );
+
+    auto& map3 = *(value2.GetMap());
+    DALI_TEST_EQUALS( 1, map3.Count(), TEST_LOCATION );
+
+    // check the value
+    DALI_TEST_EQUALS( 3, map3[ 3 ].Get< int >(), TEST_LOCATION );
+  }
+
+  END_TEST;
+}
index bbdeb56..edc79d5 100644 (file)
@@ -40,6 +40,15 @@ Property::Array::Array()
 {
 }
 
+Property::Array::Array( const std::initializer_list< Property::Value >& values )
+: Array()
+{
+  for( auto&& value : values )
+  {
+    PushBack( value );
+  }
+}
+
 Property::Array::Array( const Property::Array& other )
 : mImpl( new Impl )
 {
index e4ac804..2b77f69 100755 (executable)
@@ -19,6 +19,7 @@
  */
 
 // EXTERNAL INCLUDES
+#include <initializer_list>
 #include <string>
 
 // INTERNAL INCLUDES
@@ -50,6 +51,14 @@ public:
   Array();
 
   /**
+   * @brief Constructor from initializer_list.
+   *
+   * @SINCE_1_4.17
+   * @param[in] values An initializer_list of values
+   */
+  Array( const std::initializer_list< Value >& values );
+
+  /**
    * @brief Copy Constructor.
    *
    * @SINCE_1_0.0
index 78e42bf..1d309d0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 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.
@@ -23,18 +23,25 @@ namespace Dali
 {
 
 Property::Key::Key( const std::string& key )
-: type(Key::STRING),
+: type( Key::STRING ),
   indexKey( Property::INVALID_INDEX ),
   stringKey( key )
 {
 }
 
 Property::Key::Key( Property::Index key )
-: type(Key::INDEX),
+: type( Key::INDEX ),
   indexKey( key )
 {
 }
 
+Property::Key::Key( const char * key )
+: type( Key::STRING ),
+  indexKey( Property::INVALID_INDEX ),
+  stringKey( key )
+{
+}
+
 bool Property::Key::operator== (const std::string& rhs)
 {
   bool result=false;
@@ -45,6 +52,11 @@ bool Property::Key::operator== (const std::string& rhs)
   return result;
 }
 
+bool Property::Key::operator== ( const char* rhs )
+{
+  return operator==( std::string( rhs ) );
+}
+
 bool Property::Key::operator== (Property::Index rhs)
 {
   bool result=false;
@@ -74,6 +86,11 @@ bool Property::Key::operator!= (const std::string& rhs)
   return !operator==(rhs);
 }
 
+bool Property::Key::operator!= ( const char* rhs )
+{
+  return !operator==(rhs);
+}
+
 bool Property::Key::operator!= (Property::Index rhs)
 {
   return !operator==(rhs);
index 831a02c..f8efea3 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_PROPERTY_KEY_H__
-#define __DALI_PROPERTY_KEY_H__
+#ifndef DALI_PROPERTY_KEY_H
+#define DALI_PROPERTY_KEY_H
 
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 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.
@@ -58,7 +58,15 @@ struct DALI_CORE_API Property::Key
    *
    * @param[in] key The string key
    */
-  explicit Key( const std::string& key );
+  Key( const std::string& key );
+
+  /**
+   * @brief Constructor
+   * @SINCE_1_4.16
+   *
+   * @param[in] key The string key as a const char *
+   */
+  Key( const char * key );
 
   /**
    * @brief Constructor
@@ -66,7 +74,7 @@ struct DALI_CORE_API Property::Key
    *
    * @param[in] key The index key
    */
-  explicit Key( Property::Index key );
+  Key( Property::Index key );
 
   /**
    * @brief The equality operator
@@ -78,6 +86,14 @@ struct DALI_CORE_API Property::Key
   bool operator== (const std::string& rhs);
 
   /**
+   * @brief Constructor
+   * @SINCE_1_4.16
+   *
+   * @param[in] key The string key as a const char *
+   */
+  bool operator== ( const char * key );
+
+  /**
    * @brief The equality operator
    * @SINCE_1_2.7
    *
@@ -106,6 +122,15 @@ struct DALI_CORE_API Property::Key
 
   /**
    * @brief The inequality operator
+   * @SINCE_1_4.16
+   *
+   * @param[in] rhs A const char* key to compare against.
+   * @return true if the key is not equal or not a string key
+   */
+  bool operator!= ( const char* rhs );
+
+  /**
+   * @brief The inequality operator
    * @SINCE_1_2.7
    *
    * @param[in] rhs An index key to compare against.
@@ -139,4 +164,4 @@ DALI_CORE_API std::ostream& operator<<( std::ostream& stream, const Property::Ke
  */
 } // namespace Dali
 
-#endif // __DALI_PROPERTY_KEY_H__
+#endif // DALI_PROPERTY_KEY_H
index a31207d..80bcf16 100644 (file)
@@ -48,6 +48,27 @@ Property::Map::Map()
 {
 }
 
+Property::Map::Map( const std::initializer_list< KeyValuePair >& values ) : Map()
+{
+  for( auto&& value : values )
+  {
+    const auto& key = value.first;
+    switch( key.type )
+    {
+      case Property::Key::INDEX:
+      {
+        Property::Map::Insert( key.indexKey, value.second );
+        break;
+      }
+      case Property::Key::STRING:
+      {
+        Property::Map::Insert( key.stringKey, value.second );
+        break;
+      }
+    }
+  }
+}
+
 Property::Map::Map( const Property::Map& other )
 : mImpl( new Impl )
 {
index f23f2b3..f1d7e5c 100755 (executable)
@@ -21,6 +21,7 @@
 // EXTERNAL INCLUDES
 #include <string>
 #include <sstream>
+#include <initializer_list>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
@@ -55,6 +56,14 @@ public:
   Map();
 
   /**
+   * @brief Constructor from initializer_list.
+   *
+   * @SINCE_1_4.17
+   * @param[in] values An initializer_list of pairs of index and value.
+   */
+  Map( const std::initializer_list< KeyValuePair >& values );
+
+  /**
    * @brief Copy Constructor.
    *
    * @SINCE_1_0.0
index fbcfc2b..73e8aab 100644 (file)
@@ -150,6 +150,12 @@ struct Property::Value::Impl
   {
   }
 
+  Impl( const std::initializer_list< KeyValuePair >& values )
+  : type( Property::MAP ),
+    mapValue( new Property::Map( values ) )
+  {
+  }
+
   /**
    * Destructor, takes care of releasing the dynamically allocated types
    */
@@ -354,6 +360,11 @@ Property::Value::Value( const Extents& extentsValue )
 {
 }
 
+Property::Value::Value( const std::initializer_list< KeyValuePair >& values )
+: mImpl( new Impl( values ) )
+{
+}
+
 Property::Value::Value( Type type )
 : mImpl( nullptr )
 {
index d8f074a..933eb8d 100755 (executable)
@@ -1,5 +1,5 @@
-#ifndef __DALI_PROPERTY_VALUE_H__
-#define __DALI_PROPERTY_VALUE_H__
+#ifndef DALI_PROPERTY_VALUE_H
+#define DALI_PROPERTY_VALUE_H
 
 /*
  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
@@ -20,6 +20,8 @@
 
 // EXTERNAL INCLUDES
 #include <iosfwd>
+#include <utility>
+#include <initializer_list>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/object/property.h>
@@ -41,6 +43,8 @@ class Matrix3;
 class Matrix;
 struct Extents;
 
+typedef std::pair< Property::Key, Property::Value > KeyValuePair;
+
 /**
  * @brief A value-type representing a property value.
  * @SINCE_1_0.0
@@ -194,6 +198,14 @@ public:
   Value( Property::Map&& mapValue );
 
   /**
+   * @brief Create a map property value from an initializer_list.
+   *
+   * @SINCE_1_4.16
+   * @param [in] values An initializer_list of pairs of index and value.
+   */
+  Value( const std::initializer_list< KeyValuePair >& values );
+
+  /**
    * @brief Creates an extents property value.
    *
    * @SINCE_1_2.62
@@ -469,4 +481,4 @@ DALI_CORE_API std::ostream& operator<<( std::ostream& ouputStream, const Propert
  */
 } // namespace Dali
 
-#endif // __DALI_PROPERTY_VALUE_H__
+#endif // DALI_PROPERTY_VALUE_H