Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extendable message passing problem

Status
Not open for further replies.

SquidReplicator

Programmer
Dec 7, 2006
1
GB
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...

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...
 
I don't really see what the problem is with using ints or enums? Couldn't you just do something like this:
Code:
// message_type.h
#ifndef MESSAGE_TYPE_H
#define MESSAGE_TYPE_H

enum MessageType
{
   KeyboardMessage = 1,
   MouseMessage    = 2,
   NetworkMessage  = 3,
   ...
};

#endif  // MESSAGE_TYPE_H
Code:
// message.h
#ifndef MESSAGE_H
#define MESSAGE_H
#include "message_type.h"

class Message
{
public:
   virtual MessageType MessageType() = 0;
};

#endif // MESSAGE_H
Code:
// keyboard_message.h
#ifndef KEYBOARD_MESSAGE_H
#define KEYBOARD_MESSAGE_H
#include "message.h"

class Message
{
public:
   virtual MessageType MessageType()
   {
      return KeyboardMessage;
   }
};

#endif // KEYBOARD_MESSAGE_H
Code:
// message_handler.h
#ifndef MESSAGE_HANDLER_H
#define MESSAGE_HANDLER_H

#include "keyboard_message.h"
#include "mouse_message.h"
...

class MessageHandler
{
public:
   void HandleMessage( const Message&  message )
   {
      switch ( message.MessageType() )
      {
         case KeyboardMessage:
            // Do something.
            break;
         case MouseMessage:
            // Do something else.
            break;
         ...
         default:
            // Unknown message type.
      }
   }
};

#endif // MESSAGE_HANDLER_H
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top