nlib
xmlrpc_value.h
1 
2 #ifndef SAMPLES_XML_RPC_XMLRPC_VALUE_H_ // NOLINT
3 #define SAMPLES_XML_RPC_XMLRPC_VALUE_H_
4 
5 #include <map>
6 #include <string>
7 #include <vector>
8 
9 #include "nn/nlib/exi/exi.h"
10 
11 #define N(x) NLIB_EXI_LITERAL(x)
12 #define M(x) NLIB_EXI_UTF8(x)
13 typedef std::basic_string< ::nlib_ns::exi::ExiChar> StdString;
14 
15 class XmlRpcArray;
16 class XmlRpcStruct;
17 
18 class XmlRpcValue {
19  public:
20  enum ValueType {
21  NONE = 0,
22  INT,
23  BOOLEAN,
24  DOUBLE,
25  STRING,
26  NIL,
27  // DATETIME and BASE64 are omitted
28  // DATETIME,
29  // BASE64
30  ARRAY,
31  STRUCT
32  };
33 
34  private:
35  union {
36  int valueInt;
37  bool valueBool;
38  double valueDouble;
39  StdString* valueString;
40  XmlRpcArray* valueArray;
41  XmlRpcStruct* valueStruct;
42  } value_holder_;
43  ValueType value_type_;
44  char _[7];
45 
46  public:
47  XmlRpcValue() {
48  value_type_ = NONE;
49  value_holder_.valueInt = 0;
50  }
51  ~XmlRpcValue() { this->Invalidate(); }
52  void SetInt(const int& rhs) {
53  this->Invalidate();
54  value_type_ = INT;
55  value_holder_.valueInt = rhs;
56  }
57  void SetBool(const bool& rhs) {
58  this->Invalidate();
59  value_type_ = BOOLEAN;
60  value_holder_.valueBool = rhs;
61  }
62  void SetDouble(const double& rhs) {
63  this->Invalidate();
64  value_type_ = DOUBLE;
65  value_holder_.valueDouble = rhs;
66  }
67  void SetString(const StdString& rhs) {
68  this->Invalidate();
69  value_type_ = STRING;
70  value_holder_.valueString = new StdString(rhs);
71  }
72  void SetString(const nlib_ns::exi::ExiChar* rhs) {
73  this->Invalidate();
74  value_type_ = STRING;
75  value_holder_.valueString = new StdString(rhs);
76  }
77  void SetArray();
78  void SetStruct();
79 
80  public:
81  ValueType GetValueType() const { return value_type_; }
82 
83  int* AsInt() { return (value_type_ == INT) ? &value_holder_.valueInt : NULL; }
84  const int* AsInt() const { return (value_type_ == INT) ? &value_holder_.valueInt : NULL; }
85 
86  bool* AsBool() { return (value_type_ == BOOLEAN) ? &value_holder_.valueBool : NULL; }
87  const bool* AsBool() const {
88  return (value_type_ == BOOLEAN) ? &value_holder_.valueBool : NULL;
89  }
90 
91  double* AsDouble() { return (value_type_ == DOUBLE) ? &value_holder_.valueDouble : NULL; }
92  const double* AsDouble() const {
93  return (value_type_ == DOUBLE) ? &value_holder_.valueDouble : NULL;
94  }
95 
96  StdString* AsString() { return (value_type_ == STRING) ? value_holder_.valueString : NULL; }
97  const StdString* AsString() const {
98  return (value_type_ == STRING) ? value_holder_.valueString : NULL;
99  }
100 
101  XmlRpcArray* AsArray() { return (value_type_ == ARRAY) ? value_holder_.valueArray : NULL; }
102  const XmlRpcArray* AsArray() const {
103  return (value_type_ == ARRAY) ? value_holder_.valueArray : NULL;
104  }
105 
106  XmlRpcStruct* AsStruct() { return (value_type_ == STRUCT) ? value_holder_.valueStruct : NULL; }
107  const XmlRpcStruct* AsStruct() const {
108  return (value_type_ == STRUCT) ? value_holder_.valueStruct : NULL;
109  }
110 
111  bool IsValid() const { return value_type_ != NONE; }
112  bool IsNil() const { return value_type_ == NIL; }
113  bool Read(nlib_ns::exi::XmlStreamReader* reader) {
114  if (nlib_ns::exi::XmlStreamReader::START_ELEMENT != reader->Next()) return false;
115  return this->Read_(reader);
116  }
117  void Write(nlib_ns::exi::XmlStreamWriter* writer) const;
118 
119  private:
120  XmlRpcValue(const XmlRpcValue& rhs); // forbidden
121  XmlRpcValue& operator=(const XmlRpcValue& rhs); // forbidden
122  void Invalidate();
123  bool Read_(nlib_ns::exi::XmlStreamReader* reader);
124 
125  friend class XmlRpcArray;
126  friend class XmlRpcStruct;
127 };
128 
129 class XmlRpcArray {
130  typedef std::vector<XmlRpcValue*> ArrayValue;
131  ArrayValue array_value_;
132 
133  public:
134  XmlRpcValue* Append() {
135  XmlRpcValue* p = new XmlRpcValue();
136  array_value_.push_back(p);
137  return p;
138  }
139  std::size_t Size() const { return array_value_.size(); }
140  XmlRpcValue* Get(std::size_t idx) {
141  return idx < array_value_.size() ? array_value_[idx] : NULL;
142  }
143  const XmlRpcValue* Get(std::size_t idx) const {
144  return idx < array_value_.size() ? array_value_[idx] : NULL;
145  }
146  XmlRpcValue* operator[](std::size_t idx) { return this->Get(idx); }
147  const XmlRpcValue* operator[](std::size_t idx) const { return this->Get(idx); }
148  bool Read(nlib_ns::exi::XmlStreamReader* reader);
149  void Write(nlib_ns::exi::XmlStreamWriter* writer) const;
150 
151  private:
152  XmlRpcArray() {}
153  ~XmlRpcArray();
154  XmlRpcArray(const XmlRpcArray& rhs); // forbidden
155  XmlRpcArray& operator=(const XmlRpcArray& rhs); // forbidden
156 
157  friend class XmlRpcValue;
158 };
159 
160 class XmlRpcStruct {
161  typedef std::map<StdString, XmlRpcValue*> StructValue;
162  StructValue struct_value_;
163 
164  public:
165  XmlRpcValue& operator[](const StdString& rhs);
166  XmlRpcValue* Insert(const StdString& rhs) { return &operator[](rhs); }
167  XmlRpcValue* Get(const StdString& rhs) const {
168  StructValue::const_iterator it = struct_value_.find(rhs);
169  return it != struct_value_.end() ? it->second : NULL;
170  }
171  bool HasMember(const StdString& rhs) const {
172  return struct_value_.end() != struct_value_.find(rhs);
173  }
174  bool Read(nlib_ns::exi::XmlStreamReader* reader);
175  void Write(nlib_ns::exi::XmlStreamWriter* writer) const;
176 
177  private:
178  XmlRpcStruct() {}
179  ~XmlRpcStruct();
180  XmlRpcStruct(const XmlRpcStruct& rhs); // forbidden
181  XmlRpcStruct& operator=(const XmlRpcStruct& rhs); // forbidden
182 
183  friend class XmlRpcValue;
184 };
185 
186 #endif // SAMPLES_XML_RPC_XMLRPC_VALUE_H_ // NOLINT
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:116
Header that includes all the headers within the nn/nlib/exi directory.
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:152
wchar_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:23