DDJ has an article about a nice and simple C++ message processing technique.

class Message
{
protected:
    template void dynamicDispatch(MessageHandlerBase* handler,MessageType* self)
    {
        dynamic_cast*>(handler)->process(self); // should test against NULL from dynamic_cast
    }
};
class Message1 : public MessageBase
{
    void dispatch(MessageHandlerBase* handler)
    {
        dynamicDispatch(handler, this);
    }
};

class MessageHandlerBase
{};
template class MessageHandler : public virtual MessageHandlerBase
{
    virtual void process(MessageType*)=0;
};
class SpecificMessageHandler : public MessageHandler, public MessageHandler
{
    void process(Message1*);
    void process(Message2*);
};

Only the handler of specific message types needs to include message declaration.

Double dispatching refers to calling handler.process from message.dispatch. Dynamic refers to the dynamic_cast. message.dispatch can be a macro to save copy-n-paste.

Advertisements