1 /*
2  *  MessageDispatcher.h
3  *  Created by Woody Zenfell, III on Sun Aug 31 2003.
4  */
5 
6 /*
7   Copyright (c) 2003, Woody Zenfell, III
8 
9   Permission is hereby granted, free of charge, to any person obtaining a copy
10   of this software and associated documentation files (the "Software"), to deal
11   in the Software without restriction, including without limitation the rights
12   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13   copies of the Software, and to permit persons to whom the Software is
14   furnished to do so, subject to the following conditions:
15 
16   The above copyright notice and this permission notice shall be included in
17   all copies or substantial portions of the Software.
18 
19   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25   SOFTWARE.
26 */
27 
28 
29 #ifndef MESSAGEDISPATCHER_H
30 #define MESSAGEDISPATCHER_H
31 
32 #include <map>
33 
34 #include "Message.h"
35 #include "MessageHandler.h"
36 
37 
38 class MessageDispatcher : public MessageHandler
39 {
40 public:
MessageDispatcher()41 	MessageDispatcher() : mDefaultHandler(NULL) {}
42 
setHandlerForType(MessageHandler * inHandler,MessageTypeID inType)43 	void setHandlerForType(MessageHandler* inHandler, MessageTypeID inType)
44 	{
45 		if(inHandler == NULL)
46 			clearHandlerForType(inType);
47 		else
48 			mMap[inType] = inHandler;
49 	}
50 
handlerForType(MessageTypeID inType)51 	MessageHandler* handlerForType(MessageTypeID inType)
52 	{
53 		MessageHandler* theHandler = mDefaultHandler;
54 
55 		MessageDispatcherMap::iterator i = mMap.find(inType);
56 		if(i != mMap.end())
57 			theHandler = i->second;
58 
59 		return theHandler;
60 	}
61 
handlerForTypeNoDefault(MessageTypeID inType)62 	MessageHandler* handlerForTypeNoDefault(MessageTypeID inType)
63 	{
64 		MessageDispatcherMap::iterator i = mMap.find(inType);
65 		return (i != mMap.end()) ? i->second : NULL;
66 	}
67 
clearHandlerForType(MessageTypeID inType)68 	void clearHandlerForType(MessageTypeID inType)
69 	{
70 		mMap.erase(inType);
71 	}
72 
setDefaultHandler(MessageHandler * inHandler)73 	void setDefaultHandler(MessageHandler* inHandler)
74 	{
75 		mDefaultHandler = inHandler;
76 	}
77 
defaultHandler()78 	MessageHandler* defaultHandler() const
79 	{
80 		return mDefaultHandler;
81 	}
82 
handle(Message * inMessage,CommunicationsChannel * inChannel)83 	void handle(Message* inMessage, CommunicationsChannel* inChannel)
84 	{
85 		MessageHandler* theHandler = handlerForType(inMessage->type());
86 
87 		if(theHandler != NULL)
88 			theHandler->handle(inMessage, inChannel);
89 	}
90 
91 private:
92 	typedef std::map<MessageTypeID, MessageHandler*> MessageDispatcherMap;
93 
94 	MessageDispatcherMap	mMap;
95 	MessageHandler*		mDefaultHandler;
96 };
97 
98 #endif // MESSAGEDISPATCHER_H
99