SObjectizer 5.8
Loading...
Searching...
No Matches
so_5/hello_evt_handler/main.cpp
/*
* A sample of a simple agent which send message to itself.
*/
#include <iostream>
// Main SObjectizer header files.
#include <so_5/all.hpp>
// An agent class definition.
class a_hello_t final : public so_5::agent_t
{
public:
// Definition of an agent for SObjectizer.
void so_define_agent() override
{
// The hello message subscription.
so_subscribe_self().event( &a_hello_t::evt_hello );
}
// A reaction to start of work in SObjectizer.
void so_evt_start() override
{
// Send hello message.
so_5::send< std::string >( *this, "Hello, world! This is SObjectizer-5.");
}
private :
// Hello message handler.
void evt_hello( const std::string & msg )
{
std::cout << msg << std::endl;
// Shutting down SObjectizer.
}
};
int main()
{
try
{
[]( so_5::environment_t & env )
{
env.register_agent_as_coop( env.make_agent< a_hello_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:841
subscription_bind_t so_subscribe_self()
Initiate subscription to agent's direct mbox.
Definition agent.hpp:1416
agent_t(environment_t &env)
Constructor.
Definition agent.cpp:646
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
Definition agent.cpp:853
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Definition agent.cpp:701
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.
void stop() noexcept
Send a shutdown signal to the Run-Time.
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:3490
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.