使用boost序列化库我有一个非常简单的serialize()
成员函数,如:
template <class Archive>
void serialize( Archive& ar, unsigned version )
{
ar & m_Searcher;
}
...我想保持这么简单(我不想特别使用拆分)。但在写作的情况下,我想在实际写作之前为m_Searcher
做一些“准备”。
{
if( this-is-a-writing-operation )
do-some-preparation( m_Searcher )
ar & m_Searcher;
}
有没有简单的方法可以区分读写操作?
答案 0 :(得分:24)
我认为你可以不分裂来做到这一点,这是通常的方式:
if (Archive::is_saving::value)
doSomething();
这是继承自档案馆使用的基本界面,位于boost/archive/detail/interface_[ia]archive.hpp
以下代码演示了1.42
似乎是一个合理的解决方案#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
// oarchive:
//text
static_assert(!boost::archive::text_oarchive::is_loading::value, "out is loading");
static_assert(boost::archive::text_oarchive::is_saving::value, "out isn't saving");
//xml
static_assert(!boost::archive::xml_oarchive::is_loading::value, "out is loading");
static_assert(boost::archive::xml_oarchive::is_saving::value, "out isn't saving");
// iarchive:
//text
static_assert(boost::archive::text_iarchive::is_loading::value, "out is loading");
static_assert(!boost::archive::text_iarchive::is_saving::value, "out isn't saving");
//xml
static_assert(boost::archive::xml_iarchive::is_loading::value, "out is loading");
static_assert(!boost::archive::xml_iarchive::is_saving::value, "out isn't saving");
我有点谨慎依赖这样的事情 - 如果有人写了一个同时输入和输出和的存档,多重继承可能会破坏它,我不清楚这是多么的永恒并公开这部分界面应该是。