#include "msgbag.h" #include "message.h" #include "except.h" class BasicPortMessage; MessageBag::MessageBag() : count(0) ,msgtime(Time::Inf) {} MessageBag::~MessageBag() { eraseAll(); } MessageBag &MessageBag::add(const BasicPortMessage* msg) { MASSERT( count == 0 || (count !=0 && msg->time() == msgtime)); if(count == 0) msgtime = msg->time(); msgs[msg->port().name()].push_back(msg); count++; return *this; } MessageBag &MessageBag::eraseAll() { MessagesOnPort::const_iterator cursor; MessageList::const_iterator portmsgs; for( cursor = msgs.begin(); cursor != msgs.end() ; cursor++) { for(portmsgs = cursor->second.begin(); portmsgs != cursor->second.end(); portmsgs++) { delete (*portmsgs); } } msgs.erase(msgs.begin(), msgs.end()); count = 0; msgtime = Time::Inf; return *this; } bool MessageBag::portHasMsgs(const string& portName) const { if(msgs.find(portName) == msgs.end() ) return false; else return true; } const MessageBag::MessageList& MessageBag::msgsOnPort(const string& portName) const { MessagesOnPort::const_iterator cursor; cursor = msgs.find(portName); MASSERTMSG(cursor != msgs.end(), "Requested messages for a port that does not have any message!"); return cursor->second; } MessageBag::iterator MessageBag::begin() const { iterator it(msgs.begin(), msgs.end() ); return it; } MessageBag::iterator MessageBag::end() const { iterator it(msgs.end(), msgs.end() ); return it; } MessageBag::iterator::iterator(const MessageBag::MessagesOnPort::const_iterator &begin, const MessageBag::MessagesOnPort::const_iterator &end, const MessageBag::MessageList* currList) : msgsxport(begin) , msgsEnd(end) , currentList(currentList) {}; MessageBag::iterator::iterator(const MessageBag::MessagesOnPort::const_iterator &begin,const MessageBag::MessagesOnPort::const_iterator &end) : msgsxport(begin) , msgsEnd(end) { if(msgsxport != msgsEnd) { currentList = & (msgsxport->second); msgs = currentList->begin(); } else currentList = NULL; }; MessageBag::iterator &MessageBag::iterator::operator ++(int) { msgs++; if(msgs == currentList->end()) { msgsxport++; if( msgsxport == msgsEnd) { currentList = NULL; } else { currentList = &(msgsxport->second); msgs = currentList->begin(); } } return *this; } const BasicPortMessage *MessageBag::iterator::operator *() const { return (*msgs); } bool MessageBag::iterator::operator ==(const iterator &it) const { if( currentList == NULL && it.currentList == NULL) return true; else if (currentList == NULL || it.currentList == NULL) return false; else { return msgs == it.msgs; } } MessageBag::iterator & MessageBag::iterator::operator=(const iterator &it) { msgsxport = it.msgsxport; msgsEnd = it.msgsEnd; currentList = it.currentList; msgs = it.msgs; return *this; }