当前位置: 首页 > 工具软件 > XMLRPC > 使用案例 >

yaml对象数组类型XmlRpc::XmlRpcValue严格限定数据类型

计泉
2023-12-01

yaml支持对象数组类型,格式如下:

ob_extracts:
- type: ["charg","charging","pile","cdz"]
  intensity_thr:
    front_laser: 150.       #包括360
    rear_laser: 150.
  pts_num: 20
- type: ["contain","container","huogui"]
  intensity_thr:
    front_laser: 150.
    rear_laser: 150.
  pts_num: 20

头文件在/opt/ros/kinetic/include/xmlrpcpp/XmlRpcValue.h,构造函数如下:


namespace XmlRpc {

  //! RPC method arguments and results are represented by Values
  //   should probably refcount them...
  class XMLRPCPP_DECL XmlRpcValue {
  public:


    enum Type {
      TypeInvalid,
      TypeBoolean,
      TypeInt,
      TypeDouble,
      TypeString,
      TypeDateTime,
      TypeBase64,
      TypeArray,
      TypeStruct
    };

    // Non-primitive types
    typedef std::vector<char> BinaryData;
    typedef std::vector<XmlRpcValue> ValueArray;
    typedef std::map<std::string, XmlRpcValue> ValueStruct;
    typedef ValueStruct::iterator iterator;
    typedef ValueStruct::const_iterator const_iterator;

    //! Constructors
    XmlRpcValue() : _type(TypeInvalid) { _value.asBinary = 0; }
    XmlRpcValue(bool value) : _type(TypeBoolean) { _value.asBool = value; }
    XmlRpcValue(int value)  : _type(TypeInt) { _value.asInt = value; }
    XmlRpcValue(double value)  : _type(TypeDouble) { _value.asDouble = value; }

    XmlRpcValue(std::string const& value) : _type(TypeString) 
    { _value.asString = new std::string(value); }

    XmlRpcValue(const char* value)  : _type(TypeString)
    { _value.asString = new std::string(value); }

    XmlRpcValue(struct tm* value)  : _type(TypeDateTime) 
    { _value.asTime = new struct tm(*value); }


    XmlRpcValue(void* value, int nBytes)  : _type(TypeBase64)
    {
      _value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
    }

    //! Construct from xml, beginning at *offset chars into the string, updates offset
    XmlRpcValue(std::string const& xml, int* offset) : _type(TypeInvalid)
    { if ( ! fromXml(xml,offset)) _type = TypeInvalid; }

    //! Copy
    XmlRpcValue(XmlRpcValue const& rhs) : _type(TypeInvalid) { *this = rhs; }

XmlRpc::XmlRpcValue类型的内置类型包含有std::vector<char>字符串、std::vector<XmlRpcValue>对象数组、std::map<std::string,XmlRpcValue>对象map、TypeInvalid类型的空、bool、int、double、std::string、const char*、tm时间以及指针+偏移的二进制数组TypeBase64类型,另外还有xml文件+偏移的二进制类型,支持拷贝构造。

同时提供了赋值运算符函数:

// Operators
    XmlRpcValue& operator=(XmlRpcValue const& rhs);
    XmlRpcValue& operator=(bool const& rhs) { return operator=(XmlRpcValue(rhs)); }
    XmlRpcValue& operator=(int const& rhs) { return operator=(XmlRpcValue(rhs)); }
    XmlRpcValue& operator=(double const& rhs) { return operator=(XmlRpcValue(rhs)); }
    XmlRpcValue& operator=(const char* rhs) { return operator=(XmlRpcValue(std::string(rhs))); }

支持XmlRpcValue对象、bool、int、double、char*类型的赋值

解码数据提供了若干类型运算符函数

operator bool&()          { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
    operator int&()           { assertTypeOrInvalid(TypeInt); return _value.asInt; }
    operator double&()        { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
    operator std::string&()   { assertTypeOrInvalid(TypeString); return *_value.asString; }
    operator BinaryData&()    { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
    operator struct tm&()     { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }

    XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
    XmlRpcValue& operator[](int i)             { assertArray(i+1); return _value.asArray->at(i); }

    XmlRpcValue& operator[](std::string const& k) const { assertStruct(); return (*_value.asStruct)[k]; }
    XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
    XmlRpcValue& operator[](const char* k) const { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
    XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }

严格限定类型,比如解码int数据不能使用double,否则会触发assertTypeOrInvalid(TypeDouble);的类型检查异常。示例:

XmlRpc::XmlRpcValue extract_params;
        _nh.getParam("ob_extracts",extract_params);
        cout<<typeid( extract_params[0]["len"]["min"] ).name()<<endl;
        cout<<"TTTest obj in dict, "<<extract_params[0]["len"]["min"]<<endl;
        cout<<"     : "<<double(extract_params[0]["len"]["min"])<<endl;
        cout<<"     : "<<int(extract_params[0]["intensity_thr"]["front_laser"])<<endl;

这里将std::map<std::string,XmlRpcValue>类型唤作ValueStruct,使用[ ]运算符传入键名返回XmlRpcValue对象,可以嵌套,可以数组

此处map是非哈希的树形有序map,还提供了begin()、end()的广度迭代器。

iterator begin() {assertStruct(); return (*_value.asStruct).begin(); }
    iterator end() {assertStruct(); return (*_value.asStruct).end(); }

    const_iterator begin() const {assertStruct(); return (*_value.asStruct).begin(); }
    const_iterator end() const {assertStruct(); return (*_value.asStruct).end(); }

还提供了提取类型和string、二进制数组base64、数组array和struct值size的函数:

//! Return the type of the value stored. \see Type.
    Type const &getType() const { return _type; }

    //! Return the size for string, base64, array, and struct values.
    int size() const;

 类似资料: