Add Internal::IndexedMap
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-info-impl.h
index 13212b7..37c43ed 100644 (file)
@@ -1,35 +1,47 @@
-#ifndef __DALI_INTERNAL_TYPE_INFO_H__
-#define __DALI_INTERNAL_TYPE_INFO_H__
-
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+#ifndef DALI_INTERNAL_TYPE_INFO_H
+#define DALI_INTERNAL_TYPE_INFO_H
+#define USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO /// For safety. If some problem occured, just remove this define.
+
+/*
+ * Copyright (c) 2022 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // EXTERNAL INCLUDES
 #include <string>
 
 // INTERNAL INCLUDES
+#ifdef USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO
+#include <dali/internal/common/indexed-const-string-map.h>
+#include <dali/internal/common/indexed-integer-map.h>
+#endif
+
+#include <dali/devel-api/object/csharp-type-info.h>
+#include <dali/internal/common/const-string.h>
+#include <dali/internal/event/object/default-property-metadata.h>
 #include <dali/public-api/object/base-handle.h>
 #include <dali/public-api/object/base-object.h>
+#include <dali/public-api/object/property.h>
 #include <dali/public-api/object/type-info.h>
 
 namespace Dali
 {
-
 namespace Internal
 {
+class PropertyDetails;
+class TypeRegistry;
 
 /**
  * A TypeInfo class to support registered type creation, and introspection of available
@@ -47,54 +59,151 @@ public:
    * @param [baseName] the base type registered name
    * @param [creator] the creator function for this type
    */
-  TypeInfo(const std::string &name, const std::string &baseName, Dali::TypeInfo::CreateFunction creator);
+  TypeInfo(const std::string& name, const std::string& baseName, Dali::TypeInfo::CreateFunction creator, const Dali::PropertyDetails* defaultProperties, Property::Index defaultPropertyCount);
 
   /**
-   *
+   * Create TypeInfo for a csharp object
+   * @param [name] the registered name
+   * @param [baseName] the base type registered name
+   * @param [creator] the creator function for this type
+   */
+  TypeInfo(const std::string& name, const std::string& baseName, Dali::CSharpTypeInfo::CreateFunction creator);
+
+  /**
+   * Destructor
    */
-  ~TypeInfo();
+  ~TypeInfo() override;
 
   /**
    * @copydoc Dali::TypeInfo::GetName
    */
-  const std::string& GetName();
+  const std::string& GetName() const;
 
   /**
    * @copydoc Dali::TypeInfo::GetBaseName
    */
-  const std::string& GetBaseName();
+  const std::string& GetBaseName() const;
 
   /**
    * @copydoc TypeInfo::CreateFunction
    */
-  BaseHandle CreateInstance();
+  BaseHandle CreateInstance() const;
 
   /**
    * @copydoc Dali::TypeInfo::GetCreator
    */
-  Dali::TypeInfo::CreateFunction GetCreator();
+  Dali::TypeInfo::CreateFunction GetCreator() const;
+
+  /**
+   * @copydoc Dali::TypeInfo::GetActionCount
+   */
+  uint32_t GetActionCount() const;
+
+  /**
+   * @copydoc Dali::TypeInfo::GetActionName
+   */
+  std::string GetActionName(uint32_t index) const;
+
+  /**
+   * @copydoc Dali::TypeInfo::GetSignalCount
+   */
+  uint32_t GetSignalCount() const;
+
+  /**
+   * @copydoc Dali::TypeInfo::GetSignalName
+   */
+  std::string GetSignalName(uint32_t index) const;
+
+  /**
+   * @copydoc Dali::TypeInfo::GetPropertyCount
+   */
+  uint32_t GetPropertyCount() const;
 
   /**
-   * @copydoc Dali::TypeInfo::GetActions
+   * Adds the property indices to the container specified.
+   * @param[in/out] indices The container where the property indices are added.
    */
-  Dali::TypeInfo::NameContainer GetActions();
+  void GetPropertyIndices(Property::IndexContainer& indices) const;
 
   /**
-   * @copydoc Dali::TypeInfo::GetSignals
+   * @copydoc Dali::TypeInfo::GetPropertyName() const
+   * this API exists to keep the old public API, which cannot be changed
    */
