SObjectizer 5.8
Loading...
Searching...
No Matches
so_5/mchain_handler_formats/main.cpp
/*
* An example for demonstration of different format of message handlers for mchain.
*/
#include <iostream>
#include <string>
// Main SObjectizer header file.
#include <so_5/all.hpp>
// Types of messages and signals to be placed into mchain.
struct two_ints
{
int a, b;
};
struct delayed_two_ints
{
int a, b;
};
struct just_signal final : public so_5::signal_t {};
struct periodic_signal final : public so_5::signal_t {};
void demo()
{
// A SObjectizer instance.
// Message chain to be used.
auto ch = so_5::create_mchain( sobj,
// Wait on overloaded mchain for 5min.
std::chrono::minutes{5},
// No more than 3 messages in chain.
3u,
// Space for mchain will be preallocated.
// What to do on overflow.
// This value has no sence because we use too large time limit.
// Because of that use hardest case.
// We must store timer_id for periodic signal somewhere.
// Otherwise periodic signal will be canceled automaticallly right after
// exit from send_periodic function.
so_5::timer_id_t periodic_signal_timer;
// Spawn another thread to fill mchain.
std::thread worker{ [&]{
// Send a string as message.
so_5::send< std::string >( ch, "Hello!" );
// Send int as message.
so_5::send< int >( ch, 42 );
// Send struct instance as message.
// Send delayed messages.
ch, std::chrono::milliseconds{150}, 1, 2 );
// Send signal.
// Send periodic signal.
periodic_signal_timer = so_5::send_periodic< periodic_signal >(
ch, std::chrono::milliseconds{20}, std::chrono::milliseconds{150} );
} };
// Read content of mchain.
// We expect exactly 8 messages/signals.
receive( from( ch ).handle_n( 8 ),
// Message instance by const reference.
[]( const std::string & v ) { std::cout << "str: " << v << std::endl; },
// Message instance by value (efficient for small types like int).
[]( int v ) { std::cout << "int: " << v << std::endl; },
// Message instance via mhood_t value.
std::cout << "two_ints: " << v->a << ", " << v->b << std::endl;
},
// Message instance via const reference to mhood_t.
std::cout << "delayed_two_ints: " << v->a << ", " << v->b << std::endl;
},
// Signal handler via mhood_t value.
std::cout << "just signal" << std::endl;
},
// Signal handler via const reference to mhood_t.
std::cout << "periodic signal" << std::endl;
} );
// Close channel with dropping all its content.
// It has sence because timer thread can send periodic signals to
// mchain while we wait on worker.join().
worker.join();
// SObjectizer will be stopped automatically.
}
int main()
{
try
{
demo();
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
}
return 0;
}
A helper header file for including all public SObjectizer stuff.
A message wrapped to be used as type of argument for event handlers.
Definition mhood.hpp:570
A base class for agent signals.
Definition message.hpp:275
An indentificator for the timer.
Definition timers.hpp:73
A wrapped environment.
@ abort_app
Application must be aborted.
@ preallocated
Storage must be preallocated once and doesn't change after that.
mchain_receive_params_t< mchain_props::msg_count_status_t::undefined > from(mchain_t chain)
A helper function for simplification of creation of mchain_receive_params instance.
Definition mchain.hpp:1540
mchain_t create_mchain(environment_t &env)
Create size-unlimited chain.
void close_drop_content(Exceptions_Control exceptions_control, const mchain_t &ch) noexcept(noexcept(details::should_terminate_if_throws_t< Exceptions_Control >::value))
Helper function for closing a message chain with dropping all its content.
Definition mchain.hpp:666
timer_id_t send_periodic(Target &&target, std::chrono::steady_clock::duration pause, std::chrono::steady_clock::duration period, Args &&... args)
A utility function for creating and delivering a periodic message to the specified destination.
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
void send_delayed(Target &&target, std::chrono::steady_clock::duration pause, Args &&... args)
A utility function for creating and delivering a delayed message to the specified destination.
constexpr exceptions_enabled_t exceptions_enabled
Value that indicates that exceptions are enabled.
mchain_receive_result_t receive(const mchain_receive_params_t< Msg_Count_Status > &params, Handlers &&... handlers)
Advanced version of receive from mchain.
Definition mchain.hpp:1828