NVUtil.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #ifdef WIN32
00021 #pragma warning( push )
00022 #pragma warning( disable : 4267 )
00023 #pragma warning( disable : 4290 )
00024 #pragma warning( disable : 4311 )
00025 #pragma warning( disable : 4312 )
00026 #endif // WIN32
00027 
00028 #include <map>
00029 #include <algorithm>
00030 #include <coil/stringutil.h>
00031 #include <rtm/NVUtil.h>
00032 #include <rtm/CORBA_SeqUtil.h>
00033 
00034 #ifdef WIN32
00035 #pragma warning( pop )
00036 #endif // WIN32
00037 
00038 namespace NVUtil
00039 {
00047   SDOPackage::NameValue newNVChar(const char* name, const CORBA::Char value)
00048   {
00049     SDOPackage::NameValue nv;
00050     nv.name = CORBA::string_dup(name);
00051     nv.value <<= CORBA::Any::from_char(value);
00052     return nv;
00053   }
00054   
00062   SDOPackage::NameValue newNVBool(const char* name, const CORBA::Boolean value)
00063   {
00064     SDOPackage::NameValue nv;
00065     nv.name = CORBA::string_dup(name);
00066     nv.value <<= CORBA::Any::from_boolean(value);
00067     return nv;
00068   }
00069   
00077   SDOPackage::NameValue newNVOctet(const char* name, const CORBA::Octet value)
00078   {
00079     SDOPackage::NameValue nv;
00080     nv.name = CORBA::string_dup(name);
00081     nv.value <<= CORBA::Any::from_octet(value);
00082     return nv;
00083   }
00084   
00092   SDOPackage::NameValue newNVAny(const char* name, const CORBA::Any& value)
00093   {
00094     SDOPackage::NameValue nv;
00095     nv.name = CORBA::string_dup(name);
00096     nv.value = value;
00097     return nv;
00098   }
00099   
00107 #ifndef ORB_IS_RTORB
00108   void copyFromProperties(SDOPackage::NVList& nv, const coil::Properties& prop)
00109 #else // ORB_IS_RTORB
00110     void copyFromProperties(SDOPackage_NVList& nv, const coil::Properties& prop)
00111 #endif // ORB_IS_RTORB
00112   {
00113     std::vector<std::string> keys;
00114     keys = prop.propertyNames();
00115     CORBA::ULong len((CORBA::ULong)keys.size());
00116     nv.length(len);
00117     
00118     for (CORBA::ULong i = 0; i < len; ++i)
00119       {
00120 // Why RtORB does not copy string to Properties.
00121 #ifndef ORB_IS_RTORB
00122         nv[i].name = CORBA::string_dup(keys[i].c_str());
00123 #else // ORB_IS_RTORB
00124         nv[i].name = (char *)keys[i].c_str();
00125 #endif // ORB_IS_RTORB
00126         nv[i].value <<= prop[keys[i]].c_str();
00127       }
00128   }
00129   
00137   void copyToProperties(coil::Properties& prop, const SDOPackage::NVList& nv)
00138   {
00139     for (CORBA::ULong i(0), len(nv.length()); i < len; ++i)
00140       {
00141         const char* value;
00142         if (nv[i].value >>= value)
00143           {
00144             const char* name(nv[i].name);
00145             prop[name] = value;
00146           };
00147       }
00148   }
00149   
00157   struct to_prop
00158   {
00159     to_prop()
00160     {
00161     };
00162     void operator()(const SDOPackage::NameValue& nv)
00163     {
00164       const char* value;
00165       if (nv.value >>= value)
00166         {
00167           m_prop.setProperty(CORBA::string_dup(nv.name), value);
00168         };
00169     }
00170     coil::Properties m_prop;
00171   };
00172   
00180   coil::Properties toProperties(const SDOPackage::NVList& nv)
00181   {
00182     to_prop p;
00183     p = CORBA_SeqUtil::for_each(nv, p);
00184     return p.m_prop;
00185   }
00186   
00194   struct nv_find
00195   {
00196     nv_find(const char* name) : m_name(name) {};
00197     bool operator()(const SDOPackage::NameValue& nv)
00198     {
00199       std::string name(nv.name);
00200       return m_name == name;
00201     }
00202     std::string m_name;
00203   };
00204   
00212   const CORBA::Any& find(const SDOPackage::NVList& nv, const char* name)
00213   {
00214     CORBA::Long index;
00215     index = CORBA_SeqUtil::find(nv, NVUtil::nv_find(name));
00216     if (index < 0) throw std::string("Not found");
00217     return nv[index].value;
00218   }
00219   
00227   const CORBA::Long find_index(const SDOPackage::NVList& nv, const char* name)
00228   {
00229     return  CORBA_SeqUtil::find(nv, NVUtil::nv_find(name));
00230   }
00231   
00239   bool isString(const SDOPackage::NVList& nv, const char* name)
00240   {
00241     try
00242       {
00243         CORBA::Any value;
00244         value = find(nv, name);
00245         const char* str_value;
00246         return value >>= str_value;
00247       }
00248     catch (...)
00249       {
00250         return false;
00251       }
00252   }
00253   
00262   bool isStringValue(const SDOPackage::NVList& nv,
00263                      const char* name, const char* value)
00264   {
00265     if (isString(nv, name))
00266       {
00267         if (toString(nv, name) == value)
00268           {
00269             return true;
00270           }
00271       }
00272     return false;
00273   }
00274   
00282   std::string toString(const SDOPackage::NVList& nv, const char* name)
00283   {
00284     const char* str_value;
00285     try
00286       {
00287         if(!(find(nv, name) >>= str_value))
00288           {
00289             str_value = "";
00290           }
00291       }
00292     catch (...)
00293       {
00294         str_value = "";
00295       }
00296 
00297     if (str_value == NULL)
00298       {
00299         str_value = "";
00300       }
00301     
00302     return str_value;
00303   }
00304   
00312 #ifndef ORB_IS_RTORB
00313   bool appendStringValue(SDOPackage::NVList& nv, const char* name,
00314                          const char* value)
00315 #else // ORB_IS_RTORB
00316   bool appendStringValue(SDOPackage_NVList& nv, const char* name,
00317                          const char* value)
00318 #endif // ORB_IS_RTORB
00319   {
00320     //    if (!isString(nv, name)) return false;
00321     
00322     CORBA::Long index;
00323     index = find_index(nv, name);
00324     
00325     if (index < 0)
00326       {
00327         CORBA_SeqUtil::push_back(nv, newNV(name, value));
00328       }
00329     else
00330       {
00331         const char* tmp_char;
00332         nv[index].value >>= tmp_char;
00333         std::string tmp_str(tmp_char);
00334         
00335         std::vector<std::string> values;
00336         values = coil::split(tmp_str, ",");
00337         if (values.end() == std::find(values.begin(), values.end(), value))
00338           {
00339             tmp_str.append(",");
00340             tmp_str.append(value);
00341             nv[index].value <<= tmp_str.c_str();
00342           }
00343       }
00344     return true;
00345   }
00346   
00354   void append(SDOPackage::NVList& dest, const SDOPackage::NVList& src)
00355   {
00356     for (CORBA::ULong i = 0, len = src.length(); i < len; ++i)
00357       {
00358         CORBA_SeqUtil::push_back(dest, src[i]);
00359       }
00360   }
00361   
00369   std::ostream& dump_to_stream(std::ostream& out, const SDOPackage::NVList& nv)
00370   {
00371     for (CORBA::ULong i(0), n(nv.length()); i < n; ++i)
00372       {
00373         const char* str_value;
00374         if (nv[i].value >>= str_value)
00375           {
00376             out << nv[i].name << ": " << str_value << std::endl;
00377           }
00378         else
00379           {
00380             out << nv[i].name << ": not a string value" << std::endl;
00381           }
00382       }
00383     return out;
00384   }
00385 
00394   void dump(const SDOPackage::NVList& nv)
00395   {
00396     dump_to_stream(std::cout, nv);
00397   }
00398 
00406   std::string toString(const SDOPackage::NVList& nv)
00407   {
00408     std::stringstream s;
00409     dump_to_stream(s, nv);
00410     return s.str();
00411   }
00412 };


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:05