Difference between revisions of "W4 Component System"

From Ciliz|W4
(Created page with "== Scope == The W4 framework comes with a component system. A component in this case is the ability to extend the functionality of an object without inheritance. System featu...")
 
Line 9: Line 9:
  
 
== IComponent class ==
 
== IComponent class ==
 +
Let's consider the main methods of the '''IComponent''' public interface:
 +
* '''const Id& id() const;''' - this method allows you to get the id of the component;
 +
* '''void enable(bool isEnabled);''' - enable/disable component. Using this method, you can temporarily disable the functionality of the component and avoid construction and destruction (save the component internal state). A disabled component disappears from the global list of components and the '''update()''' method is no longer called for it;
 +
* '''bool isEnabled() const;''' - this method lets you know if the component is enabled;
 +
* '''template<typename T> T& as();''' - this method casts the pointer/reference to the base component to the component of the desired type;
 +
* '''::w4::core::Node & getOwner();''' - this method allows you to get the owner of the given component (the container in which it is contained);
 +
* '''const core::TypeInfo& getTypeInfo() const;''' - this method allows you to get information about the type of the component by the pointer/reference to the base component;
 +
This class has methods that are not recommended to be called directly (this can lead to incorrect application behavior).

Revision as of 11:55, 3 August 2020

Scope

The W4 framework comes with a component system. A component in this case is the ability to extend the functionality of an object without inheritance.

System features:

  • All components inherit from the base IComponent class.
  • Each component has a unique identifier for its type, by which the component can always be obtained from the container.
  • The container for components is the Node class (see W4 Coordinate system and Node structure).
  • A global list of components is maintained, allowing access to all components of a certain type from anywhere in the application.

IComponent class

Let's consider the main methods of the IComponent public interface:

  • const Id& id() const; - this method allows you to get the id of the component;
  • void enable(bool isEnabled); - enable/disable component. Using this method, you can temporarily disable the functionality of the component and avoid construction and destruction (save the component internal state). A disabled component disappears from the global list of components and the update() method is no longer called for it;
  • bool isEnabled() const; - this method lets you know if the component is enabled;
  • template<typename T> T& as(); - this method casts the pointer/reference to the base component to the component of the desired type;
  • ::w4::core::Node & getOwner(); - this method allows you to get the owner of the given component (the container in which it is contained);
  • const core::TypeInfo& getTypeInfo() const; - this method allows you to get information about the type of the component by the pointer/reference to the base component;

This class has methods that are not recommended to be called directly (this can lead to incorrect application behavior).