XmlRpcValue.h
Go to the documentation of this file.00001
00002 #ifndef _XMLRPCVALUE_H_
00003 #define _XMLRPCVALUE_H_
00004
00005
00006
00007
00013 #if defined(_MSC_VER)
00014 # pragma warning(disable:4786) // identifier was truncated in debug info
00015 #endif
00016
00017 #ifndef MAKEDEPEND
00018 # include <map>
00019 # include <string>
00020 # include <vector>
00021 # include <time.h>
00022 #endif
00023
00024 namespace XmlRpc {
00025
00030
00031 class XmlRpcValue {
00032 public:
00033
00035 enum Type {
00036 TypeInvalid,
00037 TypeBoolean,
00038 TypeInt,
00039 TypeDouble,
00040 TypeString,
00041 TypeDateTime,
00042 TypeBase64,
00043 TypeArray,
00044 TypeStruct
00045 };
00046
00047
00048 typedef std::vector<char> BinaryData;
00049 typedef std::vector<XmlRpcValue> ValueArray;
00050 typedef std::map<std::string, XmlRpcValue> ValueStruct;
00051
00052
00053
00055 XmlRpcValue() : _type(TypeInvalid) { _value.asBinary = 0; }
00056
00058 XmlRpcValue(bool value) : _type(TypeBoolean) { _value.asBool = value; }
00059
00061 XmlRpcValue(int value) : _type(TypeInt) { _value.asInt = value; }
00062
00064 XmlRpcValue(double value) : _type(TypeDouble) { _value.asDouble = value; }
00065
00067 XmlRpcValue(std::string const& value) : _type(TypeString)
00068 { _value.asString = new std::string(value); }
00069
00072 XmlRpcValue(const char* value) : _type(TypeString)
00073 { _value.asString = new std::string(value); }
00074
00077 XmlRpcValue(struct tm* value) : _type(TypeDateTime)
00078 { _value.asTime = new struct tm(*value); }
00079
00083 XmlRpcValue(void* value, int nBytes) : _type(TypeBase64)
00084 {
00085 _value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
00086 }
00087
00089 XmlRpcValue(std::string const& xml, int* offset) : _type(TypeInvalid)
00090 { if ( ! fromXml(xml,offset)) _type = TypeInvalid; }
00091
00093 XmlRpcValue(XmlRpcValue const& rhs) : _type(TypeInvalid) { *this = rhs; }
00094
00096 ~XmlRpcValue() { invalidate(); }
00097
00099 void clear() { invalidate(); }
00100
00101
00104 XmlRpcValue& operator=(XmlRpcValue const& rhs);
00105
00107 XmlRpcValue& operator=(int const& rhs) { return operator=(XmlRpcValue(rhs)); }
00108
00110 XmlRpcValue& operator=(double const& rhs) { return operator=(XmlRpcValue(rhs)); }
00111
00113 XmlRpcValue& operator=(const char* rhs) { return operator=(XmlRpcValue(std::string(rhs))); }
00114
00116 bool operator==(XmlRpcValue const& other) const;
00117
00119 bool operator!=(XmlRpcValue const& other) const;
00120
00124 operator bool&() { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
00125
00129 operator int&() { assertTypeOrInvalid(TypeInt); return _value.asInt; }
00130
00134 operator double&() { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
00135
00139 operator std::string&() { assertTypeOrInvalid(TypeString); return *_value.asString; }
00140
00144 operator BinaryData&() { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
00145
00149 operator struct tm&() { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
00150
00151
00156 XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
00157
00161 XmlRpcValue& operator[](int i) { assertArray(i+1); return _value.asArray->at(i); }
00162
00165 XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
00166
00169 XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
00170
00173 operator ValueStruct const&() { assertStruct(); return *_value.asStruct; }
00174
00175
00177 bool isValid() const { return _type != TypeInvalid; }
00178
00180 Type const &getType() const { return _type; }
00181
00183 int size() const;
00184
00186 void setSize(int size) { assertArray(size); }
00187
00189 bool hasMember(const std::string& name) const;
00190
00192 bool fromXml(std::string const& valueXml, int* offset);
00193
00195 std::string toXml() const;
00196
00198 std::ostream& write(std::ostream& os) const;
00199
00200
00202 static std::string const& getDoubleFormat() { return _doubleFormat; }
00203
00205 static void setDoubleFormat(const char* f) { _doubleFormat = f; }
00206
00207
00208 protected:
00209
00210 void invalidate();
00211
00212
00213 void assertTypeOrInvalid(Type t);
00214 void assertArray(int size) const;
00215 void assertArray(int size);
00216 void assertStruct();
00217
00218
00219 bool boolFromXml(std::string const& valueXml, int* offset);
00220 bool intFromXml(std::string const& valueXml, int* offset);
00221 bool doubleFromXml(std::string const& valueXml, int* offset);
00222 bool stringFromXml(std::string const& valueXml, int* offset);
00223 bool timeFromXml(std::string const& valueXml, int* offset);
00224 bool binaryFromXml(std::string const& valueXml, int* offset);
00225 bool arrayFromXml(std::string const& valueXml, int* offset);
00226 bool structFromXml(std::string const& valueXml, int* offset);
00227
00228
00229 std::string boolToXml() const;
00230 std::string intToXml() const;
00231 std::string doubleToXml() const;
00232 std::string stringToXml() const;
00233 std::string timeToXml() const;
00234 std::string binaryToXml() const;
00235 std::string arrayToXml() const;
00236 std::string structToXml() const;
00237
00238
00239 static std::string _doubleFormat;
00240
00241
00242 Type _type;
00243
00244
00245
00246 union {
00247 bool asBool;
00248 int asInt;
00249 double asDouble;
00250 struct tm* asTime;
00251 std::string* asString;
00252 BinaryData* asBinary;
00253 ValueArray* asArray;
00254 ValueStruct* asStruct;
00255 } _value;
00256
00257 };
00258 }
00259
00260
00261 std::ostream& operator<<(std::ostream& os, XmlRpc::XmlRpcValue& v);
00262
00263
00264 #endif // _XMLRPCVALUE_H_