|
SerializationArchive Class Usage |
// a portable text archive
boost::archive::text_oarchive(ostream &s) // saving
boost::archive::text_iarchive(istream &s) // loading
// a portable text archive using a wide character stream
boost::archive::text_woarchive(wostream &s) // saving
boost::archive::text_wiarchive(wistream &s) // loading
// a non-portable native binary archive
boost::archive::binary_oarchive(ostream &s) // saving
boost::archive::binary_iarchive(istream &s) // loading
// a portable XML archive
boost::archive::xml_oarchive(ostream &s) // saving
boost::archive::xml_iarchive(istream &s) // loading
// a portable XML archive which uses wide characters - use for utf-8 output
boost::archive::xml_woarchive(wostream &s) // saving
boost::archive::xml_wiarchive(wistream &s) // loading
namespace boost {
namespace archive {
enum archive_flags {
no_header = 1, // suppress archive header info
no_codecvt = 2, // suppress alteration of codecvt facet
no_xml_tag_checking = 4 // suppress checking of xml tags - igored on saving
};
template<class OStream>
class oarchive : ...
{
...
public:
// called to save objects
template<class T>
oarchive & operator<<(const T & t);
template<class T>
oarchive & operator&(T & t)
{
return *this << t;
}
void save_binary(const void *address, std::size_t count);
template<class T>
register_type(T * t = NULL);
unsigned int library_version() const;
struct is_saving {
typedef mpl::bool_<true> type;
BOOST_STATIC_CONSTANT(bool, value=true);
};
struct is_loading {
typedef mpl::bool_<false> type;
BOOST_STATIC_CONSTANT(bool, value=false);
};
oarchive(OStream & os, unsigned int flags = 0);
~oarchive();
};
template<class T>
oarchive & operator<<(const T & t);
template<class T>
oarchive & operator&(T & t);
Appends an object of type T to the archive. The object may be
serialize
function has been defined.
void save_binary(const void *address, std::size_t count);
count
bytes found at
address
.
template<class T>
register_type(T * t = NULL);
unsigned int library_version() const;
is_saving::type = mpl::bool<true>;
is_saving::value= true;
is_loading::type = mpl::bool<false>;
is_loading::value= false;
oarchive(OStream & os, unsigned int flags = 0);
stream
as
an argument and optional flags. For most applications there will be no need to use
flags. Flags are taken from the following table and can be combined with the
|
operator.By default, archives prepend
output with initial data which helps identify them as archives produced by this system.
This permits a more graceful in the case where attempt is made to load an archive
from an invalid file format. In addition to this, each type of archive might have
its own information. For example, native binary archives include information about
sizes of native types and endianess to gracefully handle the case where it has been
erroneously assumed that such an archive is portable across platforms. In some cases
where this extra overhead might be considered objectionable, it can be suppressed with the
no_header
flag.
In some cases, an archive may alter (and later restore)
the codecvt facet of the stream locale. To suppress this action,
include the no_codecvt
flag.
XML archives contain nested tags signifying the start and end of data fields.
These tags are normally checked for aggreement with the object name when
data is loaded. If a mismatch occurs an exception is thrown. Its possible
that this may not be desired behavior. To suppress this checking of XML
tags, use no_xml_tag_checking
flag.
~oarchive();
template<class IStream>
class iarchive : ...
{
...
public:
// called to load objects
template<class T>
iarchive & operator>>(T & t);
template<class T>
iarchive & operator&(T & t)
{
return *this >> t;
}
void delete_created_pointers();
void load_binary(void *address, std::size_t count);
template<class T>
register_type(T * t = NULL);
unsigned int library_version() const;
struct is_saving {
typedef mpl::bool_<false> type;
BOOST_STATIC_CONSTANT(bool, value=false);
};
struct is_loading {
typedef mpl::bool_<true> type;
BOOST_STATIC_CONSTANT(bool, value=true);
};
iarchive(IStream & is, unsigned int flags = 0);
~iarchive();
};
} //namespace archive
) //namespace boost
template<class T>
iarchive & operator>>(T & t);
template<class T>
iarchive & operator&(T & t);
Retrieves an object of type T from the archive. The object may be
serialize
function has been defined.
void load_binary(void *address, std::size_t count);
count
bytes and stores
them in memory starting at address
.
void delete_created_pointers();
template<class T>
register_type(T * t = NULL);
unsigned int library_version() const;
is_saving::type = mpl::bool<false>;
is_saving::value= false;
is_loading::type = mpl::bool<true>;
is_loading::value= true;
iarchive(IStream & is, unsigned int flags = 0);
stream
as
an argument and optional flags. If flags are used, they should be the same
as those used when the archive was created. Function and usage of flags is described
above.
~iarchive();
The existence of the <<
and >>
suggest
a relationship between archives and C++ i/o streams. Archives are not
C++ i/o streams. All the archives included with this system take a stream
as an argument in the constructor and that stream is used for output or input.
However, this is not a requirement of the serialization functions or the
archive interface. It just turns out that the archives written so far have
found it useful to base their implementation on streams.
The template operators &, <<, and >> of the archive classes described above will generate code to save/load all primitive types to/from an archive. This code will usually just add the data to the archive according to the archive format. For example, a four byte integer is appended to a binary archive as 4 binary bytes while a to a text archive it would be rendered as a space followed by a string representation.
template<class Base, class Derived>
Base & base_object(Derived &d);
which should be used to create a reference to an object of the base
which can be used as an argument to the archive save/load operators:
ar & boost::serialization::base_object<Base>(*this);
Resist the temptation to just cast *this
to the base class.
This might seem to work but may fail to invoke code necessary for
proper serialization.
To properly save and restore an object through a pointer the following situations must be addressed:
Saving a pointer:
<<
and >>
operators
xml_w?archive
) renders it output as UTF-8 which can
handle any wide character without loss of information.
std::string
data is converted from multi-byte format to wide
character format using the current
locale
. Hence this version should give a fair rendering of all
C++ data for all cases. This could result in some unexpected behavior.
Suppose an std::string
is created with the locale
character
set to hebrew characters. On output this is converted to wide characters.
On input however, there could be a problem if the locale
is
not set the same as when the archive is created.
The normal character version (xml_?archive
) renders
std::string
output without any conversion. Though this may work
fine for serialization, it may create difficulties if the XML archive is used
for some other purpose.
© 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)