SquidReplicator
Programmer
Hi all, I have a small C++ problem - although I think it might be a general OO problem.
I am writing a user interface code using message passing and I want both the message class and the message handling class to be extendable - so that new message types can be created in the future and new message handlers can be created in the future.
The problem is that I don't want to have to resort to a managed message ID system, since in a large software project that could get messy, and since the original base code for the message abstract class needs to be frozen (as if distributed in a library) we can't just start adding new message ID's there.
Here is some code to illustrate the problem a little more concisely...
how can I keep both the message type and the message handler extendable, without resorting to managed Message ID assignment or dynamic_cast<> testing?
Thanks...
I am writing a user interface code using message passing and I want both the message class and the message handling class to be extendable - so that new message types can be created in the future and new message handlers can be created in the future.
The problem is that I don't want to have to resort to a managed message ID system, since in a large software project that could get messy, and since the original base code for the message abstract class needs to be frozen (as if distributed in a library) we can't just start adding new message ID's there.
Here is some code to illustrate the problem a little more concisely...
Code:
class Message
{
public:
};
class KeyboardMessage : public Message
{
public:
int Key;
};
... other message types
class MessageHandler // abstract message handler
{
public:
virtual void HandleMessage( Message& cMessage ) = 0;
};
class MessageHandlerA : public MessageHandler // concrete message handler
{
public:
virtual void HandleMessage( Message& cMessage )
{
// how in here can I decifer which message is being passed in
// I can't do it through the normal method - of having the
// message have some sort of virtual function, inside of which
// it would know what type of message it was, since then I would
// have to kill the extendability of the message handler (that
// message type would have to know about all the possible types
// of message handlers)
// I'm told a list of dymanic_cast<> tests (with different
// message passing class types to find out the type of the
// message) shows an error in design... also RTTI is not
// guaranteed available - so what is the answer? HUH?
// we could give each message a unique ID but how do we manage
// this? It could get messy when 2 groups of developers are creating new
// message types at the same time - whats to stop them giving
// their messages the same ID. If we don't have access to
// the base class code we can't put a message ID enum there -
// we'd have to have some sort of messy INT type or something
// ... :(
}
};
int main()
{
MessageHandlerA cMHandler;
KeyboardMessage cKBMsg;
cKBMsg.Key = DIK_RETURN;
cMHandler.HandleMessage( cKBMsg );
}
how can I keep both the message type and the message handler extendable, without resorting to managed Message ID assignment or dynamic_cast<> testing?
Thanks...