gpt4 book ai didi

c++ - 如何使用boost序列化轻松序列化包含另一个自定义类的STL容器的自定义类?

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:20 26 4
gpt4 key购买 nike

我已经阅读了这里的教程http://www.boost.org/doc/libs/1_59_0/libs/serialization/doc/index.html并搜索了很多页面,但我仍然不确定该怎么做。

我已经阅读了如何序列化自定义类以及如何序列化包含 std::string 或指针的 STL 容器的自定义类。但是我不确定如何使用 boost 序列化轻松序列化包含另一个自定义类的 STL 容器的自定义类?由于 std::string 和指针是标准的 C++ 类型,boost 知道如何处理它们。如果是自定义类的STL容器,我觉得我必须告诉boost在序列化时如何处理它。但是怎么办?这是我自定义的2个类。

class Instance
{
public:
Instance(){}
~Instance();
//... a lot of functions here.
private:
InstanceIdentity identity_;
mutable ClassLabel class_label_;
mutable FeatureGroup features_;
friend class InstanceManager;
};

class InstanceManager
{
public:
InstanceManager(){}
~InstanceManager();
// a lot of functions here.
private:
set<Instance> instances_;
};

根据http://www.boost.org/doc/libs/1_59_0/libs/serialization/doc/index.html的教程部分,因为boost知道如何处理STL容器,所以我需要做的就是添加

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & instances_;
}

在 InstanceManager 类中。并添加

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// A lot of things like the following example.
//ar & degrees;
//ar & minutes;
//ar & seconds;
}

在实例类中。是对的吗?或者有更好的方法。通过编码来测试它需要很多时间。所以,谁能告诉我答案?

最佳答案

答案是肯定的。这是一个完整的工作示例。我发明了一些简单的类型:

struct InstanceIdentity { int id;            };
struct ClassLabel { std::string value; };
struct FeatureGroup { size_t count; };

使用非侵入式序列化:

namespace boost { namespace serialization {
template <typename Ar>
void serialize(Ar& ar, InstanceIdentity& ii, unsigned) { ar & ii.id; }
template <typename Ar>
void serialize(Ar& ar, ClassLabel& cl, unsigned) { ar & cl.value; }
template <typename Ar>
void serialize(Ar& ar, FeatureGroup& fg, unsigned) { ar & fg.count; }
} }

InstanceInstanceManager 类将它们的序列化方法作为成员(由于私有(private)成员):

template <typename Ar>
void Instance::serialize(Ar& ar, unsigned) { ar & identity_ & class_label_ & features_; }

template <typename Ar>
void InstanceManager::serialize(Ar& ar, unsigned) { ar & instances_; }

Live On Coliru

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/set.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/access.hpp>

struct InstanceIdentity { int id; };
struct ClassLabel { std::string value; };
struct FeatureGroup { size_t count; };

class Instance
{
public:
Instance(){}
~Instance() = default;
//... a lot of functions here.

bool operator<(Instance const& other) const {
return identity_.id < other.identity_.id;
}
private:
InstanceIdentity identity_;
mutable ClassLabel class_label_;
mutable FeatureGroup features_;
friend class InstanceManager;

friend class boost::serialization::access;
template <typename Ar>
void serialize(Ar& ar, unsigned) { ar & identity_ & class_label_ & features_; }
};

class InstanceManager
{
public:
InstanceManager()
{
Instance a, b, c;

a.class_label_.value = "label a";
b.class_label_.value = "label b";
c.class_label_.value = "label c";

a.features_.count = 42;
b.features_.count = 24;
c.features_.count = -9;

a.identity_.id = rand();
b.identity_.id = rand();
c.identity_.id = rand();

instances_.insert(instances_.end(), a);
instances_.insert(instances_.end(), b);
instances_.insert(instances_.end(), c);
}

~InstanceManager() = default;
// a lot of functions here.
private:
std::set<Instance> instances_;

friend class boost::serialization::access;
template <typename Ar>
void serialize(Ar& ar, unsigned) { ar & instances_; }
};

namespace boost { namespace serialization {
template <typename Ar>
void serialize(Ar& ar, InstanceIdentity& ii, unsigned) { ar & ii.id; }
template <typename Ar>
void serialize(Ar& ar, ClassLabel& cl, unsigned) { ar & cl.value; }
template <typename Ar>
void serialize(Ar& ar, FeatureGroup& fg, unsigned) { ar & fg.count; }
} }

int main() {

InstanceManager im;

boost::archive::text_oarchive oa(std::cout);
oa << im;

}

打印

22 serialization::archive 12 0 0 0 0 3 0 0 0 0 0 846930886 0 0 7 label b 0 0 24 1681692777 7 label c 18446744073709551607 1804289383 7 label a 42

关于c++ - 如何使用boost序列化轻松序列化包含另一个自定义类的STL容器的自定义类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32547183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com