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 !!*writer_; }
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 writer_->WriteStartDocument();
69 ::serializer::serialize(*
this, x);
70 writer_->WriteEndDocument();
75 void SimpleSerializer::serialize(
const T& x) {
76 writer_->WriteStartElement(N(
"o"));
77 const_cast<T&
>(x).serialize(*
this);
78 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 writer_->WriteStartElement(N(
"v"));
98 for (i = 0; i < N; ++i) {
101 writer_->WriteEndElement();
105 template <
class T,
class Al>
106 void SimpleSerializer::serialize(
const std::vector<T, Al>& seq) {
107 writer_->WriteStartElement(N(
"v"));
109 std::size_t size = seq.size();
110 for (i = 0; i < size; ++i) {
113 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 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 writer_->WriteEndElement();
129 class SimpleDeserializer {
130 nlib_ns::exi::XmlStreamReader::XmlStreamConstants cache_;
137 ~SimpleDeserializer() {}
141 void operator&(T& x) {
143 ::serializer::serialize(*
this, x);
147 void operator>>(T& x);
149 bool IsSuccess()
const {
return !!*reader_ && !is_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
Defines that class that is corresponding to std::unique_ptr.
Header that includes all the headers within the nn/nlib/exi directory.
The base class for output streams. This class cannot be instantiated.