nlib
xmlrpc_value.h
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #ifndef SAMPLES_XML_RPC_XMLRPC_VALUE_H_ // NOLINT
16 #define SAMPLES_XML_RPC_XMLRPC_VALUE_H_
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "nn/nlib/exi/exi.h"
23 
24 #define N(x) NLIB_EXI_LITERAL(x)
25 #define M(x) NLIB_EXI_UTF8(x)
26 typedef std::basic_string< ::nlib_ns::exi::ExiChar> StdString;
27 
28 class XmlRpcArray;
29 class XmlRpcStruct;
30 
31 class XmlRpcValue {
32  public:
33  enum ValueType {
34  NONE = 0,
35  INT,
36  BOOLEAN,
37  DOUBLE,
38  STRING,
39  NIL,
40  // DATETIME and BASE64 are omitted
41  // DATETIME,
42  // BASE64
43  ARRAY,
44  STRUCT
45  };
46 
47  private:
48  union {
49  int value_int;
50  bool value_bool;
51  double value_double;
52  StdString* value_string;
53  XmlRpcArray* value_array;
54  XmlRpcStruct* value_struct;
55  } value_holder_;
56  ValueType value_type_;
57  char _[7];
58 
59  public:
60  XmlRpcValue() {
61  value_type_ = NONE;
62  value_holder_.value_int = 0;
63  }
64  ~XmlRpcValue() { this->Invalidate(); }
65  void SetInt(const int& rhs) {
66  this->Invalidate();
67  value_type_ = INT;
68  value_holder_.value_int = rhs;
69  }
70  void SetBool(const bool& rhs) {
71  this->Invalidate();
72  value_type_ = BOOLEAN;
73  value_holder_.value_bool = rhs;
74  }
75  void SetDouble(const double& rhs) {
76  this->Invalidate();
77  value_type_ = DOUBLE;
78  value_holder_.value_double = rhs;
79  }
80  void SetString(const StdString& rhs) {
81  this->Invalidate();
82  value_type_ = STRING;
83  value_holder_.value_string = new StdString(rhs);
84  }
85  void SetString(const nlib_ns::exi::ExiChar* rhs) {
86  this->Invalidate();
87  value_type_ = STRING;
88  value_holder_.value_string = new StdString(rhs);
89  }
90  void SetArray();
91  void SetStruct();
92 
93  public:
94  ValueType GetValueType() const { return value_type_; }
95 
96  int* AsInt() { return (value_type_ == INT) ? &value_holder_.value_int : NULL; }
97  const int* AsInt() const { return (value_type_ == INT) ? &value_holder_.value_int : NULL; }
98 
99  bool* AsBool() { return (value_type_ == BOOLEAN) ? &value_holder_.value_bool : NULL; }
100  const bool* AsBool() const {
101  return (value_type_ == BOOLEAN) ? &value_holder_.value_bool : NULL;
102  }
103 
104  double* AsDouble() { return (value_type_ == DOUBLE) ? &value_holder_.value_double : NULL; }
105  const double* AsDouble() const {
106  return (value_type_ == DOUBLE) ? &value_holder_.value_double : NULL;
107  }
108 
109  StdString* AsString() { return (value_type_ == STRING) ? value_holder_.value_string : NULL; }
110  const StdString* AsString() const {
111  return (value_type_ == STRING) ? value_holder_.value_string : NULL;
112  }
113 
114  XmlRpcArray* AsArray() { return (value_type_ == ARRAY) ? value_holder_.value_array : NULL; }
115  const XmlRpcArray* AsArray() const {
116  return (value_type_ == ARRAY) ? value_holder_.value_array : NULL;
117  }
118 
119  XmlRpcStruct* AsStruct() { return (value_type_ == STRUCT) ? value_holder_.value_struct : NULL; }
120  const XmlRpcStruct* AsStruct() const {
121  return (value_type_ == STRUCT) ? value_holder_.value_struct : NULL;
122  }
123 
124  bool IsValid() const { return value_type_ != NONE; }
125  bool IsNil() const { return value_type_ == NIL; }
126  bool Read(nlib_ns::exi::XmlStreamReader* reader) {
127  if (nlib_ns::exi::XmlStreamReader::START_ELEMENT != reader->Next()) return false;
128  return this->Read_(reader);
129  }
130  void Write(nlib_ns::exi::XmlStreamWriter* writer) const;
131 
132  private:
133  XmlRpcValue(const XmlRpcValue& rhs); // forbidden
134  XmlRpcValue& operator=(const XmlRpcValue& rhs); // forbidden
135  void Invalidate();
136  bool Read_(nlib_ns::exi::XmlStreamReader* reader);
137 
138  friend class XmlRpcArray;
139  friend class XmlRpcStruct;
140 };
141 
142 class XmlRpcArray {
143  typedef std::vector<XmlRpcValue*> ArrayValue;
144  ArrayValue array_value_;
145 
146  public:
147  XmlRpcValue* Append() {
148  XmlRpcValue* p = new XmlRpcValue();
149  array_value_.push_back(p);
150  return p;
151  }
152  std::size_t Size() const { return array_value_.size(); }
153  XmlRpcValue* Get(std::size_t idx) {
154  return idx < array_value_.size() ? array_value_[idx] : NULL;
155  }
156  const XmlRpcValue* Get(std::size_t idx) const {
157  return idx < array_value_.size() ? array_value_[idx] : NULL;
158  }
159  XmlRpcValue* operator[](std::size_t idx) { return this->Get(idx); }
160  const XmlRpcValue* operator[](std::size_t idx) const { return this->Get(idx); }
161  bool Read(nlib_ns::exi::XmlStreamReader* reader);
162  void Write(nlib_ns::exi::XmlStreamWriter* writer) const;
163 
164  private:
165  XmlRpcArray() {}
166  ~XmlRpcArray();
167  XmlRpcArray(const XmlRpcArray& rhs); // forbidden
168  XmlRpcArray& operator=(const XmlRpcArray& rhs); // forbidden
169 
170  friend class XmlRpcValue;
171 };
172 
173 class XmlRpcStruct {
174  typedef std::map<StdString, XmlRpcValue*> StructValue;
175  StructValue struct_value_;
176 
177  public:
178  XmlRpcValue& operator[](const StdString& rhs);
179  XmlRpcValue* Insert(const StdString& rhs) { return &operator[](rhs); }
180  XmlRpcValue* Get(const StdString& rhs) const {
181  StructValue::const_iterator it = struct_value_.find(rhs);
182  return it != struct_value_.end() ? it->second : NULL;
183  }
184  bool HasMember(const StdString& rhs) const {
185  return struct_value_.end() != struct_value_.find(rhs);
186  }
187  bool Read(nlib_ns::exi::XmlStreamReader* reader);
188  void Write(nlib_ns::exi::XmlStreamWriter* writer) const;
189 
190  private:
191  XmlRpcStruct() {}
192  ~XmlRpcStruct();
193  XmlRpcStruct(const XmlRpcStruct& rhs); // forbidden
194  XmlRpcStruct& operator=(const XmlRpcStruct& rhs); // forbidden
195 
196  friend class XmlRpcValue;
197 };
198 
199 #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:136
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:172
nlib_utf8_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:40