SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt for details)
6  */
7 
8 #pragma once
9 #include "Colors.h"
10 #include "Logging.h"
11 #include <glm/vec3.hpp>
12 #include <glm/vec2.hpp>
13 #include <cereal/archives/json.hpp>
14 #include <cereal/archives/binary.hpp>
15 #include <cereal/types/vector.hpp>
16 #include <cereal/types/array.hpp>
17 #include <cereal/types/string.hpp>
18 #include <cereal/cereal.hpp>
19 
20 #include <QtCore/QString>
21 #include <QtCore/QFile>
22 
23 template<class T>
24 void commonSaveTo(const T & target, const char *classname, const QString & baseName, bool text_format)
25 {
26  QString target_fname;
27  if(text_format)
28  target_fname = baseName + ".crl.json";
29  else
30  target_fname = baseName + ".crl.bin";
31  std::ostringstream tgt;
32  QFile tgt_fle(target_fname);
33  if(text_format) {
34  cereal::JSONOutputArchive ar( tgt );
35  ar(cereal::make_nvp(classname,target));
36  if(!tgt_fle.open(QFile::WriteOnly|QFile::Text)) {
37  qCritical() << "Failed to open"<<target_fname<<"in write mode";
38  return;
39  }
40  }
41  else {
42  cereal::BinaryOutputArchive ar( tgt );
43  ar(cereal::make_nvp(classname,target));
44  if(!tgt_fle.open(QFile::WriteOnly)) {
45  qCritical() << "Failed to open"<<target_fname<<"in write mode";
46  return;
47  }
48  }
49  tgt_fle.write(tgt.str().c_str(),tgt.str().size());
50 }
51 
52 template<class T>
53 bool commonReadFrom(const QString &crl_path,const char *classname, T &target) {
54  QFile ifl(crl_path);
55  if(crl_path.endsWith("json") || crl_path.endsWith("crl_json")) {
56  if(!ifl.open(QFile::ReadOnly|QFile::Text))
57  {
58  qWarning() << "Failed to open" << crl_path;
59  return false;
60  }
61 
62  std::istringstream istr(ifl.readAll().toStdString());
63 
64  cereal::JSONInputArchive arc(istr);
65  arc(cereal::make_nvp(classname,target));
66  }
67  else if(crl_path.endsWith(".crl.bin"))
68  {
69  if(!ifl.open(QFile::ReadOnly))
70  {
71  qWarning() << "Failed to open" << crl_path;
72  return false;
73  }
74  std::istringstream istr(ifl.readAll().toStdString());
75 
76  cereal::BinaryInputArchive arc(istr);
77  arc(cereal::make_nvp(classname,target));
78  }
79  else {
80  qWarning() << "Invalid serialized data extension in" <<crl_path;
81  }
82  return true;
83 }
84 
85 template<class T>
86 void serializeToQString(const T &data, QString &tgt)
87 {
88  std::ostringstream ostr;
89  {
90  cereal::JSONOutputArchive ar(ostr);
91  ar(data);
92  }
93  tgt = QString::fromStdString(ostr.str());
94 }
95 
96 template<class T>
97 void serializeFromQString(T &data,const QString &src)
98 {
99  if(src.isEmpty())
100  return;
101  std::istringstream istr;
102  istr.str(src.toStdString());
103  {
104  cereal::JSONInputArchive ar(istr);
105  ar(data);
106  }
107 }
108 
109 namespace cereal {
110 inline void epilogue(BinaryOutputArchive &, QString const &) { }
111 inline void epilogue(BinaryInputArchive &, QString const &) { }
112 inline void epilogue(JSONOutputArchive &, QString const &) { }
113 inline void epilogue(JSONInputArchive &, QString const &) { }
114 
115 inline void prologue(JSONOutputArchive &, QString const &) { }
116 inline void prologue(JSONInputArchive &, QString const &) { }
117 inline void prologue(BinaryOutputArchive &, QString const &) { }
118 inline void prologue(BinaryInputArchive &, QString const &) { }
119 
120 template<class Archive> inline void CEREAL_SAVE_FUNCTION_NAME(Archive & ar, ::QString const & str)
121 {
122  ar( str.toStdString() );
123 }
124 
126 template<class Archive> inline void CEREAL_LOAD_FUNCTION_NAME(Archive & ar, ::QString & str)
127 {
128  std::string rd;
129  ar( rd );
130  str = QString::fromStdString(rd);
131 }
132 
133 template<class Archive>
134 void serialize(Archive & archive, glm::vec3 & m)
135 {
136  size_type size=3;
137  archive( make_size_tag( size ) ); // this is new
138  for( int i=0; i<3; ++i )
139  archive( m[i] );
140 }
141 
142 template<class Archive>
143 void serialize(Archive & archive, glm::vec2 & m)
144 {
145  size_type size=2;
146  archive( make_size_tag( size ) ); // this is new
147  for( int i=0; i<2; ++i )
148  archive( m[i] );
149 }
150 
151 template<class Archive>
152 void serialize(Archive & archive, RGBA & m)
153 {
154  archive(cereal::make_nvp("rgba",m.val));
155 }
156 
157 } // namespace cereal
Definition: Colors.h:62
void serializeToQString(const T &data, QString &tgt)
Definition: serialization_common.h:86
void epilogue(BinaryOutputArchive &, QString const &)
Definition: serialization_common.h:110
void serialize(Archive &archive, Parse_Origin &target)
Definition: def_serializers.cpp:91
void prologue(JSONOutputArchive &, QString const &)
Definition: serialization_common.h:115
bool commonReadFrom(const QString &crl_path, const char *classname, T &target)
Definition: serialization_common.h:53
void serializeFromQString(T &data, const QString &src)
Definition: serialization_common.h:97
void commonSaveTo(const T &target, const char *classname, const QString &baseName, bool text_format)
Definition: serialization_common.h:24
void CEREAL_SAVE_FUNCTION_NAME(Archive &ar,::QString const &str)
Definition: serialization_common.h:120
uint32_t val
Definition: Colors.h:67
archive(cereal::make_nvp("Mods", k.Mods))
void CEREAL_LOAD_FUNCTION_NAME(Archive &ar,::QString &str)
Serialization for basic_string types, if binary data is supported.
Definition: serialization_common.h:126