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