序列化非默认的constructible和std :: reference包装器替代

时间:2018-03-15 13:40:03

标签: c++ boost boost-serialization reference-wrapper

我尝试将我的(神经)网络序列化并且目前卡住了。 问题似乎是您无法序列化std::reference_wrapper。我不确定我是应该改变存储对上层节点的引用的方式,还是想出一种序列化它们的方法。

是否有reference_wrapper的替代品,我忽略了它并仍然避免使用c样式指针? (就我所知,这是应该避免的)

#include <iostream>
#include <fstream>
#include <list>
#include <vector>
#include <functional>

#include <boost/archive/tmpdir.hpp>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>

typedef float nodeValueType;
typedef std::pair<std::vector<nodeValueType>, std::vector<nodeValueType>> Example;
typedef std::list<Example> ExampleList;

class Node;

class Link
{
public:
Link() = delete;
Link(Node& upperNode, Node& lowerNode)
: Link(upperNode, lowerNode, 1.0e-3f * (std::rand() / (nodeValueType) RAND_MAX))
{
}
Link(Node& upperNode, Node& lowerNode, nodeValueType weight)
: weight_(weight), upperNode_(upperNode), lowerNode_(lowerNode)
{
}
Link(const Link&) = delete;
Link& operator=(const Link&) = delete;

nodeValueType weight_;
std::reference_wrapper<Node> upperNode_;
std::reference_wrapper<Node> lowerNode_;


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

class Node
{
public:
Node();

// making it hard to copy us since we really never want to move.
// we are referred in loads of pointers
// therefore moving us invalidates all of them TODO invalidation scheme?
Node(const Node&) = delete;
Node& operator=(const Node&) = delete;

void linkTo(Node& other)
{
assert(this->lowerLinks_.max_size() > (this->lowerLinks_.size() + 1) * 2);
// Link creation
this->lowerLinks_.push_back(std::shared_ptr<Link>(new Link(*this, other)));
other.upperLinks_.push_back(std::shared_ptr<Link>(this->lowerLinks_.back()));
}

std::vector<std::shared_ptr<Link>> lowerLinks_;
std::vector<std::shared_ptr<Link>> upperLinks_;
// serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version){
ar & this->lowerLinks_;
}
};

int main()
{
Node n1;
Node n2;

n2.linkTo(n1);

std::string filename(boost::archive::tmpdir());
filename += "/demofile.txt";

std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << n1 << n2;

Node n3,n4;

// open the archive
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);

// restore the schedule from the archive
ia >> n3 >> n4;
return 0;
}

1 个答案:

答案 0 :(得分:0)

有两个问题需要克服。

首先,std :: reference_wrapper不能为null,因此您可能需要考虑'可空引用'类型:

template<class T> 
struct NullableReference
{
    constexpr NullableReference() : ptr_(nullptr) {}
    constexpr NullableReference(T& ref) : ptr_(std::addressof(ref)) {}

    constexpr auto operator*() const -> T& { assert(!empty()); return *ptr_; }
    constexpr operator T&() const { assert(!empty()); return *ptr_; }

    constexpr bool empty() const { return !ptr_; }

    template<class Archive>
    void serialize(Archive& ar, unsigned int version)
    {
        ar & ptr_;
    }

private:
    T* ptr_;
};

另一个是Link没有默认构造函数(毫无疑问是因为不可为空的引用包装器)。

因此,您可能需要在反序列化链接时考虑构造函数的自定义处理(这在升级文档IIRC中有所介绍)。

当然,既然你正在使用NullableReference,你可以允许Link的默认构造函数:

...

Link() : weight_(), upperNode_(), lowerNode_() {};

nodeValueType weight_;
NullableReference<Node> upperNode_;
NullableReference<Node> lowerNode_;

....