Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
Helper class to manage decoupling of components. More...
#include <cs_Component.h>
Public Member Functions | |
template<class T > | |
T * | getComponent (Component *requester=nullptr) |
Returns a component of type T* from _parent->children(), If not found try again with ancestors: _parent-> ... ->_parent->children(). More... | |
virtual cs_ret_code_t | init () |
Components can implement this if they need to get references to sibling or if they need to do specific initialization. More... | |
void | parentAllChildren () |
utility that loops over all elements of getChildren() and setParent on the non-nullptr ones. More... | |
virtual | ~Component ()=default |
Protected Member Functions | |
virtual std::vector< Component * > | getChildren () |
Components with children can override this method to return them. More... | |
cs_ret_code_t | initChildren () |
void | setParent (Component *p) |
Children that are instantiated later can also be added individually. More... | |
Private Attributes | |
Component * | _parent = nullptr |
Helper class to manage decoupling of components.
Features that have many parts, can be organized in components E.g.:
These parts may depend on each other, but we don't want to expose their existance to the whole firmware through global/static variables.
Using this class 'sibling components' can query for each others presence in a unified way, without need for static classes or other rigid dependencies.
|
virtualdefault |
|
inlineprotectedvirtual |
Components with children can override this method to return them.
This is used by getComponent<> to search for available components.
Reimplemented in Crownstone, AssetFiltering, and SwitchAggregator.
T * Component::getComponent | ( | Component * | requester = nullptr | ) |
Returns a component of type T* from _parent->children(), If not found try again with ancestors: _parent-> ... ->_parent->children().
If none-exists, a nullptr is returned.
Usage: auto sibling = getComponent<SiblingComponentType>();
|
inlinevirtual |
Components can implement this if they need to get references to sibling or if they need to do specific initialization.
E.g.
class componentX : public Component { public: cs_ret_code_t init() { // construct childA ... // construct childZ
return initChildren(); } };
Reimplemented in AssetFiltering, AssetFilterStore, AssetFilterSyncer, AssetForwarder, BehaviourHandler, BehaviourStore, TwilightHandler, AssetStore, NearestCrownstoneTracker, and PresenceHandler.
|
protected |
Components are not required to use this. They can call all inits of children in custom order if they need to. (And implement elegant failure.)
Initialisation is in order of the elements of the returnvalue of getChildren.
void Component::parentAllChildren | ( | ) |
utility that loops over all elements of getChildren() and setParent on the non-nullptr ones.
|
protected |
Children that are instantiated later can also be added individually.
|
private |