Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Elem.h"
00026 #include "XMLTag.h"
00027 #include "Signature.h"
00028 #include "Object.h"
00029 #include "Descriptor.h"
00030 #include "Class.h"
00031
00032 #include <time.h>
00033
00034
00035 #include <pluginlib/class_loader.h>
00036
00037 using namespace cop;
00038
00039
00040 ObjectID_t Elem::m_LastID = 0;
00041
00042
00043
00044
00045
00046 Elem::Elem ( ) :
00047 m_ID(m_LastID++),
00048 m_timestamp((unsigned long)time(NULL)),
00049 m_creator(0)
00050 {
00051 }
00052
00053 Elem::Elem ( ObjectID_t id ) :
00054 m_ID(id),
00055 m_timestamp((unsigned long)time(NULL)),
00056 m_creator(0)
00057 {
00058 if(m_LastID < m_ID && m_ID < FORBIDDEN_ID_RANGE_MIN)
00059 m_LastID = m_ID + 1;
00060 }
00061
00062 void Elem::SetData ( XMLTag* creator)
00063 {
00064 ElemWriteLock lk(m_mutexElems);
00065 if(creator != NULL)
00066 {
00067 m_ID = creator->GetPropertyInt(XML_PROPERTY_ELEMID);
00068 m_creator = creator->GetPropertyInt(XML_PROPERTY_PPID, 0);
00069 if(m_LastID < m_ID && m_ID < FORBIDDEN_ID_RANGE_MIN)
00070 m_LastID = m_ID + 1;
00071
00072 m_timestamp = creator->date();
00073 m_timestamp -= 2;
00074 }
00075 m_timestamp = ((unsigned long)time(NULL));
00076 }
00077
00078 bool StringEquals(std::string name1, std::string name2)
00079 {
00080 return name1.compare(name2) == 0;
00081 }
00082
00083 pluginlib::ClassLoader<Descriptor> s_descr_loader("cognitive_perception", "Descriptor");
00084
00085 Elem* Elem::ElemFactory ( XMLTag* tag)
00086 {
00087 if(tag == NULL)
00088 throw "WRONG NODE";
00089 std::string name = tag->GetName();
00090 Elem* elem = NULL;
00091 if(StringEquals(name, XML_NODE_CLASS))
00092 {
00093 elem = new Class();
00094 elem->SetData(tag);
00095 }
00096 else if(StringEquals(name, XML_NODE_DESCRIPTOR))
00097 {
00098 elem = new Descriptor();
00099 elem->SetData(tag);
00100 }
00101 else if(StringEquals(name, XML_NODE_ELEMENT))
00102 {
00103 elem = new Elem();
00104 elem->SetData(tag);
00105 }
00106 else if(StringEquals(name, XML_NODE_OBJECT))
00107 {
00108 elem = new Object();
00109 elem->SetData(tag);
00110 }
00111 else if(StringEquals(name, XML_NODE_SIGNATURE))
00112 {
00113 elem = new Signature();
00114 elem->SetData(tag);
00115 }
00116 else
00117 {
00118
00119
00120 try
00121 {
00122 elem = s_descr_loader.createClassInstance(name);
00123 elem->SetData(tag);
00124 }
00125 catch(pluginlib::PluginlibException& ex)
00126 {
00127
00128 ROS_WARN("The plugin failed to load for some reason. Error: %s\n", ex.what());
00129 ROS_WARN("Tag failed: %s\n", tag->GetName().c_str());
00130 }
00131 }
00132 return elem;
00133 }
00134
00135 Elem* Elem::Duplicate(bool bStaticCopy)
00136 {
00137 XMLTag* tag = this->Save();
00138 if(!bStaticCopy)
00139 {
00140 tag->AddProperty(XML_PROPERTY_ELEMID, m_LastID++);
00141 }
00142 Elem* copy = Elem::ElemFactory(tag);
00143 delete tag;
00144 return copy;
00145 }
00146
00147 void Elem::Touch()
00148 {
00149 m_timestamp = (unsigned long)time(NULL);
00150 }
00151
00152
00153
00154 Elem::~Elem ( ) { }
00155
00156
00157
00158
00159 XMLTag* Elem::Save(bool full_pose)
00160 {
00161 ElemWriteLock lk(m_mutexElems);
00162 m_fullPose = full_pose;
00163 XMLTag* ret = new XMLTag(GetNodeName());
00164 ret->AddProperty(XML_PROPERTY_ELEMID, m_ID);
00165 ret->AddProperty(XML_PROPERTY_PPID, m_creator);
00166 SaveTo(ret);
00167 return ret;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177