#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#if defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif
{
public :
{}
{
m_next = next;
}
{
if( m_next )
else
} );
}
private :
};
{
public :
a_coordinator_t( context_t ctx, unsigned int iterations )
, m_remaining_iterations( iterations )
{}
{
--m_remaining_iterations;
show_remaining_iterations();
if( m_remaining_iterations )
create_new_child_coop();
else
{
std::cout << "no more iterations left, finishing..." << std::endl;
}
} );
}
{
show_remaining_iterations();
create_new_child_coop();
}
private :
unsigned int m_remaining_iterations;
void show_remaining_iterations() const
{
std::cout << m_remaining_iterations << ": iterations left...\r"
<< std::flush;
}
void create_new_child_coop()
{
*this,
disp.binder() );
coop->add_dereg_notificator(
auto first_mbox = fill_coop( *coop );
}
{
std::vector< so_5::mbox_t > mboxes;
mboxes.reserve( agent_count );
std::vector< a_chain_member_t * > agents;
agents.reserve( agent_count );
{
agents.push_back( a );
mboxes.push_back( a->so_direct_mbox() );
}
agents[ i ]->set_next( mboxes[ i + 1 ] );
return mboxes[ 0 ];
}
};
unsigned int detect_iteration_count( int argc, char ** argv )
{
unsigned int r = 5000;
if( 2 == argc )
{
auto a = std::atoi( argv[ 1 ] );
if( a > 0 )
r = static_cast< unsigned int >( a );
else
throw std::invalid_argument(
"invalid iteration count: " + std::to_string( a ) );
}
return r;
}
int main( int argc, char ** argv )
{
try
{
const auto iterations = detect_iteration_count( argc, argv );
env.
make_agent< a_coordinator_t >( iterations ) );
} );
return 0;
}
catch( const std::exception & x )
{
std::cerr << "*** Exception caught: " << x.what() << std::endl;
}
return 2;
}
A helper header file for including all public SObjectizer stuff.
virtual void so_define_agent()
Hook on define agent for SObjectizer.
void so_deregister_agent_coop_normally()
A helper method for deregistering agent's coop in case of normal deregistration.
const mbox_t & so_direct_mbox() const
Get the agent's direct mbox.
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
const state_t & so_default_state() const
Access to the agent's default state.
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Agent * make_agent(Args &&... args)
Helper method for simplification of agents creation.
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.
coop_handle_t register_coop(coop_unique_holder_t agent_coop)
Register a cooperation.
A base class for agent signals.
const state_t & event(Args &&... args) const
Helper for subscription of event handler in this state.
SO_5_FUNC dispatcher_handle_t make_dispatcher(environment_t &env, const std::string_view data_sources_name_base, disp_params_t params)
Create an instance of active_obj dispatcher.
SO_5_FUNC suffix_t agent_count()
Suffix for data source with count of agents bound to some entity.
Private part of message limit implementation.
void launch(Init_Routine &&init_routine)
Launch a SObjectizer Environment with default parameters.
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
coop_unique_holder_t create_child_coop(agent_t &owner, Args &&... args)
A simple way for creating child cooperation.
auto make_coop_dereg_notificator(mbox_t target) noexcept
Create notificator about cooperation deregistration completion.
Message about cooperation deregistration completion.