OverSim
XmlRpcValue.h
Go to the documentation of this file.
1 
2 #ifndef _XMLRPCVALUE_H_
3 #define _XMLRPCVALUE_H_
4 //
5 // XmlRpc++ Copyright (c) 2002-2003 by Chris Morley
6 //
7 
13 #if defined(_MSC_VER)
14 # pragma warning(disable:4786) // identifier was truncated in debug info
15 #endif
16 
17 #ifndef MAKEDEPEND
18 # include <map>
19 # include <string>
20 # include <vector>
21 # include <time.h>
22 #endif
23 
24 namespace XmlRpc {
25 
30  // should probably refcount them...
31  class XmlRpcValue {
32  public:
33 
35  enum Type {
45  };
46 
47  // Non-primitive types
48  typedef std::vector<char> BinaryData;
49  typedef std::vector<XmlRpcValue> ValueArray;
50  typedef std::map<std::string, XmlRpcValue> ValueStruct;
51 
52 
53  // Constructors
55  XmlRpcValue() : _type(TypeInvalid) { _value.asBinary = 0; }
56 
58  XmlRpcValue(bool value) : _type(TypeBoolean) { _value.asBool = value; }
59 
61  XmlRpcValue(int value) : _type(TypeInt) { _value.asInt = value; }
62 
64  XmlRpcValue(double value) : _type(TypeDouble) { _value.asDouble = value; }
65 
67  XmlRpcValue(std::string const& value) : _type(TypeString)
68  { _value.asString = new std::string(value); }
69 
72  XmlRpcValue(const char* value) : _type(TypeString)
73  { _value.asString = new std::string(value); }
74 
77  XmlRpcValue(struct tm* value) : _type(TypeDateTime)
78  { _value.asTime = new struct tm(*value); }
79 
83  XmlRpcValue(void* value, int nBytes) : _type(TypeBase64)
84  {
85  _value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
86  }
87 
89  XmlRpcValue(std::string const& xml, int* offset) : _type(TypeInvalid)
90  { if ( ! fromXml(xml,offset)) _type = TypeInvalid; }
91 
93  XmlRpcValue(XmlRpcValue const& rhs) : _type(TypeInvalid) { *this = rhs; }
94 
96  /*virtual*/ ~XmlRpcValue() { invalidate(); }
97 
99  void clear() { invalidate(); }
100 
101  // Operators
104  XmlRpcValue& operator=(XmlRpcValue const& rhs);
105 
107  XmlRpcValue& operator=(int const& rhs) { return operator=(XmlRpcValue(rhs)); }
108 
110  XmlRpcValue& operator=(double const& rhs) { return operator=(XmlRpcValue(rhs)); }
111 
113  XmlRpcValue& operator=(const char* rhs) { return operator=(XmlRpcValue(std::string(rhs))); }
114 
116  bool operator==(XmlRpcValue const& other) const;
117 
119  bool operator!=(XmlRpcValue const& other) const;
120 
124  operator bool&() { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
125 
129  operator int&() { assertTypeOrInvalid(TypeInt); return _value.asInt; }
130 
134  operator double&() { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
135 
139  operator std::string&() { assertTypeOrInvalid(TypeString); return *_value.asString; }
140 
144  operator BinaryData&() { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
145 
149  operator struct tm&() { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
150 
151 
156  XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
157 
161  XmlRpcValue& operator[](int i) { assertArray(i+1); return _value.asArray->at(i); }
162 
165  XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
166 
169  XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
170 
173  operator ValueStruct const&() { assertStruct(); return *_value.asStruct; }
174 
175  // Accessors
177  bool isValid() const { return _type != TypeInvalid; }
178 
180  Type const &getType() const { return _type; }
181 
183  int size() const;
184 
186  void setSize(int size) { assertArray(size); }
187 
189  bool hasMember(const std::string& name) const;
190 
192  bool fromXml(std::string const& valueXml, int* offset);
193 
195  std::string toXml() const;
196 
198  std::ostream& write(std::ostream& os) const;
199 
200  // Formatting
202  static std::string const& getDoubleFormat() { return _doubleFormat; }
203 
205  static void setDoubleFormat(const char* f) { _doubleFormat = f; }
206 
207 
208  protected:
209  // Clean up
210  void invalidate();
211 
212  // Type checking
213  void assertTypeOrInvalid(Type t);
214  void assertArray(int size) const;
215  void assertArray(int size);
216  void assertStruct();
217 
218  // XML decoding
219  bool boolFromXml(std::string const& valueXml, int* offset);
220  bool intFromXml(std::string const& valueXml, int* offset);
221  bool doubleFromXml(std::string const& valueXml, int* offset);
222  bool stringFromXml(std::string const& valueXml, int* offset);
223  bool timeFromXml(std::string const& valueXml, int* offset);
224  bool binaryFromXml(std::string const& valueXml, int* offset);
225  bool arrayFromXml(std::string const& valueXml, int* offset);
226  bool structFromXml(std::string const& valueXml, int* offset);
227 
228  // XML encoding
229  std::string boolToXml() const;
230  std::string intToXml() const;
231  std::string doubleToXml() const;
232  std::string stringToXml() const;
233  std::string timeToXml() const;
234  std::string binaryToXml() const;
235  std::string arrayToXml() const;
236  std::string structToXml() const;
237 
238  // Format strings
239  static std::string _doubleFormat;
240 
241  // Type tag and values
243 
244  // At some point I will split off Arrays and Structs into
245  // separate ref-counted objects for more efficient copying.
246  union {
247  bool asBool;
248  int asInt;
249  double asDouble;
250  struct tm* asTime;
251  std::string* asString;
255  } _value;
256 
257  };
258 } // namespace XmlRpc
259 
260 
261 std::ostream& operator<<(std::ostream& os, XmlRpc::XmlRpcValue& v);
262 
263 
264 #endif // _XMLRPCVALUE_H_