#include <iostream>
using namespace std::chrono_literals;
enum class status_t { inprogress, completed, aborted };
template< typename Completion_Handler >
{
public:
operation_performer_t(
context_t ctx,
std::chrono::milliseconds duration,
Completion_Handler && completion_handler )
, m_duration{ duration }
, m_completion_handler{
std::move(completion_handler) }
{}
{
m_status = status_t::completed;
m_completion_handler( m_status );
} );
}
{
}
{
if( status_t::inprogress == m_status )
{
std::this_thread::sleep_for( 15ms );
m_completion_handler( status_t::aborted );
}
}
private:
status_t m_status{ status_t::inprogress };
const std::chrono::milliseconds m_duration;
Completion_Handler m_completion_handler;
};
class async_operation_t final
{
public:
async_operation_t( const async_operation_t & ) = delete;
async_operation_t( async_operation_t && ) = delete;
: m_env{ env }
{}
~async_operation_t()
{
cancel();
}
template< typename Completion_Handler >
void
start(
std::chrono::milliseconds duration,
Completion_Handler && completion_handler )
{
m_performer_coop = m_env.register_agent_as_coop(
m_env.make_agent< operation_performer_t< Completion_Handler > >(
duration,
std::move(completion_handler) ) );
}
void
cancel()
{
if( m_performer_coop )
m_env.deregister_coop(
std::move(m_performer_coop),
}
private:
};
{
public:
async_operation_issuer_t(
context_t ctx,
std::string name,
std::chrono::milliseconds operation_duration,
std::chrono::milliseconds lifetime )
: agent_t{
std::move(ctx) }
, m_name{
std::move(name) }
, m_operation_duration{ operation_duration }
, m_lifetime{ lifetime }
{}
~async_operation_issuer_t() override
{
std::cout << m_name << " destroyed" << std::endl;
}
{
} );
}
{
m_async_op.start( m_operation_duration,
std::cout << self->m_name << " -> "
<< (status_t::aborted ==
status ?
"aborted" :
"completed")
<< std::endl;
} );
}
{
m_async_op.cancel();
}
private:
const std::chrono::milliseconds m_operation_duration;
const std::chrono::milliseconds m_lifetime;
};
int main()
{
try
{
{
"The First Issuer (with rather long name)", 125ms, 50ms ) );
"The Second Issuer (with yet more long name)", 125ms, 100ms ) );
"The Third Issuer", 125ms, 150ms ) );
} );
}
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.
const name_for_agent_t m_name
Optional name for the agent.
virtual void so_define_agent()
Hook on define agent for SObjectizer.
subscription_bind_t so_subscribe_self()
Initiate subscription to agent's direct mbox.
void so_deregister_agent_coop_normally()
A helper method for deregistering agent's coop in case of normal deregistration.
virtual void so_evt_finish()
Hook of agent finish in SObjectizer.
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Type of smart handle for a cooperation.
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.
A base class for agent signals.
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.
const int normal
Normal deregistration.
status
Status of the message chain.
status_t
Status of message delivery tracing.
Private part of message limit implementation.
void launch(Init_Routine &&init_routine)
Launch a SObjectizer Environment with default parameters.
intrusive_ptr_t< Derived > make_agent_ref(Derived *agent)
Helper function template for the creation of smart pointer to an agent.
void send_delayed(Target &&target, std::chrono::steady_clock::duration pause, Args &&... args)
A utility function for creating and delivering a delayed message to the specified destination.