Boost logo

Boost Users :

Subject: [Boost-users] [serialization] binary archiver with generic primitive types
From: Florent Teichteil (florent.teichteil_at_[hidden])
Date: 2009-08-31 09:34:07


Hi all,

The binary_oarchive and binary_iarchive classes serialize an object to a
stream whose buffer type is streambuf=basic_streambuf<char>. It means
that the serialized object is translated into an array of chars.

It is fine if you can work with an array containing chars.
Unfortunately, some libraries need serialized objects that are
translated into arrays whose type is different from char. For instance,
I'm using a library that requires an array of unsigned chars.

For that reason, I have defined generic (templated) tbinary_oarchive and
tbinary_iarchive classes based on the ones provided by the boost
serialization library. It is straightforward since the original boost
serialization classes use the std::ostream type that is just an
instantiation of the generic basic_ostream class with the char type
(ostream=basic_ostream<char>). For instance, the tbinary_oarchive class
is now defined as follows:

// CODE BEGIN
template <class Tdata>
class tbinary_oarchive :
     public binary_oarchive_impl<
         tbinary_oarchive<Tdata>, typename
std::basic_ostream<Tdata>::char_type, typename
std::basic_ostream<Tdata>::traits_type
>
{
public:
     tbinary_oarchive(std::basic_ostream<Tdata> & os, unsigned int flags
= 0) :
         binary_oarchive_impl<
             tbinary_oarchive, typename
std::basic_ostream<Tdata>::char_type, typename
std::basic_ostream<Tdata>::traits_type
>(os, flags)
     {}
     tbinary_oarchive(std::basic_streambuf<Tdata> & bsb, unsigned int
flags = 0) :
         binary_oarchive_impl<
             tbinary_oarchive, typename
std::basic_ostream<Tdata>::char_type, typename
std::basic_ostream<Tdata>::traits_type
>(bsb, flags)
     {}
};
// CODE END

Then, when I need to use a binary archiver with the type unsigned char,
I just put the following lines in my code:

// CODE BEGIN
typedef boost::archive::tbinary_oarchive<unsigned char> BinaryOBArchive;
typedef boost::archive::tbinary_iarchive<unsigned char> BinaryIBArchive;
BOOST_SERIALIZATION_REGISTER_ARCHIVE(BinaryOBArchive)
BOOST_SERIALIZATION_REGISTER_ARCHIVE(BinaryIBArchive)
// CODE END

This compiles fine except that I get some linker errors concerning the
basic_binary_oarchive and the basic_binary_oprimitive classes. For instance:

undefined reference to
`boost::archive::basic_binary_oprimitive<boost::archive::tbinary_oarchive<unsigned
char>, unsigned char, std::char_traits<unsigned char>
>::~basic_binary_oprimitive()'

It seems that parts of the serialization library's code is outside the
definition classes, preventing to extend it with different binary
types... Is there a workaround?

Thank you very much for your patience reading this e-mail entirely,
Florent.


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