Boost logo

Boost Users :

Subject: [Boost-users] Deriving custom archive classes from boost::archive::text_oarchive_impl and boost::archive::text_iarchive_impl
From: Aldo Longhi (aldo.longhi_at_[hidden])
Date: 2012-05-21 15:57:22


*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_oarchive). 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 SP1 (64-bit)*



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