c++ - boost serialization - polymorphic archives - archive-type-dependent behavior -
i'm using boost serialization (v 1.55 , i'm trying implement serialization behavior foo
dependent on archive type (xml or binary). @ same time, need use polymorphic archive types. here trivial example:
#include <sstream> #include <boost/archive/polymorphic_binary_iarchive.hpp> #include <boost/archive/polymorphic_binary_oarchive.hpp> #include <boost/archive/polymorphic_xml_iarchive.hpp> #include <boost/archive/polymorphic_xml_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/archive/binary_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> using namespace boost::archive; typedef polymorphic_binary_iarchive bi; typedef polymorphic_binary_oarchive bo; typedef polymorphic_xml_iarchive xi; typedef polymorphic_xml_oarchive xo; /* typedef binary_iarchive bi; typedef binary_oarchive bo; typedef xml_iarchive xi; typedef xml_oarchive xo; */ struct foo { void save(bo & ar, unsigned int const & version) const {} void load(bi & ar, unsigned int const & version) {} void save(xo & ar, unsigned int const & version) const {} void load(xi & ar, unsigned int const & version) {} boost_serialization_split_member(); }; int main() { std::stringstream ss; xo ar(ss); foo f; ar << boost_serialization_nvp(f); }
the code compiles if use non-polymorphic archive types, polymorphic types, following error
error: no matching function call ‘foo::save(boost::archive::polymorphic_oarchive&, const unsigned int&) const
so seems archive type changes in ar <<
call. know how implement this?
the whole point of polymorphic archives cannot know front archive implementation gonna used. in fact, archive type doesn't yet exist, implemented in dynamically loaded, 3rdparty library:
the main utility of polymorphic archives permit buiding of class dlls include serialization code present , future archives no redundant code. (docs)
as start doing special handling specific archive types, you're @ least swimming upstream, , possibly asking trouble.
i suggest rethink perceived need detect archive implementation @ runtime. if there way @ rtti typeid
of implementing type @ runtime,
- how handle unknown types?
- how handle type inherits from, say, xml_archive? (there's no way detect @ runtime)
- how handle type aggregates such xml_archive? (there isn't way detect @ compile-time, let alone @ runtime)
in short, if have special handling, codify it. implement savexml
function or saveas(saveoptions format)
. can use polymorphic archives implement 1 of two, , take xml_oarchive&
reference (so accept derived implementations fine).
$0.02
Comments
Post a Comment