denso_base.cpp
Go to the documentation of this file.
1 
25 #include <stdlib.h>
28 
29 #define BCAP_GET_OBJECT_ARGS (3)
30 #define BCAP_GET_OBJECTNAMES_ARGS (2)
31 
32 namespace denso_robot_core {
33 
34 std::string DensoBase::ConvertBSTRToString(const BSTR bstr)
35 {
36  std::string strRet;
37  char *chTmp;
38 
39  chTmp = ConvertWideChar2MultiByte(bstr);
40  if(chTmp != NULL) {
41  strRet = chTmp;
42  free(chTmp);
43  }
44 
45  return strRet;
46 }
47 
48 BSTR DensoBase::ConvertStringToBSTR(const std::string& str)
49 {
50  BSTR strRet = NULL;
51  wchar_t *chTmp;
52 
53  chTmp = ConvertMultiByte2WideChar(str.c_str());
54  if(chTmp != NULL) {
55  strRet = SysAllocString(chTmp);
56  free(chTmp);
57  }
58 
59  return strRet;
60 }
61 
62 std::string DensoBase::RosName() const
63 {
64  std::string tmpName = m_name;
65 
66  // Replace space to _
67  std::replace(tmpName.begin(), tmpName.end(), ' ', '_');
68 
69  // Replace backslash to /
70  std::replace(tmpName.begin(), tmpName.end(), '\\', '/');
71 
72  // Erase @
73  size_t c;
74  while((c = tmpName.find_first_of("@")) != std::string::npos) {
75  tmpName.erase(c,1);
76  }
77 
78  // Erase *
79  while((c = tmpName.find_first_of("*")) != std::string::npos) {
80  tmpName.erase(c,1);
81  }
82 
83  return tmpName;
84 }
85 
87  const std::string& name,
88  DensoVariable_Vec& vecVar,
89  int16_t vt, bool bRead, bool bWrite, bool bID, int iDuration)
90 {
91  DensoBase_Vec vecBase;
92  vecBase.insert(vecBase.end(), vecVar.begin(), vecVar.end());
93 
94  if(E_HANDLE == get_Object(vecBase, name, NULL)) {
95  Handle_Vec vecHandle;
96  HRESULT hr = AddObject(get_id, name, vecHandle);
97  if(FAILED(hr)) return hr;
98 
99  DensoVariable_Ptr var(new DensoVariable(this,
100  m_vecService, vecHandle, name, m_mode,
101  vt, bRead, bWrite, bID, iDuration));
102 
103  vecVar.push_back(var);
104  }
105 
106  return S_OK;
107 }
108 
110  const XMLElement *xmlVar,
111  DensoVariable_Vec& vecVar)
112 {
113  const char *chTmp;
114 
115  std::string name;
116  int16_t vt = VT_EMPTY;
117  bool bRead = false, bWrite = false, bID = false;
118  int iDuration = BCAP_VAR_DEFAULT_DURATION;
119 
120  name = xmlVar->GetText();
121 
122  chTmp = xmlVar->Attribute(XML_ATTR_VARTYPE);
123  if(chTmp != NULL) vt = atoi(chTmp);
124 
125  chTmp = xmlVar->Attribute(XML_ATTR_READ);
126  if(chTmp != NULL) bRead = (strcasecmp(chTmp, "true") == 0);
127 
128  chTmp = xmlVar->Attribute(XML_ATTR_WRITE);
129  if(chTmp != NULL) bWrite = (strcasecmp(chTmp, "true") == 0);
130 
131  chTmp = xmlVar->Attribute(XML_ATTR_ID);
132  if(chTmp != NULL) bID = (strcasecmp(chTmp, "true") == 0);
133 
134  chTmp = xmlVar->Attribute(XML_ATTR_DURATION);
135  if(chTmp != NULL) iDuration = atoi(chTmp);
136 
137  Handle_Vec vecHandle;
138  HRESULT hr = AddObject(get_id, name, vecHandle);
139  if(FAILED(hr)) return hr;
140 
141  DensoVariable_Ptr var(new DensoVariable(this,
142  m_vecService, vecHandle, name, m_mode,
143  vt, bRead, bWrite, bID, iDuration));
144 
145  vecVar.push_back(var);
146 
147  return S_OK;
148 }
149 
151  int32_t get_id, const std::string& name,
152  Handle_Vec& vecHandle)
153 {
154  int srvs, argc;
155  HRESULT hr;
156 
157  for(srvs = SRV_MIN; srvs <= SRV_MAX; srvs++) {
158  VARIANT_Ptr vntRet(new VARIANT());
159  VARIANT_Vec vntArgs;
160 
161  VariantInit(vntRet.get());
162 
163  for(argc = 0; argc < BCAP_GET_OBJECT_ARGS; argc++) {
164  VARIANT_Ptr vntTmp(new VARIANT());
165  VariantInit(vntTmp.get());
166 
167  switch(argc) {
168  case 0:
169  vntTmp->vt = VT_UI4;
170  vntTmp->ulVal = m_vecHandle[srvs];
171  break;
172  case 1:
173  vntTmp->vt = VT_BSTR;
174  vntTmp->bstrVal = ConvertStringToBSTR(name);
175  break;
176  case 2:
177  vntTmp->vt = VT_BSTR;
178  vntTmp->bstrVal = SysAllocString(L"");
179  break;
180  }
181 
182  vntArgs.push_back(*vntTmp.get());
183  }
184 
185  hr = m_vecService[srvs]->ExecFunction(get_id, vntArgs, vntRet);
186  if(FAILED(hr)) break;
187 
188  vecHandle.push_back(vntRet->ulVal);
189  }
190 
191  return hr;
192 }
193 
195 {
196  uint32_t argc, i, num;
197  HRESULT hr;
198  VARIANT_Ptr vntRet(new VARIANT());
199  VARIANT_Vec vntArgs;
200 
201  VariantInit(vntRet.get());
202 
203  for(argc = 0; argc < BCAP_GET_OBJECTNAMES_ARGS; argc++) {
204  VARIANT_Ptr vntTmp(new VARIANT());
205  VariantInit(vntTmp.get());
206 
207  if(argc == 0) {
208  vntTmp->vt = VT_UI4;
209  vntTmp->ulVal = m_vecHandle[SRV_MIN];
210  } else {
211  vntTmp->vt = VT_BSTR;
212  vntTmp->bstrVal = SysAllocString(L"");
213  }
214 
215  vntArgs.push_back(*vntTmp.get());
216  }
217 
218  hr = m_vecService[SRV_MIN]->ExecFunction(func_id, vntArgs, vntRet);
219  if(SUCCEEDED(hr)) {
220  BSTR *pbstr;
221  VARIANT *pvnt;
222  switch(vntRet->vt) {
223  case (VT_ARRAY | VT_BSTR):
224  num = vntRet->parray->rgsabound->cElements;
225  SafeArrayAccessData(vntRet->parray, (void**)&pbstr);
226  for(i = 0; i < num; i++) {
227  vecName.push_back(ConvertBSTRToString(pbstr[i]));
228  }
229  SafeArrayUnaccessData(vntRet->parray);
230  break;
231  case (VT_ARRAY | VT_VARIANT):
232  num = vntRet->parray->rgsabound->cElements;
233  SafeArrayAccessData(vntRet->parray, (void**)&pvnt);
234  for(i = 0; i < num; i++){
235  if(pvnt[i].vt != VT_BSTR) {
236  hr = E_FAIL;
237  break;
238  }
239  vecName.push_back(ConvertBSTRToString(pvnt[i].bstrVal));
240  }
241  SafeArrayUnaccessData(vntRet->parray);
242  break;
243  default:
244  hr = S_FALSE;
245  break;
246  }
247  }
248 
249  return hr;
250 }
251 
253  int index, DensoBase_Ptr *obj)
254 {
255  try {
256  if(obj != NULL) {
257  *obj = vecBase.at(index);
258  }
259  } catch (std::out_of_range&) {
260  return E_HANDLE;
261  }
262 
263  return S_OK;
264 }
265 
267  const std::string& name, DensoBase_Ptr *obj)
268 {
269  DensoBase_Vec::const_iterator it;
270  for(it = vecBase.begin(); it != vecBase.end(); it++) {
271  if(!strcasecmp((*it)->Name().c_str(), name.c_str())) {
272  if(obj != NULL) {
273  *obj = *it;
274  }
275  return S_OK;
276  }
277  }
278 
279  return E_HANDLE;
280 }
281 
282 }
#define S_FALSE
HRESULT SafeArrayUnaccessData(SAFEARRAY *psa)
short int16_t
unsigned uint32_t
HRESULT AddVariable(int32_t get_id, const std::string &name, std::vector< boost::shared_ptr< class DensoVariable > > &vecVar, int16_t vt=VT_EMPTY, bool bRead=false, bool bWrite=false, bool bID=false, int iDuration=BCAP_VAR_DEFAULT_DURATION)
#define XML_ATTR_WRITE
#define S_OK
uint32_t cElements
wchar_t * BSTR
std::vector< VARIANT, VariantAllocator< VARIANT > > VARIANT_Vec
std::vector< uint32_t > Handle_Vec
Definition: denso_base.h:67
HRESULT GetObjectNames(int32_t func_id, Name_Vec &vecName)
Definition: denso_base.cpp:194
#define XML_ATTR_VARTYPE
#define FAILED(hr)
#define E_HANDLE
VT_BSTR
VT_EMPTY
void VariantInit(VARIANT *pvarg)
#define XML_ATTR_READ
#define E_FAIL
int32_t HRESULT
BSTR SysAllocString(const wchar_t *sz)
VT_ARRAY
static std::string ConvertBSTRToString(const BSTR bstr)
Definition: denso_base.cpp:34
const char * Attribute(const char *name, const char *value=0) const
Definition: tinyxml2.cpp:1402
std::vector< DensoVariable_Ptr > DensoVariable_Vec
VT_UI4
const char * GetText() const
Definition: tinyxml2.cpp:1415
boost::interprocess::unique_ptr< VARIANT, variant_deleter > VARIANT_Ptr
SAFEARRAY * parray
HRESULT SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
VT_VARIANT
static BSTR ConvertStringToBSTR(const std::string &str)
Definition: denso_base.cpp:48
#define XML_ATTR_DURATION
#define SUCCEEDED(hr)
HRESULT get_Object(const std::vector< boost::shared_ptr< DensoBase > > &vecBase, int index, boost::shared_ptr< DensoBase > *obj)
std::vector< std::string > Name_Vec
Definition: denso_base.h:66
#define XML_ATTR_ID
#define BCAP_GET_OBJECTNAMES_ARGS
Definition: denso_base.cpp:30
int int32_t
SAFEARRAYBOUND rgsabound[1]
std::vector< DensoBase_Ptr > DensoBase_Vec
Definition: denso_base.h:173
std::string RosName() const
Definition: denso_base.cpp:62
#define BCAP_GET_OBJECT_ARGS
Definition: denso_base.cpp:29
#define BCAP_VAR_DEFAULT_DURATION
Definition: denso_base.h:62
_DN_EXP_COMMON char * ConvertWideChar2MultiByte(const wchar_t *chSrc)
HRESULT AddObject(int32_t get_id, const std::string &name, Handle_Vec &vecHandle)
Definition: denso_base.cpp:150
_DN_EXP_COMMON wchar_t * ConvertMultiByte2WideChar(const char *chSrc)


denso_robot_core
Author(s): DENSO WAVE INCORPORATED
autogenerated on Mon Jun 10 2019 13:12:27