Serialization

Derivation from an Existing Archive

Log Archive

It may happen that one wants to create a new archive class by derivation from one
of the included ones. Included is a sample program that shows how to derive a
new archive from one of the ones included with the library. The first example is
demo_log.cpp.

This derivation from the xml archive writes output in xml without the extra
information required to read the data back into the application. It might be
used to export one's data as simple xml for other applications or for logging
data while debugging.

To this end it is derived from the included xml archive and the save functions for
some types are specialized for this application.

The serialization library is
implemented using the Curiously Recurring Template
Pattern (CRTP). Also, all common code is factored out into
separate modules to minimize code repetition. This makes derivation from
an existing archive less straight forward than it would otherwise be.

This example illustrates several issues that have to be addressed when doing
something like this

It is derived fromxml_oarchive_implNOTxml_oarchive
As described in the comments in
xml_oarchive.hpp.
xml_oarchive really a shorthand name for
xml_oarchive_impl<xml_oarchive>. So we should derive
from xml_oarchive_impl<log_archive> rather
than xml_oarchive.

Note thelog_archivebetween 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.

Base classes need to be explicitly given access to the derived class.
This can be done by making members public or by including friend declarations for
the base classes.

Reread Archive Internals.
This describes the class hierarchy so that you know what to override.

Note the usage of PFTO. Some compilers fail to provide support for
partial function template ordering. The serialization library works around by
using Partial Function Template Ordering in several places.
In archive classes, this takes
. This is done
in several places, including the archive classes themselves.

Base class functions will usually need to be explicitly invoked
We commonly specialize the function name save_override
for saving primitives. 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:

for just this purpose. Failure to include required template definitions
will result in undefined symbol errors when the program is linked.

Without alteration, this class cannot be further derived from
Base classes using CRTP must be templates with a parameter corresponding to
the most derived class. As presented here, this class doesn't qualify, so
it cannot be used as a base class. In order to derive further from this class,
it would have to be reorganized along the lines of the original xml_oarchive.
Specifically, it would look something like: