CKeyValuePair.cpp
Go to the documentation of this file.
00001 #include <designators/CKeyValuePair.h>
00002 
00003 
00004 CKeyValuePair::CKeyValuePair() {
00005   this->init();
00006 }
00007 
00008 CKeyValuePair::CKeyValuePair(string strKey) {
00009   this->init();
00010   this->setKey(strKey);
00011 }
00012 
00013 CKeyValuePair::CKeyValuePair(string strKey, string strValue) {
00014   this->init();
00015   this->setKey(strKey);
00016   this->setValue(strValue);
00017 }
00018 
00019 CKeyValuePair::CKeyValuePair(string strKey, float fValue) {
00020   this->init();
00021   this->setKey(strKey);
00022   this->setValue(fValue);
00023 }
00024 
00025 CKeyValuePair::CKeyValuePair(string strKey, char* acValue, unsigned int unLength) {
00026   this->init();
00027   this->setKey(strKey);
00028   this->setValue(acValue, unLength);
00029 }
00030 
00031 CKeyValuePair::CKeyValuePair(designator_integration_msgs::KeyValuePair kvpContent) {
00032   this->init();
00033   this->fillWithKeyValueContent(kvpContent);
00034 }
00035 
00036 CKeyValuePair::CKeyValuePair(string strKey, geometry_msgs::PoseStamped psPoseStampedValue) {
00037   this->init();
00038   this->setKey(strKey);
00039   this->setValue(psPoseStampedValue);
00040 }
00041 
00042 CKeyValuePair::CKeyValuePair(string strKey, geometry_msgs::Pose posPoseValue) {
00043   this->init();
00044   this->setKey(strKey);
00045   this->setValue(posPoseValue);
00046 }
00047 
00048 CKeyValuePair::CKeyValuePair(list<CKeyValuePair*> lstChildren) {
00049   this->init();
00050   this->setChildren(lstChildren);
00051 }
00052 
00053 CKeyValuePair::~CKeyValuePair() {
00054   this->clearDataValue();
00055 }
00056 
00057 void CKeyValuePair::init() {
00058   m_nParent = 0;
00059   m_nID = 0;
00060   m_strValue = "";
00061   m_fValue = 0.0;
00062   m_evtType = STRING;
00063   m_acValue = NULL;
00064   m_unValueLength = 0;
00065 }
00066 
00067 void CKeyValuePair::fillWithKeyValueContent(designator_integration_msgs::KeyValuePair kvpContent) {
00068   m_nID = kvpContent.id;
00069   m_nParent = kvpContent.parent;
00070   m_evtType = (enum ValueType)kvpContent.type;
00071   
00072   m_strKey = kvpContent.key;
00073   m_strValue = kvpContent.value_string;
00074   m_fValue = kvpContent.value_float;
00075   m_psPoseStampedValue = kvpContent.value_posestamped;
00076   m_posPoseValue = kvpContent.value_pose;
00077   
00078   m_unValueLength = kvpContent.value_data.size();
00079   if(m_unValueLength > 0) {
00080     m_acValue = new char[m_unValueLength]();
00081     for(int nI = 0; nI < m_unValueLength; nI++) {
00082       m_acValue[nI] = kvpContent.value_data[nI];
00083     }
00084   }
00085 }
00086 
00087 enum ValueType CKeyValuePair::type() {
00088   return m_evtType;
00089 }
00090 
00091 string CKeyValuePair::stringValue() {
00092   // NOTE: Strings and floats can be returned as strings. Everything
00093   // else turns into a blank string.
00094   switch(m_evtType) {
00095   case STRING: {
00096     return m_strValue;
00097   } break;
00098     
00099   case FLOAT: {
00100     stringstream sts;
00101     sts << m_fValue;
00102     return sts.str();
00103   } break;
00104     
00105   default: {
00106     return "";
00107   } break;
00108   }
00109 }
00110 
00111 float CKeyValuePair::floatValue() {
00112   // NOTE: Strings and floats can be returned as floats. Everything
00113   // else turns into the float value 0.0f. Strings that are returned
00114   // as floats must be valid number literals. If this is not the case,
00115   // an error message will be printed and the value 0.0f will be
00116   // returned.
00117   switch(m_evtType) {
00118   case STRING: {
00119     float fValue = 0.0f;
00120     if(sscanf(m_strValue.c_str(), "%f", &fValue) == EOF) {
00121       // Something went wrong.
00122       cerr << "Error while converting '" << m_strValue << "' to float." << endl;
00123       return 0.0f;
00124     } else {
00125       return fValue;
00126     }
00127   } break;
00128     
00129   case FLOAT: {
00130     return m_fValue;
00131   } break;
00132     
00133   default: {
00134     return 0.0f;
00135   } break;
00136   }
00137 }
00138 
00139 char* CKeyValuePair::dataValue() {
00140   return m_acValue;
00141 }
00142 
00143 unsigned int CKeyValuePair::dataValueLength() {
00144   return m_unValueLength;
00145 }
00146 
00147 char* CKeyValuePair::dataValue(unsigned int &unLength) {
00148   unLength = this->dataValueLength();
00149   
00150   return this->dataValue();
00151 }
00152 
00153 geometry_msgs::PoseStamped CKeyValuePair::poseStampedValue() {
00154   return m_psPoseStampedValue;
00155 }
00156 
00157 geometry_msgs::Pose CKeyValuePair::poseValue() {
00158   return m_posPoseValue;
00159 }
00160 
00161 int CKeyValuePair::id() {
00162   return m_nID;
00163 }
00164 
00165 int CKeyValuePair::parent() {
00166   return m_nParent;
00167 }
00168 
00169 string CKeyValuePair::key() {
00170   return m_strKey;
00171 }
00172 
00173 CKeyValuePair *CKeyValuePair::addChild(string strKey, bool bAppendNew) {
00174   CKeyValuePair *ckvpNewChild = NULL;
00175   
00176   if(!bAppendNew) {
00177     ckvpNewChild = this->childForKey(strKey);
00178   }
00179   
00180   if(!ckvpNewChild) {
00181     ckvpNewChild = new CKeyValuePair();
00182     ckvpNewChild->setKey(strKey);
00183     this->addChild(ckvpNewChild);
00184   }
00185   
00186   return ckvpNewChild;
00187 }
00188 
00189 void CKeyValuePair::addChild(CKeyValuePair *ckvpChildAdd) {
00190   m_lstChildren.push_back(ckvpChildAdd);
00191   this->setType(LIST);
00192 }
00193 
00194 list<CKeyValuePair*> CKeyValuePair::children() {
00195   return m_lstChildren;
00196 }
00197 
00198 void CKeyValuePair::setChildren(list<CKeyValuePair*> lstChildren) {
00199   m_lstChildren = lstChildren;
00200 }
00201 
00202 void CKeyValuePair::printPair(int nSpaceOffset, bool bOffsetRegular, bool bNewline) {
00203   cout << "(" << m_strKey << " ";
00204   
00205   switch(m_evtType) {
00206   case STRING: {
00207     cout << "\"" << m_strValue << "\")";
00208   } break;
00209     
00210   case FLOAT: {
00211     cout << m_fValue << ")";
00212   } break;
00213     
00214   case POSE: {
00215     cout << "[pose: [position: "
00216          << m_posPoseValue.position.x << ", "
00217          << m_posPoseValue.position.y << ", "
00218          << m_posPoseValue.position.z << "], "
00219          << "[orientation: "
00220          << m_posPoseValue.orientation.x << ", "
00221          << m_posPoseValue.orientation.y << ", "
00222          << m_posPoseValue.orientation.z << ", "
00223          << m_posPoseValue.orientation.w << "]])";
00224   } break;
00225     
00226   case POSESTAMPED: {
00227     cout << "[pose: "
00228          << "[stamp: " << m_psPoseStampedValue.header.stamp.toSec() << "], "
00229          << "[frame-id: " << m_psPoseStampedValue.header.frame_id << "], "
00230          << "[position: "
00231          << m_psPoseStampedValue.pose.position.x << ", "
00232          << m_psPoseStampedValue.pose.position.y << ", "
00233          << m_psPoseStampedValue.pose.position.z << "], "
00234          << "[orientation: "
00235          << m_psPoseStampedValue.pose.orientation.x << ", "
00236          << m_psPoseStampedValue.pose.orientation.y << ", "
00237          << m_psPoseStampedValue.pose.orientation.z << ", "
00238          << m_psPoseStampedValue.pose.orientation.w << "]])";
00239   } break;
00240     
00241   case DESIGNATOR_ACTION:
00242   case DESIGNATOR_OBJECT:
00243   case DESIGNATOR_LOCATION:
00244   case LIST: {
00245     string strPrefix;
00246     switch(m_evtType) {
00247     case DESIGNATOR_ACTION:
00248       strPrefix = "<action (";
00249       break;
00250     case DESIGNATOR_OBJECT:
00251       strPrefix = "<object (";
00252       break;
00253     case DESIGNATOR_LOCATION:
00254       strPrefix = "<location (";
00255       break;
00256     case LIST:
00257       strPrefix = "(";
00258       break;
00259     }
00260     cout << "(";
00261     bool bFirst = true;
00262     
00263     for(int nI = 0; nI < m_lstChildren.size(); nI++) {
00264       list<CKeyValuePair*>::iterator itChild = m_lstChildren.begin();
00265       advance(itChild, nI);
00266       CKeyValuePair *ckvpChild = *itChild;
00267       
00268       if(!bFirst) {
00269         this->printSpaces(nSpaceOffset + m_strKey.length() + 3);
00270       } else {
00271         bFirst = false;
00272       }
00273       
00274       ckvpChild->printPair(nSpaceOffset + m_strKey.length() + 3);
00275       
00276       if(nI < m_lstChildren.size() - 1) {
00277         cout << endl;
00278       }
00279     }
00280     
00281     cout << ")";
00282   } break;
00283     
00284   default:
00285     break;
00286   }
00287   
00288   // switch(m_evtType) {
00289   // case STRING: {
00290   //   if(bOffsetRegular) {
00291   //     this->printSpaces(nSpaceOffset);
00292   //   }
00293   //   cout << "(" << m_strKey << " " << m_strValue << ")";
00294   //   if(bNewline) {
00295   //     cout << endl;
00296   //   }
00297   // } break;
00298 
00299   // case FLOAT: {
00300   //   if(bOffsetRegular) {
00301   //     this->printSpaces(nSpaceOffset);
00302   //   }
00303   //   cout << "(" << m_strKey << " " << m_fValue << ")";
00304   //   if(bNewline) {
00305   //     cout << endl;
00306   //   }
00307   // } break;
00308 
00309   // case POSESTAMPED: {
00310   //   // print this
00311   // } break;
00312 
00313   // case DATA: {
00314   //   if(bOffsetRegular) {
00315   //     this->printSpaces(nSpaceOffset);
00316   //   }
00317   //   cout << "(" << m_strKey << " " << "[data])";
00318   //   if(bNewline) {
00319   //     cout << endl;
00320   //   }
00321   // } break;
00322 
00323   // case LIST: {
00324   //   this->printSpaces(nSpaceOffset + 1);
00325   //   cout << "(" << m_strKey << endl;
00326   //   this->printSpaces(nSpaceOffset + 2);
00327     
00328   //   for(list<CKeyValuePair*>::iterator itChild = m_lstChildren.begin();
00329   //    itChild != m_lstChildren.end();
00330   //    itChild++) {
00331   //     CKeyValuePair *kvpChild = *itChild;
00332   //     list<CKeyValuePair*>::iterator itNext = itChild++;
00333   //     bool bLast = (itNext == m_lstChildren.end());
00334   //     itChild--;
00335       
00336   //     bool bFirst = (itChild == m_lstChildren.begin());
00337       
00338   //     if(itChild == m_lstChildren.begin()) {
00339   //    cout << "(";
00340   //     }
00341       
00342   //     kvpChild->printPair(nSpaceOffset + 3, !bFirst, !bLast);
00343   //   }
00344     
00345   //   cout << ")" << endl;
00346   // } break;
00347   // }
00348 }
00349 
00350 void CKeyValuePair::printSpaces(int nSpaces) {
00351   for(int nI = 0; nI < nSpaces; nI++) {
00352     cout << " ";
00353   }
00354 }
00355 
00356 void CKeyValuePair::setID(int nID) {
00357   m_nID = nID;
00358 }
00359 
00360 void CKeyValuePair::setParent(int nParent) {
00361   m_nParent = nParent;
00362 }
00363 
00364 void CKeyValuePair::setValue(string strValue) {
00365   m_strValue = strValue;
00366   this->setType(STRING);
00367 }
00368 
00369 void CKeyValuePair::setValue(float fValue) {
00370   m_fValue = fValue;
00371   this->setType(FLOAT);
00372 }
00373 
00374 void CKeyValuePair::setValue(geometry_msgs::PoseStamped psPoseStampedValue) {
00375   m_psPoseStampedValue = psPoseStampedValue;
00376   this->setType(POSESTAMPED);
00377 }
00378 
00379 void CKeyValuePair::setValue(geometry_msgs::Pose posPoseValue) {
00380   m_posPoseValue = posPoseValue;
00381   this->setType(POSE);
00382 }
00383 
00384 void CKeyValuePair::clearDataValue() {
00385   if(m_acValue != NULL) {
00386     delete[] m_acValue;
00387     m_acValue = NULL;
00388     m_unValueLength = 0;
00389   }
00390 }
00391 
00392 void CKeyValuePair::setValue(void* vdValue, unsigned int unLength) {
00393   this->clearDataValue();
00394   
00395   m_acValue = new char[unLength]();
00396   memcpy(m_acValue, vdValue, unLength);
00397   
00398   this->setType(DATA);
00399 }
00400 
00401 void CKeyValuePair::setValue(string strKey, enum ValueType evtType, list<CKeyValuePair*> lstDescription) {
00402   CKeyValuePair *ckvpChild = this->childForKey(strKey);
00403   
00404   if(ckvpChild) {
00405     ckvpChild->setValue(evtType, lstDescription);
00406   } else {
00407     this->addChild(strKey, evtType, lstDescription);
00408   }
00409 }
00410 
00411 void CKeyValuePair::setValue(enum ValueType evtType, list<CKeyValuePair*> lstDescription) {
00412   m_lstChildren = lstDescription;
00413   this->setType(evtType);
00414 }
00415 
00416 CKeyValuePair* CKeyValuePair::addChild(string strKey, enum ValueType evtType, list<CKeyValuePair*> lstDescription) {
00417   CKeyValuePair *ckvpNewChild = this->addChild(strKey);
00418   ckvpNewChild->setValue(evtType, lstDescription);
00419   
00420   return ckvpNewChild;
00421 }
00422 
00423 void CKeyValuePair::setLocationDesignatorDescription(string strKey, enum ValueType evtType, list<CKeyValuePair*> lstDescription) {
00424   this->setValue(strKey, DESIGNATOR_LOCATION, lstDescription);
00425 }
00426 
00427 void CKeyValuePair::setActionDesignatorDescription(string strKey, list<CKeyValuePair*> lstDescription) {
00428   this->setValue(strKey, DESIGNATOR_ACTION, lstDescription);
00429 }
00430 
00431 void CKeyValuePair::setObjectDesignatorDescription(string strKey, list<CKeyValuePair*> lstDescription) {
00432   this->setValue(strKey, DESIGNATOR_OBJECT, lstDescription);
00433 }
00434 
00435 void CKeyValuePair::setKey(string strKey) {
00436   m_strKey = strKey;
00437 }
00438 
00439 void CKeyValuePair::setType(enum ValueType evtType) {
00440   m_evtType = evtType;
00441 }
00442 
00443 CKeyValuePair *CKeyValuePair::addChild(string strKey, string strValue) {
00444   CKeyValuePair *ckvpNewChild = this->addChild(strKey);
00445   ckvpNewChild->setValue(strValue);
00446   
00447   return ckvpNewChild;
00448 }
00449 
00450 CKeyValuePair *CKeyValuePair::addChild(string strKey, float fValue) {
00451   CKeyValuePair *ckvpNewChild = this->addChild(strKey);
00452   ckvpNewChild->setValue(fValue);
00453   
00454   return ckvpNewChild;
00455 }
00456 
00457 CKeyValuePair *CKeyValuePair::addChild(string strKey, geometry_msgs::PoseStamped psPoseStampedValue) {
00458   CKeyValuePair *ckvpNewChild = this->addChild(strKey);
00459   ckvpNewChild->setValue(psPoseStampedValue);
00460   
00461   return ckvpNewChild;
00462 }
00463 
00464 CKeyValuePair *CKeyValuePair::addChild(string strKey, geometry_msgs::Pose posPoseValue) {
00465   CKeyValuePair *ckvpNewChild = this->addChild(strKey);
00466   ckvpNewChild->setValue(posPoseValue);
00467   
00468   return ckvpNewChild;
00469 }
00470 
00471 CKeyValuePair *CKeyValuePair::addChild(string strKey, char *acValue, unsigned int unLength) {
00472   CKeyValuePair *ckvpNewChild = this->addChild(strKey);
00473   ckvpNewChild->setValue(acValue, unLength);
00474   
00475   return ckvpNewChild;
00476 }
00477 
00478 vector<designator_integration_msgs::KeyValuePair> CKeyValuePair::serializeToMessage(int nParent, int nID) {
00479   vector<designator_integration_msgs::KeyValuePair> vecReturn;
00480   designator_integration_msgs::KeyValuePair kvpSerialized;
00481   
00482   // Serialization header
00483   kvpSerialized.id = nID;
00484   kvpSerialized.parent = nParent;
00485   
00486   // Meta data
00487   kvpSerialized.key = m_strKey;
00488   kvpSerialized.type = (int)m_evtType;
00489   
00490   // Values
00491   kvpSerialized.value_string = m_strValue;
00492   kvpSerialized.value_float = m_fValue;
00493   kvpSerialized.value_posestamped = m_psPoseStampedValue;
00494   kvpSerialized.value_pose = m_posPoseValue;
00495   
00496   for(int nI = 0; nI < m_unValueLength; nI++) {
00497     kvpSerialized.value_data.push_back(m_acValue[nI]);
00498   }
00499   
00500   vecReturn.push_back(kvpSerialized);
00501   
00502   int nHighestID = nID;
00503   for(list<CKeyValuePair*>::iterator itChild = m_lstChildren.begin();
00504       itChild != m_lstChildren.end();
00505       itChild++) {
00506     vector<designator_integration_msgs::KeyValuePair> vecChildren = (*itChild)->serializeToMessage(nID, nHighestID + 1);
00507     
00508     for(vector<designator_integration_msgs::KeyValuePair>::iterator itMsg = vecChildren.begin();
00509         itMsg != vecChildren.end();
00510         itMsg++) {
00511       designator_integration_msgs::KeyValuePair kvpChild = *itMsg;
00512       
00513       if(kvpChild.id > nHighestID) {
00514         nHighestID = kvpChild.id;
00515       }
00516       
00517       vecReturn.push_back(kvpChild);
00518     }
00519   }
00520   
00521   return vecReturn;
00522 }
00523 
00524 CKeyValuePair* CKeyValuePair::childForKey(string strKey) {
00525   CKeyValuePair *ckvpReturn = NULL;
00526   
00527   for(list<CKeyValuePair*>::iterator itKVP = m_lstChildren.begin();
00528       itKVP != m_lstChildren.end();
00529       itKVP++) {
00530     CKeyValuePair *ckvpCurrent = *itKVP;
00531     
00532     if(strcasecmp(ckvpCurrent->key().c_str(), strKey.c_str()) == 0) {
00533       ckvpReturn = ckvpCurrent;
00534       break;
00535     }
00536   }
00537   
00538   return ckvpReturn;
00539 }
00540 
00541 bool CKeyValuePair::removeChildForKey(string strKey) {
00542   bool bResult = false;
00543   
00544   CKeyValuePair* ckvpChild = this->childForKey(strKey);
00545   if(ckvpChild) {
00546     m_lstChildren.remove(ckvpChild);
00547     delete ckvpChild;
00548   }
00549   
00550   return bResult;
00551 }
00552 
00553 string CKeyValuePair::stringValue(string strChildKey) {
00554   CKeyValuePair *ckvpChild = this->childForKey(strChildKey);
00555   
00556   if(ckvpChild) {
00557     return ckvpChild->stringValue();
00558   }
00559   
00560   return "";
00561 }
00562 
00563 float CKeyValuePair::floatValue(string strChildKey) {
00564   CKeyValuePair *ckvpChild = this->childForKey(strChildKey);
00565   
00566   if(ckvpChild) {
00567     return ckvpChild->floatValue();
00568   }
00569   
00570   return 0.0;
00571 }
00572 
00573 geometry_msgs::PoseStamped CKeyValuePair::poseStampedValue(string strChildKey) {
00574   CKeyValuePair *ckvpChild = this->childForKey(strChildKey);
00575   
00576   if(ckvpChild) {
00577     return ckvpChild->poseStampedValue();
00578   }
00579   
00580   geometry_msgs::PoseStamped psEmpty;
00581   return psEmpty;
00582 }
00583 
00584 geometry_msgs::Pose CKeyValuePair::poseValue(string strChildKey) {
00585   CKeyValuePair *ckvpChild = this->childForKey(strChildKey);
00586   
00587   if(ckvpChild) {
00588     return ckvpChild->poseValue();
00589   }
00590   
00591   geometry_msgs::Pose posEmpty;
00592   return posEmpty;
00593 }
00594 
00595 void CKeyValuePair::setValue(string strKey, string strValue) {
00596   CKeyValuePair *ckvpChild = this->childForKey(strKey);
00597   
00598   if(ckvpChild) {
00599     ckvpChild->setValue(strValue);
00600   } else {
00601     this->addChild(strKey, strValue);
00602   }
00603 }
00604 
00605 void CKeyValuePair::setValue(string strKey, float fValue) {
00606   CKeyValuePair *ckvpChild = this->childForKey(strKey);
00607   
00608   if(ckvpChild) {
00609     ckvpChild->setValue(fValue);
00610   } else {
00611     this->addChild(strKey, fValue);
00612   }
00613 }
00614 
00615 void CKeyValuePair::setValue(string strKey, geometry_msgs::PoseStamped psPoseStampedValue) {
00616   CKeyValuePair *ckvpChild = this->childForKey(strKey);
00617   
00618   if(ckvpChild) {
00619     ckvpChild->setValue(psPoseStampedValue);
00620   } else {
00621     this->addChild(strKey, psPoseStampedValue);
00622   }
00623 }
00624 
00625 void CKeyValuePair::setValue(string strKey, geometry_msgs::Pose posPoseValue) {
00626   CKeyValuePair *ckvpChild = this->childForKey(strKey);
00627   
00628   if(ckvpChild) {
00629     ckvpChild->setValue(posPoseValue);
00630   } else {
00631     this->addChild(strKey, posPoseValue);
00632   }
00633 }
00634 
00635 void CKeyValuePair::clear() {
00636   m_lstChildren.clear();
00637 }
00638 
00639 CKeyValuePair* CKeyValuePair::copy() {
00640   CKeyValuePair *ckvpCopy = new CKeyValuePair();
00641   
00642   ckvpCopy->setValue(this->stringValue());
00643   ckvpCopy->setValue(this->floatValue());
00644   ckvpCopy->setValue(this->poseStampedValue());
00645   ckvpCopy->setValue(this->poseValue());
00646   ckvpCopy->setValue(this->dataValue(), this->dataValueLength());
00647   ckvpCopy->setType(this->type());
00648   
00649   ckvpCopy->setKey(this->key());
00650   ckvpCopy->setParent(this->parent());
00651   ckvpCopy->setID(this->id());
00652   
00653   for(list<CKeyValuePair*>::iterator itChild = m_lstChildren.begin();
00654       itChild != m_lstChildren.end();
00655       itChild++) {
00656     ckvpCopy->addChild((*itChild)->copy());
00657   }
00658   
00659   return ckvpCopy;
00660 }
00661 
00662 list<string> CKeyValuePair::keys() {
00663   list<string> lstKeys;
00664   list<CKeyValuePair*> lstChildren = this->children();
00665   
00666   for(list<CKeyValuePair*>::iterator itPair = lstChildren.begin();
00667       itPair != lstChildren.end();
00668       itPair++) {
00669     lstKeys.push_back((*itPair)->key());
00670   }
00671   
00672   return lstKeys;
00673 }


designator_integration
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 23:26:09