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  } m_ValueHolder;
43  ValueType m_ValueType;
44  char _[7];
45 
46  public:
47  XmlRpcValue() {
48  m_ValueType = NONE;
49  m_ValueHolder.valueInt = 0;
50  }
51  ~XmlRpcValue() { this->Invalidate(); }
52  void SetInt(const int& rhs) {
53  this->Invalidate();
54  m_ValueType = INT;
55  m_ValueHolder.valueInt = rhs;
56  }
57  void SetBool(const bool& rhs) {
58  this->Invalidate();
59  m_ValueType = BOOLEAN;
60  m_ValueHolder.valueBool = rhs;
61  }
62  void SetDouble(const double& rhs) {
63  this->Invalidate();
64  m_ValueType = DOUBLE;
65  m_ValueHolder.valueDouble = rhs;
66  }
67  void SetString(const StdString& rhs) {
68  this->Invalidate();
69  m_ValueType = STRING;
70  m_ValueHolder.valueString = new StdString(rhs);
71  }
72  void SetString(const nlib_ns::exi::ExiChar* rhs) {
73  this->Invalidate();
74  m_ValueType = STRING;
75  m_ValueHolder.valueString = new StdString(rhs);
76  }
77  void SetArray();
78  void SetStruct();
79 
80  public:
81  ValueType GetValueType() const { return m_ValueType; }
82 
83  int* AsInt() { return (m_ValueType == INT) ? &m_ValueHolder.valueInt : NULL; }
84  const int* AsInt() const { return (m_ValueType == INT) ? &m_ValueHolder.valueInt : NULL; }
85 
86  bool* AsBool() { return (m_ValueType == BOOLEAN) ? &m_ValueHolder.valueBool : NULL; }
87  const bool* AsBool() const {
88  return (m_ValueType == BOOLEAN) ? &m_ValueHolder.valueBool : NULL;
89  }
90 
91  double* AsDouble() { return (m_ValueType == DOUBLE) ? &m_ValueHolder.valueDouble : NULL; }
92  const double* AsDouble() const {
93  return (m_ValueType == DOUBLE) ? &m_ValueHolder.valueDouble : NULL;
94  }
95 
96  StdString* AsString() { return (m_ValueType == STRING) ? m_ValueHolder.valueString : NULL; }
97  const StdString* AsString() const {
98  return (m_ValueType == STRING) ? m_ValueHolder.valueString : NULL;
99  }
100 
101  XmlRpcArray* AsArray() { return (m_ValueType == ARRAY) ? m_ValueHolder.valueArray : NULL; }
102  const XmlRpcArray* AsArray() const {
103  return (m_ValueType == ARRAY) ? m_ValueHolder.valueArray : NULL;
104  }
105 
106  XmlRpcStruct* AsStruct() { return (m_ValueType == STRUCT) ? m_ValueHolder.valueStruct : NULL; }
107  const XmlRpcStruct* AsStruct() const {
108  return (m_ValueType == STRUCT) ? m_ValueHolder.valueStruct : NULL;
109  }
110 
111  bool IsValid() const { return m_ValueType != NONE; }
112  bool IsNil() const { return m_ValueType == 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 m_ArrayValue;
132 
133  public:
134  XmlRpcValue* Append() {
135  XmlRpcValue* p = new XmlRpcValue();
136  m_ArrayValue.push_back(p);
137  return p;
138  }
139  std::size_t Size() const { return m_ArrayValue.size(); }
140  XmlRpcValue* Get(std::size_t idx) {
141  return idx < m_ArrayValue.size() ? m_ArrayValue[idx] : NULL;
142  }
143  const XmlRpcValue* Get(std::size_t idx) const {
144  return idx < m_ArrayValue.size() ? m_ArrayValue[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 m_StructValue;
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 = m_StructValue.find(rhs);
169  return it != m_StructValue.end() ? it->second : NULL;
170  }
171  bool HasMember(const StdString& rhs) const {
172  return m_StructValue.end() != m_StructValue.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:121
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:158
wchar_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:23