15 #ifndef SAMPLES_SERIALIZER_DEF_SERIALIZER_H_ // NOLINT 16 #define SAMPLES_SERIALIZER_DEF_SERIALIZER_H_ 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;
31 template <
class Archive,
class T>
32 void serialize(Archive& ar, T& x) {
37 class SimpleSerializer {
42 ~SimpleSerializer() {}
46 void operator&(
const T& x) {
48 ::serializer::serialize(*
this, x);
53 void operator<<(
const T& x);
56 bool IsSuccess()
const {
return !!*writer_; }
60 void serialize(
const T& x);
62 template <
class T, std::
size_t N>
63 void serialize(
const T (&seq)[N]);
65 template <
class T,
class Al>
66 void serialize(
const std::vector<T, Al>& seq);
68 template <
class Key,
class Value,
class Pred,
class Al>
69 void serialize(
const std::map<Key, Value, Pred, Al>& m);
73 void operator&(
const T* );
75 template <
class Archive,
class T>
76 friend void serializer::serialize(Archive& ar, T& x);
80 void SimpleSerializer::operator<<(
const T& x) {
81 writer_->WriteStartDocument();
82 ::serializer::serialize(*
this, x);
83 writer_->WriteEndDocument();
88 void SimpleSerializer::serialize(
const T& x) {
89 writer_->WriteStartElement(N(
"o"));
90 const_cast<T&
>(x).serialize(*
this);
91 writer_->WriteEndElement();
96 void SimpleSerializer::serialize(
const StdString& x);
100 void SimpleSerializer::serialize(
const int& x);
104 void SimpleSerializer::serialize(
const float& x);
107 template <
class T, std::
size_t N>
108 void SimpleSerializer::serialize(
const T (&seq)[N]) {
109 writer_->WriteStartElement(N(
"v"));
111 for (i = 0; i < N; ++i) {
114 writer_->WriteEndElement();
118 template <
class T,
class Al>
119 void SimpleSerializer::serialize(
const std::vector<T, Al>& seq) {
120 writer_->WriteStartElement(N(
"v"));
122 std::size_t size = seq.size();
123 for (i = 0; i < size; ++i) {
126 writer_->WriteEndElement();
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) {
137 (*this) & it->second;
139 writer_->WriteEndElement();
142 class SimpleDeserializer {
143 nlib_ns::exi::XmlStreamReader::XmlStreamConstants cache_;
150 ~SimpleDeserializer() {}
154 void operator&(T& x) {
156 ::serializer::serialize(*
this, x);
160 void operator>>(T& x);
162 bool IsSuccess()
const {
return !!*reader_ && !is_failed_; }
166 void serialize(T& x);
168 template <
class T, std::
size_t N>
169 void serialize(T (&seq)[N]);
171 template <
class T,
class Al>
172 void serialize(std::vector<T, Al>& seq);
174 template <
class Key,
class Value,
class Pred,
class Al>
175 void serialize(std::map<Key, Value, Pred, Al>& m);
177 void Next(nlib_ns::exi::XmlStreamReader::XmlStreamConstants expect);
178 bool Peek(nlib_ns::exi::XmlStreamReader::XmlStreamConstants expect);
181 void operator&(
const T* );
183 template <
class Archive,
class T>
184 friend void serializer::serialize(Archive& ar, T& x);
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);
196 void SimpleDeserializer::serialize(T& x) {
203 void SimpleDeserializer::serialize(StdString& x);
206 void SimpleDeserializer::serialize(
int& x);
209 void SimpleDeserializer::serialize(
float& x);
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);
216 for (i = 0; i < N; ++i) {
219 this->Next(XmlStreamReader::END_ELEMENT);
222 template <
class T,
class Al>
223 void SimpleDeserializer::serialize(std::vector<T, Al>& seq) {
224 using nlib_ns::exi::XmlStreamReader;
225 this->Next(XmlStreamReader::START_ELEMENT);
227 while (this->Peek(XmlStreamReader::START_ELEMENT)) {
232 this->Next(XmlStreamReader::END_ELEMENT);
235 template <
class Key,
class Value,
class Pred,
class Al>
236 void SimpleDeserializer::serialize(std::map<Key, Value, Pred, Al>& m) {
237 using nlib_ns::exi::XmlStreamReader;
238 this->Next(XmlStreamReader::START_ELEMENT);
240 while (this->Peek(XmlStreamReader::START_ELEMENT)) {
247 this->Next(XmlStreamReader::END_ELEMENT);
250 #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.