OpenTracker

An Open Architecture for Reconfigurable Tracking based on XML | Contact

Event.cxx

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 #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     // typesafe null reference
00056     Event Event::null;
00057 
00058     // standard constructor
00059     Event::Event()
00060     {
00061         timeStamp();
00062     }
00063 
00064     // copy constructor, copying all attributes
00065     Event::Event(const Event &rv)
00066     {
00067         *this = rv;
00068     }
00069 
00070     // destructor, deleting all attributes
00071     Event::~Event()
00072     {
00073         clearAttributes();
00074     }
00075 
00076     // assignment operator, copying all attributes
00077     Event& Event::operator=(const Event& rv) //throw (std::runtime_error)
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     // copy all but standard data fields (used for transformation nodes)
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     // get name of EventAttributeBase by index
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     // get index of EventAttributeBase by name
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     // get attribute type
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     // get generic attribute type name
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     // get attribute value encoded in a string
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     // get position
00184     std::vector<float>& Event::getPosition()
00185     {
00186         return getAttribute("position", std::vector<float>(3, .0f));
00187     };
00188 
00189     // get orientation
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     // get confidence
00198     float& Event::getConfidence()
00199     {
00200         return getAttribute("confidence", 1.0f);
00201     }
00202 
00203     // get button
00204     unsigned short& Event::getButton()
00205     {
00206         return getAttribute(std::string("button"), (unsigned short)0);
00207     }
00208 
00209     // serialize the event
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     // deserialize the event
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         // read "{", time , "-", size, ":" and data string
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         // read data
00260         for (i = 0; i < mapSize; i++)
00261         {
00262             // segmentation of data string
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             // check if segmentation is correct
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     // add attribute using strings encoding type and value
00305     bool Event::addAttribute(const std::string &type, const std::string &name, const std::string &value) //throw (std::runtime_error)
00306     {
00307         if (hasAttribute(name))
00308             return false;
00309 
00310         EventAttributeBase *att = EventAttributeBase::create(type); // may throw std::runtime_error
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     // set attribute using a string encoding the value
00325     bool Event::setAttribute(const std::string &type, const std::string &name, const std::string &value) //throw (std::runtime_error)
00326     {
00327         if (!hasAttribute(name))
00328             attributes[name] = EventAttributeBase::create(type); // may throw std::runtime_error
00329         EventAttributeBase *att = attributes[name];
00330         std::stringstream ss(value);
00331         if (ss >> *att)
00332             return true;
00333         return false;
00334     }
00335 
00336     // delete attribute by name
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     // delete all attributes
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     // print out all attributes
00360     void Event::printout() const
00361     {
00362     LOG_ACE_INFO(getPrintOut().c_str());
00363     LOG_ACE_INFO("\n");
00364         //std::cout << getPrintOut() << std::endl;
00365     }
00366 
00367     // get string for print out
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     // rename attribute
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     // check for attribute
00397     bool Event::hasAttribute(const std::string &name) const
00398     {
00399         return (attributes.find(name) != attributes.end());
00400     }
00401 
00402     // check whether type name was registered
00403     bool Event::knowsType(const std::string typeName)
00404     {
00405         return EventAttributeBase::translator.knowsType(typeName);
00406     }
00407 
00408     // register all known types
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     // timestamp the event to current time
00429     void Event::timeStamp()
00430     {
00431         time = OSUtils::currentTime();
00432     }
00433 
00434     // stream the event to an input stream
00435     std::istream& operator>>(std::istream &in, ot::Event &event)
00436     {
00437         event.deserialize(in);
00438         return in;
00439     };
00440 
00441     // stream the event to an output stream
00442     std::ostream& operator<<(std::ostream &out, const ot::Event &event)
00443     {
00444         event.serialize(out);
00445         return out;
00446     };
00447 
00448 } // namespace ot
00449 
00450 /* 
00451  * ------------------------------------------------------------
00452  *   End of Event.cxx
00453  * ------------------------------------------------------------
00454  *   Automatic Emacs configuration follows.
00455  *   Local Variables:
00456  *   mode:c++
00457  *   c-basic-offset: 4
00458  *   eval: (c-set-offset 'substatement-open 0)
00459  *   eval: (c-set-offset 'case-label '+)
00460  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00461  *   eval: (setq indent-tabs-mode nil)
00462  *   End:
00463  * ------------------------------------------------------------ 
00464  */

copyright (c) 2006 Graz University of Technology