From e6f417dab998ffd3a524ae01119f0c209059541c Mon Sep 17 00:00:00 2001 From: Subhransu Mohanty Date: Mon, 14 Sep 2020 10:00:34 +0900 Subject: [PATCH] refactor PropertyMetadata table. - keep string_view insted of const char* for performance. - make both table constexpr so that we can sanity check during compile time. Change-Id: I13a424b3f9c5fadd52ea02889ba6d687f8f41346 --- dali/internal/event/common/property-helper.h | 34 +++++----------------- dali/internal/event/common/type-info-impl.cpp | 4 +-- .../event/object/default-property-metadata.h | 23 +++++++++++++-- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/dali/internal/event/common/property-helper.h b/dali/internal/event/common/property-helper.h index 74484ba..2d9146f 100644 --- a/dali/internal/event/common/property-helper.h +++ b/dali/internal/event/common/property-helper.h @@ -34,34 +34,16 @@ namespace Internal /** * These macros are used to define a table of property details per object. - * The index property is only compiled in for DEBUG_ENABLED builds and allows checking the table index VS the property enum index. - * DALI_PROPERTY_TABLE_END Forces a run-time check that will happen once. + * Checking of the table index VS the property enum index happens during compile time. * the macros define an instance of PropertyMetadata with the name that is passed to DALI_PROPERTY_TABLE_END */ -#define DALI_PROPERTY_TABLE_BEGIN const Dali::PropertyDetails DEFAULT_PROPERTY_DETAILS[] = { -#ifdef DEBUG_ENABLED -#define DALI_PROPERTY_TABLE_END( startIndex, constantName ) }; const Property::Index DEFAULT_PROPERTY_COUNT = static_cast( sizeof( DEFAULT_PROPERTY_DETAILS ) / sizeof( Dali::PropertyDetails ) ); \ - struct PROPERTY_CHECK \ - { \ - PROPERTY_CHECK() \ - { \ - for( int i = 0; i < DEFAULT_PROPERTY_COUNT; i++ ) \ - { \ - if ( DEFAULT_PROPERTY_DETAILS[i].enumIndex != ( startIndex + i ) ) \ - { \ - DALI_LOG_ERROR( "Checking property failed: index:%d, enumIndex:%d == index+start:%d, (name:%s)\n", i, \ - DEFAULT_PROPERTY_DETAILS[i].enumIndex, (startIndex + i), DEFAULT_PROPERTY_DETAILS[i].name ); \ - DALI_ASSERT_DEBUG( false && "Property enumeration mismatch" ); \ - } \ - } \ - } \ - }; \ - constexpr Dali::DefaultPropertyMetadata constantName{ DEFAULT_PROPERTY_DETAILS, DEFAULT_PROPERTY_COUNT }; \ - static PROPERTY_CHECK PROPERTY_CHECK_INSTANCE; -#else -#define DALI_PROPERTY_TABLE_END( startIndex, constantName ) }; const Property::Index DEFAULT_PROPERTY_COUNT = static_cast( sizeof( DEFAULT_PROPERTY_DETAILS ) / sizeof( Dali::PropertyDetails ) );\ - constexpr Dali::DefaultPropertyMetadata constantName{ DEFAULT_PROPERTY_DETAILS, DEFAULT_PROPERTY_COUNT }; -#endif +#define DALI_PROPERTY_TABLE_BEGIN static constexpr Dali::PropertyDetails DEFAULT_PROPERTY_DETAILS[] = { + +#define DALI_PROPERTY_TABLE_END(startIndex, tableName) \ + }; \ + static constexpr auto tableName = GeneratePropertyMetadata(DEFAULT_PROPERTY_DETAILS); \ + static_assert(CheckPropertyMetadata(tableName, startIndex), "Property enumeration mismatch"); + #define DALI_PROPERTY( text, type, writable, animatable, constraint, index ) { text, index, Dali::Property::type, writable, animatable, constraint }, /** diff --git a/dali/internal/event/common/type-info-impl.cpp b/dali/internal/event/common/type-info-impl.cpp index 8fd9fb8..48a549e 100644 --- a/dali/internal/event/common/type-info-impl.cpp +++ b/dali/internal/event/common/type-info-impl.cpp @@ -413,7 +413,7 @@ std::string TypeInfo::GetPropertyName( Property::Index index ) const // default or custom if ( mDefaultProperties && ( index < DEFAULT_PROPERTY_MAX_COUNT ) ) { - const char* name = nullptr; + std::string_view name; if( GetDefaultPropertyField( mDefaultProperties, mDefaultPropertyCount,index, &Dali::PropertyDetails::name, name ) ) { propertyName = name; @@ -630,7 +630,7 @@ Property::Index TypeInfo::GetPropertyIndex( const std::string& name ) const { for( Property::Index tableIndex = 0; tableIndex < mDefaultPropertyCount; ++tableIndex ) { - if( 0 == name.compare( mDefaultProperties[ tableIndex ].name ) ) + if(mDefaultProperties[tableIndex].name == name) { index = mDefaultProperties[ tableIndex ].enumIndex; found = true; diff --git a/dali/internal/event/object/default-property-metadata.h b/dali/internal/event/object/default-property-metadata.h index a95a9cd..734eb61 100644 --- a/dali/internal/event/object/default-property-metadata.h +++ b/dali/internal/event/object/default-property-metadata.h @@ -18,6 +18,9 @@ * */ +// EXTERNAL INCLUDES +#include + // INTERNAL INCLUDES #include @@ -33,7 +36,7 @@ namespace Dali */ struct PropertyDetails { - const char* name; ///< The name of the property. + std::string_view name; ///< The name of the property. Property::Index enumIndex; ///< Used to check the index is correct within a debug build. Property::Type type; ///< The property type. bool writable; ///< Whether the property is writable @@ -47,9 +50,25 @@ struct PropertyDetails struct DefaultPropertyMetadata { const PropertyDetails* propertyTable; ///< address of the table defining property meta-data. - Property::Index propertyCount; ///< count of the default properties. + const Property::Index propertyCount; ///< count of the default properties. }; +inline constexpr bool CheckPropertyMetadata(const DefaultPropertyMetadata& table, Property::Index startIndex) noexcept +{ + for(int i = 0; i < table.propertyCount; i++) + { + if(table.propertyTable[i].enumIndex != startIndex + i) + return false; + } + return true; +} + +template +inline constexpr DefaultPropertyMetadata GeneratePropertyMetadata(const PropertyDetails (&array)[N]) noexcept +{ + return {array, N}; +} + } // namespace Dali #endif // DALI_DEFAULT_PROPERTY_METADATA_H -- 2.7.4