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 #ifdef USE_CORBA
00058 #include <OpenTracker/skeletons/OT_CORBA.hh>
00059 #endif //USE_CORBA
00060
00061 namespace ot
00062 {
00067 typedef std::map<std::string, EventAttributeBase*> AttributeMap;
00076 template <typename T>
00077 static T* copyV2A(const std::vector<T> &vector, T* array)
00078 {
00079 for (unsigned int i = 0; i < vector.size(); i++)
00080 array[i] = vector[i];
00081 return array;
00082 };
00092 template <typename T>
00093 static std::vector<T>& copyA2V(const T *array, const int arraySize, std::vector<T> &vector)
00094 {
00095 vector.clear();
00096 for (int i = 0; i < arraySize; i++)
00097 vector.push_back(array[i]);
00098 return vector;
00099 };
00108 template <typename T>
00109 static const std::vector<T> copyA2V(const T *array, const int arraySize)
00110 {
00111 std::vector<T> vector;
00112 for (int i = 0; i < arraySize; i++)
00113 vector.push_back(array[i]);
00114 return vector;
00115 };
00116
00117
00129 class OPENTRACKER_API Event
00130 {
00131 public:
00135 Event();
00140 Event(const Event &rv);
00141
00146 #ifdef USE_CORBA
00147 Event(const OT_CORBA::Event &ev);
00148 #endif
00149
00153 ~Event();
00154
00155
00160 Event& operator=(const Event &rv);
00166 bool hasAttribute(const std::string &name) const;
00172 bool delAttribute(const std::string &name);
00179 bool renAttribute(const std::string &oldName, const std::string &newName);
00185 void copyAllButStdAttr(const Event &rv);
00189 void clearAttributes();
00194 void printout() const;
00200 const std::string getPrintOut() const;
00206 void getPrintOut(std::string &outstr) const;
00210 void timeStamp();
00215 void serialize(std::ostream &out) const;
00220 const std::string serialize() const;
00227 std::istream& deserialize(std::istream &in);
00233 void deserialize(std::string &str);
00238 inline int getSize() const { return (int)attributes.size(); };
00245 const std::type_info& getAttributeType(const std::string &name) const throw (std::invalid_argument);
00252 const std::string& getAttributeTypeName(const std::string &name) const throw (std::invalid_argument);
00259 const std::string& getAttributeName(const int index) const throw (std::invalid_argument);
00266 int getAttributeIndex(const std::string &name) const throw (std::invalid_argument);
00267
00268
00281
00286 std::vector<float>& getPosition();
00291 inline const std::vector<float>& getPosition() const { return (const_cast<Event *>(this))->getPosition(); }
00296 std::vector<float>& getOrientation();
00301 inline const std::vector<float>& getOrientation() const { return (const_cast<Event *>(this))->getOrientation(); }
00306 float& getConfidence();
00311 const float& getConfidence() const { return (const_cast<Event *>(this))->getConfidence(); }
00316 unsigned short& getButton();
00321 const unsigned short& getButton() const { return (const_cast<Event *>(this))->getButton(); }
00326 inline void setPosition(const std::vector<float> &value) { setAttribute("position", value); };
00331 inline void setPosition(const float *value) { setAttribute("position", copyA2V(value, 3)); };
00336 inline void setOrientation(const std::vector<float> &value) { setAttribute("orientation", value); };
00341 inline void setOrientation(const float *value) { setAttribute("orientation", copyA2V(value, 4)); };
00346 inline void setConfidence(const float &value) { setAttribute("confidence", value); };
00351 inline void setButton(const unsigned short &value) { setAttribute("button", value); };
00352
00354
00355
00362
00371 bool addAttribute(const std::string &type, const std::string &name, const std::string &value);
00381 bool setAttribute(const std::string &type, const std::string &name, const std::string &value);
00387 const std::string getAttributeValueString(const std::string &name) const throw (std::invalid_argument);
00388
00390
00401 static void registerAllKnownTypes();
00407 static bool knowsType(const std::string typeName);
00425 #ifdef USE_CORBA
00426 OT_CORBA::Event getCORBAEvent();
00427 #endif
00428
00429 template <typename T>
00430 static void registerGenericTypeName(const T *dummy, const std::string &genericTypeName)
00431 {
00432 EventAttributeBase::registerType(genericTypeName, typeid(T), EventAttribute<T>::create);
00433 };
00441 template <typename T>
00442 T& getAttribute(const T *dummy, const std::string &name)
00443 {
00444 AttributeMap::const_iterator it = attributes.find(name);
00445 if (it == attributes.end())
00446 {
00447 std::string errorStr = "event does not have attribute called '" + name + "'";
00448 throw std::invalid_argument(errorStr);
00449 }
00450 else
00451 {
00452 EventAttribute<T> *att = dynamic_cast<EventAttribute<T>*>((*it).second);
00453 if (att)
00454 return att->get();
00455 else
00456 {
00457 std::string errorStr = "attribute called '" + name + "' is not of type '" + typeid(T).name() + "'";
00458 throw std::invalid_argument(errorStr);
00459 }
00460 }
00461 };
00469 template <typename T>
00470 const T& getAttribute(const T *dummy, const std::string &name) const throw (std::invalid_argument)
00471 {
00472 return (const_cast<Event *>(this))->getAttribute(dummy, name);
00473 };
00482 template <typename T>
00483 T& getAttribute(const std::string &name, const T &defValue)
00484 {
00485 try
00486 {
00487 return getAttribute((T*)NULL, name);
00488 }
00489 catch (std::invalid_argument)
00490 {
00491 addAttribute(name, defValue);
00492 return getAttribute((T*)NULL, name);
00493 }
00494 };
00501 template <typename T>
00502 bool addAttribute(const std::string &name, const T &value)
00503 {
00504 if (hasAttribute(name))
00505 return false;
00506 else
00507 {
00508 EventAttributeBase *att = EventAttributeBase::create(typeid(T));
00509 attributes[name] = att;
00510 return setAttribute(name, value);
00511 }
00512 };
00520 template <typename T>
00521 bool setAttribute(const std::string &name, const T &value)
00522 {
00523 if (!hasAttribute(name))
00524 attributes[name] = EventAttributeBase::create(typeid(T));
00525 EventAttribute<T> *att = dynamic_cast<EventAttribute<T>*>(attributes[name]);
00526 if (att)
00527 att->set(value);
00528 return (att != NULL);
00529 };
00530
00531 public:
00533 double time;
00535 static Event null;
00536 private:
00538 AttributeMap attributes;
00539 };
00540
00547 OPENTRACKER_API std::istream& operator>>(std::istream &in, ot::Event &event);
00554 OPENTRACKER_API std::ostream& operator<<(std::ostream &out, const ot::Event &event);
00555
00556 }
00557
00558 #endif
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574