From: Tom Robinson Date: Tue, 26 Jul 2016 14:32:53 +0000 (+0100) Subject: Helper method for getting enum properties as ints and strings X-Git-Tag: dali_1.1.45~6 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-core.git;a=commitdiff_plain;h=4ca95a150b05032a3d42faa44252227f8cc7c556 Helper method for getting enum properties as ints and strings Change-Id: I9a1b40988f8abaff7b6f9ac02bffbdeb6dfe2516 --- diff --git a/automated-tests/src/dali/utc-Dali-Scripting.cpp b/automated-tests/src/dali/utc-Dali-Scripting.cpp index c0208b7..5b4bb99 100644 --- a/automated-tests/src/dali/utc-Dali-Scripting.cpp +++ b/automated-tests/src/dali/utc-Dali-Scripting.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2016 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. @@ -831,6 +831,85 @@ int UtcDaliScriptingGetLinearEnumerationNameN(void) END_TEST; } +int UtcDaliScriptingGetEnumerationProperty(void) +{ + /* + * This test function performs the following checks: + * - An enum can be looked up from a Property::Value of type INTEGER. + * - An enum can be looked up from a Property::Value of type STRING. + * - An enum can NOT be looked up other Property::Value types. + * - The return value is "true" if the property can be successfully converted AND it has changed. + * - The return value is "false" if the property can be successfully converted BUT it has NOT changed. + * - The return value is "false" if the property can not be successfully converted. + * - The result value is only updated if the return value is "true" (IE. successful conversion and property value has changed). + */ + + // String to Enum property table to test with (equivalent to ones used within DALi). + const Dali::Scripting::StringEnum testTable[] = { + { "NONE", FaceCullingMode::NONE }, + { "FRONT", FaceCullingMode::FRONT }, + { "BACK", FaceCullingMode::BACK }, + { "FRONT_AND_BACK", FaceCullingMode::FRONT_AND_BACK } + }; const unsigned int testTableCount = sizeof( testTable ) / sizeof( testTable[0] ); + + // TEST: An enum can be looked up from a Property::Value of type INTEGER. + // Initialise to first element. + FaceCullingMode::Type result = FaceCullingMode::NONE; + // Set the input property value to a different value (to emulate a change). + Property::Value propertyValueInteger( FaceCullingMode::FRONT ); + + // Perform the lookup. + bool returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueInteger, testTable, testTableCount, result ); + + // TEST: The return value is "true" if the property can be successfully converted AND it has changed + // Check the property could be converted. + DALI_TEST_CHECK( returnValue ); + + DALI_TEST_EQUALS( static_cast( result ), static_cast( FaceCullingMode::FRONT ), TEST_LOCATION ); + + // Now emulate a property-set with the same value. false should be returned. + returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueInteger, testTable, testTableCount, result ); + + // TEST: The return value is "false" if the property can be successfully converted BUT it has NOT changed. + DALI_TEST_CHECK( !returnValue ); + + // The result should remain the same. + DALI_TEST_EQUALS( static_cast( result ), static_cast( FaceCullingMode::FRONT ), TEST_LOCATION ); + + // TEST: An enum can be looked up from a Property::Value of type STRING. + // Set the input property value to a different value (to emulate a change). + Property::Value propertyValueString( "BACK" ); + + returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueString, testTable, testTableCount, result ); + + DALI_TEST_CHECK( returnValue ); + + // The result should remain the same. + DALI_TEST_EQUALS( static_cast( result ), static_cast( FaceCullingMode::BACK ), TEST_LOCATION ); + + returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueString, testTable, testTableCount, result ); + + DALI_TEST_CHECK( !returnValue ); + + // The result should remain the same. + DALI_TEST_EQUALS( static_cast( result ), static_cast( FaceCullingMode::BACK ), TEST_LOCATION ); + + // TEST: An enum can NOT be looked up other Property::Value types. + Property::Value propertyValueBoolean( true ); + + returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueBoolean, testTable, testTableCount, result ); + + // TEST: The return value is "false" if the property can not be successfully converted. + // Return value should be false as Property::Value was of an unsupported type for enum properties. + DALI_TEST_CHECK( !returnValue ); + + // TEST: The result value is only updated if the return value is "true" (IE. successful conversion and property value has changed). + // The result should remain the same. + DALI_TEST_EQUALS( static_cast( result ), static_cast( FaceCullingMode::BACK ), TEST_LOCATION ); + + END_TEST; +} + int UtcDaliScriptingFindEnumIndexN(void) { const Scripting::StringEnum myTable[] = @@ -859,7 +938,7 @@ int UtcDaliScriptingEnumStringToIntegerP(void) }; const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] ); - unsigned int integerEnum = 0; + int integerEnum = 0; DALI_TEST_CHECK( EnumStringToInteger( "ONE", myTable, myTableCount, integerEnum ) ); DALI_TEST_EQUALS( integerEnum, (1<<1), TEST_LOCATION ); @@ -905,7 +984,7 @@ int UtcDaliScriptingEnumStringToIntegerN(void) }; const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] ); - unsigned int integerEnum = 0; + int integerEnum = 0; DALI_TEST_CHECK( !EnumStringToInteger( "Foo", myTable, myTableCount, integerEnum ) ); DALI_TEST_CHECK( !EnumStringToInteger( "", myTable, myTableCount, integerEnum ) ); @@ -934,7 +1013,7 @@ int UtcDaliScriptingEnumStringToIntegerInvalidEnumP(void) const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] ); - unsigned int integerEnum = 0; + int integerEnum = 0; DALI_TEST_CHECK( EnumStringToInteger( "", myTable, myTableCount, integerEnum ) ); DALI_TEST_EQUALS( integerEnum, 1, TEST_LOCATION ); @@ -952,7 +1031,7 @@ int UtcDaliScriptingEnumStringToIntegerInvalidEnumN(void) const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] ); - unsigned int integerEnum = 0; + int integerEnum = 0; DALI_TEST_CHECK( !EnumStringToInteger( NULL, NULL, 0, integerEnum ) ); DALI_TEST_CHECK( !EnumStringToInteger( "ONE", NULL, 0, integerEnum ) ); diff --git a/dali/devel-api/scripting/scripting.cpp b/dali/devel-api/scripting/scripting.cpp index 9d4b8e6..883592f 100644 --- a/dali/devel-api/scripting/scripting.cpp +++ b/dali/devel-api/scripting/scripting.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2016 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. @@ -111,9 +111,9 @@ const unsigned int imageTypeCount = sizeof( ImageTypeName ) / sizeof( const char } // unnamed namespace -bool EnumStringToInteger( const char * const value, const StringEnum* const enumTable, unsigned int tableCount, unsigned int& integerEnum ) +bool EnumStringToInteger( const char * const value, const StringEnum* const enumTable, unsigned int tableCount, int& integerEnum ) { - unsigned int ret = 0; + int ret = 0; bool found = false; bool done = false; diff --git a/dali/devel-api/scripting/scripting.h b/dali/devel-api/scripting/scripting.h index 544c48f..e688435 100644 --- a/dali/devel-api/scripting/scripting.h +++ b/dali/devel-api/scripting/scripting.h @@ -1,8 +1,8 @@ -#ifndef __DALI_SCRIPTING_H__ -#define __DALI_SCRIPTING_H__ +#ifndef DALI_SCRIPTING_H +#define DALI_SCRIPTING_H /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2016 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. @@ -68,7 +68,7 @@ DALI_IMPORT_API unsigned int FindEnumIndex( const char* value, const StringEnum* * @param[out] integerEnum The value of the enum. * @return true if one or more enums in value. */ -DALI_IMPORT_API bool EnumStringToInteger( const char* const value, const StringEnum* const table, unsigned int tableCount, unsigned int& integerEnum ); +DALI_IMPORT_API bool EnumStringToInteger( const char* const value, const StringEnum* const table, unsigned int tableCount, int& integerEnum ); /** * @brief Chooses the appropriate enumeration for the provided string from the given table. @@ -86,7 +86,7 @@ bool GetEnumeration( const char* value, const StringEnum* table, unsigned int ta bool retVal( false ); if( table ) { - unsigned int integerEnum = 0; + int integerEnum = 0; // check to avoid crash, not asserting on purpose, error is logged instead if( EnumStringToInteger( value, table, tableCount, integerEnum ) ) { @@ -98,6 +98,54 @@ bool GetEnumeration( const char* value, const StringEnum* table, unsigned int ta } /** + * @brief Gets the enumeration value from an enumeration property. + * An enumeration property is a property that can be set with either an INTEGER or STRING. + * + * @param[in] PropertyValue The property containing the int or string value. + * @param[in] table A pointer to an array with the enumeration to string equivalents. + * @param[in] tableCount Number of items in the array. + * @param[out] result The enum value. This is not modified if the enumeration could not be converted. + * @return True if the value was found successfully AND the value has changed. This is to allow the caller to do nothing if there is no change. + */ +template< typename T > +bool GetEnumerationProperty( const Property::Value& PropertyValue, const StringEnum* table, unsigned int tableCount, T& result ) +{ + int newValue; + bool set = false; + Property::Type type = PropertyValue.GetType(); + + if( type == Property::INTEGER ) + { + // Attempt to fetch the property as an INTEGER type. + if( PropertyValue.Get( newValue ) ) + { + // Success. + set = true; + } + } + else if( type == Property::STRING ) + { + // Attempt to fetch the property as an STRING type, and convert it from string to enumeration value. + std::string propertyString; + if( table && PropertyValue.Get( propertyString ) && EnumStringToInteger( propertyString.c_str(), table, tableCount, newValue ) ) + { + // Success. + set = true; + } + } + + // If the property was converted OK, AND the value has changed, update the result and return true. + if( set && ( result != static_cast( newValue ) ) ) + { + result = static_cast( newValue ); + return true; + } + + // No change. + return false; +} + +/** * @brief Chooses the appropriate string for the provided enumeration from the given table. * * @param[in] value The enumeration. @@ -222,4 +270,4 @@ DALI_IMPORT_API void NewAnimation( const Property::Map& map, Dali::AnimationData } // namespace Dali -#endif // __DALI_SCRIPTING_H__ +#endif // DALI_SCRIPTING_H