(Scripting) Using const char * instead of std::string 36/35536/4
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 17 Feb 2015 09:39:46 +0000 (09:39 +0000)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Wed, 18 Feb 2015 07:31:02 +0000 (07:31 +0000)
Change-Id: I7a145a25028d36506a313ac24ec0728f10a6f2ca

automated-tests/src/dali/utc-Dali-Scripting.cpp
dali/internal/event/actors/actor-impl.cpp
dali/public-api/scripting/scripting.cpp
dali/public-api/scripting/scripting.h

index 2ed6d3b..fa6476b 100644 (file)
@@ -1033,3 +1033,23 @@ int UtcDaliScriptingGetEnumerationTemplates(void)
 
   END_TEST;
 }
+
+int UtcDaliScriptingCompareEnums(void)
+{
+  // EQUAL
+  DALI_TEST_CHECK( CompareEnums( "", "" ) );
+  DALI_TEST_CHECK( CompareEnums( "HELLO", "HELLO" ) );
+  DALI_TEST_CHECK( CompareEnums( "HELLO", "hello" ) );
+  DALI_TEST_CHECK( CompareEnums( "hello", "HELLO" ) );
+  DALI_TEST_CHECK( CompareEnums( "hello-world", "HELLO_WORLD" ) );
+  DALI_TEST_CHECK( CompareEnums( "hello_WORLD", "HELLO-world" ) );
+  DALI_TEST_CHECK( CompareEnums( "hello_WORLD-", "HELLO-world_" ) );
+  DALI_TEST_CHECK( CompareEnums( "_hello_WORLD-", "-HELLO-world_" ) );
+  DALI_TEST_CHECK( CompareEnums( "-hello_WORLD-", "_HELLO-world_" ) );
+  DALI_TEST_CHECK( CompareEnums( "hello123", "HELLO123" ) );
+
+  // NOT EQUAL
+  DALI_TEST_CHECK( ! CompareEnums( "hello", "HELLOWORLD" ) );
+
+  END_TEST;
+}
index c19b6ed..b6a3ea4 100644 (file)
@@ -2635,7 +2635,7 @@ void Actor::SetDefaultProperty( Property::Index index, const Property::Value& pr
 
     case Dali::Actor::SIZE_MODE:
     {
-      SetSizeMode( Scripting::GetEnumeration< SizeMode >( property.Get<std::string>(), SIZE_MODE_TABLE, SIZE_MODE_TABLE_COUNT ) );
+      SetSizeMode( Scripting::GetEnumeration< SizeMode >( property.Get<std::string>().c_str(), SIZE_MODE_TABLE, SIZE_MODE_TABLE_COUNT ) );
       break;
     }
 
index 3903439..5e34b56 100644 (file)
@@ -153,36 +153,29 @@ const unsigned int IMAGE_SCALING_MODE_TABLE_COUNT = sizeof( IMAGE_SCALING_MODE_T
 
 } // unnamed namespace
 
-bool CompareEnums(const std::string& a, const std::string& b)
+bool CompareEnums( const char * a, const char * b )
 {
-  std::string::const_iterator ia = a.begin();
-  std::string::const_iterator ib = b.begin();
-
-  while( (ia != a.end()) && (ib != b.end()) )
+  while( ( *a != '\0' ) && ( *b != '\0' ) )
   {
-    char ca = *ia;
-    char cb = *ib;
+    char ca = *a;
+    char cb = *b;
 
-    if(ca == '-' || ca == '_')
+    if( ( ( ca == '-' ) || ( ca == '_') ) &&
+        ( ( cb == '-' ) || ( cb == '_') ) )
     {
-      ++ia;
+      ++a;
+      ++b;
       continue;
     }
 
-    if(cb == '-' || cb == '_')
+    if( ( 'A' <= ca ) && ( ca <= 'Z') )
     {
-      ++ib;
-      continue;
+      ca = ca + ( 'a' - 'A' );
     }
 
-    if( 'A' <= ca && ca <= 'Z')
+    if( ( 'A' <= cb ) && ( cb <= 'Z') )
     {
-      ca = ca + ('a' - 'A');
-    }
-
-    if( 'A' <= cb && cb <= 'Z')
-    {
-      cb = cb + ('a' - 'A');
+      cb = cb + ( 'a' - 'A' );
     }
 
     if( ca != cb )
@@ -190,25 +183,22 @@ bool CompareEnums(const std::string& a, const std::string& b)
       return false;
     }
 
-    ++ia;
-    ++ib;
+    ++a;
+    ++b;
   }
 
-  if( (ia == a.end() && ib == b.end() ) )
+  if( ( *a == '\0' ) && ( *b == '\0' ) )
   {
     return true;
   }
-  else
-  {
-    return false;
-  }
 
+  return false;
 }
 
 
 ColorMode GetColorMode( const std::string& value )
 {
-  return GetEnumeration< ColorMode >( value, COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
+  return GetEnumeration< ColorMode >( value.c_str(), COLOR_MODE_TABLE, COLOR_MODE_TABLE_COUNT );
 }
 
 
@@ -219,7 +209,7 @@ std::string GetColorMode( ColorMode value )
 
 PositionInheritanceMode GetPositionInheritanceMode( const std::string& value )
 {
-  return GetEnumeration< PositionInheritanceMode >( value, POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
+  return GetEnumeration< PositionInheritanceMode >( value.c_str(), POSITION_INHERITANCE_MODE_TABLE, POSITION_INHERITANCE_MODE_TABLE_COUNT );
 }
 
 
@@ -231,7 +221,7 @@ std::string GetPositionInheritanceMode( PositionInheritanceMode value )
 
 DrawMode::Type GetDrawMode( const std::string& value )
 {
-  return GetEnumeration< DrawMode::Type >( value, DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
+  return GetEnumeration< DrawMode::Type >( value.c_str(), DRAW_MODE_TABLE, DRAW_MODE_TABLE_COUNT );
 }
 
 
@@ -243,7 +233,7 @@ std::string GetDrawMode( DrawMode::Type value )
 
 Vector3 GetAnchorConstant( const std::string& value )
 {
-  return GetEnumeration< Vector3 >( value, ANCHOR_CONSTANT_TABLE, ANCHOR_CONSTANT_TABLE_COUNT );
+  return GetEnumeration< Vector3 >( value.c_str(), ANCHOR_CONSTANT_TABLE, ANCHOR_CONSTANT_TABLE_COUNT );
 }
 
 
@@ -270,7 +260,7 @@ Image NewImage( const Property::Value& map )
     {
       DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image load-policy property is not a string" );
       std::string v(map.GetValue(field).Get<std::string>());
-      loadPolicy = GetEnumeration< ResourceImage::LoadPolicy >( v, IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT );
+      loadPolicy = GetEnumeration< ResourceImage::LoadPolicy >( v.c_str(), IMAGE_LOAD_POLICY_TABLE, IMAGE_LOAD_POLICY_TABLE_COUNT );
     }
 
     field = "release-policy";
@@ -278,7 +268,7 @@ Image NewImage( const Property::Value& map )
     {
       DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image release-policy property is not a string" );
       std::string v(map.GetValue(field).Get<std::string>());
-      releasePolicy = GetEnumeration< Image::ReleasePolicy >( v, IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT );
+      releasePolicy = GetEnumeration< Image::ReleasePolicy >( v.c_str(), IMAGE_RELEASE_POLICY_TABLE, IMAGE_RELEASE_POLICY_TABLE_COUNT );
     }
 
     if( map.HasKey("width") && map.HasKey("height") )
@@ -315,7 +305,7 @@ Image NewImage( const Property::Value& map )
     {
       DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image release-policy property is not a string" );
       std::string s(map.GetValue(field).Get<std::string>());
-      attributes.SetPixelFormat( GetEnumeration< Pixel::Format >( s, PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT ));
+      attributes.SetPixelFormat( GetEnumeration< Pixel::Format >( s.c_str(), PIXEL_FORMAT_TABLE, PIXEL_FORMAT_TABLE_COUNT ));
     }
 
     field = "scaling-mode";
@@ -323,7 +313,7 @@ Image NewImage( const Property::Value& map )
     {
       DALI_ASSERT_ALWAYS(map.GetValue(field).GetType() == Property::STRING && "Image release-policy property is not a string" );
       std::string s(map.GetValue(field).Get<std::string>());
-      attributes.SetScalingMode( GetEnumeration< ImageAttributes::ScalingMode >( s, IMAGE_SCALING_MODE_TABLE, IMAGE_SCALING_MODE_TABLE_COUNT ) );
+      attributes.SetScalingMode( GetEnumeration< ImageAttributes::ScalingMode >( s.c_str(), IMAGE_SCALING_MODE_TABLE, IMAGE_SCALING_MODE_TABLE_COUNT ) );
     }
 
     if( map.HasKey("type") )
index 6e801b2..17cc49d 100644 (file)
@@ -51,34 +51,34 @@ struct StringEnum
  * @brief Permissive comparison for string enums.
  *
  * Case insensitive and ignores '_', '-' in either string when comparing.
- * If both strings are empty return true; ie like if(a==b) but not like a.compare(b)
- * @param[in] input The input string
- * @param[in] enumString The enum string
- * @return true if the strings are equal as defined above
+ *
+ * @note If both strings are empty return true;
+ *
+ * @param[in] a The first string
+ * @param[in] b The string to compare
+ * @return true if the strings are equal as defined above. If both empty, then return true.
  */
-DALI_IMPORT_API bool CompareEnums(const std::string& input, const std::string& enumString);
+DALI_IMPORT_API bool CompareEnums( const char * a, const char * b );
 
 /**
  * @brief Set the value if strings pass a permissive compare.
  *
  * @param[in] a The input string
- * @param[in] b The input string
+ * @param[in] b The string to compare
  * @param[in] set The variable to set
  * @param[in] value The value to set
  * @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)
+bool SetIfEqual(const char * a, const char * b, T& set, const T& value)
 {
-  if( CompareEnums(a, b ) )
+  if( CompareEnums( a, b ) )
   {
     set = value;
     return true;
   }
-  else
-  {
-    return false;
-  }
+
+  return false;
 }
 
 /**
@@ -91,7 +91,7 @@ bool SetIfEqual(const std::string& a, const std::string& b, T& set, T value)
  * @return The equivalent enumeration for the given string.
  */
 template< typename T >
-T GetEnumeration( const std::string& value, const StringEnum< T >* table, unsigned int tableCount )
+T GetEnumeration( const char * value, const StringEnum< T >* table, unsigned int tableCount )
 {
   T retVal( table->value );
   bool set( false );
@@ -117,23 +117,22 @@ T GetEnumeration( const std::string& value, const StringEnum< T >* table, unsign
  * @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.
+ * @return The equivalent enumeration for the given string. Will return NULL if the value does not exist
+ *
+ * @note The caller is NOT responsible for cleaning up the returned pointer as it is statically allocated.
  */
 template< typename T >
-std::string GetEnumerationName( T value, const StringEnum< T >* table, unsigned int tableCount )
+const char * GetEnumerationName( T value, const StringEnum< T >* table, 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 table[ i ].string;
     }
   }
 
-  return string;
+  return NULL;
 }
 
 /**