Event.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00042
00043
00044 #include <string>
00045 #include <sstream>
00046 #include <vector>
00047
00048 #include "Event.h"
00049 #include "Translator.h"
00050 #include "../tool/OT_ACE_Log.h"
00051
00052 namespace ot
00053 {
00054
00055
00056 Event Event::null;
00057
00058
00059 Event::Event()
00060 {
00061 timeStamp();
00062 }
00063
00064
00065 Event::Event(const Event &rv)
00066 {
00067 *this = rv;
00068 }
00069
00070
00071 Event::~Event()
00072 {
00073 clearAttributes();
00074 }
00075
00076
00077 Event& Event::operator=(const Event& rv)
00078 {
00079 if( this == &rv ) return *this;
00080
00081 time = rv.time;
00082 clearAttributes();
00083 const AttributeMap &rvMap = rv.attributes;
00084 for (AttributeMap::const_iterator it = rvMap.begin(); it != rvMap.end(); ++it)
00085 {
00086 std::string name = (*it).first;
00087 EventAttributeBase *rvAtt = (*it).second;
00088 EventAttributeBase *myAtt = EventAttributeBase::create(rvAtt->getGenericTypeName());
00089 *myAtt = *rvAtt;
00090 attributes[name] = myAtt;
00091 }
00092 return *this;
00093 }
00094
00095
00096 void Event::copyAllButStdAttr(const Event &rv)
00097 {
00098 const AttributeMap &rvMap = rv.attributes;
00099 for (AttributeMap::const_iterator it = rvMap.begin(); it != rvMap.end(); ++it)
00100 {
00101 std::string name = (*it).first;
00102 if (name != "position" &&
00103 name != "orientation" &&
00104 name != "confidence" &&
00105 name != "button")
00106 {
00107 EventAttributeBase *rvAtt = (*it).second;
00108 EventAttributeBase *newAtt = EventAttributeBase::create(rvAtt->getGenericTypeName());
00109 *newAtt = *rvAtt;
00110 delAttribute(name);
00111 attributes[name] = newAtt;
00112 }
00113 }
00114 }
00115
00116
00117 const std::string& Event::getAttributeName(const int index) const throw (std::invalid_argument)
00118 {
00119 int i = 0;
00120 for (AttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it)
00121 {
00122 if (i == index)
00123 return (*it).first;
00124 i++;
00125 }
00126 std::stringstream ss;
00127 ss << "event has no attribute with index '" << index << "', check size using Event::size() before access by index!";
00128 throw std::invalid_argument(ss.str());
00129 }
00130
00131
00132 int Event::getAttributeIndex(const std::string &name) const throw (std::invalid_argument)
00133 {
00134 int index = 0;
00135 for (AttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it)
00136 {
00137 if ((*it).first == name)
00138 return index;
00139 index++;
00140 }
00141 std::string errorStr = "event does not have attribute called '" + name + "'";
00142 throw std::invalid_argument(errorStr);
00143 }
00144
00145
00146 const std::type_info& Event::getAttributeType(const std::string &name) const throw (std::invalid_argument)
00147 {
00148 if (hasAttribute(name))
00149 {
00150 EventAttributeBase *att = (*attributes.find(name)).second;
00151 return att->getType();
00152 }
00153 std::string errorStr = "event does not have attribute called '" + name + "'";
00154 throw std::invalid_argument(errorStr);
00155 }
00156
00157
00158 const std::string& Event::getAttributeTypeName(const std::string &name) const throw (std::invalid_argument)
00159 {
00160 if (hasAttribute(name))
00161 {
00162 EventAttributeBase *att = (*attributes.find(name)).second;
00163 return att->getGenericTypeName();
00164 }
00165 std::string errorStr = "event does not have attribute called '" + name + "'";
00166 throw std::invalid_argument(errorStr);
00167 }
00168
00169
00170 const std::string Event::getAttributeValueString(const std::string &name) const throw (std::invalid_argument)
00171 {
00172 if (hasAttribute(name))
00173 {
00174 std::stringstream ss;
00175 EventAttributeBase *att = (*attributes.find(name)).second;
00176 ss << *att;
00177 return ss.str();
00178 }
00179 std::string errorStr = "event does not have attribute called '" + name + "'";
00180 throw std::invalid_argument(errorStr);
00181 }
00182
00183
00184 std::vector<float>& Event::getPosition()
00185 {
00186 return getAttribute("position", std::vector<float>(3, .0f));
00187 };
00188
00189
00190 std::vector<float>& Event::getOrientation()
00191 {
00192 std::vector<float> defVec = std::vector<float>(3, .0f);
00193 defVec.push_back(1.f);
00194 return getAttribute("orientation", defVec);
00195 }
00196
00197
00198 float& Event::getConfidence()
00199 {
00200 return getAttribute("confidence", 1.0f);
00201 }
00202
00203
00204 unsigned short& Event::getButton()
00205 {
00206 return getAttribute(std::string("button"), (unsigned short)0);
00207 }
00208
00209
00210 void Event::serialize(std::ostream &out) const
00211 {
00212
00213 out.precision(24);
00214 out << "{" << std::fixed << time << "-" << attributes.size() << ":";
00215 for (AttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it)
00216 {
00217 if (it != attributes.begin())
00218 out << ",";
00219 out << (*it).second->getGenericTypeName() << "." << (*it).first << "=" << *((*it).second);
00220 }
00221 out << "}";
00222 }
00223
00224 const std::string Event::serialize() const
00225 {
00226 std::stringstream ss;
00227 ss << *this;
00228 return ss.str();
00229 }
00230
00231 void Event::deserialize(std::string &str)
00232 {
00233 std::stringstream ss(str);
00234 ss >> *this;
00235 }
00236
00237
00238 std::istream& Event::deserialize(std::istream &in)
00239 {
00240 char c;
00241 std::string dataStr, typeStr, nameStr, attrStr;
00242 std::string::size_type typeDelimiter, nameDelimiter, attrDelimiter;
00243 AttributeMap::size_type i, mapSize;
00244
00245 clearAttributes();
00246
00247
00248 if (!(in >> c) || c != '{'
00249 || !(in >> time)
00250 || !(in >> c) || c != '-'
00251 || !(in >> mapSize)
00252 || !(in >> c) || c != ':'
00253 || !(in >> dataStr))
00254 {
00255 in.setstate(std::ios_base::failbit);
00256 return in;
00257 }
00258
00259
00260 for (i = 0; i < mapSize; i++)
00261 {
00262
00263 typeDelimiter = dataStr.find(".");
00264 nameDelimiter = dataStr.find("=");
00265 attrDelimiter = dataStr.find_first_of(",}");
00266
00267 typeStr = dataStr.substr(0, typeDelimiter);
00268 nameStr = dataStr.substr(typeDelimiter + 1, nameDelimiter - (typeDelimiter + 1));
00269 attrStr = dataStr.substr(nameDelimiter + 1, attrDelimiter - (nameDelimiter + 1));
00270 dataStr = dataStr.substr(attrDelimiter + 1, std::string::npos);
00271
00272
00273 if (typeDelimiter == std::string::npos
00274 || nameDelimiter == std::string::npos
00275 || attrDelimiter == std::string::npos
00276 || typeDelimiter >= nameDelimiter
00277 || typeDelimiter >= attrDelimiter
00278 || hasAttribute(nameStr))
00279 {
00280 in.setstate(std::ios_base::failbit);
00281 return in;
00282 }
00283 try
00284 {
00285 EventAttributeBase *att = EventAttributeBase::create(typeStr);
00286 std::stringstream ss(attrStr);
00287 if (ss >> *att)
00288 attributes[nameStr] = att;
00289 else
00290 {
00291 in.setstate(std::ios_base::failbit);
00292 return in;
00293 }
00294 }
00295 catch (std::runtime_error)
00296 {
00297 in.setstate(std::ios_base::failbit);
00298 return in;
00299 }
00300 }
00301 return in;
00302 }
00303
00304
00305 bool Event::addAttribute(const std::string &type, const std::string &name, const std::string &value)
00306 {
00307 if (hasAttribute(name))
00308 return false;
00309
00310 EventAttributeBase *att = EventAttributeBase::create(type);
00311 std::stringstream ss(value);
00312 if (ss >> *att)
00313 {
00314 attributes[name] = att;
00315 return true;
00316 }
00317 else
00318 {
00319 delete att;
00320 return false;
00321 }
00322 }
00323
00324
00325 bool Event::setAttribute(const std::string &type, const std::string &name, const std::string &value)
00326 {
00327 if (!hasAttribute(name))
00328 attributes[name] = EventAttributeBase::create(type);
00329 EventAttributeBase *att = attributes[name];
00330 std::stringstream ss(value);
00331 if (ss >> *att)
00332 return true;
00333 return false;
00334 }
00335
00336
00337 bool Event::delAttribute(const std::string &name)
00338 {
00339 AttributeMap::iterator it = attributes.find(name);
00340 if (it == attributes.end())
00341 return false;
00342 else
00343 {
00344 delete (*it).second;
00345 (*it).second = NULL;
00346 attributes.erase(it);
00347 return true;
00348 }
00349 }
00350
00351
00352 void Event::clearAttributes()
00353 {
00354 for (AttributeMap::iterator it = attributes.begin(); it != attributes.end(); ++it)
00355 delete (*it).second;
00356 attributes.clear();
00357 }
00358
00359
00360 void Event::printout() const
00361 {
00362 LOG_ACE_INFO(getPrintOut().c_str());
00363 LOG_ACE_INFO("\n");
00364
00365 }
00366
00367
00368 const std::string Event::getPrintOut() const
00369 {
00370 std::string printOut;
00371 std::stringstream ss;
00372
00373 ss << " timestamp: " << std::fixed << time << std::endl;
00374
00375 for (AttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it)
00376 ss << " " << (*it).first << " (" << (*it).second->getGenericTypeName() << "): " << *((*it).second) << std::endl;
00377 return ss.str();
00378 }
00379
00380
00381 bool Event::renAttribute(const std::string &oldName, const std::string &newName)
00382 {
00383 AttributeMap::iterator oldIt = attributes.find(oldName);
00384 AttributeMap::iterator newIt = attributes.find(newName);
00385
00386 if (newIt != attributes.end() || oldIt == attributes.end())
00387 return false;
00388 else
00389 {
00390 attributes[newName] = (*oldIt).second;
00391 attributes.erase(oldIt);
00392 return true;
00393 }
00394 }
00395
00396
00397 bool Event::hasAttribute(const std::string &name) const
00398 {
00399 return (attributes.find(name) != attributes.end());
00400 }
00401
00402
00403 bool Event::knowsType(const std::string typeName)
00404 {
00405 return EventAttributeBase::translator.knowsType(typeName);
00406 }
00407
00408
00409 void Event::registerAllKnownTypes()
00410 {
00411 registerGenericTypeName((bool*)NULL, "bool");
00412 registerGenericTypeName((char*)NULL, "char");
00413 registerGenericTypeName((signed char*)NULL, "signed_char");
00414 registerGenericTypeName((unsigned char*)NULL, "unsigned_char");
00415 registerGenericTypeName((int*)NULL, "int");
00416 registerGenericTypeName((long*)NULL, "long");
00417 registerGenericTypeName((short*)NULL, "short");
00418 registerGenericTypeName((unsigned int*)NULL, "unsigned_int");
00419 registerGenericTypeName((unsigned long*)NULL, "unsigned_long");
00420 registerGenericTypeName((unsigned short*)NULL, "unsigned_short");
00421 registerGenericTypeName((double*)NULL, "double");
00422 registerGenericTypeName((long double*)NULL, "long_double");
00423 registerGenericTypeName((float*)NULL, "float");
00424 registerGenericTypeName((std::string*)NULL, "string");
00425 registerGenericTypeName((std::vector<float>*)NULL, "vector<float>");
00426 }
00427
00428
00429 void Event::timeStamp()
00430 {
00431 time = OSUtils::currentTime();
00432 }
00433
00434
00435 std::istream& operator>>(std::istream &in, ot::Event &event)
00436 {
00437 event.deserialize(in);
00438 return in;
00439 };
00440
00441
00442 std::ostream& operator<<(std::ostream &out, const ot::Event &event)
00443 {
00444 event.serialize(out);
00445 return out;
00446 };
00447
00448 }
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464