如何使用谷物序列化 boost::ptr_vector?
How to serialize a boost::ptr_vector using cereal?
是否可以使用谷物序列化 boost::ptr_vector
实例?如果可以,怎么做?
绝对有可能。您可以像这样在存档和指针类型上创建外部 save()
和 load()
模板函数:
#include <iostream>
#include <sstream>
#include <boost/ptr_container/ptr_vector.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/types/string.hpp>
// Sample serializable object.
struct MyRecord {
std::string s_;
MyRecord(const std::string s = std::string())
: s_(s) {
}
template <class Archive>
void serialize(Archive& ar ) {
ar(s_);
}
};
// External save function for boost::ptr_vector<T>.
template<class Archive, class T>
void save(Archive& ar, const boost::ptr_vector<T>& pv) {
ar(pv.size());
for (const auto& element : pv)
ar(element);
}
// External load function for boost::ptr_vector<T>.
template<class Archive, class T>
void load(Archive& ar, boost::ptr_vector<T>& pv) {
size_t n;
ar(n);
pv.reserve(n);
for (size_t i = 0; i < n; ++i) {
pv.push_back(new T);
ar(pv.back());
}
}
int main() {
// Serialize a boost::ptr_vector to a buffer.
std::ostringstream os;
{
boost::ptr_vector<MyRecord> saved;
saved.push_back(new MyRecord("how"));
saved.push_back(new MyRecord("now"));
saved.push_back(new MyRecord("brown"));
saved.push_back(new MyRecord("cow"));
cereal::BinaryOutputArchive oa(os);
oa(saved);
}
// Serialize from the buffer.
boost::ptr_vector<MyRecord> loaded;
{
std::istringstream is(os.str());
cereal::BinaryInputArchive ia(is);
ia(loaded);
}
for (const auto& element : loaded)
std::cout << element.s_ << '\n';
return 0;
}
这应该适用于任何可默认构造且已经可单独序列化的包含类型。
是否可以使用谷物序列化 boost::ptr_vector
实例?如果可以,怎么做?
绝对有可能。您可以像这样在存档和指针类型上创建外部 save()
和 load()
模板函数:
#include <iostream>
#include <sstream>
#include <boost/ptr_container/ptr_vector.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/types/string.hpp>
// Sample serializable object.
struct MyRecord {
std::string s_;
MyRecord(const std::string s = std::string())
: s_(s) {
}
template <class Archive>
void serialize(Archive& ar ) {
ar(s_);
}
};
// External save function for boost::ptr_vector<T>.
template<class Archive, class T>
void save(Archive& ar, const boost::ptr_vector<T>& pv) {
ar(pv.size());
for (const auto& element : pv)
ar(element);
}
// External load function for boost::ptr_vector<T>.
template<class Archive, class T>
void load(Archive& ar, boost::ptr_vector<T>& pv) {
size_t n;
ar(n);
pv.reserve(n);
for (size_t i = 0; i < n; ++i) {
pv.push_back(new T);
ar(pv.back());
}
}
int main() {
// Serialize a boost::ptr_vector to a buffer.
std::ostringstream os;
{
boost::ptr_vector<MyRecord> saved;
saved.push_back(new MyRecord("how"));
saved.push_back(new MyRecord("now"));
saved.push_back(new MyRecord("brown"));
saved.push_back(new MyRecord("cow"));
cereal::BinaryOutputArchive oa(os);
oa(saved);
}
// Serialize from the buffer.
boost::ptr_vector<MyRecord> loaded;
{
std::istringstream is(os.str());
cereal::BinaryInputArchive ia(is);
ia(loaded);
}
for (const auto& element : loaded)
std::cout << element.s_ << '\n';
return 0;
}
这应该适用于任何可默认构造且已经可单独序列化的包含类型。