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 using namespace std;
00007
00008 const EventBase::classTypeID_t TimerEvent::autoRegisterTimerEvent=getTypeRegistry().registerType<TimerEvent>(makeClassTypeID("TIMR"));
00009
00010 std::string
00011 TimerEvent::getDescription(bool showTypeSpecific, unsigned int verbosity) const {
00012 if(!showTypeSpecific)
00013 return EventBase::getDescription(showTypeSpecific,verbosity);
00014 std::ostringstream logdata;
00015 logdata << EventBase::getDescription(showTypeSpecific,verbosity) << '\t';
00016 if(BehaviorBase * beh=dynamic_cast<BehaviorBase*>(target)) {
00017 logdata << beh->getClassName() << "(" << beh->getName() << ")@" << beh;
00018 } else {
00019 logdata << "Listener@" << target << endl;
00020 }
00021 return logdata.str();
00022 }
00023
00024 unsigned int
00025 TimerEvent::getBinSize() const {
00026 unsigned int used=EventBase::getBinSize();
00027 if(saveFormat==XML)
00028 return used;
00029
00030 used+=creatorSize("EventBase::TimerEvent");
00031 used+=getSerializedSize(target);
00032 return used;
00033 }
00034
00035 unsigned int
00036 TimerEvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00037 unsigned int origlen=len;
00038 if(!checkInc(EventBase::loadBinaryBuffer(buf,len),buf,len)) return 0;
00039 if(!checkCreatorInc("EventBase::TimerEvent",buf,len,true)) return 0;
00040 unsigned long long tgt;
00041 if(!decodeInc(tgt,buf,len)) return 0;
00042 target=reinterpret_cast<EventListener*>(tgt);
00043 return origlen-len;
00044 }
00045
00046 unsigned int
00047 TimerEvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00048 unsigned int origlen=len;
00049 if(!checkInc(EventBase::saveBinaryBuffer(buf,len),buf,len)) return 0;
00050 if(!saveCreatorInc("EventBase::TimerEvent",buf,len)) return 0;
00051 if(!encodeInc(reinterpret_cast<unsigned long long>(target),buf,len)) return 0;
00052 return origlen-len;
00053 }
00054
00055 void TimerEvent::loadXML(xmlNode* node) {
00056 if(node==NULL)
00057 return;
00058
00059 EventBase::loadXML(node);
00060
00061 for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00062 if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00063 continue;
00064
00065 xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00066 if(name==NULL)
00067 throw bad_format(cur,"property missing name");
00068
00069 xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00070 if(val==NULL)
00071 throw bad_format(cur,"property missing value");
00072
00073 if(xmlStrcmp(name, (const xmlChar *)"target")==0)
00074 target=reinterpret_cast<EventListener*>(strtol((char*)val,NULL,0));
00075
00076 xmlFree(val);
00077 xmlFree(name);
00078 }
00079 }
00080
00081 void TimerEvent::saveXML(xmlNode * node) const {
00082 if(node==NULL)
00083 return;
00084 EventBase::saveXML(node);
00085
00086
00087 for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00088 if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00089 xmlUnlinkNode(cur);
00090 xmlFreeNode(cur);
00091 cur = skipToElement(node->children);
00092 } else
00093 cur = skipToElement(cur->next);
00094 }
00095
00096 xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL);
00097 if(cur==NULL)
00098 throw bad_format(node,"Error: VisionObjectEvent xml error on saving param");
00099 xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)"target");
00100 char tmp[20];
00101 snprintf(tmp,20,"%p",target);
00102 xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)tmp);
00103 }
00104
00105
00106
00107
00108
00109