SObjectizer 5.8
Loading...
Searching...
No Matches
so_5::single_sink_binding_t Class Reference

Helper class for managing single sink bindings. More...

#include <single_sink_binding.hpp>

Classes

struct  binding_info_t
 

Public Member Functions

 single_sink_binding_t () noexcept=default
 
 ~single_sink_binding_t () noexcept
 
 single_sink_binding_t (const single_sink_binding_t &)=delete
 
single_sink_binding_toperator= (const single_sink_binding_t &)=delete
 
 single_sink_binding_t (single_sink_binding_t &&other) noexcept
 
single_sink_binding_toperator= (single_sink_binding_t &&other) noexcept
 
bool has_value () const noexcept
 
bool empty () const noexcept
 
void clear () noexcept
 
void unbind () noexcept
 
void bind_for_msg_type (const std::type_index &msg_type, const mbox_t &source, const msink_t &sink_owner)
 
template<typename Msg >
void bind (const mbox_t &source, const msink_t &sink_owner)
 
void bind_for_msg_type (const std::type_index &msg_type, const mbox_t &source, const msink_t &sink_owner, delivery_filter_unique_ptr_t delivery_filter)
 
template<typename Msg >
void bind (const mbox_t &source, const msink_t &sink_owner, delivery_filter_unique_ptr_t delivery_filter)
 
template<typename Msg , typename Lambda >
void bind (const mbox_t &source, const msink_t &sink_owner, Lambda &&filter)
 

Private Attributes

std::optional< binding_info_tm_info
 

Friends

void swap (single_sink_binding_t &a, single_sink_binding_t &b) noexcept
 

Detailed Description

Helper class for managing single sink bindings.

An instance of single_sink_binding_t drops the binding in the destructor. If it's necessary to drop the binding manually then clear()/unbind() methods can be used.

Usage examples:

// Use as a part of an agent.
class coordinator final : public so_5::agent_t
{
const so_5::mbox_t broadcasting_mbox_;
...
void on_some_event(mhood_t<msg_some_command> cmd) {
// Create a child coop and bind an agent to broadcasting mbox.
auto * worker = coop.make_agent<worker>(...);
auto worker_msink = so_5::wrap_to_msink(worker->so_direct_mbox());
bindings_.bind<msg_some_data>(broadcasting_mbox_, worker_msink);
...
});
}
};
// Use as object controlled by a coop.
so_5::environment_t & env = ...;
env.introduce_coop([](so_5::coop_t & coop) {
const auto broadcasting_mbox = coop.environment().create_mbox();
auto * first = coop.make_agent<first_worker>(...);
auto * first_binding = coop.take_under_control(
std::make_unique<so_5::single_sink_binding_t>() );
first_binding->bind<msg_some_data>(broadcasting_mbox,
so_5::wrap_to_msink(first->so_direct_mbox()));
auto * second = coop.make_agent<second_worker>(...);
auto * second_binding = coop.take_under_control(
std::make_unique<so_5::single_sink_binding_t>() );
second_binding->bind<msg_some_data>(broadcasting_mbox,
so_5::wrap_to_msink(second->so_direct_mbox()));
...
});
A base class for agents.
Definition agent.hpp:673
Agent cooperation.
Definition coop.hpp:389
environment_t & environment() const noexcept
Access to SO Environment for which cooperation is bound.
Definition coop.hpp:453
Agent * make_agent(Args &&... args)
Helper method for simplification of agents creation.
Definition coop.hpp:792
T * take_under_control(std::unique_ptr< T > resource)
Take a user resouce under cooperation control.
Definition coop.hpp:710
SObjectizer Environment.
mbox_t create_mbox()
Create an anonymous MPMC mbox.
A message wrapped to be used as type of argument for event handlers.
Definition mhood.hpp:570
Helper class for managing single sink bindings.
void bind(const mbox_t &source, const msink_t &sink_owner)
decltype(auto) introduce_child_coop(agent_t &owner, Args &&... args)
A simple way for creating and registering child cooperation.
msink_t SO_5_FUNC wrap_to_msink(const mbox_t &mbox, priority_t sink_priority=prio::p0)
Helper for wrapping an existing mbox into message_sink.
Definition mbox.cpp:109

