| 1 | /*************************************************************************** |
| 2 | message.cpp - description |
| 3 | ------------------- |
| 4 | begin : nov 29th, 2005 |
| 5 | copyright : (C) 2005-2010 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | author : Victor STINNER |
| 8 | |
| 9 | $Id: message.cpp 448 2010-05-09 15:18:32Z neoneurone $ |
| 10 | ***************************************************************************/ |
| 11 | |
| 12 | /*************************************************************************** |
| 13 | * * |
| 14 | * This program is free software; you can redistribute it and/or modify * |
| 15 | * it under the terms of the GNU General Public License as published by * |
| 16 | * the Free Software Foundation; either version 2 of the License, or * |
| 17 | * any later version. * |
| 18 | * * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | |
| 22 | #include "message.h" |
| 23 | #include "agent.h" |
| 24 | |
| 25 | #include <cassert> |
| 26 | |
| 27 | |
| 28 | /*=====================================================================*/ |
| 29 | Message::Message(): |
| 30 | _sender(NULL), |
| 31 | _type(MSG_UNDEFINED) |
| 32 | {} |
| 33 | |
| 34 | |
| 35 | /*=====================================================================*/ |
| 36 | Message::Message( MAS_MESSAGE_TYPE type, Agent *sender ): |
| 37 | _sender(sender), |
| 38 | _type(type) |
| 39 | {} |
| 40 | |
| 41 | |
| 42 | /*=====================================================================*/ |
| 43 | MAS_MESSAGE_TYPE |
| 44 | Message::getType() const |
| 45 | { |
| 46 | return _type; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /*=====================================================================*/ |
| 51 | Agent* Message::getSender() const |
| 52 | { |
| 53 | return _sender; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /*=====================================================================*/ |
| 58 | Message& |
| 59 | Message::setSender(Agent* sender) |
| 60 | { |
| 61 | _sender = sender; |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /*=====================================================================*/ |
| 67 | unsigned int Message::size() const |
| 68 | { |
| 69 | return _arguments.size(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /*=====================================================================*/ |
| 74 | Message& |
| 75 | Message::operator<< (int value) |
| 76 | { |
| 77 | _arguments.push_back(Any(value)); |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /*=====================================================================*/ |
| 83 | Message& |
| 84 | Message::operator<< (unsigned int value) |
| 85 | { |
| 86 | _arguments.push_back(Any(value)); |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /*=====================================================================*/ |
| 92 | Message& |
| 93 | Message::operator<< (double value) |
| 94 | { |
| 95 | _arguments.push_back(Any(value)); |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /*=====================================================================*/ |
| 101 | Message& |
| 102 | Message::operator<< (const std::string &value) |
| 103 | { |
| 104 | _arguments.push_back(Any(value)); |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /*=====================================================================*/ |
| 110 | Any& |
| 111 | Message::operator[] (unsigned int index) |
| 112 | { |
| 113 | return _arguments.at(index); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /*=====================================================================*/ |
| 118 | const Any& |
| 119 | Message::operator[] (unsigned int index) const |
| 120 | { |
| 121 | return _arguments.at(index); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /*=====================================================================*/ |
| 126 | std::ostream& |
| 127 | operator<< (std::ostream& os, const Message &msg) |
| 128 | { |
| 129 | std::vector<Any>::const_iterator |
| 130 | it = msg._arguments.begin(), |
| 131 | end = msg._arguments.end(); |
| 132 | bool separator = false; |
| 133 | |
| 134 | os << "Message <type=" << msg.getType() << ", args={"; |
| 135 | for (; it != end; ++it) |
| 136 | { |
| 137 | if (separator) |
| 138 | os << ", "; |
| 139 | else |
| 140 | separator = true; |
| 141 | os << *it; |
| 142 | } |
| 143 | os << "}>"; |
| 144 | |
| 145 | return os; |
| 146 | } |