SEGS  0.6.1-develop [d2cac982]
Super Entity Game Server
serialization_common.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2019 SEGS Team (see AUTHORS.md)
5  * This software is licensed under the terms of the 3-clause BSD License. See LICENSE.md for details.
6  */
7 
8 #pragma once
9 #include <cereal/archives/json.hpp>
10 #include <cereal/archives/binary.hpp>
11 #include <cereal/types/vector.hpp>
12 #include <cereal/types/deque.hpp>
13 #include <cereal/types/array.hpp>
14 #include <cereal/types/string.hpp>
15 #include <cereal/cereal.hpp>
16 
17 #include <QtCore/QString>
18 #include <QtCore/QDir>
19 #include <QtCore/QFile>
20 #include <QtCore/QDebug>
21 
22 template<class T>
23 void commonSaveTo(const T & target, const char *classname, const QString & baseName, bool text_format)
24 {
25  QString target_fname;
26  if(text_format)
27  target_fname = baseName + ".crl.json";
28  else
29  target_fname = baseName + ".crl.bin";
30  std::ostringstream tgt;
31  QFile tgt_fle(target_fname);
32  try
33  {
34  if(text_format) {
35  cereal::JSONOutputArchive ar( tgt );
36  ar(cereal::make_nvp(classname,target));
37  if(!tgt_fle.open(QFile::WriteOnly|QFile::Text)) {
38  qCritical() << "Failed to open"<<target_fname<<"in write mode";
39  return;
40  }
41  }
42  else {
43  cereal::BinaryOutputArchive ar( tgt );
44  ar(cereal::make_nvp(classname,target));
45  if(!tgt_fle.open(QFile::WriteOnly)) {
46  qCritical() << "Failed to open"<<target_fname<<"in write mode";
47  return;
48  }
49  }
50  tgt_fle.write(tgt.str().c_str(),tgt.str().size());
51  }
52  catch(cereal::RapidJSONException &e)
53  {
54  qWarning() << e.what();
55  }
56  catch(std::exception &e)
57  {
58  qCritical() << e.what();
59  }
60 }
61 
62 template<class T>
63 bool commonReadFrom(const QString &crl_path,const char *classname, T &target)
64 {
65  QFile ifl(crl_path);
66  if(crl_path.endsWith("json") || crl_path.endsWith("crl_json"))
67  {
68  if(!ifl.open(QFile::ReadOnly|QFile::Text))
69  {
70  qWarning() << "Failed to open" << crl_path;
71  return false;
72  }
73 
74  std::istringstream istr(ifl.readAll().toStdString());
75 
76  try
77  {
78  cereal::JSONInputArchive arc(istr);
79  arc(cereal::make_nvp(classname,target));
80  }
81  catch(cereal::RapidJSONException &e)
82  {
83  qWarning() << e.what();
84  }
85  catch (std::exception &e)
86  {
87  qCritical() << e.what();
88  }
89  }
90  else if(crl_path.endsWith(".crl.bin"))
91  {
92  if(!ifl.open(QFile::ReadOnly))
93  {
94  qWarning() << "Failed to open" << crl_path;
95  return false;
96  }
97  std::istringstream istr(ifl.readAll().toStdString());
98 
99  try
100  {
101  cereal::BinaryInputArchive arc(istr);
102  arc(cereal::make_nvp(classname,target));
103  }
104  catch(cereal::RapidJSONException &e)
105  {
106  qWarning() << e.what();
107  }
108  catch (std::exception &e)
109  {
110  qCritical() << e.what();
111  }
112  }
113  else {
114  qWarning() << "Invalid serialized data extension in" <<crl_path;
115  }
116  return true;
117 }
118 
119 template<class T>
120 void serializeToQString(const T &data, QString &tgt)
121 {
122  std::ostringstream ostr;
123  {
124  cereal::JSONOutputArchive ar(ostr);
125  ar(data);
126  }
127  tgt = QString::fromStdString(ostr.str());
128 }
129 
130 template<class T>
131 void serializeFromQString(T &data,const QString &src)
132 {
133  if(src.isEmpty())
134  return;
135  std::istringstream istr;
136  istr.str(src.toStdString());
137  {
138  cereal::JSONInputArchive ar(istr);
139  ar(data);
140  }
141 }
142 #define SPECIALIZE_SERIALIZATIONS(type)\
143 template \
144 void type::serialize<cereal::JSONOutputArchive>(cereal::JSONOutputArchive & archive);\
145 template \
146 void type::serialize<cereal::JSONInputArchive>(cereal::JSONInputArchive & archive);\
147 template \
148 void type::serialize<cereal::BinaryInputArchive>(cereal::BinaryInputArchive & archive);\
149 template \
150 void type::serialize<cereal::BinaryOutputArchive>(cereal::BinaryOutputArchive & archive);
151 #define SPECIALIZE_VERSIONED_SERIALIZATIONS(type)\
152 template \
153 void serialize<cereal::JSONOutputArchive>(cereal::JSONOutputArchive & archive, type & m, uint32_t const version);\
154 template \
155 void serialize<cereal::JSONInputArchive>(cereal::JSONInputArchive & archive, type & m, uint32_t const version);\
156 template \
157 void serialize<cereal::BinaryInputArchive>(cereal::BinaryInputArchive & archive, type & m, uint32_t const version);\
158 template \
159 void serialize<cereal::BinaryOutputArchive>(cereal::BinaryOutputArchive & archive, type & m, uint32_t const version);
160 #define SPECIALIZE_CLASS_VERSIONED_SERIALIZATIONS(type)\
161 template \
162 void type::serialize<cereal::JSONOutputArchive>(cereal::JSONOutputArchive & archive, uint32_t const version);\
163 template \
164 void type::serialize<cereal::JSONInputArchive>(cereal::JSONInputArchive & archive, uint32_t const version);\
165 template \
166 void type::serialize<cereal::BinaryInputArchive>(cereal::BinaryInputArchive & archive, uint32_t const version);\
167 template \
168 void type::serialize<cereal::BinaryOutputArchive>(cereal::BinaryOutputArchive & archive, uint32_t const version);
169 
170 #define SPECIALIZE_SPLIT_SERIALIZATIONS(type)\
171 template \
172 void CEREAL_SAVE_FUNCTION_NAME<cereal::JSONOutputArchive>(cereal::JSONOutputArchive & archive, const type & m);\
173 template \
174 void CEREAL_SAVE_FUNCTION_NAME<cereal::BinaryOutputArchive>(cereal::BinaryOutputArchive & archive, const type & m);\
175 template \
176 void CEREAL_LOAD_FUNCTION_NAME<cereal::JSONInputArchive>(cereal::JSONInputArchive & archive, type & m);\
177 template \
178 void CEREAL_LOAD_FUNCTION_NAME<cereal::BinaryInputArchive>(cereal::BinaryInputArchive & archive, type & m);
179 
void serializeToQString(const T &data, QString &tgt)
Definition: serialization_common.h:120
bool commonReadFrom(const QString &crl_path, const char *classname, T &target)
Definition: serialization_common.h:63
void serializeFromQString(T &data, const QString &src)
Definition: serialization_common.h:131
void commonSaveTo(const T &target, const char *classname, const QString &baseName, bool text_format)
Definition: serialization_common.h:23