Background component executor. More...
#include <branch.hpp>
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 | |
Branch & | operator= (const Branch &)=delete |
Branch (Root &&)=delete | |
Branch & | operator= (Branch &&)=delete |
virtual | ~Branch () |
Branch destructor. More... | |
const WrappedComponent & | component () const |
Obtain a constant reference to the wrapped component. More... | |
WrappedComponent & | component () |
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 | |
Root & | operator= (const Root &)=delete |
Root (Root &&)=delete | |
Root & | operator= (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 | |
Component & | operator= (const Component &)=delete |
Component (Component &&)=delete | |
Component & | operator= (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 |
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.
using kouta::base::Branch< TWrapped >::WrappedComponent = TWrapped |
|
delete |
|
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.
TArgs | Types of the arguments to provide the wrapped component. |
[in] | parent | Parent component. The lifetime of the parent must surpass that of the child. |
[in] | args | Arguments to provide the wrapped component. |
|
delete |
|
delete |
|
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.
|
inline |
Obtain a mutable reference to the wrapped component.
|
inline |
Obtain a constant reference to the wrapped component.
|
delete |
|
delete |
|
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.
TArgs | Types of the arguments that the method accepts. |
TMethodArgs | Types of the arguments that the method accepts. |
TArgs | Types of the arguments provided to the invocation. |
[in] | method | Method to execute. Its signature must match void(TArgs...) |
[in] | args | Arguments to execute the method with. |
|
inlineoverridevirtual |
Run the event loop in the worker thread.
Reimplemented from kouta::base::Root.
|
inlineprivate |
Run the event loop.
|
private |
|
private |