Compare commits
1 Commits
40718158ae
...
1f8d5cc300
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f8d5cc300 |
@@ -1,190 +1,190 @@
|
|||||||
#ifndef _ROBOT_XMLRPCVALUE_H_
|
#ifndef _ROBOT_XMLRPCVALUE_H_
|
||||||
#define _ROBOT_XMLRPCVALUE_H_
|
#define _ROBOT_XMLRPCVALUE_H_
|
||||||
//
|
//
|
||||||
// XmlRpc++ Copyright (c) 2002-2003 by Chris Morley
|
// XmlRpc++ Copyright (c) 2002-2003 by Chris Morley
|
||||||
//
|
//
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
# pragma warning(disable:4786) // identifier was truncated in debug info
|
# pragma warning(disable:4786) // identifier was truncated in debug info
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MAKEDEPEND
|
#ifndef MAKEDEPEND
|
||||||
# include <map>
|
# include <map>
|
||||||
# include <string>
|
# include <string>
|
||||||
# include <vector>
|
# include <vector>
|
||||||
# include <time.h>
|
# include <time.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace robot_xmlrpcpp {
|
namespace robot_xmlrpcpp {
|
||||||
|
|
||||||
//! RPC method arguments and results are represented by Values
|
//! RPC method arguments and results are represented by Values
|
||||||
// should probably refcount them...
|
// should probably refcount them...
|
||||||
class XmlRpcValue {
|
class XmlRpcValue {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
TypeInvalid,
|
TypeInvalid,
|
||||||
TypeBoolean,
|
TypeBoolean,
|
||||||
TypeInt,
|
TypeInt,
|
||||||
TypeDouble,
|
TypeDouble,
|
||||||
TypeString,
|
TypeString,
|
||||||
TypeDateTime,
|
TypeDateTime,
|
||||||
TypeBase64,
|
TypeBase64,
|
||||||
TypeArray,
|
TypeArray,
|
||||||
TypeStruct
|
TypeStruct
|
||||||
};
|
};
|
||||||
|
|
||||||
// Non-primitive types
|
// Non-primitive types
|
||||||
typedef std::vector<char> BinaryData;
|
typedef std::vector<char> BinaryData;
|
||||||
typedef std::vector<XmlRpcValue> ValueArray;
|
typedef std::vector<XmlRpcValue> ValueArray;
|
||||||
typedef std::map<std::string, XmlRpcValue> ValueStruct;
|
typedef std::map<std::string, XmlRpcValue> ValueStruct;
|
||||||
|
|
||||||
|
|
||||||
//! Constructors
|
//! Constructors
|
||||||
XmlRpcValue() : _type(TypeInvalid) { _value.asBinary = 0; }
|
XmlRpcValue() : _type(TypeInvalid) { _value.asBinary = 0; }
|
||||||
XmlRpcValue(bool value) : _type(TypeBoolean) { _value.asBool = value; }
|
XmlRpcValue(bool value) : _type(TypeBoolean) { _value.asBool = value; }
|
||||||
XmlRpcValue(int value) : _type(TypeInt) { _value.asInt = value; }
|
XmlRpcValue(int value) : _type(TypeInt) { _value.asInt = value; }
|
||||||
XmlRpcValue(double value) : _type(TypeDouble) { _value.asDouble = value; }
|
XmlRpcValue(double value) : _type(TypeDouble) { _value.asDouble = value; }
|
||||||
|
|
||||||
XmlRpcValue(std::string const& value) : _type(TypeString)
|
XmlRpcValue(std::string const& value) : _type(TypeString)
|
||||||
{ _value.asString = new std::string(value); }
|
{ _value.asString = new std::string(value); }
|
||||||
|
|
||||||
XmlRpcValue(const char* value) : _type(TypeString)
|
XmlRpcValue(const char* value) : _type(TypeString)
|
||||||
{ _value.asString = new std::string(value); }
|
{ _value.asString = new std::string(value); }
|
||||||
|
|
||||||
XmlRpcValue(struct tm* value) : _type(TypeDateTime)
|
XmlRpcValue(struct tm* value) : _type(TypeDateTime)
|
||||||
{ _value.asTime = new struct tm(*value); }
|
{ _value.asTime = new struct tm(*value); }
|
||||||
|
|
||||||
|
|
||||||
XmlRpcValue(void* value, int nBytes) : _type(TypeBase64)
|
XmlRpcValue(void* value, int nBytes) : _type(TypeBase64)
|
||||||
{
|
{
|
||||||
_value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
|
_value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Construct from xml, beginning at *offset chars into the string, updates offset
|
//! Construct from xml, beginning at *offset chars into the string, updates offset
|
||||||
XmlRpcValue(std::string const& xml, int* offset) : _type(TypeInvalid)
|
XmlRpcValue(std::string const& xml, int* offset) : _type(TypeInvalid)
|
||||||
{ if ( ! fromXml(xml,offset)) _type = TypeInvalid; }
|
{ if ( ! fromXml(xml,offset)) _type = TypeInvalid; }
|
||||||
|
|
||||||
//! Copy
|
//! Copy
|
||||||
XmlRpcValue(XmlRpcValue const& rhs) : _type(TypeInvalid) { *this = rhs; }
|
XmlRpcValue(XmlRpcValue const& rhs) : _type(TypeInvalid) { *this = rhs; }
|
||||||
|
|
||||||
//! Destructor (make virtual if you want to subclass)
|
//! Destructor (make virtual if you want to subclass)
|
||||||
/*virtual*/ ~XmlRpcValue() { invalidate(); }
|
/*virtual*/ ~XmlRpcValue() { invalidate(); }
|
||||||
|
|
||||||
//! Erase the current value
|
//! Erase the current value
|
||||||
void clear() { invalidate(); }
|
void clear() { invalidate(); }
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
XmlRpcValue& operator=(XmlRpcValue const& rhs);
|
XmlRpcValue& operator=(XmlRpcValue const& rhs);
|
||||||
XmlRpcValue& operator=(int 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=(double const& rhs) { return operator=(XmlRpcValue(rhs)); }
|
||||||
XmlRpcValue& operator=(const char* rhs) { return operator=(XmlRpcValue(std::string(rhs))); }
|
XmlRpcValue& operator=(const char* rhs) { return operator=(XmlRpcValue(std::string(rhs))); }
|
||||||
|
|
||||||
bool operator==(XmlRpcValue const& other) const;
|
bool operator==(XmlRpcValue const& other) const;
|
||||||
bool operator!=(XmlRpcValue const& other) const;
|
bool operator!=(XmlRpcValue const& other) const;
|
||||||
|
|
||||||
operator bool&() { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
|
operator bool&() { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
|
||||||
operator int&() { assertTypeOrInvalid(TypeInt); return _value.asInt; }
|
operator int&() { assertTypeOrInvalid(TypeInt); return _value.asInt; }
|
||||||
operator double&() { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
|
operator double&() { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
|
||||||
operator std::string&() { assertTypeOrInvalid(TypeString); return *_value.asString; }
|
operator std::string&() { assertTypeOrInvalid(TypeString); return *_value.asString; }
|
||||||
operator BinaryData&() { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
|
operator BinaryData&() { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
|
||||||
operator struct tm&() { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
|
operator struct tm&() { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
|
||||||
|
|
||||||
XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
|
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[](int i) { assertArray(i+1); return _value.asArray->at(i); }
|
||||||
|
|
||||||
XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
|
XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
|
||||||
XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
|
XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
|
||||||
|
|
||||||
// Accessors
|
// Accessors
|
||||||
//! Return true if the value has been set to something.
|
//! Return true if the value has been set to something.
|
||||||
bool valid() const { return _type != TypeInvalid; }
|
bool valid() const { return _type != TypeInvalid; }
|
||||||
|
|
||||||
//! Return the type of the value stored. \see Type.
|
//! Return the type of the value stored. \see Type.
|
||||||
Type const &getType() const { return _type; }
|
Type const &getType() const { return _type; }
|
||||||
|
|
||||||
//! Return the size for string, base64, array, and struct values.
|
//! Return the size for string, base64, array, and struct values.
|
||||||
int size() const;
|
int size() const;
|
||||||
|
|
||||||
//! Specify the size for array values. Array values will grow beyond this size if needed.
|
//! Specify the size for array values. Array values will grow beyond this size if needed.
|
||||||
void setSize(int size) { assertArray(size); }
|
void setSize(int size) { assertArray(size); }
|
||||||
|
|
||||||
//! Check for the existence of a struct member by name.
|
//! Check for the existence of a struct member by name.
|
||||||
bool hasMember(const std::string& name) const;
|
bool hasMember(const std::string& name) const;
|
||||||
|
|
||||||
//! If this value is a struct, returns pointer to its members; otherwise nullptr.
|
//! If this value is a struct, returns pointer to its members; otherwise nullptr.
|
||||||
const ValueStruct *getStructMembers() const;
|
const ValueStruct *getStructMembers() const;
|
||||||
|
|
||||||
//! Decode xml. Destroys any existing value.
|
//! Decode xml. Destroys any existing value.
|
||||||
bool fromXml(std::string const& valueXml, int* offset);
|
bool fromXml(std::string const& valueXml, int* offset);
|
||||||
|
|
||||||
//! Encode the Value in xml
|
//! Encode the Value in xml
|
||||||
std::string toXml() const;
|
std::string toXml() const;
|
||||||
|
|
||||||
//! Write the value (no xml encoding)
|
//! Write the value (no xml encoding)
|
||||||
std::ostream& write(std::ostream& os) const;
|
std::ostream& write(std::ostream& os) const;
|
||||||
|
|
||||||
// Formatting
|
// Formatting
|
||||||
//! Return the format used to write double values.
|
//! Return the format used to write double values.
|
||||||
static std::string const& getDoubleFormat() { return _doubleFormat; }
|
static std::string const& getDoubleFormat() { return _doubleFormat; }
|
||||||
|
|
||||||
//! Specify the format used to write double values.
|
//! Specify the format used to write double values.
|
||||||
static void setDoubleFormat(const char* f) { _doubleFormat = f; }
|
static void setDoubleFormat(const char* f) { _doubleFormat = f; }
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Clean up
|
// Clean up
|
||||||
void invalidate();
|
void invalidate();
|
||||||
|
|
||||||
// Type checking
|
// Type checking
|
||||||
void assertTypeOrInvalid(Type t);
|
void assertTypeOrInvalid(Type t);
|
||||||
void assertArray(int size) const;
|
void assertArray(int size) const;
|
||||||
void assertArray(int size);
|
void assertArray(int size);
|
||||||
void assertStruct();
|
void assertStruct();
|
||||||
|
|
||||||
// XML decoding
|
// XML decoding
|
||||||
bool boolFromXml(std::string const& valueXml, int* offset);
|
bool boolFromXml(std::string const& valueXml, int* offset);
|
||||||
bool intFromXml(std::string const& valueXml, int* offset);
|
bool intFromXml(std::string const& valueXml, int* offset);
|
||||||
bool doubleFromXml(std::string const& valueXml, int* offset);
|
bool doubleFromXml(std::string const& valueXml, int* offset);
|
||||||
bool stringFromXml(std::string const& valueXml, int* offset);
|
bool stringFromXml(std::string const& valueXml, int* offset);
|
||||||
bool timeFromXml(std::string const& valueXml, int* offset);
|
bool timeFromXml(std::string const& valueXml, int* offset);
|
||||||
bool binaryFromXml(std::string const& valueXml, int* offset);
|
bool binaryFromXml(std::string const& valueXml, int* offset);
|
||||||
bool arrayFromXml(std::string const& valueXml, int* offset);
|
bool arrayFromXml(std::string const& valueXml, int* offset);
|
||||||
bool structFromXml(std::string const& valueXml, int* offset);
|
bool structFromXml(std::string const& valueXml, int* offset);
|
||||||
|
|
||||||
// XML encoding
|
// XML encoding
|
||||||
std::string boolToXml() const;
|
std::string boolToXml() const;
|
||||||
std::string intToXml() const;
|
std::string intToXml() const;
|
||||||
std::string doubleToXml() const;
|
std::string doubleToXml() const;
|
||||||
std::string stringToXml() const;
|
std::string stringToXml() const;
|
||||||
std::string timeToXml() const;
|
std::string timeToXml() const;
|
||||||
std::string binaryToXml() const;
|
std::string binaryToXml() const;
|
||||||
std::string arrayToXml() const;
|
std::string arrayToXml() const;
|
||||||
std::string structToXml() const;
|
std::string structToXml() const;
|
||||||
|
|
||||||
// Format strings
|
// Format strings
|
||||||
static std::string _doubleFormat;
|
static std::string _doubleFormat;
|
||||||
|
|
||||||
// Type tag and values
|
// Type tag and values
|
||||||
Type _type;
|
Type _type;
|
||||||
|
|
||||||
// At some point I will split off Arrays and Structs into
|
// At some point I will split off Arrays and Structs into
|
||||||
// separate ref-counted objects for more efficient copying.
|
// separate ref-counted objects for more efficient copying.
|
||||||
union {
|
union {
|
||||||
bool asBool;
|
bool asBool;
|
||||||
int asInt;
|
int asInt;
|
||||||
double asDouble;
|
double asDouble;
|
||||||
struct tm* asTime;
|
struct tm* asTime;
|
||||||
std::string* asString;
|
std::string* asString;
|
||||||
BinaryData* asBinary;
|
BinaryData* asBinary;
|
||||||
ValueArray* asArray;
|
ValueArray* asArray;
|
||||||
ValueStruct* asStruct;
|
ValueStruct* asStruct;
|
||||||
} _value;
|
} _value;
|
||||||
|
|
||||||
};
|
};
|
||||||
} // namespace robot_xmlrpcpp
|
} // namespace robot_xmlrpcpp
|
||||||
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, robot_xmlrpcpp::XmlRpcValue& v);
|
std::ostream& operator<<(std::ostream& os, robot_xmlrpcpp::XmlRpcValue& v);
|
||||||
|
|
||||||
#endif // _ROBOT_XMLRPCVALUE_H_
|
#endif // _ROBOT_XMLRPCVALUE_H_
|
||||||
|
|||||||
1234
src/XmlRpcValue.cpp
1234
src/XmlRpcValue.cpp
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user