Kouta
A small application framework based on Boost
kouta::base::Component Class Reference

Base class for asynchronous components. More...

#include <component.hpp>

Inheritance diagram for kouta::base::Component:
Collaboration diagram for kouta::base::Component:

Public Member Functions

 Component ()=delete
 
 Component (Component *parent)
 Constructor. More...
 
 Component (const Component &)=delete
 
Componentoperator= (const Component &)=delete
 
 Component (Component &&)=delete
 
Componentoperator= (Component &&)=delete
 
virtual ~Component ()
 Component destructor. More...
 
virtual boost::asio::io_context & context ()
 Obtain a reference to the underlying I/O context. More...
 
void add_child (Component *component)
 Add a child component to the list. More...
 
void remove_child (Component *component)
 Remove a child component from the list. More...
 
template<class TClass , class... TMethodArgs, class... TArgs>
void post (void(TClass::*method)(TMethodArgs...), TArgs... args)
 Post a method call to the event loop for deferred execution. More...
 
template<class... TFuncArgs, class... TArgs>
void post (const std::function< void(TFuncArgs...)> &functor, TArgs... args)
 Post a function call to the event loop for deferred execution. More...
 
template<class TFunctor >
void post (TFunctor &&functor)
 Post a functor call to the event loop for deferred execution. More...
 

Private Attributes

Componentm_parent
 
std::vector< Component * > m_children
 

Detailed Description

Base class for asynchronous components.

A component provies access to the underlying event loop which, by default, belongs to the parent component. Moreover, specifying a parent will add the component to its children list and make sure that the component is deleted when the parent is destroyed (if the component was allocated on the heap).

Constructor & Destructor Documentation

◆ Component() [1/4]

kouta::base::Component::Component ( )
delete

◆ Component() [2/4]

kouta::base::Component::Component ( Component parent)
inlineexplicit

Constructor.

Parameters
[in]parentParent component. The lifetime of the parent must surpass that of the child.

◆ Component() [3/4]

kouta::base::Component::Component ( const Component )
delete

◆ Component() [4/4]

kouta::base::Component::Component ( Component &&  )
delete

◆ ~Component()

virtual kouta::base::Component::~Component ( )
inlinevirtual

Component destructor.

The component will go through its list of children and delete them one at a time. This is only useful if the components were allocated in the heap, as stack-allocated ones will probably have been deleted automatically prior to calling this destructor.

In addition, once a component has deleted its children, it will remove itself from its parent.

Note
Child deletion happens in reverse order.

Member Function Documentation

◆ add_child()

void kouta::base::Component::add_child ( Component component)
inline

Add a child component to the list.

This is used to keep track of objects to delete when the component has been allocated in the heap.

Note
Normally, this will only be called from the Constructor of the component.
Parameters
[in]componentPointer to the component to add.

◆ context()

virtual boost::asio::io_context& kouta::base::Component::context ( )
inlinevirtual

Obtain a reference to the underlying I/O context.

Note
By default, this I/O context comes from the parent component.

Reimplemented in kouta::base::Root.

◆ operator=() [1/2]

Component& kouta::base::Component::operator= ( Component &&  )
delete

◆ operator=() [2/2]

Component& kouta::base::Component::operator= ( const Component )
delete

◆ post() [1/3]

template<class... TFuncArgs, class... TArgs>
void kouta::base::Component::post ( const std::function< void(TFuncArgs...)> &  functor,
TArgs...  args 
)
inline

Post a function call to the event loop for deferred execution.

This allows other components, even those residing in another thread/event loop, to post a function to this specific component.

Warning
Arguments are copied before being passed to the event loop.
Template Parameters
TFuncArgsTypes of the arguments that the function accepts.
TArgsTypes of the arguments provided to the invocation.
Parameters
[in]functorFunctor to invoke. Its signature must match void(TArgs...)
[in]argsArguments to invoke the functor with.

◆ post() [2/3]

template<class TFunctor >
void kouta::base::Component::post ( TFunctor &&  functor)
inline

Post a functor call to the event loop for deferred execution.

This allows other components, even those residing in another thread/event loop, to post a functor to this specific component, for example a lambda.

Template Parameters
TFunctorFunctor type.
Parameters
[in]functorFunctor to invoke.s

◆ post() [3/3]

template<class TClass , class... TMethodArgs, class... TArgs>
void kouta::base::Component::post ( void(TClass::*)(TMethodArgs...)  method,
TArgs...  args 
)
inline

Post a method call to the event loop for deferred execution.

This allows other components, even those residing in another thread/event loop, to post a call to this specific component.

Warning
Arguments are copied before being passed to the event loop.
Template Parameters
TClassChild class whose method is going to be invoked.
TMethodArgsTypes of the arguments that the method accepts.
TArgsTypes of the arguments provided to the invocation.
Parameters
[in]methodMethod to invoke. Its signature must match void(TArgs...)
[in]argsArguments to invoke the method with.

◆ remove_child()

void kouta::base::Component::remove_child ( Component component)
inline

Remove a child component from the list.

When a child is removed, the parent component will not attempt to delete it itself. Note that when a component allocated in the stack is destroyed, it will remove itself from the list and prevent double-free issues.

Member Data Documentation

◆ m_children

std::vector<Component*> kouta::base::Component::m_children
private

◆ m_parent

Component* kouta::base::Component::m_parent
private

The documentation for this class was generated from the following file: