(Scripting) Some internal structs/templates public so that they can be re-used in...
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Fri, 9 May 2014 18:05:42 +0000 (19:05 +0100)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 27 May 2014 14:18:23 +0000 (15:18 +0100)
Change-Id: I08966d5ce46918957027e776450fdaebdee3c31e
Signed-off-by: Adeel Kazmi <adeel.kazmi@samsung.com>
automated-tests/src/dali-unmanaged/tct-dali-unmanaged-core.h
automated-tests/src/dali-unmanaged/utc-Dali-Scripting.cpp
dali/public-api/scripting/scripting.cpp
dali/public-api/scripting/scripting.h

index 17a0792..ee48621 100644 (file)
@@ -194,6 +194,7 @@ extern int UtcDaliScriptingNewActorProperties(void);
 extern int UtcDaliScriptingNewActorChildren(void);
 extern int UtcDaliScriptingCreatePropertyMapActor(void);
 extern int UtcDaliScriptingCreatePropertyMapImage(void);
+extern int UtcDaliScriptingGetEnumerationTemplates(void);
 extern int UtcDaliSplineGetYFromMonotonicX(void);
 extern int utcDaliSplineGetKnot01(void);
 extern int utcDaliSplineGetKnot02(void);
@@ -442,6 +443,7 @@ testcase tc_array[] = {
     {"UtcDaliScriptingNewActorChildren", UtcDaliScriptingNewActorChildren, utc_dali_unmanaged_startup, utc_dali_unmanaged_cleanup},
     {"UtcDaliScriptingCreatePropertyMapActor", UtcDaliScriptingCreatePropertyMapActor, utc_dali_unmanaged_startup, utc_dali_unmanaged_cleanup},
     {"UtcDaliScriptingCreatePropertyMapImage", UtcDaliScriptingCreatePropertyMapImage, utc_dali_unmanaged_startup, utc_dali_unmanaged_cleanup},
+    {"UtcDaliScriptingGetEnumerationTemplates", UtcDaliScriptingGetEnumerationTemplates, utc_dali_unmanaged_startup, utc_dali_unmanaged_cleanup},
     {"UtcDaliSplineGetYFromMonotonicX", UtcDaliSplineGetYFromMonotonicX, utc_dali_unmanaged_startup, utc_dali_unmanaged_cleanup},
     {"utcDaliSplineGetKnot01", utcDaliSplineGetKnot01, utc_dali_unmanaged_startup, utc_dali_unmanaged_cleanup},
     {"utcDaliSplineGetKnot02", utcDaliSplineGetKnot02, utc_dali_unmanaged_startup, utc_dali_unmanaged_cleanup},
index 4174b04..9a52ed1 100644 (file)
@@ -26,13 +26,7 @@ using namespace Dali::Scripting;
 namespace
 {
 
-struct StringEnum
-{
-  const char * string;
-  int value;
-};
-
-const StringEnum COLOR_MODE_VALUES[] =
+const StringEnum< int > COLOR_MODE_VALUES[] =
 {
     { "USE_OWN_COLOR", USE_OWN_COLOR },
     { "USE_PARENT_COLOR", USE_PARENT_COLOR },
@@ -41,7 +35,7 @@ const StringEnum COLOR_MODE_VALUES[] =
 };
 const unsigned int COLOR_MODE_VALUES_COUNT = sizeof( COLOR_MODE_VALUES ) / sizeof( COLOR_MODE_VALUES[0] );
 
-const StringEnum POSITION_INHERITANCE_MODE_VALUES[] =
+const StringEnum< int > POSITION_INHERITANCE_MODE_VALUES[] =
 {
     { "INHERIT_PARENT_POSITION", INHERIT_PARENT_POSITION },
     { "USE_PARENT_POSITION", USE_PARENT_POSITION },
@@ -50,7 +44,7 @@ const StringEnum POSITION_INHERITANCE_MODE_VALUES[] =
 };
 const unsigned int POSITION_INHERITANCE_MODE_VALUES_COUNT = sizeof( POSITION_INHERITANCE_MODE_VALUES ) / sizeof( POSITION_INHERITANCE_MODE_VALUES[0] );
 
-const StringEnum DRAW_MODE_VALUES[] =
+const StringEnum< int > DRAW_MODE_VALUES[] =
 {
     { "NORMAL", DrawMode::NORMAL },
     { "OVERLAY", DrawMode::OVERLAY },
@@ -70,7 +64,7 @@ const unsigned int DRAW_MODE_VALUES_COUNT = sizeof( DRAW_MODE_VALUES ) / sizeof(
 template< typename T, typename X >
 void TestEnumStrings(
   Property::Map& map,                       // The map used to create instance of type X
-  const StringEnum* values,                 // An array of string values
+  const StringEnum< int >* values,          // An array of string values
   unsigned int num,                         // Number of items in the array
   T ( X::*method )() const,                 // The member method of X to call to get the enum
   X ( *creator ) ( const Property::Value& ) // The method which creates an instance of type X
@@ -104,7 +98,7 @@ template< typename T >
 void TestEnumStrings(
   const char * const keyName,               // The name of the key to check
   TestApplication& application,             // Reference to the application class
-  const StringEnum* values,                 // An array of string values
+  const StringEnum< int >* values,          // An array of string values
   unsigned int num,                         // Number of items in the array
   void ( Actor::*method )( T )              // The Actor member method to set the enumeration
 )
@@ -462,7 +456,7 @@ int UtcDaliScriptingNewImage(void)
   // load-policy
   map.push_back( Property::StringValuePair( "load-policy", "" ) );
   {
-    const StringEnum values[] =
+    const StringEnum< int > values[] =
     {
         { "IMMEDIATE", Image::Immediate },
         { "ON_DEMAND", Image::OnDemand }
@@ -473,7 +467,7 @@ int UtcDaliScriptingNewImage(void)
   // release-policy
   map.push_back( Property::StringValuePair( "release-policy", "" ) );
   {
-    const StringEnum values[] =
+    const StringEnum< int > values[] =
     {
         { "UNUSED", Image::Unused },
         { "NEVER", Image::Never }
@@ -505,7 +499,7 @@ int UtcDaliScriptingNewImage(void)
   // pixel-format
   map.push_back( Property::StringValuePair( "pixel-format", "" ) );
   {
-    const StringEnum values[] =
+    const StringEnum< int > values[] =
     {
         { "A8", Pixel::A8 },
         { "L8", Pixel::L8 },
@@ -541,7 +535,7 @@ int UtcDaliScriptingNewImage(void)
   // scaling-mode
   map.push_back( Property::StringValuePair( "scaling-mode", "" ) );
   {
-    const StringEnum values[] =
+    const StringEnum< int > values[] =
     {
         { "SHRINK_TO_FIT", ImageAttributes::ShrinkToFit },
         { "SCALE_TO_FILL", ImageAttributes::ScaleToFill },
@@ -1035,3 +1029,32 @@ int UtcDaliScriptingCreatePropertyMapImage(void)
   }
   END_TEST;
 }
+
+int UtcDaliScriptingGetEnumerationTemplates(void)
+{
+  TestApplication application;
+
+  const Scripting::StringEnum< int > myTable[] =
+  {
+    { "ONE",    1 },
+    { "TWO",    2 },
+    { "THREE",  3 },
+    { "FOUR",   4 },
+    { "FIVE",   5 },
+  };
+  const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] );
+
+  for ( unsigned int i = 0; i < myTableCount; ++i )
+  {
+    tet_printf("Checking: %s\n", myTable[ i ].string );
+    DALI_TEST_EQUALS( myTable[ i ].value, GetEnumeration( myTable[ i ].string, myTable, myTableCount ), TEST_LOCATION );
+  }
+
+  for ( unsigned int i = 0; i < myTableCount; ++i )
+  {
+    tet_printf("Checking: %d\n", myTable[ i ].value );
+    DALI_TEST_EQUALS( myTable[ i ].string, GetEnumerationName( myTable[ i ].value, myTable, myTableCount ), TEST_LOCATION );
+  }
+
+  END_TEST;
+}
index 864663e..1c12b8a 100644 (file)
@@ -36,14 +36,7 @@ namespace Scripting
 namespace
 {
 
-// Helpers for converting strings to the enumerations and vice versa
-
-template< typename T >
-struct StringEnum
-{
-  const std::string string;
-  const T value;
-};
+// Tables used here for converting strings to the enumerations and vice versa
 
 const StringEnum< ColorMode > COLOR_MODE_TABLE[] =
 {
@@ -157,41 +150,6 @@ const StringEnum< ImageAttributes::ScalingMode > IMAGE_SCALING_MODE_TABLE[] =
 };
 const unsigned int IMAGE_SCALING_MODE_TABLE_COUNT = sizeof( IMAGE_SCALING_MODE_TABLE ) / sizeof( IMAGE_SCALING_MODE_TABLE[0] );
 
-template< typename T >
-T GetEnumeration( const std::string& value, const StringEnum< T >* table, const unsigned int tableCount )
-{
-  T v( table->value );
-  bool set( false );
-
-  for ( unsigned int i = 0; ( i < tableCount ) && ( !set ); ++i )
-  {
-    set = SetIfEqual(value, table->string, v, table->value );
-    ++table;
-  }
-
-  if ( !set )
-  {
-    DALI_ASSERT_ALWAYS( !"Unknown enumeration string" );
-  }
-
-  return v;
-}
-
-template< typename T >
-const std::string& GetEnumerationName( const T& value, const StringEnum< T >* table, const unsigned int tableCount )
-{
-  for ( unsigned int i = 0; i < tableCount; ++i )
-  {
-    if ( value == table[i].value )
-    {
-      return table[i].string;
-      break;
-    }
-  }
-
-  return String::EMPTY;
-}
-
 } // unnamed namespace
 
 bool CompareEnums(const std::string& a, const std::string& b)
@@ -253,7 +211,7 @@ ColorMode GetColorMode( const std::string& value )
 }
 
 
-const std::string& GetColorMode( ColorMode value )
+std::string GetColorMode( ColorMode value )
 {
   return GetEnumerationName< ColorMode >( value, COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
 }
@@ -264,7 +222,7 @@ PositionInheritanceMode GetPositionInheritanceMode( const std::string& value )
 }
 
 
-const std::string& GetPositionInheritanceMode( PositionInheritanceMode value )
+std::string GetPositionInheritanceMode( PositionInheritanceMode value )
 {
   return GetEnumerationName< PositionInheritanceMode >( value, POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
 }
@@ -276,7 +234,7 @@ DrawMode::Type GetDrawMode( const std::string& value )
 }
 
 
-const std::string& GetDrawMode( DrawMode::Type value )
+std::string GetDrawMode( DrawMode::Type value )
 {
   return GetEnumerationName< DrawMode::Type >( value, DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
 }
index 8ec57b8..b7ef072 100644 (file)
@@ -36,6 +36,16 @@ namespace Scripting
 {
 
 /**
+ * @brief Template structure which stores an enumeration and its string equivalent.
+ */
+template< typename T >
+struct StringEnum
+{
+  const char* string; ///< The string representation
+  const T value;      ///< The actual enumeration
+};
+
+/**
  * @brief Permissive comparison for string enums.
  *
  * Case insensitive and ignores '_', '-' in either string when comparing.
@@ -53,7 +63,7 @@ bool CompareEnums(const std::string& input, const std::string& enumString);
  * @param[in] b The input string
  * @param[in] set The variable to set
  * @param[in] value The value to set
- * @return true if the strings pass the permissve compare
+ * @return true if the strings pass the permissive compare
  */
 template <typename T>
 bool SetIfEqual(const std::string& a, const std::string& b, T& set, T value)
@@ -70,6 +80,61 @@ bool SetIfEqual(const std::string& a, const std::string& b, T& set, T value)
 }
 
 /**
+ * @brief Chooses the appropriate enumeration for the provided string from the given table.
+ *
+ * @param[in]  value       The string equivalent (case-insensitive).
+ * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
+ * @param[in]  tableCount  Number of items in the array.
+ *
+ * @return The equivalent enumeration for the given string.
+ */
+template< typename T >
+T GetEnumeration( const std::string& value, const StringEnum< T >* table, const unsigned int tableCount )
+{
+  T retVal( table->value );
+  bool set( false );
+
+  for ( unsigned int i = 0; ( i < tableCount ) && ( !set ); ++i )
+  {
+    set = SetIfEqual( value, table->string, retVal, table->value );
+    ++table;
+  }
+
+  if ( !set )
+  {
+    DALI_ASSERT_ALWAYS( !"Unknown enumeration string" );
+  }
+
+  return retVal;
+}
+
+/**
+ * @brief Chooses the appropriate string for the provided enumeration from the given table.
+ *
+ * @param[in]  value       The enumeration.
+ * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
+ * @param[in]  tableCount  Number of items in the array.
+ *
+ * @return The equivalent enumeration for the given string.
+ */
+template< typename T >
+std::string GetEnumerationName( const T& value, const StringEnum< T >* table, const unsigned int tableCount )
+{
+  std::string string( String::EMPTY );
+
+  for ( unsigned int i = 0; i < tableCount; ++i )
+  {
+    if ( value == table[ i ].value )
+    {
+      string = table[ i ].string;
+      break;
+    }
+  }
+
+  return string;
+}
+
+/**
  * @brief Takes a string and returns the appropriate color mode.
  *
  * @param[in] value The input string
@@ -83,7 +148,7 @@ ColorMode GetColorMode( const std::string& value );
  * @param[in] value The color mode
  * @return The corresponding string.
  */
-const std::string& GetColorMode( ColorMode value );
+std::string GetColorMode( ColorMode value );
 
 /**
  * @brief Takes a string and returns the appropriate position inheritance mode.
@@ -99,7 +164,7 @@ PositionInheritanceMode GetPositionInheritanceMode( const std::string& value );
  * @param[in] value The position-inheritance-mode.
  * @return The corresponding string.
  */
-const std::string& GetPositionInheritanceMode( PositionInheritanceMode value );
+std::string GetPositionInheritanceMode( PositionInheritanceMode value );
 
 /**
  * @brief Takes a string and returns the appropriate draw mode.
@@ -115,7 +180,7 @@ DrawMode::Type GetDrawMode( const std::string& value );
  * @param[in] value The draw-mode.
  * @return The corresponding string.
  */
-const std::string& GetDrawMode( DrawMode::Type value );
+std::string GetDrawMode( DrawMode::Type value );
 
 /**
  * @brief Takes a string and returns the appropriate anchor-point or parent-origin constant.