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;
201  } _value;
202 
203  };
204 
205  // This prevents GTest to understand all XmlRpcValues as structs and trying to print them as such.
206  // That results in assertStruct() being called an exception being thrown in GTest EXPECT* methods.
207  inline void PrintTo(const XmlRpcValue& value, ::std::ostream* os)
208  {
209  if (os)
210  *os << value.toXml();
211  }
212 
213 } // namespace XmlRpc
214 
215 
216 XMLRPCPP_DECL std::ostream& operator<<(std::ostream& os, const XmlRpc::XmlRpcValue& v);
217 
218 
219 #endif // _XMLRPCVALUE_H_
XmlRpc::XmlRpcValue::toXml
std::string toXml() const
Encode the Value in xml.
Definition: XmlRpcValue.cpp:277
XmlRpc::XmlRpcValue::asInt
int asInt
Definition: XmlRpcValue.h:194
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(XmlRpcValue const &rhs)
Copy.
Definition: XmlRpcValue.h:73
XmlRpc::PrintTo
void PrintTo(const XmlRpcValue &value, ::std::ostream *os)
Definition: XmlRpcValue.h:207
XmlRpc::XmlRpcValue::asStruct
ValueStruct * asStruct
Definition: XmlRpcValue.h:200
XmlRpc::XmlRpcValue::BinaryData
std::vector< char > BinaryData
Definition: XmlRpcValue.h:41
XmlRpc::XmlRpcValue::TypeBase64
@ TypeBase64
Definition: XmlRpcValue.h:35
XmlRpc::XmlRpcValue::operator=
XmlRpcValue & operator=(int const &rhs)
Definition: XmlRpcValue.h:84
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue()
Constructors.
Definition: XmlRpcValue.h:48
XmlRpc::XmlRpcValue::ValueStruct
std::map< std::string, XmlRpcValue > ValueStruct
Definition: XmlRpcValue.h:43
s
XmlRpcServer s
Definition: HelloServer.cpp:11
XmlRpc::XmlRpcValue::operator=
XmlRpcValue & operator=(double const &rhs)
Definition: XmlRpcValue.h:85
XmlRpc::XmlRpcValue::_doubleFormat
static std::string _doubleFormat
Definition: XmlRpcValue.h:185
time.h
XmlRpc::XmlRpcValue::const_iterator
ValueStruct::const_iterator const_iterator
Definition: XmlRpcValue.h:45
XmlRpc::XmlRpcValue::TypeInt
@ TypeInt
Definition: XmlRpcValue.h:31
XmlRpc::XmlRpcValue::TypeInvalid
@ TypeInvalid
Definition: XmlRpcValue.h:29
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(double value)
Definition: XmlRpcValue.h:51
XmlRpc
Definition: XmlRpcClient.h:20
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(void *value, int nBytes)
Definition: XmlRpcValue.h:63
XmlRpcDecl.h
XmlRpc::XmlRpcValue::clear
void clear()
Erase the current value.
Definition: XmlRpcValue.h:79
XmlRpc::XmlRpcValue::getDoubleFormat
static std::string const & getDoubleFormat()
Return the format used to write double values.
Definition: XmlRpcValue.h:146
XmlRpc::XmlRpcValue::operator=
XmlRpcValue & operator=(const char *rhs)
Definition: XmlRpcValue.h:86
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(struct tm *value)
Definition: XmlRpcValue.h:59
XmlRpc::XmlRpcValue::operator[]
XmlRpcValue & operator[](std::string const &k)
Definition: XmlRpcValue.h:109
XmlRpc::XmlRpcValue::TypeDouble
@ TypeDouble
Definition: XmlRpcValue.h:32
XmlRpc::XmlRpcValue::~XmlRpcValue
~XmlRpcValue()
Destructor (make virtual if you want to subclass)
Definition: XmlRpcValue.h:76
operator<<
XMLRPCPP_DECL std::ostream & operator<<(std::ostream &os, const XmlRpc::XmlRpcValue &v)
Definition: XmlRpcValue.cpp:693
operator==
bool operator==(const in6_addr a, const in6_addr b)
Definition: test_socket.cpp:628
XmlRpc::XmlRpcValue::operator=
XmlRpcValue & operator=(bool const &rhs)
Definition: XmlRpcValue.h:83
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(std::string const &value)
Definition: XmlRpcValue.h:53
XmlRpc::XmlRpcValue::asArray
ValueArray * asArray
Definition: XmlRpcValue.h:199
XmlRpc::XmlRpcValue::operator[]
XmlRpcValue & operator[](std::string const &k) const
Definition: XmlRpcValue.h:108
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(int value)
Definition: XmlRpcValue.h:50
XmlRpc::XmlRpcValue::TypeString
@ TypeString
Definition: XmlRpcValue.h:33
XmlRpc::XmlRpcValue::_type
Type _type
Definition: XmlRpcValue.h:188
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(const char *value)
Definition: XmlRpcValue.h:56
XmlRpc::XmlRpcValue::Type
Type
Definition: XmlRpcValue.h:28
XmlRpc::XmlRpcValue::ValueArray
std::vector< XmlRpcValue > ValueArray
Definition: XmlRpcValue.h:42
XmlRpc::XmlRpcValue::begin
const_iterator begin() const
Definition: XmlRpcValue.h:116
XmlRpc::XmlRpcValue::operator[]
XmlRpcValue const & operator[](int i) const
Definition: XmlRpcValue.h:105
XmlRpc::XmlRpcValue::asBinary
BinaryData * asBinary
Definition: XmlRpcValue.h:198
XmlRpc::XmlRpcValue::iterator
ValueStruct::iterator iterator
Definition: XmlRpcValue.h:44
XmlRpc::XmlRpcValue::asTime
struct tm * asTime
Definition: XmlRpcValue.h:196
XmlRpc::XmlRpcValue::end
iterator end()
Definition: XmlRpcValue.h:114
XmlRpc::XmlRpcValue::getType
const Type & getType() const
Return the type of the value stored.
Definition: XmlRpcValue.h:124
XmlRpc::XmlRpcValue::TypeArray
@ TypeArray
Definition: XmlRpcValue.h:36
XmlRpc::XmlRpcValue::valid
bool valid() const
Return true if the value has been set to something.
Definition: XmlRpcValue.h:121
XmlRpc::XmlRpcValue::asString
std::string * asString
Definition: XmlRpcValue.h:197
XmlRpc::XmlRpcValue::asDouble
double asDouble
Definition: XmlRpcValue.h:195
XmlRpc::XmlRpcValue::setDoubleFormat
static void setDoubleFormat(const char *f)
Specify the format used to write double values.
Definition: XmlRpcValue.h:149
XmlRpc::XmlRpcValue::setSize
void setSize(int size)
Specify the size for array values. Array values will grow beyond this size if needed.
Definition: XmlRpcValue.h:130
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(bool value)
Definition: XmlRpcValue.h:49
XMLRPCPP_DECL
#define XMLRPCPP_DECL
Definition: XmlRpcDecl.h:52
XmlRpc::XmlRpcValue::operator[]
XmlRpcValue & operator[](const char *k)
Definition: XmlRpcValue.h:111
XmlRpc::fromXml
XmlRpcValue fromXml(std::string const &data)
Definition: xmlrpcvalue_base64.cpp:22
XmlRpc::XmlRpcValue::begin
iterator begin()
Definition: XmlRpcValue.h:113
XmlRpc::XmlRpcValue::TypeBoolean
@ TypeBoolean
Definition: XmlRpcValue.h:30
XmlRpc::XmlRpcValue::TypeDateTime
@ TypeDateTime
Definition: XmlRpcValue.h:34
XmlRpc::XmlRpcValue::asBool
bool asBool
Definition: XmlRpcValue.h:193
XmlRpc::XmlRpcValue::end
const_iterator end() const
Definition: XmlRpcValue.h:117
XmlRpc::XmlRpcValue::operator[]
XmlRpcValue & operator[](const char *k) const
Definition: XmlRpcValue.h:110
XmlRpc::XmlRpcValue::XmlRpcValue
XmlRpcValue(std::string const &xml, int *offset)
Construct from xml, beginning at *offset chars into the string, updates offset.
Definition: XmlRpcValue.h:69
XmlRpc::XmlRpcValue
RPC method arguments and results are represented by Values.
Definition: XmlRpcValue.h:24
XmlRpc::XmlRpcValue::operator[]
XmlRpcValue & operator[](int i)
Definition: XmlRpcValue.h:106


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:32