NVUtil.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #ifdef WIN32
21 #pragma warning( push )
22 #pragma warning( disable : 4267 )
23 #pragma warning( disable : 4290 )
24 #pragma warning( disable : 4311 )
25 #pragma warning( disable : 4312 )
26 #endif // WIN32
27 
28 #include <map>
29 #include <algorithm>
30 #include <coil/stringutil.h>
31 #include <rtm/NVUtil.h>
32 #include <rtm/CORBA_SeqUtil.h>
33 
34 #ifdef WIN32
35 #pragma warning( pop )
36 #endif // WIN32
37 
38 namespace NVUtil
39 {
47  SDOPackage::NameValue newNVChar(const char* name, const CORBA::Char value)
48  {
49  SDOPackage::NameValue nv;
50  nv.name = CORBA::string_dup(name);
51  nv.value <<= CORBA::Any::from_char(value);
52  return nv;
53  }
54 
62  SDOPackage::NameValue newNVBool(const char* name, const CORBA::Boolean value)
63  {
64  SDOPackage::NameValue nv;
65  nv.name = CORBA::string_dup(name);
66  nv.value <<= CORBA::Any::from_boolean(value);
67  return nv;
68  }
69 
77  SDOPackage::NameValue newNVOctet(const char* name, const CORBA::Octet value)
78  {
79  SDOPackage::NameValue nv;
80  nv.name = CORBA::string_dup(name);
81  nv.value <<= CORBA::Any::from_octet(value);
82  return nv;
83  }
84 
92  SDOPackage::NameValue newNVAny(const char* name, const CORBA::Any& value)
93  {
94  SDOPackage::NameValue nv;
95  nv.name = CORBA::string_dup(name);
96  nv.value = value;
97  return nv;
98  }
99 
107 #ifndef ORB_IS_RTORB
109 #else // ORB_IS_RTORB
110  void copyFromProperties(SDOPackage_NVList& nv, const coil::Properties& prop)
111 #endif // ORB_IS_RTORB
112  {
113  std::vector<std::string> keys;
114  keys = prop.propertyNames();
115  CORBA::ULong len((CORBA::ULong)keys.size());
116  nv.length(len);
117 
118  for (CORBA::ULong i = 0; i < len; ++i)
119  {
120 // Why RtORB does not copy string to Properties.
121 #ifndef ORB_IS_RTORB
122  nv[i].name = CORBA::string_dup(keys[i].c_str());
123 #else // ORB_IS_RTORB
124  nv[i].name = (char *)keys[i].c_str();
125 #endif // ORB_IS_RTORB
126  nv[i].value <<= prop[keys[i]].c_str();
127  }
128  }
129 
138  {
139  for (CORBA::ULong i(0), len(nv.length()); i < len; ++i)
140  {
141  const char* value;
142  if (nv[i].value >>= value)
143  {
144  const char* name(nv[i].name);
145  prop[name] = value;
146  };
147  }
148  }
149 
157  struct to_prop
158  {
160  {
161  };
162  void operator()(const SDOPackage::NameValue& nv)
163  {
164  const char* value;
165  if (nv.value >>= value)
166  {
167  m_prop.setProperty(CORBA::string_dup(nv.name), value);
168  };
169  }
171  };
172 
181  {
182  to_prop p;
183  p = CORBA_SeqUtil::for_each(nv, p);
184  return p.m_prop;
185  }
186 
194  struct nv_find
195  {
196  nv_find(const char* name) : m_name(name) {};
197  bool operator()(const SDOPackage::NameValue& nv)
198  {
199  std::string name(nv.name);
200  return m_name == name;
201  }
202  std::string m_name;
203  };
204 
212  const CORBA::Any& find(const SDOPackage::NVList& nv, const char* name)
213  {
214  CORBA::Long index;
215  index = CORBA_SeqUtil::find(nv, NVUtil::nv_find(name));
216  if (index < 0) throw std::string("Not found");
217  return nv[index].value;
218  }
219 
227  const CORBA::Long find_index(const SDOPackage::NVList& nv, const char* name)
228  {
229  return CORBA_SeqUtil::find(nv, NVUtil::nv_find(name));
230  }
231 
239  bool isString(const SDOPackage::NVList& nv, const char* name)
240  {
241  try
242  {
243  CORBA::Any value;
244  value = find(nv, name);
245  const char* str_value;
246  return value >>= str_value;
247  }
248  catch (...)
249  {
250  return false;
251  }
252  }
253 
263  const char* name, const char* value)
264  {
265  if (isString(nv, name))
266  {
267  if (toString(nv, name) == value)
268  {
269  return true;
270  }
271  }
272  return false;
273  }
274 
282  std::string toString(const SDOPackage::NVList& nv, const char* name)
283  {
284  const char* str_value;
285  try
286  {
287  if(!(find(nv, name) >>= str_value))
288  {
289  str_value = "";
290  }
291  }
292  catch (...)
293  {
294  str_value = "";
295  }
296 
297  if (str_value == NULL)
298  {
299  str_value = "";
300  }
301 
302  return str_value;
303  }
304 
312 #ifndef ORB_IS_RTORB
313  bool appendStringValue(SDOPackage::NVList& nv, const char* name,
314  const char* value)
315 #else // ORB_IS_RTORB
316  bool appendStringValue(SDOPackage_NVList& nv, const char* name,
317  const char* value)
318 #endif // ORB_IS_RTORB
319  {
320  // if (!isString(nv, name)) return false;
321 
322  CORBA::Long index;
323  index = find_index(nv, name);
324 
325  if (index < 0)
326  {
327  CORBA_SeqUtil::push_back(nv, newNV(name, value));
328  }
329  else
330  {
331  const char* tmp_char;
332  nv[index].value >>= tmp_char;
333  std::string tmp_str(tmp_char);
334 
335  std::vector<std::string> values;
336  values = coil::split(tmp_str, ",");
337  if (values.end() == std::find(values.begin(), values.end(), value))
338  {
339  tmp_str.append(",");
340  tmp_str.append(value);
341  nv[index].value <<= tmp_str.c_str();
342  }
343  }
344  return true;
345  }
346 
355  {
356  for (CORBA::ULong i = 0, len = src.length(); i < len; ++i)
357  {
358  CORBA_SeqUtil::push_back(dest, src[i]);
359  }
360  }
361 
369  std::ostream& dump_to_stream(std::ostream& out, const SDOPackage::NVList& nv)
370  {
371  for (CORBA::ULong i(0), n(nv.length()); i < n; ++i)
372  {
373  const char* str_value;
374  if (nv[i].value >>= str_value)
375  {
376  out << nv[i].name << ": " << str_value << std::endl;
377  }
378  else
379  {
380  out << nv[i].name << ": not a string value" << std::endl;
381  }
382  }
383  return out;
384  }
385 
394  void dump(const SDOPackage::NVList& nv)
395  {
396  dump_to_stream(std::cout, nv);
397  }
398 
406  std::string toString(const SDOPackage::NVList& nv)
407  {
408  std::stringstream s;
409  dump_to_stream(s, nv);
410  return s.str();
411  }
412 };
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:79
Functor to transform NVList into the properties.
Definition: NVUtil.cpp:157
coil::Properties m_prop
Definition: NVUtil.cpp:170
bool isString(const SDOPackage::NVList &nv, const char *name)
Validate whether value type specified by name is string type.
Definition: NVUtil.cpp:239
std::ostream & dump_to_stream(std::ostream &out, const SDOPackage::NVList &nv)
Print information configured in NVList as a string type.
Definition: NVUtil.cpp:369
SDOPackage::NameValue newNVOctet(const char *name, const CORBA::Octet value)
Create NameValue typed CORBA::Octet.
Definition: NVUtil.cpp:77
bool appendStringValue(SDOPackage::NVList &nv, const char *name, const char *value)
Append the specified string to element of NVList.
Definition: NVUtil.cpp:313
std::vector< std::pair< std::string, std::string > > NVList
Definition: IRTC.h:67
nv_find(const char *name)
Definition: NVUtil.cpp:196
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
Definition: stringutil.cpp:341
SDOPackage::NameValue newNVBool(const char *name, const CORBA::Boolean value)
This operation creates NameValue typed CORBA::Boolean.
Definition: NVUtil.cpp:62
bool operator()(const SDOPackage::NameValue &nv)
Definition: NVUtil.cpp:197
Utility for NameValue.
Definition: NVUtil.cpp:38
const CORBA::Long find_index(const SDOPackage::NVList &nv, const char *name)
Return the index of element specified by name from NVList.
Definition: NVUtil.cpp:227
void operator()(const SDOPackage::NameValue &nv)
Definition: NVUtil.cpp:162
void copyToProperties(coil::Properties &prop, const SDOPackage::NVList &nv)
Copy NVList to the Proeprties.
Definition: NVUtil.cpp:137
std::string toString(const SDOPackage::NVList &nv, const char *name)
Get NVList of specifid name as string.
Definition: NVUtil.cpp:282
const CORBA::Any & find(const SDOPackage::NVList &nv, const char *name)
Return the value specified by name from NVList.
Definition: NVUtil.cpp:212
CORBA::Long find(const CorbaSequence &seq, Functor f)
Return the index of CORBA sequence element that functor matches.
list index
Definition: rtimages.py:10
std::string setProperty(const std::string &key, const std::string &value)
Set a value associated with key in the property list.
Definition: Properties.cpp:236
NameValue and NVList utility functions.
CORBA sequence utility template functions.
std::string m_name
Definition: NVUtil.cpp:202
void dump(const SDOPackage::NVList &nv)
Print information configured in NVList as a string type to Standard Outport.
Definition: NVUtil.cpp:394
void append(SDOPackage::NVList &dest, const SDOPackage::NVList &src)
Append an element to NVList.
Definition: NVUtil.cpp:354
prop
Organization::get_organization_property ();.
coil::Properties toProperties(const SDOPackage::NVList &nv)
Transform NVList to the properties.
Definition: NVUtil.cpp:180
Functor to find a NVList.
Definition: NVUtil.cpp:194
Class represents a set of properties.
Definition: Properties.h:101
SDOPackage::NameValue newNVChar(const char *name, const CORBA::Char value)
Create NameValue typed CORBA::Char.
Definition: NVUtil.cpp:47
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
bool isStringValue(const SDOPackage::NVList &nv, const char *name, const char *value)
Check whether the value of specified name specified matches the specified string. ...
Definition: NVUtil.cpp:262
void copyFromProperties(SDOPackage::NVList &nv, const coil::Properties &prop)
Copy the properties to NVList.
Definition: NVUtil.cpp:108
std::vector< std::string > propertyNames(void) const
Return an vector of all the keys in this property.
Definition: Properties.cpp:410
SDOPackage::NameValue newNVAny(const char *name, const CORBA::Any &value)
Create NameValue typed CORBA::Any.
Definition: NVUtil.cpp:92
Functor for_each(CorbaSequence &seq, Functor f)
Apply the functor to all CORBA sequence elements.
Definition: CORBA_SeqUtil.h:98


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:53