SObjectizer 5.8
Loading...
Searching...
No Matches
so_5/blinking_led/main.cpp
/*
* An example of using composite agent state, on_enter/on_exit handlers and
* time_limit for states.
*/
#include <iostream>
#include <so_5/all.hpp>
using namespace std::chrono_literals;
class blinking_led final : public so_5::agent_t
{
state_t off{ this }, blinking{ this },
blink_on{ initial_substate_of{ blinking } },
blink_off{ substate_of{ blinking } };
public :
struct turn_on_off final : public so_5::signal_t {};
blinking_led( context_t ctx ) : so_5::agent_t{ ctx }
{
this >>= off;
off.just_switch_to< turn_on_off >( blinking );
blinking.just_switch_to< turn_on_off >( off );
blink_on
.on_enter( []{ std::cout << "ON" << std::endl; } )
.on_exit( []{ std::cout << "off" << std::endl; } )
.time_limit( std::chrono::milliseconds{1250}, blink_off );
blink_off
.time_limit( std::chrono::milliseconds{750}, blink_on );
}
};
int main()
{
try
{
auto m = env.introduce_coop( []( so_5::coop_t & coop ) {
return coop.make_agent< blinking_led >()->so_direct_mbox();
} );
std::cout << "Turn blinking on for 10s" << std::endl;
std::this_thread::sleep_for( 10s );
std::cout << "Turn blinking off for 5s" << std::endl;
std::this_thread::sleep_for( 5s );
std::cout << "Turn blinking on for 5s" << std::endl;
std::this_thread::sleep_for( 5s );
std::cout << "Stopping..." << std::endl;
env.stop();
} );
}
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 base class for agents.
Definition agent.hpp:673
Agent cooperation.
Definition coop.hpp:389
Agent * make_agent(Args &&... args)
Helper method for simplification of agents creation.
Definition coop.hpp:792
SObjectizer Environment.
void stop() noexcept
Send a shutdown signal to the Run-Time.
decltype(auto) introduce_coop(Args &&... args)
Helper method for simplification of cooperation creation and registration.
A base class for agent signals.
Definition message.hpp:275
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.