Skip to content

Commit 7639935

Browse files
committed
Fix for #34.
1 parent 6913d34 commit 7639935

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

dev/so_5/coop.hpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,59 @@ class coop_t : public std::enable_shared_from_this<coop_t>
516516
*
517517
* \brief Add notificator about cooperation registration event.
518518
*
519+
* Notificator should be a function or functional object with the
520+
* following signature:
521+
* \code
522+
* void(so_5::environment_t & env, const so_5::coop_handle_t & handle) noexcept;
523+
* \endcode
524+
*
525+
* An example:
526+
* \code
527+
* class parent_agent final : public so_5::agent_t
528+
* {
529+
* // Map of registered cooperations.
530+
* std::map<std::string, so_5::coop_handle_t> live_coops_;
531+
* ...
532+
* struct coop_is_alive final : public so_5::message_t
533+
* {
534+
* const std::string name_;
535+
* const so_5::coop_handle_t handle_;
536+
*
537+
* coop_is_alive(std::string name, so_5::coop_handle_t handle)
538+
* : m_name{std::move(name)}
539+
* , m_handle{std::move(handle)}
540+
* {}
541+
* };
542+
*
543+
* void evt_coop_is_alive(mhood_t<coop_is_alive> cmd) {
544+
* m_live_coops_[cmd->name_] = cmd->handle_;
545+
* }
546+
*
547+
* void evt_make_new_coop(mhood_t<new_coop_info> cmd) {
548+
* so_5::introduce_child_coop(*this, [&](so_5::coop_t & coop) {
549+
* ... // Fill the coop.
550+
* // Add reg-notificator.
551+
* coop.add_reg_notificator(
552+
* [this, name=cmd->source_name](
553+
* so_5::environment_t &,
554+
* const so_5::coop_handle_t & handle) noexcept
555+
* {
556+
* // Inform the parent about the registration.
557+
* so_5::send<coop_is_alive>(*this, name, handle);
558+
* }
559+
* } );
560+
* }
561+
*
562+
* ...
563+
* }
564+
* \endcode
565+
*
566+
* \note
567+
* reg_notificator can (and most likely will) be called from some
568+
* different thread, not the thread where reg_notificator was added. So
569+
* please take an additional care if your notificator changes some shared
570+
* data -- it can break thread safety easily.
571+
*
519572
* \attention
520573
* Since v.5.6.0 reg_notificator should be a noexcept function or
521574
* functor. Because of that a check in performed during compile time. An
@@ -547,6 +600,65 @@ class coop_t : public std::enable_shared_from_this<coop_t>
547600
*
548601
* \brief Add notificator about cooperation deregistration event.
549602
*
603+
* Notificator should be a function or functional object with the
604+
* following signature:
605+
* \code
606+
* void(
607+
* so_5::environment_t & env,
608+
* const so_5::coop_handle_t & handle,
609+
* const so_5::coop_dereg_reason_t & reason) noexcept;
610+
* \endcode
611+
*
612+
* An example:
613+
* \code
614+
* class parent_agent final : public so_5::agent_t
615+
* {
616+
* // Map of registered cooperations.
617+
* std::map<std::string, so_5::coop_handle_t> live_coops_;
618+
* ...
619+
* struct coop_is_dead final : public so_5::message_t
620+
* {
621+
* const std::string name_;
622+
*
623+
* explicit coop_is_dead(std::string name)
624+
* : m_name{std::move(name)}
625+
* {}
626+
* };
627+
*
628+
* void evt_coop_is_dead(mhood_t<coop_is_dead> cmd) {
629+
* m_live_coops_.erase(cmd->name_);
630+
* }
631+
*
632+
* void evt_make_new_coop(mhood_t<new_coop_info> cmd) {
633+
* so_5::introduce_child_coop(*this, [&](so_5::coop_t & coop) {
634+
* ... // Fill the coop.
635+
*
636+
* // Add reg-notificator.
637+
* coop.add_reg_notificator(...);
638+
*
639+
* // Add deref-notificator.
640+
* coop.add_dereg_notificator(
641+
* [this, name=cmd->source_name](
642+
* so_5::environment_t &,
643+
* const so_5::coop_handle_t & handle,
644+
* const so_5::coop_dereg_reason_t &) noexcept
645+
* {
646+
* // Inform the parent about the deregistration.
647+
* so_5::send<coop_is_dead>(*this, name);
648+
* }
649+
* } );
650+
* }
651+
*
652+
* ...
653+
* }
654+
* \endcode
655+
*
656+
* \note
657+
* dereg_notificator can (and most likely will) be called from some
658+
* different thread, not the thread where dereg_notificator was added. So
659+
* please take an additional care if your notificator changes some shared
660+
* data -- it can break thread safety easily.
661+
*
550662
* \attention
551663
* Since v.5.6.0 dereg_notificator should be a noexcept function or
552664
* functor. Because of that a check in performed during compile time. An

0 commit comments

Comments
 (0)