SObjectizer  5.8
Loading...
Searching...
No Matches
so_5/custom_error_logger/main.cpp
/*
* A sample for the custom error logger.
*/
#include <iostream>
#include <stdexcept>
// Main SObjectizer header file.
#include <so_5/all.hpp>
// A class that uses limit_then_redirect incorrectly.
// Message is redirected too many times and this lead to error message.
class actor_t final : public so_5::agent_t
{
struct hello final : public so_5::signal_t {};
struct bye final : public so_5::signal_t {};
public :
actor_t( context_t ctx )
// There is a mistake: message is redirected to the same mbox.
1, [this]{ return so_direct_mbox(); } )
+ limit_then_abort< bye >( 1 ) }
{}
void so_define_agent() override
{
.event( []( mhood_t<hello> ) { std::cout << "Hello!" << std::endl; } )
.event( [this]( mhood_t<bye> ) { so_deregister_agent_coop_normally(); } );
}
void so_evt_start() override
{
so_5::send< hello >( *this ); // This message will be redirected.
}
};
// Custom error logger.
class custom_logger_t final : public so_5::error_logger_t
{
public :
custom_logger_t()
{}
void log(
const char * file_name,
unsigned int line,
const std::string & message ) override
{
std::clog
<< "############################################################\n"
<< file_name << "(" << line << "): error: "
<< message << "\n"
"############################################################"
<< std::endl;
}
};
int main()
{
try
{
// The SObjectizer Environment initialization.
[]( so_5::environment_t & env )
{
// Creating and registering a cooperation.
env.register_agent_as_coop( env.make_agent< actor_t >() );
},
// Parameters for SObjectizer Environment.
{
params.error_logger(
std::make_shared< custom_logger_t >() );
} );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
A helper header file for including all public SObjectizer stuff.
A base class for agents.
Definition agent.hpp:673
virtual void so_define_agent()
Hook on define agent for SObjectizer.
Definition agent.cpp:975
subscription_bind_t so_subscribe_self()
Initiate subscription to agent's direct mbox.
Definition agent.hpp:1461
void so_deregister_agent_coop_normally()
A helper method for deregistering agent's coop in case of normal deregistration.
Definition agent.cpp:1116
const mbox_t & so_direct_mbox() const
Get the agent's direct mbox.
Definition agent.cpp:887
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Definition agent.cpp:832
Parameters for the SObjectizer Environment initialization.
SObjectizer Environment.
coop_handle_t register_agent_as_coop(std::unique_ptr< A > agent)
Register single agent as a cooperation.
std::unique_ptr< Agent > make_agent(Args &&... args)
Helper method for simplification of agents creation.
An interface for logging error messages.
virtual void log(const char *file_name, unsigned int line, const std::string &message)=0
A method for logging message.
A base class for agent signals.
Definition message.hpp:275
std::enable_if< details::is_agent_method_pointer< details::method_arity::unary, Method_Pointer >::value, subscription_bind_t & >::type event(Method_Pointer pfn, thread_safety_t thread_safety=not_thread_safe)
Make subscription to the message.
Definition agent.hpp:3657
void launch(Init_Routine &&init_routine)
Launch a SObjectizer Environment with default parameters.
Definition api.hpp:142
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
static redirect_indicator_t< Msg, Lambda > limit_then_redirect(unsigned int limit, Lambda dest_getter)
A helper function for creating redirect_indicator.