Skip to content

so5extra 1.3 Proxy Mbox

Yauheni Akhotnikau edited this page Aug 30, 2019 · 1 revision

Purpose

A new so_5::extra::mboxes::proxy namespace containt tools for simplification of implementation of custom mboxes. Sometimes a new custom mbox requires an actual mbox for performing message-delivery functionality. For example:

class my_custom_mbox final : public so_5::abstract_message_box_t {
    // Actual mbox to be used for message delivery and message filtering.
    const so_5::mbox_t underlying_mbox_;
    ...
public:
    my_custom_mbox(so_5::environment_t & env) : underlying_mbox_{env.create_mbox()}, ... {}
    
    so_5::mbox_id_t id() const override { return underlying_mbox_->id(); }
    std::string query_name() const override { return underlying_mbox_->query_name(); }
    so_5::mbox_type_t type() const override { return underlying_mbox_->type(); }
    void subscribe_event_handler(
            const std::type_index & msg_type,
            const so_5::message_limit::control_block_t * limit,
            so_5::agent_t * subscriber) override {
        ... // Some specific stuff.
        // Delegation of actual work to underlying mbox.
        underlying_mbox_->subscribe_event_handler(msg_type, limit, subscriber);
    }
    ... // And so on.
};

In such cases a user has to reimplement all pure virtual methods from so_5::abstract_message_box_t interface. It is a boring task. To simplify it so_5::extra::mboxes::proxy contains a class simple_t that holds an underlying mbox and delegates all calls to it.

Header File

The stuff related to so_5::extra::mboxes::proxy namespace is defined in so_5_extra/mboxes/proxy.hpp. So it is necessary to do the following includes in your code:

#include <so_5_extra/mboxes/proxy.hpp>

#include <so_5/all.hpp>

Class simple_t

Class so_5::extra::mboxes::proxy::simple_t implements a very simple proxy that holds an underlying mbox and delegates all calls to it. This class can be used as a base for custom mboxes where reimplementation of only a few mbox's methods is necessary.

For example, there is a custom MPMC-mbox that allows subscription for a restricted set of messages only:

class my_restricted_mbox final : public so_5::extra::mboxes::proxy::simple_t {
    using base_type = so_5::extra::mboxes::proxy::simple_t;
    
    bool is_appropriate_message_type(const std::type_index & msg_type) const noexcept {
        ... // some type checking.
    }
public:
    my_restricted_mbox(so_5::mbox_t actual_mbox) : base_type{std::move(actual_mbox)} {}
    
    void subscribe_event_handler(
            const std::type_index & msg_type,
            const so_5::message_limit::control_block_t * limit,
            so_5::agent_t * subscriber) override {
        if(!is_appropriate_message_type(msg_type))
            throw prohibited_message_type_error(msg_type);
            
        // Delegation of actual work to underlying mbox.
        base_type::subscribe_event_handler(msg_type, limit, subscriber);
    }
};
Clone this wiki locally