Boost logo

Boost Users :

Subject: [Boost-users] BOOST_CLASS_EXPORT_GUID with custom archive
From: Elizabeta (elizabeta.petreska_at_[hidden])
Date: 2014-12-11 09:48:29


Hi
I am using boost 1.49. I have custom archives derived from the boost binary
archives. However it seems that when using BOOST_CLASS_EXPORT_GUID the types
are not actually registered for the custom archive types, and I am getting
unregistered_class exception when serializing derived class from base
pointer. If I am using the boost binary original archives this is not the
case. My custom archive classes are defined in in its own header file and at
the end of the file I am including BOOST_SERIALIZATION_REGISTER_ARCHIVE,
according to this in the documentation :
"
The newly created archive will usually be stored in its own header module.
All that is necessary is to include the header and construct an instance of
the new archive. EXCEPT for one special case.
Instances of a derived class are serialized through a base class pointer.
Such instances are not "registered" neither implicitly nor explicitly. That
is, the macro BOOT_CLASS_EXPORT is used to instantiate the serialization
code for the included archives.
To make this work, the following should be included after the archive class
definition.

#define BOOST_SERIALIZATION_REGISTER_ARCHIVE(Archive)
Failure to do this will not inhibit the program from compiling, linking and
executing properly - except in one case. If an instance of a derived class
is serialized through a pointer to its base class, the program will throw an
unregistered_class exception."

here is my archive h file :
-------------------
customarchive.h
--------------------
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_oarchive_impl.hpp>
#include <boost/archive/binary_iarchive_impl.hpp>
#include <boost/archive/shared_ptr_helper.hpp>

class CloneBinaryIArchive :
    // don't derive from binary_oarchive !!!
    public boost::archive::binary_iarchive_impl<
    CloneBinaryIArchive,
    std::istream::char_type,
    std::istream::traits_type
>,
    public boost::archive::detail::shared_ptr_helper
{
    typedef CloneBinaryIArchive derived_t;
    typedef boost::archive::binary_iarchive_impl<
        CloneBinaryIArchive,
        std::istream::char_type,
        std::istream::traits_type
> base_t;

public:
    CloneBinaryIArchive(std::istream & is,unsigned int flags = 0) :
      base_t(is, flags)
      {}
      CloneBinaryIArchive(std::streambuf & bsb, unsigned int flags = 0) :
      base_t(bsb, flags)
      {}

      int foo;
};
BOOST_SERIALIZATION_REGISTER_ARCHIVE(CloneBinaryIArchive)

and here is the cpp file in each I am registering all classes :
--------------
register.cpp
---------------
#include "customarchive.h"
#include <boost/serialization/export.hpp>
#include "A.h"
BOOST_CLASS_EXPORT_GUID(A,"A")

--
View this message in context: http://boost.2283326.n4.nabble.com/BOOST-CLASS-EXPORT-GUID-with-custom-archive-tp4670051.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net