Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

TimerEvent.cc

Go to the documentation of this file.
00001 #include "TimerEvent.h"
00002 #include "Behaviors/BehaviorBase.h"
00003 #include <sstream>
00004 #include <libxml/tree.h>
00005 
00006 //better to put this here instead of the header
00007 using namespace std; 
00008 
00009 std::string
00010 TimerEvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00011   if(!showTypeSpecific)
00012     return EventBase::getDescription(showTypeSpecific,verbosity);
00013   std::ostringstream logdata;
00014   logdata << EventBase::getDescription(showTypeSpecific,verbosity) << '\t';
00015   if(BehaviorBase * beh=dynamic_cast<BehaviorBase*>(target)) {
00016     logdata << beh->getClassName() << "(" << beh->getName() << ")@" << beh;
00017   } else {
00018     logdata << "Listener@" << target << endl;
00019   }
00020   return logdata.str();
00021 }
00022 
00023 unsigned int
00024 TimerEvent::getBinSize() const {
00025   unsigned int used=EventBase::getBinSize();
00026   if(saveFormat==XML)
00027     return used; //if using XML, the XMLLoadSave::getBinSize (called by EventBase::getBinSize) is all we need
00028   //otherwise need to add our own fields
00029   used+=creatorSize("EventBase::TimerEvent");
00030   used+=getSerializedSize(target); // not that a pointer is of any direct use once loaded externally, but still useful as an identifier
00031   return used;
00032 }
00033 
00034 unsigned int
00035 TimerEvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00036   unsigned int origlen=len;
00037   if(!checkInc(EventBase::loadBinaryBuffer(buf,len),buf,len)) return 0;
00038   if(!checkCreatorInc("EventBase::TimerEvent",buf,len,true)) return 0;
00039   unsigned long long tgt;
00040   if(!decodeInc(tgt,buf,len)) return 0;
00041   target=reinterpret_cast<EventListener*>(tgt);
00042   return origlen-len; 
00043 }
00044 
00045 unsigned int
00046 TimerEvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00047   unsigned int origlen=len;
00048   if(!checkInc(EventBase::saveBinaryBuffer(buf,len),buf,len)) return 0;
00049   if(!saveCreatorInc("EventBase::TimerEvent",buf,len)) return 0;
00050   if(!encodeInc(reinterpret_cast<unsigned long long>(target),buf,len)) return 0;
00051   return origlen-len;
00052 }
00053 
00054 void TimerEvent::loadXML(xmlNode* node) {
00055   if(node==NULL)
00056     return;
00057   
00058   EventBase::loadXML(node);
00059   
00060   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00061     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00062       continue;
00063     
00064     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00065     if(name==NULL)
00066       throw bad_format(cur,"property missing name");
00067     
00068     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00069     if(val==NULL)
00070       throw bad_format(cur,"property missing value");
00071     
00072     if(xmlStrcmp(name, (const xmlChar *)"target")==0)
00073       target=reinterpret_cast<EventListener*>(strtol((char*)val,NULL,0));
00074     
00075     xmlFree(val);
00076     xmlFree(name);
00077   }
00078 }
00079 
00080 void TimerEvent::saveXML(xmlNode * node) const {
00081   if(node==NULL)
00082     return;
00083   EventBase::saveXML(node);
00084   
00085   //clear old params first
00086   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00087     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00088       xmlUnlinkNode(cur);
00089       xmlFreeNode(cur);
00090       cur = skipToElement(node->children); //restart the search (boo)
00091     } else
00092       cur = skipToElement(cur->next);
00093   }
00094   
00095   xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL);
00096   if(cur==NULL)
00097     throw bad_format(node,"Error: VisionObjectEvent xml error on saving param");
00098   xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)"target");
00099   char tmp[20];
00100   snprintf(tmp,20,"%p",target);
00101   xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)tmp);
00102 }
00103 
00104 
00105 /*! @file
00106  * @brief 
00107  * @author Ethan Tira-Thompson (ejt) (Creator)
00108  *
00109  * $Author: ejt $
00110  * $Name: tekkotsu-3_0 $
00111  * $Revision: 1.2 $
00112  * $State: Exp $
00113  * $Date: 2006/09/09 04:32:49 $
00114  */

Tekkotsu v3.0
Generated Wed Oct 4 00:03:46 2006 by Doxygen 1.4.7