我有以下形式的代码:
ODIstream &operator<<(ODIstream& vis, const Song &lyrics)
{
vis >> doh;
vis >> a;
vis >> deer;
vis >> a;
vis >> female;
vis >> deer;
}
其中ODIStream是旧的已弃用的类库的序列化函数,而female是来自已弃用的类库的容器。它们在相互构建时协同工作。我的任务是更新此代码并删除此库,因此我们开始:
istream &operator<<(istream& vis, const Song &lyrics)
{
vis >> doh;
vis >> a;
vis >> deer;
vis >> a;
vis >> female;
vis >> deer;
}
但是女性不能使用ostream。当我将女性改为stl :: list时,没有构建序列化运算符(各个类元素都有)。
我打算使用boost:serialisation code,但我不确定如何将存档与当前代码模型集成。
有没有人这样做过?
答案 0 :(得分:1)
请注意,序列化的目标与流式传输不同。
序列化会产生存档。因此,您不会写信ostream
或从istream
读取。相反,您将写入oarchive
(文本,二进制,xml)或从相应的iarchive
读取。
每个档案将携带(相当多)档案标题。因此,将存档分解为流媒体运营商内部的细节似乎是一个坏主意,快速演示:
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/serialization.hpp>
struct Simplest {
int i;
template <typename Archive> void serialize(Archive& ar, unsigned /*version*/) {
ar & i;
}
friend std::ostream& operator<<(std::ostream& os, Simplest const& data) {
boost::archive::text_oarchive oa(os);
oa << data;
return os;
}
};
int main() {
Simplest a { 4215680 }, b { -42 };
std::cout << a << b;
}
这会导致
22 serialization::archive 10 0 0 4215680
22 serialization::archive 10 0 0 -42
所有只是为了序列化...... 2个整数。
此外,Boost Serialization旨在处理存档错误。但是,如果读取('解析')失败,通常需要输入流操作符离开输入位置,并且必须小心将流状态保持在适当的状态,以便仍然可以使用流。
我建议采用以下两种方法之一:
想象一下示范struct
struct Demo
{
int i;
std::string truth;
using Vars = std::map<std::string, double>;
Vars vars;
};
使用简单的序列化实现和(以及帮助程序只进行调试打印):
int main()
{
Demo a { 42, "LtUaE", { { "PI", 3.1415926 }, { "e", std::exp(1.0) } } };
std::cout << "Debug : " << a << "\n";
std::string const serialized = serialize(a);
std::cout << "Serious serialization: " << serialized << "\n";
// to parse back:
Demo roundtrip = deserialize(serialized);
std::cout << "Parsed back: " << roundtrip << "\n";
}
我们得到了
Debug : 42;LtUaE;PI;3.14159;e;2.71828;
Serious serialization: 22 serialization::archive 10 0 0 42 5 LtUaE 0 0 2 0 0 0 2 PI 3.1415926000000001 1 e 2.7182818284590451
Parsed back: 42;LtUaE;PI;3.14159;e;2.71828;
这里的一大胜利是很容易获得二进制流:Live On Coliru too:
#include <map>
#include <sstream>
#include <iomanip>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/map.hpp>
struct Demo
{
int i;
std::string truth;
using Vars = std::map<std::string, double>;
Vars vars;
template <typename Archive> void serialize(Archive& ar, unsigned /*version*/)
{
ar & i;
ar & truth;
ar & vars;
}
friend std::ostream& operator<<(std::ostream& os, Demo const& demo)
{
os << demo.i << ';' << demo.truth << ";";
for (auto& e : demo.vars)
os << e.first << ";" << e.second << ";";
return os;
}
};
static std::string as_hex(std::string const& binary)
{
std::ostringstream oss;
for (unsigned ch: binary)
oss << std::setw(2) << std::setfill('0') << std::hex << ch;
return oss.str();
}
static std::string serialize(Demo const& data)
{
std::ostringstream oss;
boost::archive::binary_oarchive oa(oss);
oa << data;
return oss.str();
}
static Demo deserialize(std::string const& text)
{
std::istringstream iss(text);
boost::archive::binary_iarchive ia(iss);
Demo data;
ia >> data;
return data;
}
int main()
{
Demo a { 42, "LtUaE", { { "PI", 3.1415926 }, { "e", std::exp(1.0) } } };
std::cout << "Debug : " << a << "\n";
std::string const serialized = serialize(a);
std::cout << "Serious serialization: " << as_hex(serialized) << "\n";
// to parse back:
Demo roundtrip = deserialize(serialized);
std::cout << "Parsed back: " << roundtrip << "\n";
}
使用相同的Demo结构和
int main()
{
Demo a { 42, "LtUaE", { { "PI", 3.1415926 }, { "e", std::exp(1.0) } } };
std::cout << "Quick serialization: " << karma::format_delimited(karma::auto_, ';', a) << "\n";
std::string const serialized = serialize(a);
std::cout << "Serious serialization: " << serialized << "\n";
// to parse back:
Demo roundtrip = deserialize(serialized);
std::cout << "Parsed back: " << karma::format_delimited(karma::auto_, ';', roundtrip) << "\n";
}
打印:
Quick serialization: 42;LtUaE;PI;3.142;e;2.718;
Serious serialization: Demo{42;LtUaE;{{PI: 3.142}, {e: 2.718}}}
Parsed back: 42;LtUaE;PI;3.142;e;2.718;
#include <map>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;
struct Demo
{
int i;
std::string truth;
using Vars = std::map<std::string, double>;
Vars vars;
};
BOOST_FUSION_ADAPT_STRUCT(Demo, (int,i)(std::string,truth)(Demo::Vars, vars))
static std::string serialize(Demo const& data)
{
std::ostringstream oss;
oss << karma::format(
"Demo{" << karma::int_ << ';' << karma::string << ';'
<< '{'
<< ('{' << karma::string << ": " << karma::double_ << '}') % ", "
<< '}'
<< '}', data);
return oss.str();
}
static Demo deserialize(std::string const& text)
{
auto f(text.begin()), l(text.end());
Demo parsed;
if (qi::parse(f, l,
"Demo{" >> qi::int_ >> ';' >> +~qi::char_(';') >> ';'
>> '{'
>> ('{' >> +~qi::char_(':') >> ": " >> qi::double_ >> '}') % ", " >> '}'
>> '}', parsed))
{
return parsed;
}
throw std::runtime_error("Parse failed at '" + std::string(f,l) + "'");
}
int main()
{
Demo a { 42, "LtUaE", { { "PI", 3.1415926 }, { "e", std::exp(1.0) } } };
std::cout << "Quick serialization: " << karma::format_delimited(karma::auto_, ';', a) << "\n";
std::string const serialized = serialize(a);
std::cout << "Serious serialization: " << serialized << "\n";
// to parse back:
Demo roundtrip = deserialize(serialized);
std::cout << "Parsed back: " << karma::format_delimited(karma::auto_, ';', roundtrip) << "\n";
}
按照快速获胜的方式使用Boost序列化;你会得到
Spirit的好处是: