|
SerializationDerivation from an Existing Archive |
binary_oarchive_impl
NOT binary_oarchive
binary_oarchive
really a shorthand name for
binary_oarchive_impl<binary_oarchive>
. So we should derive
from binary_oarchive_impl<portable_binary_oarchive>
rather
than binary_oarchive
.
class portable_binary_oarchive :
// don't derive from binary_oarchive !!!
public binary_oarchive_impl<portable_binary_oarchive>
{
...
portable_binary_oarchive
between the <>
This is required so that base classes can downcast their this
pointer
to the most derived class. This is referred to as Curiously Recurring
Template Pattern (CRTP) [11].
It is used to implement static polymorphism.
typedef portable_binary_oarchive derived_t;
friend class detail::common_oarchive<derived_t>;
friend class basic_binary_oarchive<derived_t>;
friend class basic_binary_oprimitive<
derived_t,
std::ostream::char_type,
std::ostream::traits_type
>;
friend class boost::serialization::save_access;
save
for saving primitives.
This is very convenient. Usage of a function name in a derived class
"hides" similarly named functions of the base class. That is,
function name overloading doesn't automatically
include base classes. To address this, we can use:
using binary_oarchive_impl<derived_t>::save;
void save(const unsigned int t);
...
which should work on conforming compilers. However, I have found
that the following equivalent works on more compilers.
// default fall through for any types not specified here
template<class T>
void save(const T & t){
binary_oarchive_impl<derived_t>::save(t);
}
void save(const unsigned int t);
...
so it's what I use.
// explicitly instantiate for this type of binary stream
#include <boost/archive/basic_binary_oprimitive.ipp>
for just this purpose. Failure to include required template definitions
will result in undefined symbol errors when the program is linked.
binary_oarchive
.
Specifically, it would look something like:
template<class Archive>
class portable_binary_oarchive_impl :
// don't derive from binary_oarchive !!!
public binary_oarchive_impl<Archive>
{
...
);
// do not derived from this class !!!
class portable_binary_oarchive :
public portable_binary_oarchive_impl<portable_binary_oarchive>
{
public:
portable_binary_oarchive(std::ostream & os, unsigned int flags = 0) :
portable_binary_oarchive_impl<binary_oarchive>(os, flags)
{}
};
The same considerations that applied when overriding the the save/load of primitives above apply here, and the code is very similar.
© Copyright Robert Ramey 2002-2004. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)