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


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley
autogenerated on Wed Mar 21 2018 07:13:24