There is a principial difference between single_sink_binding_t and multi_sink_binding_t: if bind() is called for single_sink_binding_t when the binding is exists, then old binding will be dropped and new binding will be created. For example, this is a valid behavior for single_sink_binding_t:

const so_5::mbox_t & source = ...;
const so_5::msink_t & dest = ...;
binding.bind<my_message>(source, dest); // New binding created.
...
binding.bind<my_message>(source, dest); // Old binding will be removed
// and new binding will be created (despite the fact that source and dest
// are the same).

Contrary, multi_sink_binding_t::bind() throws if a binding for triplet (message, source, dest) already exists.

Attention
The instance of single_sink_binding_t is not thread safe. If a user wants to work with an instance of single_sink_binding_t from different threads then the user has to protect the instance by her/himself.
Note
This class is Moveable, but is not Copyable.
Since
v.5.8.0

Definition at line 170 of file single_sink_binding.hpp.

Constructor & Destructor Documentation

◆ single_sink_binding_t() [1/3]

so_5::single_sink_binding_t::single_sink_binding_t ( )
defaultnoexcept

◆ ~single_sink_binding_t()

so_5::single_sink_binding_t::~single_sink_binding_t ( )
inlinenoexcept

Definition at line 218 of file single_sink_binding.hpp.

◆ single_sink_binding_t() [2/3]

so_5::single_sink_binding_t::single_sink_binding_t ( const single_sink_binding_t & )
delete

◆ single_sink_binding_t() [3/3]

so_5::single_sink_binding_t::single_sink_binding_t ( single_sink_binding_t && other)
inlinenoexcept

Definition at line 229 of file single_sink_binding.hpp.

Member Function Documentation

◆ bind() [1/3]

template<typename Msg >
void so_5::single_sink_binding_t::bind ( const mbox_t & source,
const msink_t & sink_owner )
inline

Create a binding for message/signal of type Msg from mbox source to the destination sink_owner.

This binding won't use a delivery filter.

If the object already holds a binding the current binding will be removed before the creation of a new one.

Usage example:

const so_5::mbox_t & source = ...;
const so_5::msink_t & dest = ...;
auto binding = std::make_unique< so_5::single_sink_binding_t >();
binding->bind<my_message>(source, dest);

It it's required to make a binding for a mutable message then so_5::mutable_msg marker has to be used:

const so_5::mbox_t & source = ...;
const so_5::msink_t & dest = ...;
auto binding = std::make_unique< so_5::single_sink_binding_t >();
binding->bind< so_5::mutable_msg<my_message> >(source, dest);
A special marker for mutable message.
Definition message.hpp:383
Parameters
sourceThe source mbox.
sink_ownerThe destination for messages/signals.

Definition at line 377 of file single_sink_binding.hpp.

◆ bind() [2/3]

template<typename Msg >
void so_5::single_sink_binding_t::bind ( const mbox_t & source,
const msink_t & sink_owner,
delivery_filter_unique_ptr_t delivery_filter )
inline

Create a binding for message of type Msg from mbox source to the destination sink_owner.

This binding should use delivery filter delivery_filter.

If the object already holds a binding the current binding will be removed before the creation of a new one.

Note
This method can't be used for binding signals.
Parameters
sourceThe source mbox.
sink_ownerThe destination for messages.
delivery_filterDelivery filter to be used. It shouldn't be nullptr.

Definition at line 460 of file single_sink_binding.hpp.

◆ bind() [3/3]

template<typename Msg , typename Lambda >
void so_5::single_sink_binding_t::bind ( const mbox_t & source,
const msink_t & sink_owner,
Lambda && filter )
inline

Create a binding for message of type Msg from mbox source to the destination sink_owner.

The lambda (or functor) filter will be used as delivery filter for messages.

If the object already holds a binding the current binding will be removed before the creation of a new one.

Note
This method can't be used for binding signals.

Usage example:

