Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/serialization/doc/derivation.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 7.4 KB
Line 
1<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!--
4(C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
5Use, modification and distribution is subject to the Boost Software
6License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7http://www.boost.org/LICENSE_1_0.txt)
8-->
9<head>
10<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
11<link rel="stylesheet" type="text/css" href="../../../boost.css">
12<link rel="stylesheet" type="text/css" href="style.css">
13<title>Serialization - Derivation from an Existing Archive</title>
14</head>
15<body link="#0000ff" vlink="#800080">
16<table border="0" cellpadding="7" cellspacing="0" width="100%" summary="header">
17  <tr> 
18    <td valign="top" width="300"> 
19      <h3><a href="../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../boost.png" border="0"></a></h3>
20    </td>
21    <td valign="top"> 
22      <h1 align="center">Serialization</h1>
23      <h2 align="center">Derivation from an Existing Archive</h2>
24    </td>
25  </tr>
26</table>
27<hr>
28<dl class="page-index">
29  <dt><a target="detail" href="#portable_archives">Portable Binary Archives</a>
30  <dt><a target="detail" href="#fast_archives">Fast Binary Archives</a>
31</dl>
32
33<a name=portable_archives>
34<h3>Portable Binary Archives</h3>
35It may happen that one wants to create a new archive class by derivation from one
36of the included ones. Included is a sample program that shows how to derive a
37new archive from one of the ones included with the library.  The first example is
38<a href="../example/demo_portable_archive.cpp" target="demo_portable_archive_cpp">demo_portable_archive.cpp</a>.
39This binary archive save/loads integers in a portable format.  To this end
40it is derived from the native binary archive and the save/load functions for
41integers are overridden with special versions which convert to big endian
42format if necessary.  It also implements an exception for the case where an
43integer saved on one platform is too large for the platform which is loading
44the archive.  This example doesn't address floating point types.
45This examples illustrates several issues that have to be addressed when doing
46something like this.  The discussion below refers to the output archive only but it
47applies equally to input archives as well.
48<ol>
49    <li><i>It is derived from</i> <code style="white-space: normal">binary_oarchive_impl<portable_binary_oarchive></code> 
50        <b>NOT</b> <code style="white-space: normal">binary_oarchive</code> <br>
51As described in the comments in
52<a href="../../../boost/archive/binary_oarchive.hpp" target="binary_oarchive_hpp">binary_oarchive.hpp</a>.
53<code style="white-space: normal">binary_oarchive</code> really a shorthand name for
54<code style="white-space: normal">binary_oarchive_impl&lt;binary_oarchive&gt;</code>.  So we should derive
55from <code style="white-space: normal">binary_oarchive_impl&lt;portable_binary_oarchive&gt;</code> rather
56than <code style="white-space: normal">binary_oarchive</code>.
57<pre><code>
58class portable_binary_oarchive :
59    // don't derive from binary_oarchive !!!
60    public binary_oarchive_impl&lt;portable_binary_oarchive&gt;
61{
62...
63</code></pre>
64    <li><i>Note the</i> <code style="white-space: normal">portable_binary_oarchive</code> <i>between the</i> &lt;&gt;
65This is required so that base classes can downcast their <code style="white-space: normal">this</code> pointer
66to the most derived class.  This is referred to as <b>C</b>uriously <b>R</b>ecurring
67<b>T</b>emplate <b>P</b>attern (<b>CRTP</b>) <a href="bibliography.html#11">[11]</a>
68It is used to implement static polymorphism.
69    <li><i>Base classes need to be explicitly given access to the derived class.</i>
70This can be done by making members public or by including friend declarations for
71the base classes.
72<pre><code>
73    typedef portable_binary_oarchive derived_t;
74    friend class detail::common_oarchive&lt;derived_t&gt;;
75    friend class basic_binary_oarchive&lt;derived_t&gt;;
76    friend class basic_binary_oprimitive&lt;derived_t, std::ostream&gt;;
77    friend class boost::serialization::save_access;
78</code></pre>
79    <li><i>Base class functions will usually need to be explicitly invoked</i>
80We commonly overload the function name <code style="white-space: normal">save</code> for saving primitives.
81This is very convenient.  Usage of a function name in a derived class
82"hides" similarly named functions of the base class.  That is,
83function name overloading doesn't automatically
84include base classes.  To address this, we can use:
85<pre><code>
86    using binary_oarchive_impl&lt;derived_t&gt;::save;
87    void save(const unsigned int t);
88    ...
89</code></pre>
90which should work on conforming compilers. However, I have found
91that the following equivalent works on more compilers.
92<pre><code>
93    // default fall through for any types not specified here
94    template&lt;class T&gt;
95    void save(const T & t){
96        binary_oarchive_impl&lt;derived_t&gt;::save(t);
97    }
98    void save(const unsigned int t);
99    ...
100</code></pre>
101so it's what I use.
102    <li><i>Template definitions of base classes may have to be included.</i>
103    The demo includes
104<pre><code>
105// explicitly instantiate for this type of binary stream
106#include &lt;boost/archive/basic_binary_oprimitive.ipp&gt;
107</code></pre>
108for just this purpose.  Failure to include required template definitions
109will result in undefined symbol errors when the program is linked.
110    <li><i>Without alteration, this class cannot be further derived from</i><br>
111Base classes using <b>CRTP</b> must be templates with a parameter corresponding to
112the most derived class.  As presented here, this class doesn't qualify, so
113it cannot be used as a base class.  In order to derive further from this class,
114it would have to be reorganized along the lines of the original <code style="white-space: normal">binary_oarchive</code>.
115Specifically, it would look something like:
116<pre><code>
117template&lt;class Archive&gt;
118class portable_binary_oarchive_impl :
119    // don't derive from binary_oarchive !!!
120    public binary_oarchive_impl&lt;Archive&gt;
121{
122    ...
123);
124
125// do not derived from this class !!!
126class portable_binary_oarchive :
127    public portable_binary_oarchive_impl&lt;portable_binary_oarchive&gt;
128{
129public:
130    portable_binary_oarchive(std::ostream & os, unsigned int flags = 0) :
131        portable_binary_oarchive_impl&lt;binary_oarchive&gt;(os, flags)
132    {}
133};
134</code></pre>
135
136</ol> 
137
138<a name=fast_archives>
139<h3>Fast Binary Archives</h3>
140The second example
141<a href="../example/demo_fast_archive.cpp" target="demo_fast_archive_cpp">demo_fast_archive.cpp</a>.
142is similar to the first one.  The difference is that it intercepts the serialization before
143the default serialization is invoked.  In this case we want to replace the default
144serialization of C arrays of integers with a faster one.  The default version
145will invoke serialization of each element of the array.  If its an array of
146integers, and we're not concerned with the archive being portable to another platform
147we can just save/load the whole array as a binary string of bytes.  This should
148be faster than the default element by element method.
149<p>
150The same considerations that applied when overriding the the save/load of primitives
151above apply here, and the code is very similar.
152<hr>
153<p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
154Distributed under the Boost Software License, Version 1.0. (See
155accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
156</i></p>
157</body>
158</html>
Note: See TracBrowser for help on using the repository browser.