denso_base.cpp
Go to the documentation of this file.
00001 
00025 #include <stdlib.h>
00026 #include "denso_robot_core/denso_base.h"
00027 #include "denso_robot_core/denso_variable.h"
00028 
00029 #define BCAP_GET_OBJECT_ARGS      (3)
00030 #define BCAP_GET_OBJECTNAMES_ARGS (2)
00031 
00032 namespace denso_robot_core {
00033 
00034 std::string DensoBase::ConvertBSTRToString(const BSTR bstr)
00035 {
00036   std::string strRet;
00037   char *chTmp;
00038 
00039   chTmp = ConvertWideChar2MultiByte(bstr);
00040   if(chTmp != NULL) {
00041     strRet = chTmp;
00042     free(chTmp);
00043   }
00044 
00045   return strRet;
00046 }
00047 
00048 BSTR DensoBase::ConvertStringToBSTR(const std::string& str)
00049 {
00050   BSTR strRet = NULL;
00051   wchar_t *chTmp;
00052 
00053   chTmp = ConvertMultiByte2WideChar(str.c_str());
00054   if(chTmp != NULL) {
00055     strRet = SysAllocString(chTmp);
00056     free(chTmp);
00057   }
00058 
00059   return strRet;
00060 }
00061 
00062 std::string DensoBase::RosName() const
00063 {
00064   std::string tmpName = m_name;
00065 
00066   // Replace space to _
00067   std::replace(tmpName.begin(), tmpName.end(), ' ', '_');
00068 
00069   // Replace backslash to /
00070   std::replace(tmpName.begin(), tmpName.end(), '\\', '/');
00071 
00072   // Erase @
00073   size_t c;
00074   while((c = tmpName.find_first_of("@")) != std::string::npos) {
00075     tmpName.erase(c,1);
00076   }
00077 
00078   // Erase *
00079   while((c = tmpName.find_first_of("*")) != std::string::npos) {
00080     tmpName.erase(c,1);
00081   }
00082 
00083   return tmpName;
00084 }
00085 
00086 HRESULT DensoBase::AddVariable(int32_t get_id,
00087     const std::string& name,
00088     DensoVariable_Vec& vecVar,
00089     int16_t vt, bool bRead, bool bWrite, bool bID, int iDuration)
00090 {
00091   DensoBase_Vec vecBase;
00092   vecBase.insert(vecBase.end(), vecVar.begin(), vecVar.end());
00093 
00094   if(E_HANDLE == get_Object(vecBase, name, NULL)) {
00095     Handle_Vec vecHandle;
00096     HRESULT hr = AddObject(get_id, name, vecHandle);
00097     if(FAILED(hr)) return hr;
00098 
00099     DensoVariable_Ptr var(new DensoVariable(this,
00100         m_vecService, vecHandle, name, m_mode,
00101         vt, bRead, bWrite, bID, iDuration));
00102 
00103     vecVar.push_back(var);
00104   }
00105 
00106   return S_OK;
00107 }
00108 
00109 HRESULT DensoBase::AddVariable(int32_t get_id,
00110     const XMLElement *xmlVar,
00111     DensoVariable_Vec& vecVar)
00112 {
00113   const char *chTmp;
00114 
00115   std::string name;
00116   int16_t vt = VT_EMPTY;
00117   bool bRead = false, bWrite = false, bID = false;
00118   int  iDuration = BCAP_VAR_DEFAULT_DURATION;
00119 
00120   name = xmlVar->GetText();
00121 
00122   chTmp = xmlVar->Attribute(XML_ATTR_VARTYPE);
00123   if(chTmp != NULL) vt = atoi(chTmp);
00124 
00125   chTmp = xmlVar->Attribute(XML_ATTR_READ);
00126   if(chTmp != NULL) bRead = (strcasecmp(chTmp, "true") == 0);
00127 
00128   chTmp = xmlVar->Attribute(XML_ATTR_WRITE);
00129   if(chTmp != NULL) bWrite = (strcasecmp(chTmp, "true") == 0);
00130 
00131   chTmp = xmlVar->Attribute(XML_ATTR_ID);
00132   if(chTmp != NULL) bID = (strcasecmp(chTmp, "true") == 0);
00133 
00134   chTmp = xmlVar->Attribute(XML_ATTR_DURATION);
00135   if(chTmp != NULL) iDuration = atoi(chTmp);
00136 
00137   Handle_Vec vecHandle;
00138   HRESULT hr = AddObject(get_id, name, vecHandle);
00139   if(FAILED(hr)) return hr;
00140 
00141   DensoVariable_Ptr var(new DensoVariable(this,
00142       m_vecService, vecHandle, name, m_mode,
00143       vt, bRead, bWrite, bID, iDuration));
00144 
00145   vecVar.push_back(var);
00146 
00147   return S_OK;
00148 }
00149 
00150 HRESULT DensoBase::AddObject(
00151     int32_t get_id, const std::string& name,
00152     Handle_Vec& vecHandle)
00153 {
00154   int srvs, argc;
00155   HRESULT hr;
00156 
00157   for(srvs = SRV_MIN; srvs <= SRV_MAX; srvs++) {
00158     VARIANT_Ptr vntRet(new VARIANT());
00159     VARIANT_Vec vntArgs;
00160 
00161     VariantInit(vntRet.get());
00162 
00163     for(argc = 0; argc < BCAP_GET_OBJECT_ARGS; argc++) {
00164       VARIANT_Ptr vntTmp(new VARIANT());
00165       VariantInit(vntTmp.get());
00166 
00167       switch(argc) {
00168         case 0:
00169           vntTmp->vt = VT_UI4;
00170           vntTmp->ulVal = m_vecHandle[srvs];
00171           break;
00172         case 1:
00173           vntTmp->vt = VT_BSTR;
00174           vntTmp->bstrVal = ConvertStringToBSTR(name);
00175           break;
00176         case 2:
00177           vntTmp->vt = VT_BSTR;
00178           vntTmp->bstrVal = SysAllocString(L"");
00179           break;
00180       }
00181 
00182       vntArgs.push_back(*vntTmp.get());
00183     }
00184 
00185     hr = m_vecService[srvs]->ExecFunction(get_id, vntArgs, vntRet);
00186     if(FAILED(hr)) break;
00187 
00188     vecHandle.push_back(vntRet->ulVal);
00189   }
00190 
00191   return hr;
00192 }
00193 
00194 HRESULT DensoBase::GetObjectNames(int32_t func_id, Name_Vec& vecName)
00195 {
00196   uint32_t argc, i, num;
00197   HRESULT hr;
00198   VARIANT_Ptr vntRet(new VARIANT());
00199   VARIANT_Vec vntArgs;
00200 
00201   VariantInit(vntRet.get());
00202 
00203   for(argc = 0; argc < BCAP_GET_OBJECTNAMES_ARGS; argc++) {
00204     VARIANT_Ptr vntTmp(new VARIANT());
00205     VariantInit(vntTmp.get());
00206 
00207     if(argc == 0) {
00208       vntTmp->vt = VT_UI4;
00209       vntTmp->ulVal = m_vecHandle[SRV_MIN];
00210     } else {
00211       vntTmp->vt = VT_BSTR;
00212       vntTmp->bstrVal = SysAllocString(L"");
00213     }
00214 
00215     vntArgs.push_back(*vntTmp.get());
00216   }
00217 
00218   hr = m_vecService[SRV_MIN]->ExecFunction(func_id, vntArgs, vntRet);
00219   if(SUCCEEDED(hr)) {
00220     BSTR    *pbstr;
00221     VARIANT *pvnt;
00222     switch(vntRet->vt) {
00223       case (VT_ARRAY | VT_BSTR):
00224         num = vntRet->parray->rgsabound->cElements;
00225         SafeArrayAccessData(vntRet->parray, (void**)&pbstr);
00226         for(i = 0; i < num; i++) {
00227           vecName.push_back(ConvertBSTRToString(pbstr[i]));
00228         }
00229         SafeArrayUnaccessData(vntRet->parray);
00230         break;
00231       case (VT_ARRAY | VT_VARIANT):
00232         num = vntRet->parray->rgsabound->cElements;
00233         SafeArrayAccessData(vntRet->parray, (void**)&pvnt);
00234         for(i = 0; i < num; i++){
00235           if(pvnt[i].vt != VT_BSTR) {
00236             hr = E_FAIL;
00237             break;
00238           }
00239           vecName.push_back(ConvertBSTRToString(pvnt[i].bstrVal));
00240         }
00241         SafeArrayUnaccessData(vntRet->parray);
00242         break;
00243       default:
00244         hr = S_FALSE;
00245         break;
00246     }
00247   }
00248 
00249   return hr;
00250 }
00251 
00252 HRESULT DensoBase::get_Object(const DensoBase_Vec& vecBase,
00253     int index, DensoBase_Ptr *obj)
00254 {
00255   try {
00256     if(obj != NULL) {
00257       *obj = vecBase.at(index);
00258     }
00259   } catch (std::out_of_range&) {
00260     return E_HANDLE;
00261   }
00262 
00263   return S_OK;
00264 }
00265 
00266 HRESULT DensoBase::get_Object(const DensoBase_Vec& vecBase,
00267     const std::string& name, DensoBase_Ptr *obj)
00268 {
00269   DensoBase_Vec::const_iterator it;
00270   for(it = vecBase.begin(); it != vecBase.end(); it++) {
00271     if(!strcasecmp((*it)->Name().c_str(), name.c_str())) {
00272       if(obj != NULL) {
00273         *obj = *it;
00274       }
00275       return S_OK;
00276     }
00277   }
00278 
00279   return E_HANDLE;
00280 }
00281 
00282 }


denso_robot_core
Author(s): DENSO WAVE INCORPORATED
autogenerated on Thu Jun 6 2019 21:00:11