-  Dali::TypeInfo::NameContainer GetSignals();
+  std::string_view GetRegisteredPropertyName(Property::Index index) const;
+
+  /**
+   * Returns the property name for given index
+   *
+   * @param index of the property
+   * @return name or empty string
+   */
+  std::string_view GetPropertyName(Property::Index index) const;
 
   /*
    * Add an action function
    */
-  void AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function );
+  void AddActionFunction(std::string actionName, Dali::TypeInfo::ActionFunction function);
 
   /*
    * Add a function for connecting a signal.
    * @param[in] signalName The name of the signal.
    * @param[in] function The function used for connecting to the signal.
    */
-  void AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunctionV2 function );
+  void AddConnectorFunction(std::string signalName, Dali::TypeInfo::SignalConnectorFunction function);
+
+  /**
+   * Adds an event-thread only property to the type.
+   * @param[in] name The name of the property.
+   * @param[in] index The index of the property.
+   * @param[in] type The Property::Type.
+   * @param[in] setFunc The function to call to set the property (Can be nullptr).
+   * @param[in] getFunc The function to call to retrieve the value of the property.
+   */
+  void AddProperty(std::string name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc);
+
+  /**
+   * Adds an event-thread only property to the type.
+   * @param[in] name The name of the property.
+   * @param[in] index The index of the property.
+   * @param[in] type The Property::Type.
+   * @param[in] setFunc The function to call to set the property (Can be nullptr).
+   * @param[in] getFunc The function to call to retrieve the value of the property.
+   */
+  void AddProperty(std::string name, Property::Index index, Property::Type type, Dali::CSharpTypeInfo::SetPropertyFunction setFunc, Dali::CSharpTypeInfo::GetPropertyFunction getFunc);
+
+  /**
+   * Adds an animatable property to the type.
+   * @param[in] name The name of the property.
+   * @param[in] index The index of the property
+   * @param[in] type The Property::Type.
+   */
+  void AddAnimatableProperty(std::string name, Property::Index index, Property::Type type);
+
+  /**
+   * Adds an animatable property with the given default value.
+   * @param[in] name The name of the property.
+   * @param[in] index The index of the property
+   * @param[in] type The Property::Type.
+   */
+  void AddAnimatableProperty(std::string name, Property::Index index, Property::Value defaultValue);
+
+  /**
+   * Adds a component of an animatable property to the type.
+   * The animatable property must have been type-registered and must be a Vector2, Vector3 or Vector4 type.
+   * @param[in] name The name of the component.
+   * @param[in] index The index of the property
+   * @param[in] baseIndex The index of the base animatable property
+   * @param[in] component The index The index of the component.
+   */
+  void AddAnimatablePropertyComponent(std::string name, Property::Index index, Property::Index baseIndex, uint32_t componentIndex);
+
+  /**
+   * Adds a child property to the type.
+   * @param[in] name The name of the property.
+   * @param[in] index The index of the property
+   * @param[in] type The Property::Type.
+   */
+  void AddChildProperty(std::string name, Property::Index index, Property::Type type);
 
   /**
    * Do an action on base object
@@ -103,7 +212,7 @@ public:
    * @param [in] properties The arguments of the action
    * @return bool If the action could be executed
    */
-  bool DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties);
+  bool DoActionTo(BaseObject* object, const std::string& actionName, const Property::Map& properties);
 
   /**
    * Connects a callback function with the object's signals.
@@ -114,21 +223,217 @@ public:
    * @return True if the signal was connected.
    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
    */
