OpenTracker

An Open Architecture for Reconfigurable Tracking based on XML | Contact

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 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]);  // do not use operator[] here, it will not work, since no memory will be allocated!
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]);  // do not use operator[] here, it will not work, since no memory will be allocated!!
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); //throw (std::runtime_error);
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); //throw (std::runtime_error);
00362         bool setAttribute(const std::string &type, const std::string &name, const std::string &value); //throw (std::runtime_error);
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) //throw (std::invalid_argument)
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) //throw (std::invalid_argument, std::runtime_error)
00454         {
00455             try
00456             {
00457                 return getAttribute((T*)NULL, name);
00458             }
00459             catch (std::invalid_argument)
00460             {
00461                 addAttribute(name, defValue); // may throw std::runtime_error
00462                 return getAttribute((T*)NULL, name); // may throw std::invalid_argument
00463             }
00464         };
00471         template <typename T>
00472             bool addAttribute(const std::string &name, const T &value) //throw (std::runtime_error)
00473         {
00474             if (hasAttribute(name))
00475                 return false;
00476             else
00477             {
00478                 EventAttributeBase *att = EventAttributeBase::create(typeid(T)); // may throw std::runtime_error
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) //throw (std::runtime_error)
00492         {
00493             if (!hasAttribute(name))
00494                 attributes[name] = EventAttributeBase::create(typeid(T)); // may throw std::runtime_error
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 } // namespace ot
00527 
00528 #endif
00529 
00530 /*
00531  * ------------------------------------------------------------
00532  *   End of Event.h
00533  * ------------------------------------------------------------
00534  *   Automatic Emacs configuration follows.
00535  *   Local Variables:
00536  *   mode:c++
00537  *   c-basic-offset: 4
00538  *   eval: (c-set-offset 'substatement-open 0)
00539  *   eval: (c-set-offset 'case-label '+)
00540  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00541  *   eval: (setq indent-tabs-mode nil)
00542  *   End:
00543  * ------------------------------------------------------------
00544  */

copyright (c) 2006 Graz University of Technology