refactor PropertyMetadata table.
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-helper.h
index a3a4cd5..2d9146f 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_PROPERTY_HELPER_H__
-#define __DALI_PROPERTY_HELPER_H__
+#ifndef DALI_PROPERTY_HELPER_H
+#define DALI_PROPERTY_HELPER_H
 
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
  *
  */
 
+// EXTERNAL INCLUDES
+#include <cstdint>
+
 // INTERNAL INCLUDES
 #include <dali/integration-api/bitmap.h>
-#include <dali/devel-api/scripting/scripting.h>
+#include <dali/devel-api/scripting/enum-helper.h>
+#include <dali/internal/event/object/default-property-metadata.h>
 
 namespace Dali
 {
@@ -29,81 +33,18 @@ namespace Internal
 {
 
 /**
- * @brief Structure for setting up default properties and their details.
+ * These macros are used to define a table of property details per object.
+ * 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
  */
-struct PropertyDetails
-{
-  const char* name;             ///< The name of the property.
-  Property::Type type;          ///< The property type.
-  bool writable:1;              ///< Whether the property is writable
-  bool animatable:1;            ///< Whether the property is animatable.
-  bool constraintInput:1;       ///< Whether the property can be used as an input to a constraint.
-#ifdef DEBUG_ENABLED
-  Property::Index enumIndex;    ///< Used to check the index is correct within a debug build.
-#endif
-};
+#define DALI_PROPERTY_TABLE_BEGIN static constexpr Dali::PropertyDetails DEFAULT_PROPERTY_DETAILS[] = {
 
-/**
- * These macros are used to define a table of property details per Actor 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.
- */
-#define DALI_PROPERTY_TABLE_BEGIN const Internal::PropertyDetails DEFAULT_PROPERTY_DETAILS[] = {
-#ifdef DEBUG_ENABLED
-#define DALI_PROPERTY_TABLE_END( startIndex )   }; const int DEFAULT_PROPERTY_COUNT = sizeof( DEFAULT_PROPERTY_DETAILS ) / sizeof( Internal::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" ); \
-        } \
-      } \
-    } \
-  }; \
-  static PROPERTY_CHECK PROPERTY_CHECK_INSTANCE;
-#else
-#define DALI_PROPERTY_TABLE_END( startIndex )   }; const int DEFAULT_PROPERTY_COUNT = sizeof( DEFAULT_PROPERTY_DETAILS ) / sizeof( Internal::PropertyDetails );
-#endif
-#ifdef DEBUG_ENABLED
-#define DALI_PROPERTY( text, type, writable, animatable, constraint, index ) { text, Dali::Property::type, writable, animatable, constraint, index },
-#else
-#define DALI_PROPERTY( text, type, writable, animatable, constraint, index ) { text, Dali::Property::type, writable, animatable, constraint },
-#endif
+#define DALI_PROPERTY_TABLE_END(startIndex, tableName)                                  \
+  };                                                                                    \
+  static constexpr auto tableName = GeneratePropertyMetadata(DEFAULT_PROPERTY_DETAILS); \
+  static_assert(CheckPropertyMetadata(tableName, startIndex), "Property enumeration mismatch");
 
-/**
- * Macros for creating value, typically enumerations, to string tables.
- * Example:
- *
- * DALI_ENUM_TO_STRING_TABLE_BEGIN( SIZE_MODE )
- * DALI_ENUM_TO_STRING( USE_OWN_SIZE )
- * DALI_ENUM_TO_STRING( SIZE_EQUAL_TO_PARENT )
- * DALI_ENUM_TO_STRING_TABLE_END( SIZE_MODE )
- *
- * Creates:
- * const Scripting::StringEnum< SizeMode > SIZE_MODE_TABLE[] = {
- * { "USE_OWN_SIZE", USE_OWN_SIZE },
- * { "SIZE_EQUAL_TO_PARENT", SIZE_EQUAL_TO_PARENT },
- * }; const unsigned int SIZE_MODE_TABLE_COUNT = sizeof( SIZE_MODE_TABLE ) / sizeof( SIZE_MODE_TABLE[0] );
- *
- * By default, Dali::Scripting::StringEnum is used as the type for the table, however, a different type can be specified by using
- * DALI_ENUM_TO_STRING_TABLE_BEGIN_WITH_TYPE.
- */
-#define DALI_ENUM_TO_STRING_TABLE_BEGIN_WITH_TYPE( type, t ) const type t##_TABLE[] = {
-#define DALI_ENUM_TO_STRING_TABLE_BEGIN( t ) DALI_ENUM_TO_STRING_TABLE_BEGIN_WITH_TYPE( Dali::Scripting::StringEnum, t )
-#define DALI_ENUM_TO_STRING_TABLE_END( t )   }; const unsigned int t##_TABLE_COUNT = sizeof( t##_TABLE ) / sizeof( t##_TABLE[0] );
-#define DALI_ENUM_TO_STRING( s ) { #s, s },
-
-/**
- * Adds a value, typically an enum, to the table within a scope but without the scope name
- * Example converts ( Layer, LAYER_2D ) to ( "LAYER_2D", Layer::Layer2D )
- */
-#define DALI_ENUM_TO_STRING_WITH_SCOPE( className, enumName ) { #enumName, className::enumName },
+#define DALI_PROPERTY( text, type, writable, animatable, constraint, index ) { text, index, Dali::Property::type, writable, animatable, constraint },
 
 /**
  * @brief Case insensitive string comparison.
@@ -117,10 +58,29 @@ struct PropertyDetails
  *
  * @return true if strings are the same
  */
-bool CompareTokens( const char * first, const char * second, size_t& size );
+bool CompareTokens( const char * first, const char * second, uint32_t& size );
+
+
+/**
+ * @brief Helper to adjust the current value of a variable from the given property-value
+ * @param[in] currentValue The current value of the property
+ * @param[in] value The relative value as a Property::Value
+ * @return true if value adjusted, false otherwise
+ */
+template< typename PropertyType >
+bool AdjustValue( PropertyType& currentValue, const Property::Value& value )
+{
+  PropertyType relativeValue;
+  if( value.Get( relativeValue ) )
+  {
+    currentValue += relativeValue;
+    return true;
+  }
+  return false;
+}
 
 } // namespace Internal
 
 } // namespace Dali
 
-#endif // __DALI_PROPERTY_HELPER_H__
+#endif // DALI_PROPERTY_HELPER_H