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 #if defined(_MSC_VER)
8 # pragma warning(disable:4786) // identifier was truncated in debug info
9 #endif
10 
11 #include "xmlrpcpp/XmlRpcDecl.h"
12 
13 #ifndef MAKEDEPEND
14 # include <map>
15 # include <string>
16 # include <vector>
17 # include <time.h>
18 #endif
19 
20 namespace XmlRpc {
21 
23  // should probably refcount them...
25  public:
26 
27 
28  enum Type {
37  TypeStruct
38  };
39 
40  // Non-primitive types
41  typedef std::vector<char> BinaryData;
42  typedef std::vector<XmlRpcValue> ValueArray;
43  typedef std::map<std::string, XmlRpcValue> ValueStruct;
44  typedef ValueStruct::iterator iterator;
45  typedef ValueStruct::const_iterator const_iterator;
46 
48  XmlRpcValue() : _type(TypeInvalid) { _value.asBinary = 0; }
49  XmlRpcValue(bool value) : _type(TypeBoolean) { _value.asBool = value; }
50  XmlRpcValue(int value) : _type(TypeInt) { _value.asInt = value; }
51  XmlRpcValue(double value) : _type(TypeDouble) { _value.asDouble = value; }
52 
53  XmlRpcValue(std::string const& value) : _type(TypeString)
54  { _value.asString = new std::string(value); }
55 
56  XmlRpcValue(const char* value) : _type(TypeString)
57  { _value.asString = new std::string(value); }
58 
59  XmlRpcValue(struct tm* value) : _type(TypeDateTime)
60  { _value.asTime = new struct tm(*value); }
61 
62 
63  XmlRpcValue(void* value, int nBytes) : _type(TypeBase64)
64  {
65  _value.asBinary = new BinaryData((char*)value, ((char*)value)+nBytes);
66  }
67 
69  XmlRpcValue(std::string const& xml, int* offset) : _type(TypeInvalid)
70  { if ( ! fromXml(xml,offset)) _type = TypeInvalid; }
71 
73  XmlRpcValue(XmlRpcValue const& rhs) : _type(TypeInvalid) { *this = rhs; }
74 
76  /*virtual*/ ~XmlRpcValue() { invalidate(); }
77 
79  void clear() { invalidate(); }
80 
81  // Operators
82  XmlRpcValue& operator=(XmlRpcValue const& rhs);
83  XmlRpcValue& operator=(bool const& rhs) { return operator=(XmlRpcValue(rhs)); }
84  XmlRpcValue& operator=(int const& rhs) { return operator=(XmlRpcValue(rhs)); }
85  XmlRpcValue& operator=(double const& rhs) { return operator=(XmlRpcValue(rhs)); }
86  XmlRpcValue& operator=(const char* rhs) { return operator=(XmlRpcValue(std::string(rhs))); }
87 
88  bool operator==(XmlRpcValue const& other) const;
89  bool operator!=(XmlRpcValue const& other) const;
90 
91  operator bool&() { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
92  operator int&() { assertTypeOrInvalid(TypeInt); return _value.asInt; }
93  operator double&() { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
94  operator std::string&() { assertTypeOrInvalid(TypeString); return *_value.asString; }
95  operator BinaryData&() { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
96  operator struct tm&() { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
97 
98  XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
99  XmlRpcValue& operator[](int i) { assertArray(i+1); return _value.asArray->at(i); }
100 
101  XmlRpcValue& operator[](std::string const& k) const { assertStruct(); return (*_value.asStruct)[k]; }
102  XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
103  XmlRpcValue& operator[](const char* k) const { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
104  XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
105 
106  iterator begin() {assertStruct(); return (*_value.asStruct).begin(); }
107  iterator end() {assertStruct(); return (*_value.asStruct).end(); }
108 
109  const_iterator begin() const {assertStruct(); return (*_value.asStruct).begin(); }
110  const_iterator end() const {assertStruct(); return (*_value.asStruct).end(); }
111 
112  // Accessors
114  bool valid() const { return _type != TypeInvalid; }
115 
117  Type const &getType() const { return _type; }
118 
120  int size() const;
121 
123  void setSize(int size) { assertArray(size); }
124 
126  bool hasMember(const std::string& name) const;
127 
129  bool fromXml(std::string const& valueXml, int* offset);
130 
132  std::string toXml() const;
133 
135  std::ostream& write(std::ostream& os) const;
136 
137  // Formatting
139  static std::string const& getDoubleFormat() { return _doubleFormat; }
140 
142  static void setDoubleFormat(const char* f) { _doubleFormat = f; }
143 
144 
145  protected:
146  // Clean up
147  void invalidate();
148 
149  // Type checking
150  void assertTypeOrInvalid(Type t);
151  void assertArray(int size) const;
152  void assertArray(int size);
153  void assertStruct() const;
154  void assertStruct();
155 
156  // XML decoding
157  bool boolFromXml(std::string const& valueXml, int* offset);
158  bool intFromXml(std::string const& valueXml, int* offset);
159  bool doubleFromXml(std::string const& valueXml, int* offset);
160  bool stringFromXml(std::string const& valueXml, int* offset);
161  bool timeFromXml(std::string const& valueXml, int* offset);
162  bool binaryFromXml(std::string const& valueXml, int* offset);
163  bool arrayFromXml(std::string const& valueXml, int* offset);
164  bool structFromXml(std::string const& valueXml, int* offset);
165 
166  // XML encoding
167  std::string boolToXml() const;
168  std::string intToXml() const;
169  std::string doubleToXml() const;
170  std::string stringToXml() const;
171  std::string timeToXml() const;
172  std::string binaryToXml() const;
173  std::string arrayToXml() const;
174  std::string structToXml() const;
175 
176  // Format strings
177  static std::string _doubleFormat;
178 
179  // Type tag and values
181 
182  // At some point I will split off Arrays and Structs into
183  // separate ref-counted objects for more efficient copying.
184  union {
185  bool asBool;
186  int asInt;
187  double asDouble;
188  struct tm* asTime;
189  std::string* asString;
190  BinaryData* asBinary;
191  ValueArray* asArray;
192  ValueStruct* asStruct;
193  } _value;
194 
195  };
196 } // namespace XmlRpc
197 
198 
199 std::ostream& operator<<(std::ostream& os, const XmlRpc::XmlRpcValue& v);
200 
201 
202 #endif // _XMLRPCVALUE_H_
Definition: XmlRpc.h:39
std::vector< XmlRpcValue > ValueArray
Definition: XmlRpcValue.h:42
std::ostream & operator<<(std::ostream &os, const XmlRpc::XmlRpcValue &v)
XmlRpcValue(bool value)
Definition: XmlRpcValue.h:49
ValueStruct::iterator iterator
Definition: XmlRpcValue.h:44
RPC method arguments and results are represented by Values.
Definition: XmlRpcValue.h:24
XmlRpcValue(XmlRpcValue const &rhs)
Copy.
Definition: XmlRpcValue.h:73
XmlRpcValue const & operator[](int i) const
Definition: XmlRpcValue.h:98
XmlRpcServer s
Definition: HelloServer.cpp:11
bool valid() const
Return true if the value has been set to something.
Definition: XmlRpcValue.h:114
const_iterator end() const
Definition: XmlRpcValue.h:110
Type const & getType() const
Return the type of the value stored.
Definition: XmlRpcValue.h:117
void clear()
Erase the current value.
Definition: XmlRpcValue.h:79
XmlRpcValue & operator[](const char *k)
Definition: XmlRpcValue.h:104
~XmlRpcValue()
Destructor (make virtual if you want to subclass)
Definition: XmlRpcValue.h:76
XmlRpcValue & operator=(int const &rhs)
Definition: XmlRpcValue.h:84
XmlRpcValue()
Constructors.
Definition: XmlRpcValue.h:48
std::map< std::string, XmlRpcValue > ValueStruct
Definition: XmlRpcValue.h:43
const_iterator begin() const
Definition: XmlRpcValue.h:109
ValueStruct::const_iterator const_iterator
Definition: XmlRpcValue.h:45
void setSize(int size)
Specify the size for array values. Array values will grow beyond this size if needed.
Definition: XmlRpcValue.h:123
static void setDoubleFormat(const char *f)
Specify the format used to write double values.
Definition: XmlRpcValue.h:142
#define XMLRPCPP_DECL
Definition: XmlRpcDecl.h:52
XmlRpcValue & operator[](std::string const &k) const
Definition: XmlRpcValue.h:101
XmlRpcValue & operator=(const char *rhs)
Definition: XmlRpcValue.h:86
std::string * asString
Definition: XmlRpcValue.h:189
XmlRpcValue & operator[](const char *k) const
Definition: XmlRpcValue.h:103
XmlRpcValue & operator[](int i)
Definition: XmlRpcValue.h:99
XmlRpcValue & operator[](std::string const &k)
Definition: XmlRpcValue.h:102
XmlRpcValue(struct tm *value)
Definition: XmlRpcValue.h:59
XmlRpcValue(std::string const &value)
Definition: XmlRpcValue.h:53
XmlRpcValue(void *value, int nBytes)
Definition: XmlRpcValue.h:63
ValueArray * asArray
Definition: XmlRpcValue.h:191
XmlRpcValue(int value)
Definition: XmlRpcValue.h:50
ValueStruct * asStruct
Definition: XmlRpcValue.h:192
XmlRpcValue(const char *value)
Definition: XmlRpcValue.h:56
std::vector< char > BinaryData
Definition: XmlRpcValue.h:41
XmlRpcValue & operator=(double const &rhs)
Definition: XmlRpcValue.h:85
XmlRpcValue(std::string const &xml, int *offset)
Construct from xml, beginning at *offset chars into the string, updates offset.
Definition: XmlRpcValue.h:69
struct tm * asTime
Definition: XmlRpcValue.h:188
static std::string const & getDoubleFormat()
Return the format used to write double values.
Definition: XmlRpcValue.h:139
static std::string _doubleFormat
Definition: XmlRpcValue.h:177
BinaryData * asBinary
Definition: XmlRpcValue.h:190
XmlRpcValue(double value)
Definition: XmlRpcValue.h:51
XmlRpcValue & operator=(bool const &rhs)
Definition: XmlRpcValue.h:83


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:24