Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/doc/derivation.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 7.5 KB
RevLine 
[29]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=UTF-8">
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;
77        derived_t,
78        std::ostream::char_type,
79        std::ostream::traits_type
80    &gt;;
81    friend class boost::serialization::save_access;
82</code></pre>
83    <li><i>Base class functions will usually need to be explicitly invoked</i>
84We commonly overload the function name <code style="white-space: normal">save</code> for saving primitives.
85This is very convenient.  Usage of a function name in a derived class
86"hides" similarly named functions of the base class.  That is,
87function name overloading doesn't automatically
88include base classes.  To address this, we can use:
89<pre><code>
90    using binary_oarchive_impl&lt;derived_t&gt;::save;
91    void save(const unsigned int t);
92    ...
93</code></pre>
94which should work on conforming compilers. However, I have found
95that the following equivalent works on more compilers.
96<pre><code>
97    // default fall through for any types not specified here
98    template&lt;class T&gt;
99    void save(const T & t){
100        binary_oarchive_impl&lt;derived_t&gt;::save(t);
101    }
102    void save(const unsigned int t);
103    ...
104</code></pre>
105so it's what I use.
106    <li><i>Template definitions of base classes may have to be included.</i>
107    The demo includes
108<pre><code>
109// explicitly instantiate for this type of binary stream
110#include &lt;boost/archive/basic_binary_oprimitive.ipp&gt;
111</code></pre>
112for just this purpose.  Failure to include required template definitions
113will result in undefined symbol errors when the program is linked.
114    <li><i>Without alteration, this class cannot be further derived from</i><br>
115Base classes using <b>CRTP</b> must be templates with a parameter corresponding to
116the most derived class.  As presented here, this class doesn't qualify, so
117it cannot be used as a base class.  In order to derive further from this class,
118it would have to be reorganized along the lines of the original <code style="white-space: normal">binary_oarchive</code>.
119Specifically, it would look something like:
120<pre><code>
121template&lt;class Archive&gt;
122class portable_binary_oarchive_impl :
123    // don't derive from binary_oarchive !!!
124    public binary_oarchive_impl&lt;Archive&gt;
125{
126    ...
127);
128
129// do not derived from this class !!!
130class portable_binary_oarchive :
131    public portable_binary_oarchive_impl&lt;portable_binary_oarchive&gt;
132{
133public:
134    portable_binary_oarchive(std::ostream & os, unsigned int flags = 0) :
135        portable_binary_oarchive_impl&lt;binary_oarchive&gt;(os, flags)
136    {}
137};
138</code></pre>
139
140</ol> 
141
142<a name=fast_archives>
143<h3>Fast Binary Archives</h3>
144The second example
145<a href="../example/demo_fast_archive.cpp" target="demo_fast_archive_cpp">demo_fast_archive.cpp</a>.
146is similar to the first one.  The difference is that it intercepts the serialization before
147the default serialization is invoked.  In this case we want to replace the default
148serialization of C arrays of integers with a faster one.  The default version
149will invoke serialization of each element of the array.  If its an array of
150integers, and we're not concerned with the archive being portable to another platform
151we can just save/load the whole array as a binary string of bytes.  This should
152be faster than the default element by element method.
153<p>
154The same considerations that applied when overriding the the save/load of primitives
155above apply here, and the code is very similar.
156<hr>
157<p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
158Distributed under the Boost Software License, Version 1.0. (See
159accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
160</i></p>
161</body>
162</html>
Note: See TracBrowser for help on using the repository browser.