I am writing message processing code in which each message is a POD structure. In the writing method, this will define an abstract base class with virtual functionality for each type of message, for example:
class AbstractHandler { public: virtual void handleMessage( const MessageType1& msg ) =0; virtual void handleMessage( const MessageType2& msg ) =0; virtual void handleMessage( const MessageType3& msg ) =0; virtual void handleMessage( const MessageType4& msg ) =0; };
And then create derived concrete classes that implement the functions of the handler:
class ConcreteHandler : public AbstractHandler { public: virtual void handleMessage( const MessageType1& msg ); virtual void handleMessage( const MessageType2& msg ); virtual void handleMessage( const MessageType3& msg ); virtual void handleMessage( const MessageType4& msg ); };
If new messages are added to the system, AbstractHandler must be updated along with all derived types.
Alternatively, I could support all supported message types in the mpl sequence and use mpl::inherit_linearly to generate an abstract base class.
(Note: I already use mpl::vector message types elsewhere in the code.)
eg:
typedef mpl::vector< MessageType1, MessageType2, MessageType3, MessageType4 > message_types; template< class Message > class Wrapper { public: virtual void handleMessage( const Message& msg ) = 0; protected: ~Wrapper(){} }; class AbstractHandler : public mpl::inherit_linearly< message_types , mpl::inherit< mpl_1, Wrapper< mpl::_2 > > >::type { public: virtual ~AbstractHandler() {} };
Concrete handlers are then derived from AbstractHandler . This means that whenever new types of messages are added to the system, this is just a case of changing the sequence mpl::vector< types... > message_types , adding new handleMessage functions to derived classes.
In my opinion, this reduces long-term maintenance, since AbstractHandler will automatically have pure virtual functions for all messages in mpl::vector message_types
Regarding performance, are there any flaws in using this approach?
What are the implications of using mpl::inherit_linearly to generate abstract base classes?