10

Note:
Boost's archive scheme is based on symmetrical input and output archive classes. It's tedious to write about both of them all the time, so I'll use ?archive to mean both oarchive and iarchive.

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.)

The mystery, and my dilemma:
A) Why does the presence of those lines foul up the works? ...and why does removing them make things work? ...and what might I have broken (without knowing it) by doing so?

B) Why were the "text_?archive.hpp" files not split up along with the "binary_?archive.hpp" files a long time ago? (Is the library broken? Should it be fixed?)

C) Is there any way to solve this in my application code without modifying my Boost installation?

P.S. I'm using Boost 1.48 and Visual Studio 2010 (64-bit)
P.P.S. I imagine all of the above would apply equally to text_w?archive

3 Answers 3

1

As best I can tell its a bug in boost serialize. We'll see here.

A)
1. Adding BOOST_SERIALIZATION_REGISTER_ARCHIVE with you new archive does not work because the default text archives have already been registered - only on registration seems to be allowed.
2. Removing them makes it work because only your custom classes are registered.
3. By removing them you've broken the ability to use the default text archive - your classes will be registered.

B)
I'm fairly certain that the "text_?archive.hpp" files should have been split up like the "binary_?archive.hpp" files are. Patch boost anyone?

C)
The best solution is to submit a patch to boost that splits the files up. For a temporary solution probably the best way is to put the patched files locally in your project until the patch makes it into boost.

1

I want this to be a comment since it is a hint rather than an answer. However I am not seeing the option to add a comment to your question (and I do not think edit button will do what I want to).

On my 1.49.0 install I see corresponding implementation files for the text type also. They are in the .ipp format in the impl directory. The time-stamp suggests that they have not been changed recently so should be the same as the 1.48. It might help you unravel the issue.

According to Dave Abrahams, .ipp files are supposed to hide the implementation. Not sure why they chose different styles.

----------+ 1 stackoverflow None 2936 Dec 5 2009 ./binary_iarchive_impl.hpp

----------+ 1 stackoverflow None 2966 Dec 5 2009 ./binary_oarchive_impl.hpp

----------+ 1 stackoverflow None 1392 Nov 25 2007 ./detail/basic_archive_impl.hpp

----------+ 1 stackoverflow None 3458 May 20 2009 ./impl/text_iarchive_impl.ipp

----------+ 1 stackoverflow None 3290 Jul 2 2005 ./impl/text_oarchive_impl.ipp

----------+ 1 stackoverflow None 3020 Jun 26 2008 ./impl/text_wiarchive_impl.ipp

----------+ 1 stackoverflow None 2244 Jul 2 2005 ./impl/text_woarchive_impl.ipp

1
  • I did notice these files (in my 1.48 installation), but they are not the solution. As a temporary fix, I have done the job of splitting up the text_?archive headers into separate files, and that change required modifications to these .ipp files as well. So I've got a temporary solution, but I still don't know if this should be reported as a bug in the library, or if these files are correct and I should be doing something differently in my code.
    – aldo
    May 30, 2012 at 21:45
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.