00001 00012 #ifndef MESSAGEPOOL_H 00013 #define MESSAGEPOOL_H 00014 00015 #include <vector> 00016 #include <string> 00017 #include <map> 00018 #include <boost/shared_ptr.hpp> 00019 #include "MessageInstance.h" 00020 #include "util/Error.h" 00021 00022 00023 namespace green 00024 { 00028 class MsgFormat { 00029 public: 00030 Key mEntityType; 00031 Key mMessageType; 00032 00033 MsgFormat() : 00034 mEntityType(0), 00035 mMessageType(0) 00036 {} 00037 00038 MsgFormat(Key entType, Key msgType) : mEntityType(entType), mMessageType(msgType) {} 00039 00040 bool operator<(const MsgFormat& rhs) const { 00041 return ((mEntityType < rhs.mEntityType) || 00042 ((mEntityType == rhs.mEntityType ) && (mMessageType < rhs.mMessageType))); 00043 } 00044 }; 00045 00052 class MessagePool 00053 { 00054 public: 00055 00056 virtual ~MessagePool() {} 00062 virtual void AddMessage(Key targetType, MessageInstancePtr msg) 00063 { 00064 CHECK_SHARED(msg); 00065 int msgKey = msg->GetTemplate(); 00066 if(msgKey != 1) 00067 printf("Adding msg targetType-%d \n",msgKey); 00068 00069 ERROR_ASSERT(msgKey < 2, "msgKey was too high"); 00070 00071 00072 MsgFormat format(targetType, msg->GetTemplate()); 00073 MsgMap::iterator i = _getList(format); 00074 00075 mMsgMap[format].push_back(msg); 00076 } 00077 00083 virtual size_t GetNumMessagesOfFormat(MsgFormat format) 00084 { 00085 MsgFormat defaultFormat(0, format.mMessageType); 00086 MsgMap::const_iterator i = _getConstList(format); 00087 MsgMap::const_iterator j = _getConstList(defaultFormat); 00088 00089 size_t exactSize = 0; 00090 size_t defaultSize = 0; 00091 00092 if(i != mMsgMap.end()) 00093 exactSize = i->second.size(); 00094 if(j != mMsgMap.end()) 00095 defaultSize = j->second.size(); 00096 00097 return exactSize + defaultSize; 00098 } 00099 00108 virtual MessageInstancePtr 00109 GetMessageOfFormat(size_t n, MsgFormat format) const 00110 { 00111 MsgFormat defaultFormat(0, format.mMessageType); 00112 MsgMap::const_iterator i = _getConstList(format); 00113 MsgMap::const_iterator j = _getConstList(defaultFormat); 00114 00115 size_t exactSize = 0; 00116 size_t defaultSize = 0; 00117 00118 if(i != mMsgMap.end()) 00119 exactSize = i->second.size(); 00120 if(j != mMsgMap.end()) 00121 defaultSize = j->second.size(); 00122 00123 ERROR_ASSERT(n < exactSize + defaultSize, "invalid index"); 00124 00125 if(n < exactSize) 00126 return i->second[n]; 00127 else 00128 return j->second[n - exactSize]; 00129 } 00130 00136 virtual void Clear() 00137 { 00138 mMsgMap.clear(); 00139 } 00140 00141 protected: 00145 typedef std::vector<MessageInstancePtr> MsgList; 00146 typedef std::map<MsgFormat, MsgList> MsgMap; 00147 00148 MsgMap::iterator _getList(MsgFormat format) 00149 { 00150 MsgMap::iterator i = mMsgMap.find(format); 00151 return i; 00152 } 00153 00154 MsgMap::const_iterator _getConstList(MsgFormat format) const 00155 { 00156 MsgMap::const_iterator i = mMsgMap.find(format); 00157 return i; 00158 } 00159 00161 MsgMap mMsgMap; 00162 }; 00163 typedef boost::shared_ptr<MessagePool> MessagePoolPtr; 00164 } 00165 00166 #endif