-  bool ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor );
+  bool ConnectSignal(BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor);
+
+  /**
+   * Given a property name, retrieve the index.
+   * @param[in] name The name of the property.
+   * @return The index associated with that name.
+   */
+  Property::Index GetPropertyIndex(ConstString name) const;
+
+  /**
+   * Given a property index, retrieve the index of its base property.
+   * @param[in] index The index of the property.
+   * @return The index of the base property associated with the given property index.
+   */
+  Property::Index GetBasePropertyIndex(Property::Index index) const;
+
+  /**
+   * Given a property index, retrieve its component index.
+   * @param[in] index The index of the property.
+   * @return The component index associated with that property index.
+   */
+  int32_t GetComponentIndex(Property::Index index) const;
+
+  /**
+   * @param[in] index The property index.
+   * @return True, if writable, false otherwise.
+   */
+  bool IsPropertyWritable(Property::Index index) const;
+
+  /**
+   * @param[in] index The property index.
+   * @return True, if animatable, false otherwise.
+   */
+  bool IsPropertyAnimatable(Property::Index index) const;
+
+  /**
+   * @param[in] index The property index.
+   * @return True, if a constraint input, false otherwise.
+   */
+  bool IsPropertyAConstraintInput(Property::Index index) const;
+
+  /**
+   * Retrieve the Property::Type of the property at the given index.
+   * @param[in] index The property index.
+   * @return The Property::Type at that index.
+   */
+  Property::Type GetPropertyType(Property::Index index) const;
+
+  /**
+   * Given a child property name, retrieve the index.
+   * @param[in] name The name of the child property.
+   * @return The index associated with that name.
+   */
+  Property::Index GetChildPropertyIndex(ConstString name) const;
+
+  /**
+   * Retrieve the name of the child property at the given index.
+   * @param[in] index The property index.
+   * @return The name of the child property.
+   */
+  std::string_view GetChildPropertyName(Property::Index index) const;
+
+  /**
+   * Retrieve the Property::Type of the child property at the given index.
+   * @param[in] index The property index.
+   * @return The Property::Type at that index.
+   */
+  Property::Type GetChildPropertyType(Property::Index index) const;
+
+  /**
+   * Retrive the child indices into the given container.
+   * @param[in,out] indices The container to put the child indices into
+   */
+  void GetChildPropertyIndices(Property::IndexContainer& indices) const;
+
+  /**
+   * Retrieve the default value of the property at the given index.
+   * @param[in] index The property index.
+   * @return The default property value at that index.
+   */
+  Property::Value GetPropertyDefaultValue(Property::Index index) const;
+
+  /**
+   * Sets the value of a property at the index specified for the given object.
+   * @param[in] object The object whose property is to be set.
+   * @param[in] index The property index.
+   * @param[in] value The value to set.
+   */
+  void SetProperty(BaseObject* object, Property::Index index, Property::Value value) const;
+
+  /**
+   * Sets the value of a property with the name specified for the given object.
+   * @param[in] object The object whose property is to be set.
+   * @param[in] name The property name.
+   * @param[in] value The value to set.
+   */
+  void SetProperty(BaseObject* object, const std::string& name, Property::Value value) const;
+
+  /**
+   * Retrieves the value of a property at the index specified for the given object.
+   * @param[in] object The object whose property is to be queried.
+   * @param[in] index The property index.
+   * @return The current value of the property.
+   */
+  Property::Value GetProperty(const BaseObject* object, Property::Index index) const;
+
+  /**
+   * Retrieves the value of a property with the name specified for the given object.
+   * @param[in] object The object whose property is to be queried.
+   * @param[in] name The property name.
+   * @return The current value of the property.
+   */
+  Property::Value GetProperty(const BaseObject* object, const std::string& name) const;
 
 private:
+  struct RegisteredProperty
+  {
+    RegisteredProperty(Property::Type propType, ConstString propName, Property::Index basePropertyIndex, int32_t componentIndex)
+    : type(propType),
+      setFunc(nullptr),
+      getFunc(nullptr),
+      name(propName),
+      basePropertyIndex(basePropertyIndex),
+      componentIndex(componentIndex)
+    {
+    }
+
+    RegisteredProperty(Property::Type propType, Dali::TypeInfo::SetPropertyFunction set, Dali::TypeInfo::GetPropertyFunction get, ConstString propName, Property::Index basePropertyIndex, int componentIndex)
+    : type(propType),
+      setFunc(set),
+      getFunc(get),
+      name(propName),
+      basePropertyIndex(basePropertyIndex),
+      componentIndex(componentIndex)
+    {
+    }
 
-  typedef std::pair<std::string, Dali::TypeInfo::SignalConnectorFunctionV2 > ConnectionPairV2;
-  typedef std::pair<std::string, Dali::TypeInfo::ActionFunction > ActionPair;
+    RegisteredProperty(Property::Type propType, Dali::CSharpTypeInfo::SetPropertyFunction set, Dali::CSharpTypeInfo::GetPropertyFunction get, ConstString propName, Property::Index basePropertyIndex, int componentIndex)
+    : type(propType),
+      cSharpSetFunc(set),
+      cSharpGetFunc(get),
+      name(propName),
+      basePropertyIndex(basePropertyIndex),
+      componentIndex(componentIndex)
+    {
+    }
 
-  typedef std::vector< ActionPair > ActionContainer;
-  typedef std::vector< ConnectionPairV2 > ConnectorContainerV2;
+    Property::Type type = Property::NONE;
+    union
+    {
+      Dali::TypeInfo::SetPropertyFunction       setFunc = nullptr;
+      Dali::CSharpTypeInfo::SetPropertyFunction cSharpSetFunc; // only one field can be initialized but this will have same value anyways
+    };
+    union
+    {
+      Dali::TypeInfo::GetPropertyFunction       getFunc = nullptr;
+      Dali::CSharpTypeInfo::GetPropertyFunction cSharpGetFunc; // only one field can be initialized but this will have same value anyways
+    };
+    ConstString     name;
+    Property::Index basePropertyIndex = Property::INVALID_INDEX;
+    int32_t         componentIndex    = Property::INVALID_COMPONENT_INDEX;
+  };
 