const so_5::mbox_t & source = ...;
const so_5::msink_t & dest = ...;
auto binding = std::make_unique< so_5::single_sink_binding_t >();
binding->bind<my_message>(source, dest,
[](const my_message & msg) {
... // should return `true` or `false`.
});

It it's required to make a binding for a mutable message then so_5::mutable_msg marker has to be used, but note the type of delivery filter argument:

const so_5::mbox_t & source = ...;
const so_5::msink_t & dest = ...;
auto binding = std::make_unique< so_5::single_sink_binding_t >();
binding->bind< so_5::mutable_msg<my_message> >(source, dest,
[](const my_message & msg) {
... // should return `true` or `false`.
});
Parameters
sourceThe source mbox.
sink_ownerThe destination for messages.
filterFilter to be used.

Definition at line 520 of file single_sink_binding.hpp.

◆ bind_for_msg_type() [1/2]

void so_5::single_sink_binding_t::bind_for_msg_type ( const std::type_index & msg_type,
const mbox_t & source,
const msink_t & sink_owner )
inline

Helper method for creation of a new binding for case when the type of message/signal is represented as std::type_index.

Note
This method is intended to be used for internal use. It's not guaranteed that it won't be changed (or removed) in future versions of SObjectizer.
Parameters
msg_typeType of the message/signal.
sourceThe source mbox.
sink_ownerThe destination for messages/signals.

Definition at line 324 of file single_sink_binding.hpp.

◆ bind_for_msg_type() [2/2]

void so_5::single_sink_binding_t::bind_for_msg_type ( const std::type_index & msg_type,
const mbox_t & source,
const msink_t & sink_owner,
delivery_filter_unique_ptr_t delivery_filter )
inline

Helper method for creation of a new binding for case when the type of message is represented as std::type_index.

If the object already holds a binding the current binding will be removed before the creation of a new one.

Note
This method is intended to be used for internal use. It's not guaranteed that it won't be changed (or removed) in future versions of SObjectizer.
This method can't be used for binding signals.
Parameters
msg_typeThe type of the message.
sourceThe source mbox.
sink_ownerThe destination for messages.
delivery_filterDelivery filter to be used. It shouldn't be nullptr.

Definition at line 405 of file single_sink_binding.hpp.

◆ clear()

void so_5::single_sink_binding_t::clear ( )
inlinenoexcept

Remove the current binding.

It's safe to call this method even if there is no binding at the moment.

The object can be used for creation of a new binding after calling clear() method. For example:

binding.clear(); // Object is empty now.
const so_5::mbox_t & source = ...;
const so_5::msink_t & dest = ...;
binding.bind<my_message>(source, dest); // New binding created.

Definition at line 278 of file single_sink_binding.hpp.

◆ empty()

bool so_5::single_sink_binding_t::empty ( ) const
inlinenodiscardnoexcept
Return values
falseif binding exists
trueif there is no binding at the moment.

Definition at line 257 of file single_sink_binding.hpp.

◆ has_value()

bool so_5::single_sink_binding_t::has_value ( ) const
inlinenodiscardnoexcept
Return values
trueif binding exists
falseif there is no binding at the moment.

Definition at line 249 of file single_sink_binding.hpp.

◆ operator=() [1/2]

single_sink_binding_t & so_5::single_sink_binding_t::operator= ( const single_sink_binding_t & )
delete

◆ operator=() [2/2]

single_sink_binding_t & so_5::single_sink_binding_t::operator= ( single_sink_binding_t && other)
inlinenoexcept

Definition at line 235 of file single_sink_binding.hpp.

◆ unbind()

void so_5::single_sink_binding_t::unbind ( )
inlinenoexcept

A synonym for the clear() method.

Definition at line 309 of file single_sink_binding.hpp.

Friends And Related Symbol Documentation

◆ swap

void swap ( single_sink_binding_t & a,
single_sink_binding_t & b )
friend

Definition at line 210 of file single_sink_binding.hpp.

Member Data Documentation

◆ m_info

std::optional< binding_info_t > so_5::single_sink_binding_t::m_info
private

Information about the current binding.

Empty value means that there is no binding at the moment.

Definition at line 206 of file single_sink_binding.hpp.


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