Boost logo

Boost :

From: JOAQUIN M. LOPEZ MUÑOZ (joaquin_at_[hidden])
Date: 2008-09-02 14:06:36


Hello,

The section on proposed case studies for Boost.Serialization talks
about the poosible inclusion of a so-called "generic runtime helper":

http://tinyurl.com/6khxns
http://www.boost.org/doc/libs/1_36_0/libs/serialization/doc/new_case_studies.html#archivehelper

which subject I had the pleasure to discuss with Mr. Ramey some
time ago. I've got a couple of questions regarding this feature:

1. Is there any tentative release version for getting this included?
2. Is the idea to include the generic runtime helper as part of the
core API of an Archive, or will it be just an adaptor? In other
words, would the core "Saving Archive" and "Loading Archive"
concpets be extended to also feature this runtime helper?

FWIW, I'm strongly in favor of having the runtime helper be part of
the core Archive concepts: it is my stance that this can be incredibly
useful to implement serialization code for types with special
lifetime semantics (as is the case with boost::shared_ptr
and boost::flyweight) without violating the encapsulation either of the
type or of Boost.Serialization. The key and only property of such a
runtime helper is its ability to associate an Archive object with a
type-dependent helper object, which I see as a very modest, yet
highly useful, extension to the Archive concept.

Just to exemplify how useful helpers can be, allow me to sketch the
serialization process of flyweight<T> as implemented with runtime
helpers:

* Saving: the helper<flyweight<T> > is just a bag of flyweight<T>s
whose elements are *exemplars*. when saving a new flyweight,
first we check whether an equivalent flyweight is already present
in the helper: if so, the index of the exemplar is output, otherwise,
the underlying T associated to the flyweight is saved and the
flyweight is added to the helper as a new exemplar.

* Loading: again the helper is a bag of flyweight<T>s. When
loading a flyweight<T>, we can read either an index or a T
object: in the former case we just initialize the flyweight with the
corresponding exampler; in the latter case we load the T object,
construct a flyweight from it, add it as new exemplar and
initialize the loading flyweight with it.

This implementation is totally non-intrusive and maximally efficient.
An observant reader will also notice that it can be translated
almost literally to a serialization scheme for shared_ptr, which shows
the potential of the runtime helper feature. Currently, shared_ptr
serialization relies on a non-documented adhoc helper, which
technically makes it a non-serializable type: promoting the runtime
helper to the Archive concept would allow to make shared_ptr a
first-class citizen. I also think more types can benefit from this
feature when implementing their serialization code.

Valid concerns may be raised that extending the Archive concept
would break backwards compatibility. However, it is easy to help
programmers migrate their user-defined archives to the new
concept: just provide a mix-in base class implementing the required
functionality, so that if

  class my_archive
  {
  };

models the original Archive concept, just adding the following:

  class my_archive: public boost::archive::runtime_helper_impl
  {
  };

would automatically make my_archive model the new concept.

Thank you,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk