我正在开发一个简单的序列化类。我一直在输入流上抛出一个异常。我已经把我想要用简单的术语完成的下面的例子放在一起。
我有一个简单的boost序列化示例,我得到了一个例外:
#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#define NVP(X) X
class base {
public:
friend class boost::serialization::access;
base (){ v1 = 10;}
int v1;
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & NVP(v1);
}
virtual void bla()=0;
};
class derived : public base {
public:
friend class boost::serialization::access;
int v2 ;
derived() { v2 = 100;}
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version){
boost::serialization::base_object<base>(* this);
ar & NVP(v2);
}
virtual void bla(){};
};
BOOST_CLASS_EXPORT(base);
BOOST_CLASS_EXPORT_GUID(derived, "derived");
int main ( )
{
std::stringstream ss;
boost::archive::text_oarchive ar(ss);
base *b = new derived();
ar << NVP(b);
std::cout << ss.str()<<std::endl;
std::istringstream ssi;
base *b1 = new derived();
{
boost::archive::text_iarchive ar1(ssi);
ar1 >> b1;
}
//std::cout << ssi.str();
std::cout << "v1: " << b1->v1 << std::endl;
}
我得到的例外是:
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
任何帮助都将不胜感激。
答案 0 :(得分:2)
你正在读一个空流:
std::istringstream ssi;
// ...
boost::archive::text_iarchive ar1(ssi);
此外,您泄漏了此对象:
base *b1 = new derived();
这是一个固定的例子,注意:
BOOST_CLASS_EXPORT_GUID(derived, "derived")
不会添加BOOST_CLASS_EXPORT(derived)
您可以有条件地打印v2:
if (auto* d = dynamic_cast<derived*>(b1))
std::cout << "v2: " << d->v2 << std::endl;
我以bla()
为例打印了值
NVP()
有点不确定。为什么不把它留给非标记的档案(即XML以外的其他档案)?如果您打算支持XML,只需使用BOOST_SERIALIZATION_NVP
,boost::serialization::make_nvp
等。
std::cout << "v2: " << b1->v2 << std::endl;
完全不合适
只需将b1
初始化为null,这样就不会泄漏它;记得释放所有指针(使用智能指针!)
您的类型中public:
和friend
的混合并没有多大意义
<强> Live On Coliru 强>
#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <sstream>
class base {
public:
base(int v1) : v1(v1) {}
virtual void bla() const = 0;
private:
friend class boost::serialization::access;
template <class Archive> void serialize(Archive &ar, unsigned /*int const file_version*/) {
ar & BOOST_SERIALIZATION_NVP(v1);
}
protected:
int v1;
};
class derived : public base {
public:
derived(int v1 = 10, int v2 = 100) : base(v1), v2(v2) {}
virtual void bla() const {
std::cout << "v1: " << v1 << ", v2: " << v2 << "\n";
}
private:
friend class boost::serialization::access;
int v2;
template <class Archive> void serialize(Archive &ar, unsigned /*int const file_version*/) {
boost::serialization::base_object<base>(*this);
ar & BOOST_SERIALIZATION_NVP(v2);
}
};
BOOST_CLASS_EXPORT(base)
BOOST_CLASS_EXPORT(derived)
int main() {
std::stringstream ss;
{
boost::archive::text_oarchive ar(ss);
base *b = new derived();
ar << boost::serialization::make_nvp("base", b);
delete b; // TODO use RAII instead
}
std::cout << ss.str() << std::endl;
base *deserialized = nullptr;
{
boost::archive::text_iarchive ar1(ss);
ar1 >> boost::serialization::make_nvp("base", deserialized);
}
deserialized->bla();
delete deserialized;
}
打印
22 serialization::archive 12 0 7 derived 1 0
0 100
v1: 10, v2: 100