C++ Boost ICL和Boost序列化的组合
我试图使用Boost序列化库来归档Boost ICL间隔集(因为我找不到任何其他或多或少的标准方法来序列化它们)。我想将C++ Boost ICL和Boost序列化的组合,c++,serialization,boost,C++,Serialization,Boost,我试图使用Boost序列化库来归档Boost ICL间隔集(因为我找不到任何其他或多或少的标准方法来序列化它们)。我想将序列化函数分为两个函数保存和加载。很抱歉,我现在被困在load功能中-我甚至无法保存间隔集的大小。 我的测试程序如下。编译器抱怨行Aiterative\u size()没有返回所需的左值。无论如何,您都无法有效地反序列化派生属性(这与向量不会(反)序列化size()成员结果的方式非常相似。相反,它会序列化所有数据,而size()最终恰好是相同的 也看看 如果没有这样的默认构造函
序列化
函数分为两个函数保存
和加载
。很抱歉,我现在被困在load
功能中-我甚至无法保存间隔集的大小。
我的测试程序如下。编译器抱怨行Aiterative\u size()
没有返回所需的左值。无论如何,您都无法有效地反序列化派生属性(这与向量不会(反)序列化size()
成员结果的方式非常相似。相反,它会序列化所有数据,而size()
最终恰好是相同的
也看看
如果没有这样的默认构造函数,则必须覆盖函数模板load\u construct\u data
和save\u construct\u data
从
在对象实例仅从(非默认)构造函数参数初始化的情况下,这可能是有益的
更新这里是一个演示实现:
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/icl/discrete_interval.hpp>
#include <boost/icl/interval_set.hpp>
namespace boost { namespace serialization {
template <typename Archive, typename V>
void save(Archive& ar, boost::icl::discrete_interval<V> const& di, unsigned) {
auto const& bb = di.bounds().bits();
auto const& l = di.lower();
auto const& u = di.upper();
ar << bb << l << u;
}
template <typename Archive, typename V>
void load(Archive& ar, boost::icl::discrete_interval<V>& di, unsigned) {
auto bb = di.bounds().bits();
V l, u;
ar >> bb >> l >> u;
di = boost::icl::discrete_interval<V>(l, u, boost::icl::interval_bounds(bb));
}
template <typename Archive, typename V>
void serialize(Archive& ar, boost::icl::discrete_interval<V>& di, unsigned v) {
split_free(ar, di, v);
}
template <typename Archive, typename V>
void save(Archive& ar, boost::icl::interval_set<V> const& is, unsigned) {
auto sz = is.iterative_size();
ar & sz;
for (auto& di : is) ar & di;
}
template <typename Archive, typename V>
void load(Archive& ar, boost::icl::interval_set<V>& is, unsigned) {
is.clear();
size_t sz;
ar & sz;
size_t counter = sz;
while (counter--) {
typename boost::icl::interval_set<V>::value_type di;
ar & di;
is.insert(is.end(), di);
}
assert(is.iterative_size() == sz);
}
template <typename Archive, typename V>
void serialize(Archive& ar, boost::icl::interval_set<V>& is, unsigned v)
{
split_free(ar, is, v);
}
} }
const std::string Filename = "tmp.archive";
typedef boost::icl::discrete_interval<int> Interval;
typedef boost::icl::interval_set<int> IntervalSet;
#include <fstream>
#include <string>
int main()
{
{
std::ofstream f(Filename);
boost::archive::binary_oarchive oa(f);
IntervalSet s;
s += Interval::closed(100, 200);
s += Interval::left_open(30,45);
s += Interval::right_open(77, 78);
oa << s;
}
{
std::ifstream f(Filename);
boost::archive::binary_iarchive ia(f);
IntervalSet s;
ia >> s;
std::cout << "Deserialized: ";
std::copy(s.begin(), s.end(), std::ostream_iterator<Interval>(std::cout, " "));
}
}
为什么@HEKTO没有问我。如果你要问我,我猜这是为了对称序列化(存档&ar,t&v,未签名)
这是支持加载和保存所必需的。我能够使用中间std::vector
实现save/load
函数-这不是很有趣,但很有效。无论如何,谢谢你的建议。@HEKTO FWIW这是我的看法-不需要临时向量
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/icl/discrete_interval.hpp>
#include <boost/icl/interval_set.hpp>
namespace boost { namespace serialization {
template <typename Archive, typename V>
void save(Archive& ar, boost::icl::discrete_interval<V> const& di, unsigned) {
auto const& bb = di.bounds().bits();
auto const& l = di.lower();
auto const& u = di.upper();
ar << bb << l << u;
}
template <typename Archive, typename V>
void load(Archive& ar, boost::icl::discrete_interval<V>& di, unsigned) {
auto bb = di.bounds().bits();
V l, u;
ar >> bb >> l >> u;
di = boost::icl::discrete_interval<V>(l, u, boost::icl::interval_bounds(bb));
}
template <typename Archive, typename V>
void serialize(Archive& ar, boost::icl::discrete_interval<V>& di, unsigned v) {
split_free(ar, di, v);
}
template <typename Archive, typename V>
void save(Archive& ar, boost::icl::interval_set<V> const& is, unsigned) {
auto sz = is.iterative_size();
ar & sz;
for (auto& di : is) ar & di;
}
template <typename Archive, typename V>
void load(Archive& ar, boost::icl::interval_set<V>& is, unsigned) {
is.clear();
size_t sz;
ar & sz;
size_t counter = sz;
while (counter--) {
typename boost::icl::interval_set<V>::value_type di;
ar & di;
is.insert(is.end(), di);
}
assert(is.iterative_size() == sz);
}
template <typename Archive, typename V>
void serialize(Archive& ar, boost::icl::interval_set<V>& is, unsigned v)
{
split_free(ar, is, v);
}
} }
const std::string Filename = "tmp.archive";
typedef boost::icl::discrete_interval<int> Interval;
typedef boost::icl::interval_set<int> IntervalSet;
#include <fstream>
#include <string>
int main()
{
{
std::ofstream f(Filename);
boost::archive::binary_oarchive oa(f);
IntervalSet s;
s += Interval::closed(100, 200);
s += Interval::left_open(30,45);
s += Interval::right_open(77, 78);
oa << s;
}
{
std::ifstream f(Filename);
boost::archive::binary_iarchive ia(f);
IntervalSet s;
ia >> s;
std::cout << "Deserialized: ";
std::copy(s.begin(), s.end(), std::ostream_iterator<Interval>(std::cout, " "));
}
}
Deserialized: (30,45] [77,78) [100,200]