Kouta
A small application framework based on Boost
kouta::base::Branch< TWrapped > Class Template Reference

Background component executor. More...

#include <branch.hpp>

Inheritance diagram for kouta::base::Branch< TWrapped >:
Collaboration diagram for kouta::base::Branch< TWrapped >:

Public Types

using WrappedComponent = TWrapped
 

Public Member Functions

 Branch ()=delete
 
template<class... TArgs>
 Branch (Component *parent, TArgs... args)
 Branch component constructor. More...
 
 Branch (const Branch &)=delete
 
Branchoperator= (const Branch &)=delete
 
 Branch (Root &&)=delete
 
Branchoperator= (Branch &&)=delete
 
virtual ~Branch ()
 Branch destructor. More...
 
const WrappedComponentcomponent () const
 Obtain a constant reference to the wrapped component. More...
 
WrappedComponentcomponent ()
 Obtain a mutable reference to the wrapped component. More...
 
void run () override
 Run the event loop in the worker thread. More...
 
template<class... TMethodArgs, class... TArgs>
void post (void(WrappedComponent::*method)(TMethodArgs...), TArgs... args)
 Post a wrapped component method call to the event loop for deferred execution. More...
 
- Public Member Functions inherited from kouta::base::Root
 Root ()
 Default constructor. More...
 
 Root (Component *parent)
 Construct from a parent. More...
 
 Root (const Root &)=delete
 
Rootoperator= (const Root &)=delete
 
 Root (Root &&)=delete
 
Rootoperator= (Root &&)=delete
 
virtual ~Root ()=default
 
boost::asio::io_context & context () override
 Obtain a reference to the underlying I/O context. More...
 
virtual void stop ()
 Stop the event loop and exit. More...
 
- Public Member Functions inherited from kouta::base::Component
 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...
 
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 Member Functions

void run_worker ()
 Run the event loop. More...
 

Private Attributes

std::thread m_worker
 
WrappedComponent m_component
 

Detailed Description

template<class TWrapped>
class kouta::base::Branch< TWrapped >

Background component executor.

A Branch wraps a Component based object so that the event loop it is attached to is executed in a separate thread. This implies that any children that have the wrapped Component as a parent will also run in the worker thread.

By default, the Branch does nothing, and its event loop must be explicitly started by calling the run() method. As opposed to the original method defined in Root, the one specified here will launch the thread (and the event loop) and return immediately.

Template Parameters
TWrappedWrapped Component type. IT is assumed that the first argument of the component will be a pointer to a parent component (which will be set to this Branch).

Member Typedef Documentation

◆ WrappedComponent

template<class TWrapped >
using kouta::base::Branch< TWrapped >::WrappedComponent = TWrapped

Constructor & Destructor Documentation

◆ Branch() [1/4]

template<class TWrapped >
kouta::base::Branch< TWrapped >::Branch ( )
delete

◆ Branch() [2/4]

template<class TWrapped >
template<class... TArgs>
kouta::base::Branch< TWrapped >::Branch ( Component parent,
TArgs...  args 
)
inline

Branch component constructor.

This constructor will register the Branch object with the parent only to manage the memory deallocation in case the object was allocated on the heap. Regardless of having a parent, the Branch owns its event loop.

Template Parameters
TArgsTypes of the arguments to provide the wrapped component.
Parameters
[in]parentParent component. The lifetime of the parent must surpass that of the child.
[in]argsArguments to provide the wrapped component.

◆ Branch() [3/4]

template<class TWrapped >
kouta::base::Branch< TWrapped >::Branch ( const Branch< TWrapped > &  )
delete

◆ Branch() [4/4]

template<class TWrapped >
kouta::base::Branch< TWrapped >::Branch ( Root &&  )
delete

◆ ~Branch()

template<class TWrapped >
virtual kouta::base::Branch< TWrapped >::~Branch ( )
inlinevirtual

Branch destructor.

The destructor takes care of the cleanup of the worker thread, by stopping its event loop and waiting for the thread to terminate before joining it.

Member Function Documentation

◆ component() [1/2]

template<class TWrapped >
WrappedComponent& kouta::base::Branch< TWrapped >::component ( )
inline

Obtain a mutable reference to the wrapped component.

◆ component() [2/2]

template<class TWrapped >
const WrappedComponent& kouta::base::Branch< TWrapped >::component ( ) const
inline

Obtain a constant reference to the wrapped component.

◆ operator=() [1/2]

template<class TWrapped >
Branch& kouta::base::Branch< TWrapped >::operator= ( Branch< TWrapped > &&  )
delete

◆ operator=() [2/2]

template<class TWrapped >
Branch& kouta::base::Branch< TWrapped >::operator= ( const Branch< TWrapped > &  )
delete

◆ post()

template<class TWrapped >
template<class... TMethodArgs, class... TArgs>
void kouta::base::Branch< TWrapped >::post ( void(WrappedComponent::*)(TMethodArgs...)  method,
TArgs...  args 
)
inline

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

This is a pass-through to the Component::post() method of the wrapped component.

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

◆ run()

template<class TWrapped >
void kouta::base::Branch< TWrapped >::run ( )
inlineoverridevirtual

Run the event loop in the worker thread.

Note
Calling this method several times has no effect.
The worker thread blocks until the event loop is terminated.

Reimplemented from kouta::base::Root.

◆ run_worker()

template<class TWrapped >
void kouta::base::Branch< TWrapped >::run_worker ( )
inlineprivate

Run the event loop.

Note
This method blocks until the event loop is terminated.

Member Data Documentation

◆ m_component

template<class TWrapped >
WrappedComponent kouta::base::Branch< TWrapped >::m_component
private

◆ m_worker

template<class TWrapped >
std::thread kouta::base::Branch< TWrapped >::m_worker
private

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