Inspirel banner

Programming Distributed Systems with YAMI4

6.2.2 C++

In C++ the callback that is provided for the given logical object can be any callable entity (function or functor) that is passed by reference and that can accept the incoming message object as the parameter of the call.

In the simplest case the callback can be implemented by a free function:

void call_me(incoming_message & message)
{
    // code processing the incoming message
    // ...
}

and registered as a callback for some object:

my_agent.register_object("my_object", call_me);

The callback can be also implemented as a stateful functor with appropriately overloaded function call operator:

class my_handler
{
public:
    void operator()(incoming_message & message)
    {
        // code processing the incoming message
        // ...
    }

    // ...
};

// ...
{
    my_handler handler;

    my_agent.register_object("my_object", handler);
    // ...
}

Stateful functor is an appropriate choice when the service exposed to remote clients can be described in object-oriented terms or where a state transitions are expected between messages. Free functions are usually appropriate when the exposed service is essentially stateless, which is the case with all example programs that are provided with YAMI4.