Opened 10 years ago

Last modified 9 years ago

#7047 new Bugs

Deriving custom archive classes from boost::archive::text_oarchive_impl and boost::archive::text_iarchive_impl

Reported by: zachais.vawter@… Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost 1.48.0 Severity: Problem
Keywords: Cc: aldo.d.longhi@…

Description

From herehttp://stackoverflow.com/questions/10691911/deriving-custom-archive-classes-from-boostarchivetext-oarchive-impl-and-boos

Summary: After changing the base classes of my custom archives from binary_?archive_impl to text_?archive_impl, my custom archive classes are no longer "found" when the compiler is instantiating the serialize(...) methods in my other classes.

Background: My application was successfully reading and writing files to disk using subclasses of binary_?archive_impl (the documentation and/or code comments recommend this over deriving from binary_?archive). I needed to switch from a binary file format to a text format, so I switched the base classes of my custom archives to text_?archive_impl. That's when everything blew up.

The problem: My custom archive classes add functionality, including some additional methods which do not exist in their Boost base classes; these methods are called in the serialize(...) methods in many of my classes, and they were working fine. After changing the base classes from binary_?archive_impl to text_?archive_impl, I received compilation errors all over the place complaining that my custom methods do not exist in text_?archive. Well, that's obvious (!!!), but they do exist in my custom archives, and they were working just fine when I was using Boost's binary base classes. What's the deal?

What I found, and my temporary - but undesirable - solution: After tearing my hair out and going around in circles for about a day, this is what I found...

1) Some time ago (Boost 1.34 I believe), the files "binary_?archive.hpp" were split up into "binary_?archive_impl.hpp" and "binary_?archive.hpp" (the latter #include the former). This was not done to "text_?archive.hpp". (As a result, I changed my application's #include lines from "binary_?archive_impl.hpp" to simply "text_?archive.hpp".)

2) If I split up "text_?archive.hpp" into two parts and #include only the "..._impl.hpp" headers, everything works. (But I really don't want to modify my Boost installation!)

3) Looking more closely at these headers and fiddling around a bit, I found that if I use the original, unmodified headers and comment out the line

BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::archive::text_oarchive)

(and likewise for text_iarchive), then everything works fine again. (By the way I have similar lines in my own archive code to "register" my custom archives.)

Change History (3)

comment:1 by Robert Ramey, 10 years ago

I would really like a better explanation of this along with a specific recommendation as to what should be changed.

You might want to post your code for your custom archive.

Hopefully this would end up being some misunderstanding or missing information which is needed to properly support a custom archive. Ideally this would be crafted as an example to be added to the package.

I'm thinking that the missing magic is:

BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::archive::custom_oarchive)

but that's just a guess.

I'll leave this ope a little while longer to give you a chance to respond.

Robert Ramey

comment:2 by zachais.vawter@…, 10 years ago

Robert,

A co-worker or I would like to submit a patch for this issue, but we don't have time at the moment.

Point 3 above addresses the use of BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::archive::custom_oarchive) does not fix the issue unless we remove the BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::archive::text_oarchive) line from the text archive headers.

I believe the correct fix is listed in point 1.

-Zac

comment:3 by anonymous, 9 years ago

I had the same problem tying to implement custom archive for my library. I've found a possible solution trick, it seems to works well so I'll share with you:

There is no way to export a class with a modified serialization syntax in boost archive, so we have to avoid it at all.

boost archive registration uses a properly overloaded function to make a pointer serialization type instance ( as in boost/archive/detail/register_archive.hpp )

# define BOOST_SERIALIZATION_REGISTER_ARCHIVE(Archive)                  \
namespace boost { namespace archive { namespace detail {                \
                                                                        \
template <class Serializable>                                           \
BOOST_DEDUCED_TYPENAME _ptr_serialization_support<Archive, Serializable>::type  \
instantiate_ptr_serialization( Serializable*, Archive*, adl_tag );              \
                                                                        \
} } }

Note that adl_tag adds a cool overload feature that can be used to make boost able to look inside our implementation. Simply put a new registration declaration as this:

// ARCHIVES REGISTRATION //

namespace MyLib {
struct adl_tag {};
}

namespace boost { namespace archive { namespace detail {
template <class Serializable>
void instantiate_ptr_serialization(Serializable*, int, MyLib::adl_tag ) {}
} } }

# define MYLIB_SERIALIZATION_REGISTER_ARCHIVE(_Archive)                   \
namespace boost { namespace archive { namespace detail {                \
template <class Serializable>                                           \
BOOST_DEDUCED_TYPENAME _ptr_serialization_support<_Archive, Serializable>::type  \
instantiate_ptr_serialization( Serializable*, _Archive*, MyLib::adl_tag ); } } }

Now you have to make your own EXPORT macro as in (/boost/serialization/export.hpp):

namespace MyLib {
namespace extra_detail {

template<class T>
struct guid_initializer
{
    void export_guid(mpl::false_) const {
        // generates the statically-initialized objects whose constructors
        // register the information allowing serialization of T objects
        // through pointers to their base classes.
        boost::archive::detail::
                instantiate_ptr_serialization((T*)0, 0,
                                              MyLib::adl_tag());
    }
    void export_guid(mpl::true_) const {
    }
    guid_initializer const & export_guid() const {
        BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value);
        // note: exporting an abstract base class will have no effect
        // and cannot be used to instantitiate serialization code
        // (one might be using this in a DLL to instantiate code)
        //BOOST_STATIC_WARNING(! boost::serialization::is_abstract< T >::value);
        export_guid(boost::serialization::is_abstract< T >());
        return *this;
    }
};

template<typename T>
struct init_guid;

} // extra_detail
} // namespace MyLib



#define  MYLIB_CLASS_EXPORT_IMPLEMENT(T)                      \
    namespace MyLib  {                                        \
    namespace extra_detail {                                 \
    template<>                                               \
    struct init_guid< T > {                                  \
        static guid_initializer< T > const & g;              \
    };                                                       \
    guid_initializer< T > const & init_guid< T >::g =        \
        ::boost::serialization::singleton<                   \
            guid_initializer< T >                            \
        >::get_mutable_instance().export_guid();             \
    }}                                                     \
/**/

Ok it's all, now you can define your custom archive and register it with:

MYLIB_SERIALIZATION_REGISTER_ARCHIVE(MyLib::xml_iarchive)

and anytime you define a serialization for your class that has a particular syntax only readable by MyLib::custom_archive you can use your export implementation

BOOST_CLASS_EXPORT_KEY(MyClass) // in header
MYLIB_CLASS_EXPORT_IMPLEMENT(MyClass) // in cpp

(Note that exporting of Key remains the same of boost ... )

This is really cool because lets your custom archives and boost archives live together without errors.. Anytime you wants a boost serialization simply use BOOST_CLASS_EXPORT, and anytime you have your class to be serialized use MYLIB_CLASS_EXPORT.

Hope that this could be useful !

Andrea Rigoni Garola (OpenCMT - Cosmic Muon Tomography library)

Note: See TracTickets for help on using tickets.