#if defined( _MSC_VER )
#if defined( __clang__ )
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#endif
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <time.h>
{
const std::string m_type_hint;
public:
state_monitor_t( const std::string & type_hint )
: m_type_hint( type_hint )
{}
{
std::cout << m_type_hint << " agent changed state to "
<< std::endl;
}
};
{
const state_t st_1{
this,
"state_1" };
const state_t st_2{
this,
"state_2" };
const state_t st_3{
this,
"state_3" };
const state_t st_shutdown{
this,
"shutdown" };
public:
a_state_swither_t( context_t ctx )
{}
void evt_handler_default();
void evt_handler_1();
void evt_handler_2();
void evt_handler_3();
void evt_handler_shutdown();
private:
void show_event_invocation( const char * event_name );
};
void a_state_swither_t::so_define_agent()
{
so_subscribe_self()
.event< msg_periodic >( &a_state_swither_t::evt_handler_default );
so_subscribe_self().in( st_1 )
.event< msg_periodic >( &a_state_swither_t::evt_handler_1 );
so_subscribe_self().in( st_2 )
.event< msg_periodic >( &a_state_swither_t::evt_handler_2 );
so_subscribe_self().in( st_3 )
.event< msg_periodic >( &a_state_swither_t::evt_handler_3 );
so_subscribe_self().in( st_shutdown )
.event< msg_periodic >( &a_state_swither_t::evt_handler_shutdown );
}
void a_state_swither_t::so_evt_start()
{
show_event_invocation( "so_evt_start()" );
m_timer_id = so_5::send_periodic< msg_periodic >(
*this,
std::chrono::seconds( 1 ),
std::chrono::seconds( 1 ) );
}
void a_state_swither_t::evt_handler_default()
{
show_event_invocation( "evt_handler_default" );
so_change_state( st_1 );
}
void a_state_swither_t::evt_handler_1()
{
show_event_invocation( "evt_handler_1" );
so_change_state( st_2 );
}
void a_state_swither_t::evt_handler_2()
{
show_event_invocation( "evt_handler_2" );
so_change_state( st_3 );
}
void a_state_swither_t::evt_handler_3()
{
show_event_invocation( "evt_handler_3" );
so_change_state( st_shutdown );
}
void a_state_swither_t::evt_handler_shutdown()
{
show_event_invocation( "evt_handler_3" );
so_change_state( so_default_state() );
std::cout << "Stop sobjectizer..." << std::endl;
so_environment().stop();
}
void a_state_swither_t::show_event_invocation( const char * event_name )
{
time_t t = time( nullptr );
std::cout << asctime( localtime( &t ) )
<< event_name << ", state: " << so_current_state().query_name()
<< std::endl;
}
state_monitor_t g_state_monitor( "nondestroyable_listener" );
{
ag->so_add_nondestroyable_listener( g_state_monitor );
ag->so_add_destroyable_listener(
new state_monitor_t( "destroyable_listener" ) ) );
}
int main()
{
try
{
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}