-  std::string mTypeName;
-  std::string mBaseTypeName;
-  Dali::TypeInfo::CreateFunction mCreate;
-  ActionContainer mActions;
-  ConnectorContainerV2 mSignalConnectors;
+  using ActionPair     = std::pair<std::string, Dali::TypeInfo::ActionFunction>;
+  using ConnectionPair = std::pair<std::string, Dali::TypeInfo::SignalConnectorFunction>;
+#ifdef USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO
+  using RegisteredPropertyPair   = std::pair<std::uint32_t, RegisteredProperty>;
+  using PropertyDefaultValuePair = std::pair<std::uint32_t, Property::Value>;
+#else
+  using RegisteredPropertyPair        = std::pair<Property::Index, RegisteredProperty>;
+  using PropertyDefaultValuePair      = std::pair<Property::Index, Property::Value>;
+#endif
+
+#ifdef USE_INDEXED_MAP_CONTAINER_AT_TYPE_INFO
+  using ActionContainer               = Dali::Internal::IndexedConstStringMap<Dali::TypeInfo::ActionFunction>;
+  using ConnectorContainer            = Dali::Internal::IndexedConstStringMap<Dali::TypeInfo::SignalConnectorFunction>;
+  using RegisteredPropertyContainer   = Dali::Internal::IndexedIntegerMap<RegisteredProperty>;
+  using PropertyDefaultValueContainer = Dali::Internal::IndexedIntegerMap<Property::Value>;
+#else
+  using ActionContainer               = std::vector<ActionPair>;
+  using ConnectorContainer            = std::vector<ConnectionPair>;
+  using RegisteredPropertyContainer   = std::vector<RegisteredPropertyPair>;
+  using PropertyDefaultValueContainer = std::vector<PropertyDefaultValuePair>;
+#endif
+
+  /**
+   * Append properties from registeredProperties onto indices.
+   * @param[in,out] indices The vector to append indices onto
+   * @param[in] registeredProperties The container to retrive indices from
+   */
+  void AppendProperties(Dali::Property::IndexContainer&              indices,
+                        const TypeInfo::RegisteredPropertyContainer& registeredProperties) const;
+
+private:
+  TypeRegistry&               mTypeRegistry;
+  mutable Internal::TypeInfo* mBaseType; // allow changing from const methods, initialised inside constructor
+  std::string                 mTypeName;
+  std::string                 mBaseTypeName;
+  union
+  {
+    Dali::TypeInfo::CreateFunction       mCreate = nullptr;
+    Dali::CSharpTypeInfo::CreateFunction mCSharpCreate; // only one field can be initialized but this will have same value anyways
+  };
+  ActionContainer               mActions;
+  ConnectorContainer            mSignalConnectors;
+  RegisteredPropertyContainer   mRegisteredProperties;
+  RegisteredPropertyContainer   mRegisteredChildProperties;
+  PropertyDefaultValueContainer mPropertyDefaultValues;
+  const Dali::PropertyDetails*  mDefaultProperties    = nullptr;
+  Property::Index               mDefaultPropertyCount = 0;
+  bool                          mCSharpType           = false; ///< Whether this type info is for a CSharp control (instead of C++)
 };
 
 } // namespace Internal
@@ -155,4 +460,4 @@ inline const Internal::TypeInfo& GetImplementation(const Dali::TypeInfo& typeInf
 
 } // namespace Dali
 
-#endif // header
+#endif // DALI_INTERNAL_TYPE_INFO_H