SObjectizer  5.5
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
so_5::coop_handle_t Class Reference

Type of smart handle for a cooperation. More...

#include <coop_handle.hpp>

Public Member Functions

 coop_handle_t ()
 
 operator bool () const noexcept
 Is this handle empty? More...
 
bool operator! () const noexcept
 Is this non-empty handle? More...
 
auto id () const noexcept
 Get the ID of the coop. More...
 

Static Public Attributes

static constexpr const coop_id_t invalid_coop_id = 0u
 

Private Member Functions

 coop_handle_t (coop_id_t id, std::shared_ptr< coop_t > coop)
 Initializing constructor. More...
 

Private Attributes

coop_id_t m_id
 ID of cooperation. More...
 
std::weak_ptr< coop_tm_coop
 Pointer for cooperation. More...
 

Friends

class so_5::coop_t
 
coop_shptr_t low_level_api::to_shptr (const coop_handle_t &)
 
coop_shptr_t low_level_api::to_shptr_noexcept (const coop_handle_t &) noexcept
 
std::ostream & operator<< (std::ostream &to, const coop_handle_t &what)
 A tool for dumping coop_handle to ostream. More...
 

Detailed Description

Type of smart handle for a cooperation.

Type coop_handle_t is used for references to registered coops. Before v.5.6 coops in SObjectizer-5 were identified via strings. Every coop had its own name.

Since v.5.6.0 names are no more used for identificators of coops. SObjectizer's environment_t returns instances of coop_handle_t for every registered coop. This handle can be used for deregistration of the coop at the appropriate time. For example:

class request_manager_t final : public so_5::agent_t {
std::map<request_id_t, so_5::coop_handle_t> active_requests_;
...
void on_new_request(mhood_t<request_t> cmd) {
// Create a new coop for handling that request.
auto coop = so_5::create_child_coop( *this );
...; // Fill the coop with agents.
// Register coop and store its handle in the map of actual requests.
active_requests_[cmd->request_id()] = so_environment().register_coop(std::move(coop));
}
...
void on_cancel_request(mhood_t<cancel_request_t> cmd) {
auto it = active_requests_.find(cmd->request_id());
if(it != active_requests_.end())
// Deregister coop for this request.
so_environment().deregister_coop(it->second);
}
};

Note that coop_handle_t is somewhat like (smart)pointer. It can be empty, e.g. do not point to any coop. Or it can be not-empty. In that case coop_handle_t is very similar to std::weak_ptr.

Since
v.5.6.0
Examples:
so_5/coop_listener/main.cpp, so_5/exception_logger/main.cpp, and so_5/ping_pong_with_owner/main.cpp.

Constructor & Destructor Documentation

◆ coop_handle_t() [1/2]

so_5::coop_handle_t::coop_handle_t ( coop_id_t  id,
std::shared_ptr< coop_t coop 
)
inlineprivate

Initializing constructor.

◆ coop_handle_t() [2/2]

so_5::coop_handle_t::coop_handle_t ( )
inline

Member Function Documentation

◆ id()

auto so_5::coop_handle_t::id ( ) const
inlinenoexcept

Get the ID of the coop.

◆ operator bool()

so_5::coop_handle_t::operator bool ( ) const
inlinenoexcept

Is this handle empty?

Handle is empty if there is no underlying coop.

Return values
trueif handle is not empty

◆ operator!()

bool so_5::coop_handle_t::operator! ( ) const
inlinenoexcept

Is this non-empty handle?

Handle is empty if there is no underlying coop.

Return values
trueif handle is empty

Friends And Related Function Documentation

◆ low_level_api::to_shptr

◆ low_level_api::to_shptr_noexcept

◆ operator<<

std::ostream& operator<< ( std::ostream &  to,
const coop_handle_t what 
)
friend

A tool for dumping coop_handle to ostream.

◆ so_5::coop_t

friend class so_5::coop_t
friend

Member Data Documentation

◆ invalid_coop_id

constexpr const coop_id_t so_5::coop_handle_t::invalid_coop_id = 0u
static

◆ m_coop

std::weak_ptr< coop_t > so_5::coop_handle_t::m_coop
private

Pointer for cooperation.

Attention
This is a weak pointer. It means that it can refer to already destroyed cooperation.

◆ m_id

coop_id_t so_5::coop_handle_t::m_id
private

ID of cooperation.


The documentation for this class was generated from the following file: