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  operator const bool&() const { assertTypeOrInvalid(TypeBoolean); return _value.asBool; }
99  operator const int&() const { assertTypeOrInvalid(TypeInt); return _value.asInt; }
100  operator const double&() const { assertTypeOrInvalid(TypeDouble); return _value.asDouble; }
101  operator const std::string&() const { assertTypeOrInvalid(TypeString); return *_value.asString; }
102  operator const BinaryData&() const { assertTypeOrInvalid(TypeBase64); return *_value.asBinary; }
103  operator const struct tm&() const { assertTypeOrInvalid(TypeDateTime); return *_value.asTime; }
104 
105  XmlRpcValue const& operator[](int i) const { assertArray(i+1); return _value.asArray->at(i); }
106  XmlRpcValue& operator[](int i) { assertArray(i+1); return _value.asArray->at(i); }
107 
108  XmlRpcValue& operator[](std::string const& k) const { assertStruct(); return (*_value.asStruct)[k]; }
109  XmlRpcValue& operator[](std::string const& k) { assertStruct(); return (*_value.asStruct)[k]; }
110  XmlRpcValue& operator[](const char* k) const { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
111  XmlRpcValue& operator[](const char* k) { assertStruct(); std::string s(k); return (*_value.asStruct)[s]; }
112 
113  iterator begin() {assertStruct(); return (*_value.asStruct).begin(); }
114  iterator end() {assertStruct(); return (*_value.asStruct).end(); }
115 
116  const_iterator begin() const {assertStruct(); return (*_value.asStruct).begin(); }
117  const_iterator end() const {assertStruct(); return (*_value.asStruct).end(); }
118 
119  // Accessors
121  bool valid() const { return _type != TypeInvalid; }
122 
124  Type const &getType() const { return _type; }
125 
127  int size() const;
128 
130  void setSize(int size) { assertArray(size); }
131 
133  bool hasMember(const std::string& name) const;
134 
136  bool fromXml(std::string const& valueXml, int* offset);
137 
139  std::string toXml() const;
140 
142  std::ostream& write(std::ostream& os) const;
143 
144  // Formatting
146  static std::string const& getDoubleFormat() { return _doubleFormat; }
147 
149  static void setDoubleFormat(const char* f) { _doubleFormat = f; }
150 
151 
152  protected:
153  // Clean up
154  void invalidate();
155 
156  // Type checking
157  void assertTypeOrInvalid(Type t) const;
158  void assertTypeOrInvalid(Type t);
159  void assertArray(int size) const;
160  void assertArray(int size);
161  void assertStruct() const;
162  void assertStruct();
163 
164  // XML decoding
165  bool boolFromXml(std::string const& valueXml, int* offset);
166  bool intFromXml(std::string const& valueXml, int* offset);
167  bool doubleFromXml(std::string const& valueXml, int* offset);
168  bool stringFromXml(std::string const& valueXml, int* offset);
169  bool timeFromXml(std::string const& valueXml, int* offset);
170  bool binaryFromXml(std::string const& valueXml, int* offset);
171  bool arrayFromXml(std::string const& valueXml, int* offset);
172  bool structFromXml(std::string const& valueXml, int* offset);
173 
174  // XML encoding
175  std::string boolToXml() const;
176  std::string intToXml() const;
177  std::string doubleToXml() const;
178  std::string stringToXml() const;
179  std::string timeToXml() const;
180  std::string binaryToXml() const;
181  std::string arrayToXml() const;
182  std::string structToXml() const;
183 
184  // Format strings
185  static std::string _doubleFormat;
186 
187  // Type tag and values
189 
190  // At some point I will split off Arrays and Structs into
191  // separate ref-counted objects for more efficient copying.
192  union {
193  bool asBool;
194  int asInt;
195  double asDouble;
196  struct tm* asTime;
197  std::string* asString;
198  BinaryData* asBinary;
199  ValueArray* asArray;
200  ValueStruct* asStruct;
201  } _value;
202 
203  };
204 } // namespace XmlRpc
205 
206 
207 XMLRPCPP_DECL std::ostream& operator<<(std::ostream& os, const XmlRpc::XmlRpcValue& v);
208 
209 
210 #endif // _XMLRPCVALUE_H_
std::vector< XmlRpcValue > ValueArray
Definition: XmlRpcValue.h:42
XmlRpcValue const & operator[](int i) const
Definition: XmlRpcValue.h:105
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
XmlRpcServer s
Definition: HelloServer.cpp:11
bool valid() const
Return true if the value has been set to something.
Definition: XmlRpcValue.h:121
bool operator==(const in6_addr a, const in6_addr b)
void clear()
Erase the current value.
Definition: XmlRpcValue.h:79
XmlRpcValue & operator[](const char *k)
Definition: XmlRpcValue.h:111
~XmlRpcValue()
Destructor (make virtual if you want to subclass)
Definition: XmlRpcValue.h:76
XmlRpcValue & operator=(int const &rhs)
Definition: XmlRpcValue.h:84
XmlRpcValue & operator[](const char *k) const
Definition: XmlRpcValue.h:110
XmlRpcValue()
Constructors.
Definition: XmlRpcValue.h:48
std::map< std::string, XmlRpcValue > ValueStruct
Definition: XmlRpcValue.h:43
Type const & getType() const
Return the type of the value stored.
Definition: XmlRpcValue.h:124
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:130
static void setDoubleFormat(const char *f)
Specify the format used to write double values.
Definition: XmlRpcValue.h:149
#define XMLRPCPP_DECL
Definition: XmlRpcDecl.h:52
const_iterator end() const
Definition: XmlRpcValue.h:117
const_iterator begin() const
Definition: XmlRpcValue.h:116
XmlRpcValue & operator=(const char *rhs)
Definition: XmlRpcValue.h:86
std::string * asString
Definition: XmlRpcValue.h:197
XmlRpcValue & operator[](int i)
Definition: XmlRpcValue.h:106
XmlRpcValue & operator[](std::string const &k)
Definition: XmlRpcValue.h:109
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:199
XmlRpcValue(int value)
Definition: XmlRpcValue.h:50
ValueStruct * asStruct
Definition: XmlRpcValue.h:200
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:196
static std::string const & getDoubleFormat()
Return the format used to write double values.
Definition: XmlRpcValue.h:146
static std::string _doubleFormat
Definition: XmlRpcValue.h:185
BinaryData * asBinary
Definition: XmlRpcValue.h:198
XmlRpcValue fromXml(std::string const &data)
XmlRpcValue(double value)
Definition: XmlRpcValue.h:51
XmlRpcValue & operator=(bool const &rhs)
Definition: XmlRpcValue.h:83
XMLRPCPP_DECL std::ostream & operator<<(std::ostream &os, const XmlRpc::XmlRpcValue &v)
XmlRpcValue & operator[](std::string const &k) const
Definition: XmlRpcValue.h:108


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:22