Event.h
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 #ifndef EVENT_HEADER
00045 #define EVENT_HEADER
00046
00047 #include <string>
00048 #include <vector>
00049 #include <stdexcept>
00050
00051 #include "../dllinclude.h"
00052 #include "EventAttribute.h"
00053 #include "EventAttributeBase.h"
00054 #include "iostream_ext.h"
00055 #include "OSUtils.h"
00056
00057 namespace ot
00058 {
00063 typedef std::map<std::string, EventAttributeBase*> AttributeMap;
00072 template <typename T>
00073 static T* copyV2A(const std::vector<T> &vector, T* array)
00074 {
00075 for (unsigned int i = 0; i < vector.size(); i++)
00076 array[i] = vector[i];
00077 return array;
00078 };
00088 template <typename T>
00089 static std::vector<T>& copyA2V(const T *array, const int arraySize, std::vector<T> &vector)
00090 {
00091 vector.clear();
00092 for (int i = 0; i < arraySize; i++)
00093 vector.push_back(array[i]);
00094 return vector;
00095 };
00104 template <typename T>
00105 static const std::vector<T> copyA2V(const T *array, const int arraySize)
00106 {
00107 std::vector<T> vector;
00108 for (int i = 0; i < arraySize; i++)
00109 vector.push_back(array[i]);
00110 return vector;
00111 };
00112
00113
00125 class OPENTRACKER_API Event
00126 {
00127 public:
00131 Event();
00136 Event(const Event &rv);
00140 ~Event();
00141
00142
00147 Event& operator=(const Event &rv);
00153 bool hasAttribute(const std::string &name) const;
00159 bool delAttribute(const std::string &name);
00166 bool renAttribute(const std::string &oldName, const std::string &newName);
00172 void copyAllButStdAttr(const Event &rv);
00176 void clearAttributes();
00181 void printout() const;
00187 const std::string getPrintOut() const;
00191 void timeStamp();
00196 void serialize(std::ostream &out) const;
00201 const std::string serialize() const;
00208 std::istream& deserialize(std::istream &in);
00214 void deserialize(std::string &str);
00219 inline int getSize() const { return attributes.size(); };
00226 const std::type_info& getAttributeType(const std::string &name) const throw (std::invalid_argument);
00233 const std::string& getAttributeTypeName(const std::string &name) const throw (std::invalid_argument);
00240 const std::string& getAttributeName(const int index) const throw (std::invalid_argument);
00247 int getAttributeIndex(const std::string &name) const throw (std::invalid_argument);
00248
00249
00262
00267 std::vector<float>& getPosition();
00272 inline const std::vector<float>& getPosition() const { return (const_cast<Event *>(this))->getPosition(); }
00277 std::vector<float>& getOrientation();
00282 inline const std::vector<float>& getOrientation() const { return (const_cast<Event *>(this))->getOrientation(); }
00287 float& getConfidence();
00292 const float& getConfidence() const { return (const_cast<Event *>(this))->getConfidence(); }
00297 unsigned short& getButton();
00302 const unsigned short& getButton() const { return (const_cast<Event *>(this))->getButton(); }
00307 inline void setPosition(const std::vector<float> &value) { setAttribute("position", value); };
00312 inline void setPosition(const float *value) { setAttribute("position", copyA2V(value, 3)); };
00317 inline void setOrientation(const std::vector<float> &value) { setAttribute("orientation", value); };
00322 inline void setOrientation(const float *value) { setAttribute("orientation", copyA2V(value, 4)); };
00327 inline void setConfidence(const float &value) { setAttribute("confidence", value); };
00332 inline void setButton(const unsigned short &value) { setAttribute("button", value); };
00333
00335
00336
00343
00352 bool addAttribute(const std::string &type, const std::string &name, const std::string &value);
00362 bool setAttribute(const std::string &type, const std::string &name, const std::string &value);
00368 const std::string getAttributeValueString(const std::string &name) const throw (std::invalid_argument);
00369
00371
00382 static void registerAllKnownTypes();
00388 static bool knowsType(const std::string typeName);
00399 template <typename T>
00400 static void registerGenericTypeName(const T *dummy, const std::string &genericTypeName)
00401 {
00402 EventAttributeBase::registerType(genericTypeName, typeid(T), EventAttribute<T>::create);
00403 };
00411 template <typename T>
00412 T& getAttribute(const T *dummy, const std::string &name)
00413 {
00414 AttributeMap::const_iterator it = attributes.find(name);
00415 if (it == attributes.end())
00416 {
00417 std::string errorStr = "event does not have attribute called '" + name + "'";
00418 throw std::invalid_argument(errorStr);
00419 }
00420 else
00421 {
00422 EventAttribute<T> *att = dynamic_cast<EventAttribute<T>*>((*it).second);
00423 if (att)
00424 return att->get();
00425 else
00426 {
00427 std::string errorStr = "attribute called '" + name + "' is not of type '" + typeid(T).name() + "'";
00428 throw std::invalid_argument(errorStr);
00429 }
00430 }
00431 };
00439 template <typename T>
00440 const T& getAttribute(const T *dummy, const std::string &name) const throw (std::invalid_argument)
00441 {
00442 return (const_cast<Event *>(this))->getAttribute(dummy, name);
00443 };
00452 template <typename T>
00453 T& getAttribute(const std::string &name, const T &defValue)
00454 {
00455 try
00456 {
00457 return getAttribute((T*)NULL, name);
00458 }
00459 catch (std::invalid_argument)
00460 {
00461 addAttribute(name, defValue);
00462 return getAttribute((T*)NULL, name);
00463 }
00464 };
00471 template <typename T>
00472 bool addAttribute(const std::string &name, const T &value)
00473 {
00474 if (hasAttribute(name))
00475 return false;
00476 else
00477 {
00478 EventAttributeBase *att = EventAttributeBase::create(typeid(T));
00479 attributes[name] = att;
00480 return setAttribute(name, value);
00481 }
00482 };
00490 template <typename T>
00491 bool setAttribute(const std::string &name, const T &value)
00492 {
00493 if (!hasAttribute(name))
00494 attributes[name] = EventAttributeBase::create(typeid(T));
00495 EventAttribute<T> *att = dynamic_cast<EventAttribute<T>*>(attributes[name]);
00496 if (att)
00497 att->set(value);
00498 return (att != NULL);
00499 };
00500
00501 public:
00503 double time;
00505 static Event null;
00506 private:
00508 AttributeMap attributes;
00509 };
00510
00517 OPENTRACKER_API std::istream& operator>>(std::istream &in, ot::Event &event);
00524 OPENTRACKER_API std::ostream& operator<<(std::ostream &out, const ot::Event &event);
00525
00526 }
00527
00528 #endif
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544