Event.h

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
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]);  // do not use operator[] here, it will not work, since no memory will be allocated!
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]);  // do not use operator[] here, it will not work, since no memory will be allocated!!
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); //throw (std::runtime_error);
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); //throw (std::runtime_error);
00381         bool setAttribute(const std::string &type, const std::string &name, const std::string &value); //throw (std::runtime_error);
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) //throw (std::invalid_argument)
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) //throw (std::invalid_argument, std::runtime_error)
00484         {
00485             try
00486             {
00487                 return getAttribute((T*)NULL, name);
00488             }
00489             catch (std::invalid_argument)
00490             {
00491                 addAttribute(name, defValue); // may throw std::runtime_error
00492                 return getAttribute((T*)NULL, name); // may throw std::invalid_argument
00493             }
00494         };
00501         template <typename T>
00502             bool addAttribute(const std::string &name, const T &value) //throw (std::runtime_error)
00503         {
00504             if (hasAttribute(name))
00505                 return false;
00506             else
00507             {
00508                 EventAttributeBase *att = EventAttributeBase::create(typeid(T)); // may throw std::runtime_error
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) //throw (std::runtime_error)
00522         {
00523             if (!hasAttribute(name))
00524                 attributes[name] = EventAttributeBase::create(typeid(T)); // may throw std::runtime_error
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 } // namespace ot
00557 
00558 #endif
00559 
00560 /*
00561  * ------------------------------------------------------------
00562  *   End of Event.h
00563  * ------------------------------------------------------------
00564  *   Automatic Emacs configuration follows.
00565  *   Local Variables:
00566  *   mode:c++
00567  *   c-basic-offset: 4
00568  *   eval: (c-set-offset 'substatement-open 0)
00569  *   eval: (c-set-offset 'case-label '+)
00570  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00571  *   eval: (setq indent-tabs-mode nil)
00572  *   End:
00573  * ------------------------------------------------------------
00574  */

Generated on Wed Feb 28 15:18:48 2007 for NaviTrack by  doxygen 1.4.6