nlib
def_serializer.h
1 
2 #ifndef SAMPLES_SERIALIZER_DEF_SERIALIZER_H_ // NOLINT
3 #define SAMPLES_SERIALIZER_DEF_SERIALIZER_H_
4 
5 #include <map>
6 #include <string>
7 #include <vector>
8 
9 #include "nn/nlib/exi/exi.h"
10 #include "nn/nlib/UniquePtr.h"
11 
12 #define N(x) NLIB_EXI_LITERAL(x)
13 #define M(x) NLIB_EXI_UTF8(x)
14 typedef std::basic_string< ::nlib_ns::exi::ExiChar> StdString;
15 
16 namespace serializer {
17 // Non-Intrusive style is possible if you define serialize() in serializer namespace.
18 template <class Archive, class T>
19 void serialize(Archive& ar, T& x) { // NOLINT
20  ar.serialize(x);
21 }
22 } // namespace serializer
23 
24 class SimpleSerializer {
26 
27  public:
28  explicit SimpleSerializer(nlib_ns::OutputStream* os);
29  ~SimpleSerializer() {}
30 
31  public:
32  template <class T>
33  void operator&(const T& x) {
34  // Calls serialize() member function via ::serializer::serialize().
35  ::serializer::serialize(*this, x);
36  }
37 
38  // Writes the object in binary XML.
39  template <class T>
40  void operator<<(const T& x);
41 
42  // Is serialization successful?
43  bool IsSuccess() const { return m_Writer->IsOk(); }
44 
45  private:
46  template <class T>
47  void serialize(const T& x);
48 
49  template <class T, std::size_t N>
50  void serialize(const T (&seq)[N]);
51 
52  template <class T, class Al>
53  void serialize(const std::vector<T, Al>& seq);
54 
55  template <class Key, class Value, class Pred, class Al>
56  void serialize(const std::map<Key, Value, Pred, Al>& m);
57 
58  private:
59  template <class T>
60  void operator&(const T* /* x */);
61 
62  template <class Archive, class T>
63  friend void serializer::serialize(Archive& ar, T& x); // NOLINT
64 };
65 
66 template <class T>
67 void SimpleSerializer::operator<<(const T& x) {
68  m_Writer->WriteStartDocument();
69  ::serializer::serialize(*this, x);
70  m_Writer->WriteEndDocument();
71  m_Writer->Flush();
72 }
73 
74 template <class T>
75 void SimpleSerializer::serialize(const T& x) {
76  m_Writer->WriteStartElement(N("o"));
77  const_cast<T&>(x).serialize(*this);
78  m_Writer->WriteEndElement();
79 }
80 
81 // Writes <s>...</s>
82 template <>
83 void SimpleSerializer::serialize(const StdString& x);
84 
85 // Writes <i>...</i>
86 template <>
87 void SimpleSerializer::serialize(const int& x);
88 
89 // Writes <f>...</f>
90 template <>
91 void SimpleSerializer::serialize(const float& x);
92 
93 // Writes <v>...</v>
94 template <class T, std::size_t N>
95 void SimpleSerializer::serialize(const T (&seq)[N]) {
96  m_Writer->WriteStartElement(N("v"));
97  std::size_t i;
98  for (i = 0; i < N; ++i) {
99  (*this) & seq[i];
100  }
101  m_Writer->WriteEndElement();
102 }
103 
104 // Writes <v>...</v>
105 template <class T, class Al>
106 void SimpleSerializer::serialize(const std::vector<T, Al>& seq) {
107  m_Writer->WriteStartElement(N("v"));
108  std::size_t i;
109  std::size_t size = seq.size();
110  for (i = 0; i < size; ++i) {
111  (*this) & seq[i];
112  }
113  m_Writer->WriteEndElement();
114 }
115 
116 // Writes <m>...</m>
117 template <class Key, class Value, class Pred, class Al>
118 void SimpleSerializer::serialize(const std::map<Key, Value, Pred, Al>& m) {
119  m_Writer->WriteStartElement(N("m"));
120  typename std::map<Key, Value, Pred, Al>::const_iterator it;
121  typename std::map<Key, Value, Pred, Al>::const_iterator itend = m.end();
122  for (it = m.begin(); it != itend; ++it) {
123  (*this) & it->first;
124  (*this) & it->second;
125  }
126  m_Writer->WriteEndElement();
127 }
128 
129 class SimpleDeserializer {
130  nlib_ns::exi::XmlStreamReader::XmlStreamConstants m_Cache;
132  bool m_Failed;
133  char _[3];
134 
135  public:
136  explicit SimpleDeserializer(nlib_ns::InputStream* is);
137  ~SimpleDeserializer() {}
138 
139  public:
140  template <class T>
141  void operator&(T& x) { // NOLINT
142  // Calls serialize() member function via ::serializer::serialize().
143  ::serializer::serialize(*this, x);
144  }
145 
146  template <class T>
147  void operator>>(T& x);
148 
149  bool IsSuccess() const { return m_Reader->IsOk() && !m_Failed; }
150 
151  private:
152  template <class T>
153  void serialize(T& x); // NOLINT
154 
155  template <class T, std::size_t N>
156  void serialize(T (&seq)[N]);
157 
158  template <class T, class Al>
159  void serialize(std::vector<T, Al>& seq); // NOLINT
160 
161  template <class Key, class Value, class Pred, class Al>
162  void serialize(std::map<Key, Value, Pred, Al>& m); // NOLINT
163 
164  void Next(nlib_ns::exi::XmlStreamReader::XmlStreamConstants expect);
165  bool Peek(nlib_ns::exi::XmlStreamReader::XmlStreamConstants expect);
166 
167  template <class T>
168  void operator&(const T* /* x */);
169 
170  template <class Archive, class T>
171  friend void serializer::serialize(Archive& ar, T& x); // NOLINT
172 };
173 
174 template <class T>
175 void SimpleDeserializer::operator>>(T& x) {
176  using nlib_ns::exi::XmlStreamReader;
177  this->Next(XmlStreamReader::START_DOCUMENT);
178  ::serializer::serialize(*this, x);
179  this->Next(XmlStreamReader::END_DOCUMENT);
180 }
181 
182 template <class T>
183 void SimpleDeserializer::serialize(T& x) { // NOLINT
184  m_Reader->Next(); // StartElement("o")
185  x.serialize(*this);
186  m_Reader->Next(); // EndElement("o");
187 }
188 
189 template <>
190 void SimpleDeserializer::serialize(StdString& x); // NOLINT
191 
192 template <>
193 void SimpleDeserializer::serialize(int& x); // NOLINT
194 
195 template <>
196 void SimpleDeserializer::serialize(float& x); // NOLINT
197 
198 template <class T, std::size_t N>
199 void SimpleDeserializer::serialize(T (&seq)[N]) {
200  using nlib_ns::exi::XmlStreamReader;
201  this->Next(XmlStreamReader::START_ELEMENT); // v
202  std::size_t i;
203  for (i = 0; i < N; ++i) {
204  (*this) & seq[i];
205  }
206  this->Next(XmlStreamReader::END_ELEMENT); // v
207 }
208 
209 template <class T, class Al>
210 void SimpleDeserializer::serialize(std::vector<T, Al>& seq) { // NOLINT
211  using nlib_ns::exi::XmlStreamReader;
212  this->Next(XmlStreamReader::START_ELEMENT); // v
213  seq.clear();
214  while (this->Peek(XmlStreamReader::START_ELEMENT)) {
215  T x;
216  (*this) & x;
217  seq.push_back(x);
218  }
219  this->Next(XmlStreamReader::END_ELEMENT); // v
220 }
221 
222 template <class Key, class Value, class Pred, class Al>
223 void SimpleDeserializer::serialize(std::map<Key, Value, Pred, Al>& m) { // NOLINT
224  using nlib_ns::exi::XmlStreamReader;
225  this->Next(XmlStreamReader::START_ELEMENT); // m
226  m.clear();
227  while (this->Peek(XmlStreamReader::START_ELEMENT)) {
228  Key key;
229  Value value;
230  (*this) & key;
231  (*this) & value;
232  m[key] = value;
233  }
234  this->Next(XmlStreamReader::END_ELEMENT); // m
235 }
236 
237 #endif // SAMPLES_SERIALIZER_DEF_SERIALIZER_H_ // NOLINT
std::unique_ptrに相当するクラスが定義されています。
入力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: InputStream.h:15
ディレクトリnn/nlib/exi内のヘッダを全てインクルードするためのヘッダです。
出力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: OutputStream.h:17