refactor PropertyMetadata table. 98/243998/3
authorSubhransu Mohanty <sub.mohanty@samsung.com>
Mon, 14 Sep 2020 01:00:34 +0000 (10:00 +0900)
committerSubhransu Mohanty <sub.mohanty@samsung.com>
Tue, 15 Sep 2020 00:51:22 +0000 (09:51 +0900)
- 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
dali/internal/event/common/type-info-impl.cpp
dali/internal/event/object/default-property-metadata.h

index 74484ba..2d9146f 100644 (file)
@@ -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<Property::Index>( 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<Property::Index>( 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 },
 
 /**
index 8fd9fb8..48a549e 100644 (file)
@@ -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;
index a95a9cd..734eb61 100644 (file)
@@ -18,6 +18,9 @@
  *
  */
 
+// EXTERNAL INCLUDES
+#include <string_view>
+
 // INTERNAL INCLUDES
 #include <dali/public-api/object/property.h>
 
@@ -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<size_t N>
+inline constexpr DefaultPropertyMetadata GeneratePropertyMetadata(const PropertyDetails (&array)[N]) noexcept
+{
+  return {array, N};
+}
+
 } // namespace Dali
 
 #endif // DALI_DEFAULT_PROPERTY_METADATA_H