Sometimes it can be necessary to check the presense of a subscription. For example there can be an observer agent which receives messages with mboxes inside. This observer must make subscription for messages from that mbox:
struct start_listen {
...
};
...
void on_start_listen(mhood_t<start_listen> cmd) {
}
void on_data(mhood_t<data> cmd) {...}
};
subscription_bind_t so_subscribe(const mbox_t &mbox_ref)
Initiate subscription.
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.
The problem is: if start_listen contain a mbox to which the observer is already subscribed the an exception will be throw in in so_subscribe.
Since v.5.5.19.5 there is a way to check presence of subscription. This check can be done by so_5::agent_t::so_has_subscription method:
...
void on_start_listen(mhood_t<start_listen> cmd) {
}
void on_data(mhood_t<data> cmd) {...}
};
bool so_has_subscription(const mbox_t &mbox, const state_t &target_state) const noexcept
Check the presence of a subscription.
Method so_has_subscription has several formats:
so_has_subscription<Msg>(mbox);
so_has_subscription<Msg>(mbox, so_default_state());
so_has_subscription(mbox, &my_agent::event_handler);
so_has_subscription(mbox, so_default_state(), &my_agent::event_handler);
Class so_5::state_t now also has a couple of has_subscription methods which allows to check the presence of a subscription:
state_t st_working{this};
...
void on_start_listen(mhood_t<start_listen> cmd) {
if(!st_working.has_subscription(cmd->from_, &obsever::on_data))
st_working.event(cmd->from_, &observer::on_data);
}
void on_data(mhood_t<data> cmd) {...}
};