gpt4 book ai didi

C++ - boost::any 序列化

转载 作者:IT老高 更新时间:2023-10-28 23:17:38 33 4
gpt4 key购买 nike

据我了解,boost::any 占位符不支持序列化(实际上是 boost::serialization)。

有人知道是否有办法序列化自定义 boost::any 实体吗?

这里的问题很明显:boost::any 使用基于模板的占位符来存储对象和 typeid 来检查 boost::any_cast 合适。

所以,有一个自定义的抽象父类(super class)placeholder和自定义的基于模板的派生类,它们的创建方式如下:

template <T> custom_placeholder : public placeholder {
virtual std::type_info type() const { return typeid(T); }
virtual ...
};

显然,这在考虑序列化这些东西时会带来一些麻烦。 也许有人知道一些技巧来进行这种序列化(当然,正确的反序列化)?

谢谢

最佳答案

如果您想坚持使用 boost::any,我不确定,但您可以编写自己的“boost::any”。我正在将此代码用于代理方法来传递参数。

#include <iostream>
#include <boost\smart_ptr\scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <sstream>
class my_placeholder
{
public:
virtual ~my_placeholder(){}
my_placeholder(){}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
//ar & boost::serialization::base_object<bus_stop>(*this);
//ar & m_placeholder;

}

};




template<typename T>
class my_derivedplaceholder:
public my_placeholder
{
public:
my_derivedplaceholder()
{

}
my_derivedplaceholder(T &value)
{
m_value=value;
}
T m_value;

private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
ar & boost::serialization::base_object<my_placeholder>(*this);
ar & m_value;

}
};


BOOST_CLASS_EXPORT_GUID(my_derivedplaceholder<int>, "p<int>");


class my_any
{
public:

my_any()
{

}

template<typename T>
my_any(const T &value)
{
m_placeholder.reset(new my_derivedplaceholder<T>(const_cast<T&>(value)));
}

template<typename T>
void operator=(const T &value)
{
m_placeholder.reset(new my_derivedplaceholder<T>(const_cast<T&>(value)));
}



protected:

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
//ar & boost::serialization::base_object<bus_stop>(*this);
ar & m_placeholder;

}

template<typename T>
friend T my_anycast(my_any &val);

boost::shared_ptr<my_placeholder> m_placeholder;
};

template<typename T>
T my_anycast(my_any &val)
{
boost::shared_ptr<my_derivedplaceholder<T>> concrete=boost::dynamic_pointer_cast<my_derivedplaceholder<T>>(val.m_placeholder);
if (concrete.get()==NULL)
throw std::invalid_argument("Not convertible");

return concrete->m_value;
}

void main()
{
my_any m=10;

int a=my_anycast<int>(m);

std::cout << a << std::endl;

std::stringstream ss,ss2;
boost::archive::text_oarchive oa(ss);

oa << m;

boost::archive::text_iarchive ia(ss);

my_any m2;
ia >> m2;

std::cout << my_anycast<int>(m2) << std::endl;
}

关于C++ - boost::any 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672315/

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