2 #ifndef SAMPLES_SERIALIZER_DEF_SERIALIZER_H_ // NOLINT
3 #define SAMPLES_SERIALIZER_DEF_SERIALIZER_H_
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;
18 template <
class Archive,
class T>
19 void serialize(Archive& ar, T& x) {
24 class SimpleSerializer {
29 ~SimpleSerializer() {}
33 void operator&(
const T& x) {
35 ::serializer::serialize(*
this, x);
40 void operator<<(
const T& x);
43 bool IsSuccess()
const {
return m_Writer->IsOk(); }
47 void serialize(
const T& x);
49 template <
class T, std::
size_t N>
50 void serialize(
const T (&seq)[N]);
52 template <
class T,
class Al>
53 void serialize(
const std::vector<T, Al>& seq);
55 template <
class Key,
class Value,
class Pred,
class Al>
56 void serialize(
const std::map<Key, Value, Pred, Al>& m);
60 void operator&(
const T* );
62 template <
class Archive,
class T>
63 friend void serializer::serialize(Archive& ar, T& x);
67 void SimpleSerializer::operator<<(
const T& x) {
68 m_Writer->WriteStartDocument();
69 ::serializer::serialize(*
this, x);
70 m_Writer->WriteEndDocument();
75 void SimpleSerializer::serialize(
const T& x) {
76 m_Writer->WriteStartElement(N(
"o"));
77 const_cast<T&
>(x).serialize(*
this);
78 m_Writer->WriteEndElement();
83 void SimpleSerializer::serialize(
const StdString& x);
87 void SimpleSerializer::serialize(
const int& x);
91 void SimpleSerializer::serialize(
const float& x);
94 template <
class T, std::
size_t N>
95 void SimpleSerializer::serialize(
const T (&seq)[N]) {
96 m_Writer->WriteStartElement(N(
"v"));
98 for (i = 0; i < N; ++i) {
101 m_Writer->WriteEndElement();
105 template <
class T,
class Al>
106 void SimpleSerializer::serialize(
const std::vector<T, Al>& seq) {
107 m_Writer->WriteStartElement(N(
"v"));
109 std::size_t size = seq.size();
110 for (i = 0; i < size; ++i) {
113 m_Writer->WriteEndElement();
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) {
124 (*this) & it->second;
126 m_Writer->WriteEndElement();
129 class SimpleDeserializer {
130 nlib_ns::exi::XmlStreamReader::XmlStreamConstants m_Cache;
137 ~SimpleDeserializer() {}
141 void operator&(T& x) {
143 ::serializer::serialize(*
this, x);
147 void operator>>(T& x);
149 bool IsSuccess()
const {
return m_Reader->IsOk() && !m_Failed; }
153 void serialize(T& x);
155 template <
class T, std::
size_t N>
156 void serialize(T (&seq)[N]);
158 template <
class T,
class Al>
159 void serialize(std::vector<T, Al>& seq);
161 template <
class Key,
class Value,
class Pred,
class Al>
162 void serialize(std::map<Key, Value, Pred, Al>& m);
164 void Next(nlib_ns::exi::XmlStreamReader::XmlStreamConstants expect);
165 bool Peek(nlib_ns::exi::XmlStreamReader::XmlStreamConstants expect);
168 void operator&(
const T* );
170 template <
class Archive,
class T>
171 friend void serializer::serialize(Archive& ar, T& x);
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);
183 void SimpleDeserializer::serialize(T& x) {
190 void SimpleDeserializer::serialize(StdString& x);
193 void SimpleDeserializer::serialize(
int& x);
196 void SimpleDeserializer::serialize(
float& x);
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);
203 for (i = 0; i < N; ++i) {
206 this->Next(XmlStreamReader::END_ELEMENT);
209 template <
class T,
class Al>
210 void SimpleDeserializer::serialize(std::vector<T, Al>& seq) {
211 using nlib_ns::exi::XmlStreamReader;
212 this->Next(XmlStreamReader::START_ELEMENT);
214 while (this->Peek(XmlStreamReader::START_ELEMENT)) {
219 this->Next(XmlStreamReader::END_ELEMENT);
222 template <
class Key,
class Value,
class Pred,
class Al>
223 void SimpleDeserializer::serialize(std::map<Key, Value, Pred, Al>& m) {
224 using nlib_ns::exi::XmlStreamReader;
225 this->Next(XmlStreamReader::START_ELEMENT);
227 while (this->Peek(XmlStreamReader::START_ELEMENT)) {
234 this->Next(XmlStreamReader::END_ELEMENT);
237 #endif // SAMPLES_SERIALIZER_DEF_SERIALIZER_H_ // NOLINT
std::unique_ptrに相当するクラスが定義されています。
ディレクトリnn/nlib/exi内のヘッダを全てインクルードするためのヘッダです。
出力ストリームの基底クラスです。このクラスを実体化することはできません。