Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/serialization/doc/archive_reference.html @ 20

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

added boost

File size: 21.0 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 - More on Archives</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">Archive Class Reference</h2>
24    </td>
25  </tr>
26</table>
27<hr>
28<dl class="page-index">
29  <dt><a href="#implementation">Implementation</a>
30  <dt><a href="#usage">Usage</a>
31  <dt><a href="#testing">Testing</a>
32  <dt><a href="#polymorphic">Polymorphic Archives</a>
33</dl>
34<h3><a name="implementation">Implementation</a></h3>
35The <a href="archives.html"><strong>Archive</strong></a> concept specifies the functions that a
36class must implement to in order to be used to serialize
37<a href="serialization.html"><strong>Serializable</strong></a> types.
38The library implements a family of archives appropriate for different purposes. 
39This section describes how they have been implemented and how one can implement his own archive class.
40Our discussion will focus on archives used for loading as the hierarchy is exactly analogous
41for archives used for saving data.
42<p>
43Our archives have been factored in to a tree of classes in order to minimize
44repetition of code.  This is shown in the accompanying
45<a target="class_diagram" href="class_diagram.html">class diagram</a>.
46All input archives should be derived from the following template:
47<pre><code>
48template&lt;class Archive&gt;
49detail::common_iarchive;
50</code></pre>
51<h4>Minimum Requirments</h4>
52An instance of the this template handles all the "bookkeeping" associated
53with serialization. In order to be a functional only the following additional
54functions <strong>must</strong> be defined:
55<dl>
56<dt><h4><code>void load(T &t);</code></h4></dt>
57<dd>
58This function must be implemented for all primitive data types.  This can be
59accomplished through the use of a member template or explict declarations
60for all prmititive types.
61</dd>
62
63<dt><h4><code>void load_binary(void *address, std::size_t size);</code></h4></dt>
64<dd>
65This function should <code style="white-space: normal">size</code> bytes from the archive, and
66copy them in to memory starting at address <code style="white-space: normal">address</code>.
67</dd>
68
69<dt><h4><code>friend class boost::archive::load_access;</code></h4></dt>
70<dd>
71In addition, such a class <strong>must</strong> provide a the following
72friend declaration grant access to the core serialization library to the functions
73of this class.
74</dd>
75
76</dl>
77
78So, the most trivial implementation of an input archive would look like this:
79
80<pre><code>
81<a href="../../../boost/archive/detail/common_iarchive.hpp" target="common_iarchive_hpp">
82#include &lt;boost/archive/detail/common_iarchive.hpp&gt;
83</a>
84
85/////////////////////////////////////////////////////////////////////////
86// class trivial_iarchive - read serialized objects from a input text stream
87class trivial_iarchive :
88    public boost::archive::detail::common_iarchive&lt;trivial_iarchive&gt;
89{
90    // permit serialization system priviledged access to permit
91    // implementation of inline templates for maximum speed.
92    friend class boost::archive::load_access;
93
94    // member template for loading primitive types.
95    // Override for any types/templates that special treatment
96    template&lt;class T&gt;
97    void load(T &amp; t);
98
99public:
100    //////////////////////////////////////////////////////////
101    // public interface used by programs that use the
102    // serialization library
103
104    // archives are expected to support this function
105    void load_binary(void *address, std::size_t count);
106};
107
108</code></pre>
109The simplest possible output archive class is exactly analogous to the above.
110In the following discussion, only input archives will be addressed.
111Output archives are exactly symmetrical to input archives.
112<p>
113Given a suitable definitions of <code style="white-space: normal">load</code>
114and <code style="white-space: normal">load_binary</code>,
115any program using serialization with a conforming C++ compiler should compile
116and run with this archive class.
117
118<h4>Optional Overrides</h4>
119
120The <code style="white-space: normal">detail::common_iarchive</code> class contains
121a number of functions that are used by various parts of the serialization library
122to help render the archive in a particular form.
123
124<dl>
125
126<dt><h4><code>void load_start()</code></h4></dt>
127<dd>
128<strong>Default</strong>:Does nothing.<br>
129<strong>Purpose</strong>:To inject/retrieve an object name into the archive.  Used
130by XML archive to inject "&lt;name " before data.
131</dd>
132<p>
133
134<dt><h4><code>void load_end()</code></h4></dt>
135<dd>
136<strong>Default</strong>:Does nothing.<br>
137<strong>Purpose</strong>:To inject/retrieve an object name into the archive. Used
138by XML archive to inject "&lt;/name&gt;" after data.
139<dd>
140</dd>
141<p>
142<dt><h4><code>void end_preamble()</code></h4></dt>
143<dd>
144<strong>Default</strong>:Does nothing.<br>
145<strong>Purpose</strong>:Called <strong>each time</strong> user data data is saved.
146Its not called when archive book keeping data is saved.  This is used by XML archives
147to determine  when to inject a "&gt;" character at end of XML header. XML output archives
148keep their own internal flag indicating that data being written is header data. This
149internal flag is reset when an object start tag is written. When
150<code style="white-space: normal">void end_preamble()</code> is invoked and this internal flag is set
151a "&gt;" character is appended to the output and the internal flag is reset. The default
152implementation for <code style="white-space: normal">void end_preamble()</code> is a no-op there by permitting it
153to be optimised away for archive classes that don't use it.
154</dd>
155<p>
156<dt><h4><code>
157template&lt;class T&gt;
158void load_override(T & t, int);
159</code></h4></dt>
160<dd>
161<strong>Default</strong>:Invokes <code style="white-space: normal">archive::load(Archive & ar, t)</code><br>
162This is the main entry into the serialization library.<br>
163<strong>Purpose</strong>:This can be overridden in cases where the data is to be written
164to the archive in some special way.  For example, XML archives implement special handling for
165name-value pairs by overriding this function template for name-value pairs. 
166This replaces the default name-value pair handling, which is just to throw away the name,
167with one appropriate for XML which writes out the start of an XML tag with the correct object name.
168<p>
169The second argument must be part of the function signature even this it is not used.
170Its purpose is to be sure that code is portable to compilers which fail to correctly
171implement partial function template ordering.  For more information see
172<a href="implementation.html#functiontemplateordering">this</a>.
173</dd>
174
175</dl>
176
177<h4>Types used by the serialization library</h4>
178The serialization library injects bookkeeping data into the serialization archive.
179This data includes things like object ids, version numbers, class names etc.  Each
180of these objects is included in a wrapper so that the archive class can override the
181implementation of <code style="white-space: normal">void load_override(T & t, int);</code>.
182For example, in the XML archive, the override for this type renders an object_id equal to 23 as
183"object_id=_23".  The following table lists the types used by the serialization library:
184<p>
185<table border>
186<tr><th align=left>type</th><th align=left><code style="white-space: normal">default<br>serialed as</code></th>
187<tr><td><code style="white-space: normal">version_type</code></td><td><code style="white-space: normal">unsigned int</code></td>             
188<tr><td><code style="white-space: normal">object_id_type</code></td><td><code style="white-space: normal">unsigned int</code></td>   
189<tr><td><code style="white-space: normal">object_id_reference_type</code></td><td><code style="white-space: normal">unsigned int</code></td>   
190<tr><td><code style="white-space: normal">class_id_type</code></td><td><code style="white-space: normal">int</code></td>   
191<tr><td><code style="white-space: normal">class_id_optional_type</code></td><td><code style="white-space: normal">nothing</code></td>   
192<tr><td><code style="white-space: normal">class_id_reference_type</code></td><td><code style="white-space: normal">int</code></td>   
193<tr><td><code style="white-space: normal">tracking_type</code></td><td><code style="white-space: normal">bool</code></td>   
194<tr><td><code style="white-space: normal">classname_type</code></td><td><code style="white-space: normal">string</code></td>   
195</table>
196<p>
197All of these are associated with a default serialization defined in terms of primitive types
198so it isn't a requirement to define <code style="white-space: normal">load_override</code> 
199for these types.
200<p>
201These are defined in
202<a href="../../../boost/archive/basic_archive.hpp" target="basic_archive_hpp"><code style="white-space: normal">basic_archive.hpp</code></a>.
203All of these types have been assigned an
204<a target="detail" href="traits.html#level">implementation level</a> of
205<code style="white-space: normal">primitive</code> and are convertible to types such as int, unsigned int, etc.
206So that they have default implementations.  This is illustrated by
207<a href="../../../boost/archive/basic_text_iarchive.hpp" target="basic_text_iarchive_hpp"><code style="white-space: normal">basic_text_iarchive.hpp</code></a>.
208which relies upon the default.  However, in some cases, overrides will have to be
209explicitly provided for these types. For an example see
210<a href="../../../boost/archive/basic_xml_iarchive.hpp" target="basic_xml_iarchive_hpp"><code style="white-space: normal">basic_xml_iarchive.hpp</code></a>.
211<p>
212In real practice, we probably won't be quite done.
213One or more of the following issues may need to be addressed:
214<ul>
215    <li>Many compilers fail to implement correct partial ordering of
216    function templates.  The archives included with this library work around
217    this using argument overloading.  This technique is described in
218    <a target="detail" href="implementation.html#functiontemplateordering">
219    another section of this manual</a>
220    <li>Even if we are using a conforming compiler, we might want our new archive class
221    to be portable to non-conforming compilers.
222    <li>Our archive format might require extra information inserted into it.  For
223    example, XML archives need &lt;name ... &gt;...&lt;/name&gt; surrounding
224    all data objects.
225    <li>Addressing any of the above may generate more issues to be addressed.
226    <li>The archives included with library are all templates which use a
227    <code style="white-space: normal">stream</code> as a template parameter rather than simple classes.
228    Combined with the above, even more issues arise with non-conforming compilers.
229</ul>
230The attached <a target="class_diagram" href="class_diagram.html">class diagram</a>
231shows the relationships between classes used to implement the serialization library.
232<p>
233A close examination of the archives included with the library illustrate
234what it takes to make a portable archive that covers all data types.
235<h3><a name="usage">Usage</a></h3>
236The newly created archive will usually be stored in its own header module.  All
237that is necessary is to include the header and construct an instance of the new archive.
238EXCEPT for one special case.
239<ul>
240    <li>Instances of a derived class are serialized through a base class pointer.
241    <li>Such instances are not "registered" neither implicitly nor explicitly. That
242    is, the macro <code style="white-space: normal">BOOT_CLASS_EXPORT</code> is used to instantiate the serialization
243    code for the included archives.
244</ul>
245The problem here is that BOOT_CLASS_EXPORT only generates code for those archives
246included with the library - not those added subsequently.  To generate code for
247newly created archive classes, the following should be used.
248<pre><code>
249#define BOOST_ARCHIVE_CUSTOM_OARCHIVE_TYPES trivial_oarchive
250#define BOOST_ARCHIVE_CUSTOM_IARCHIVE_TYPES trivial_iarchive
251</code></pre>
252before <code style="white-space: normal">BOOST_CLASS_EXPORT</code> is invoked for any serializable class.
253Failure to do this will not inhibit the program from compiling, linking
254and executing properly - except in one case.  If an instance of a derived
255class is serialized through a pointer to its base class, the program
256will throw an
257<a href="exceptions.html#unregistered_class"><code style="white-space: normal">unregistered_class</code></a>
258exception.
259<p>
260Only one of the above statements is permitted, However, any number of new archive
261classes can be specified as list separated by commas.
262
263<h4><a name="testing">Testing</h4>
264Exhaustive testing of the library requires testing the different aspects of object
265serialization with each archive.  There are 36 different tests that can run with any archive.  There are
2665 "standard archives" included with the system. (3 in systems which don't support wide
267charactor i/o).
268<p>
269In addition, there are 22 other tests which aren't related to any particular archive
270class.
271<p>
272The default <code style="white-space: normal">bjam</code> testing setup will run all
273the above described tests.  This will result in as many as 39 archive tests * 5
274standard archives + 25 general tests = 220 tests. Note that a complete test of the
275library would include DLL vs static library, release vs debug so the actual total
276would be closer to 880 tests.
277<p>
278For each archive there is a header file in the test directory similar to the one below.
279The name of this archive is passed to the test program by setting the
280environmental variable <code style="white-space: normal">BOOST_ARCHIVE_TEST</code>
281to the name of the header.  Here is the header file
282<code style="white-space: normal">test_archive.hpp</code> . Test header files for
283other archives are similar.
284<pre><code>
285// text_archive test header
286// include output archive header
287#include &lt;boost/archive/text_oarchive.hpp&gt;
288// set name of test output archive
289typedef boost::archive::text_oarchive test_oarchive;
290// set name of test output stream
291typedef std::ofstream test_ostream;
292
293// repeat the above for input archive
294#include &lt;boost/archive/text_iarchive.hpp&gt;
295typedef boost::archive::text_iarchive test_iarchive;
296typedef std::ifstream test_istream;
297
298// define open mode for streams
299//   binary archives should use std::ios_base::binary
300#define TEST_STREAM_FLAGS (std::ios_base::openmode)0
301</code></pre>
302
303To test a new archive, for example, portable binary archives, make a
304header file <code style="white-space: normal">portable_binary_archive.hpp</code>
305and invoke <code style="white-space: normal">bjam</code> with
306<pre><code> 
307-sBOOST_ARCHIVE_LIST=portable_binary_archive.hpp
308</code></pre>
309This process in encapsulated in the shell script
310<code style="white-space: normal">run_archive_test</code> whose command line is
311<pre><code>
312run_archive_test &lt;test header file&gt; &lt;toolset&gt; [&lt;boost root&gt;] [&lt;target directory&gt;]
313</code></pre>
314<h3><a name="polymorphic">Polymorphic Archives</a></h3>
315
316<h4>Motivation</h4>
317
318All archives described so far are implemented as templates.  Code to save and load
319data to archives is regenerated for each combination of archive class and data type.
320Under these cirumstances, a good optimizing compiler that can expand
321<code>inline</code> functions to enough depth will generate fast code. 
322However:
323<ul>
324<li>Much inline code may be replicated.
325<li>If there are several archive classes, code will be regenerated for each archive class.
326<li>If seriaiization code is placed in a library, that library must be rebuilt
327each time a new archive class is created.
328<li>If serialization code is placed in a DLL,
329  <ul>
330  <li>The DLL will contain versions of code for each known archive type. 
331    This would result in loading of DLLs which contain
332    much code that is not used - basically defeating one of the main motivations
333    for choosing to use a DLL in the first place.
334  <li>If a new archive is created and an application shipped, all DLLs have to be
335    rebuilt, and reshipped along with the application which uses the new archive.  Thus
336    the other main motivation for using a DLL is defeated.
337  </ul>
338</ul>
339
340<h4>Implementation</h4>
341The solution is the the pair <code>polymorphic_oarchive</code>
342and <code>polymorphic_iarchive</code>.  They present a common interface of virtual
343functions - no templates - that is equivalent to the standard templated one.
344
345This is shown in the accompanying
346<a target="class_diagram" href="class_diagram.html">class diagram</a>
347<p>
348The accompanying demo program in files
349
350<a target=demo_polymorphic_cp href="../example/demo_polymorphic.cpp"><code style="white-space: normal">demo_polymorphic.cpp</code></a>,
351<a target=demo_polymorphic_A_hpp href="../example/demo_polymorphic_A.hpp"><code style="white-space: normal">demo_polymorphic_A.hpp</code></a>, and
352<a target=demo_polymorphic_A_cpp href="../example/demo_polymorphic_A.cpp"><code style="white-space: normal">demo_polymorphic_A</code></a>
353show how polymorphic archives are to be used. Note the following:
354<ul>
355  <li><a target=demo_polymorphic_A_hpp href="../example/demo_polymorphic_A.hpp"><code style="white-space: normal">demo_polymorphic_A.hpp</code></a> and
356<a target=demo_polymorphic_A_cpp href="../example/demo_polymorphic_A.cpp"><code style="white-space: normal">demo_polymorphic_A.cpp</code></a>
357contain no templates and no reference to any specific archive implementation.  That is, they will
358only have to be compiled once for all archive implementations.  The even applies to archives classes
359created in the future.
360  <li>The main program <a target=demo_polymorphic_cp href="../example/demo_polymorphic.cpp"><code style="white-space: normal">demo_polymorphic.cpp</code></a>
361specifies a specific archive implementation. 
362</ul>
363As can be seen in the
364<a target="class_diagram" href="class_diagram.html">class diagram</a>
365and the header files, this implementation is just a composition of the polymorphic
366interface and the standard template driven implementation.  This composition is
367accomplished by the templates
368<a target=polymorphic_iarchive_impl_hpp href="../../../boost/archive/detail/polymorphic_iarchive_impl.hpp"><code style="white-space: normal">polymorphic_iarchive_impl.hpp</code></a>
369and
370<a target=polymorphic_oarchive_impl_hpp href="../../../boost/archive/detail/polymorphic_oarchive_impl.hpp"><code style="white-space: normal">polymorphic_oarchive_impl.hpp</code></a>.
371As these contain no code specific to the particular implementation archive, they can be used to create
372a polymorphic archive implementation from any functioning templated archive implementation.
373<p>
374As a convenience, small header files have been included which contain
375<code style="white-space: normal">typedef</code> for polymorphic implementation for each corresponding
376templated one.  For example, the headers
377<a target=polymorphic_text_iarchive_hpp href="../../../boost/archive/polymorphic_text_iarchive.hpp"><code style="white-space: normal">polymorphic_text_iarchive.hpp</code></a>
378and
379<a target=polymorphic_text_oarchive_hpp href="../../../boost/archive/polymorphic_text_oarchive.hpp"><code style="white-space: normal">polymorphic_text_oarchive.hpp</code></a>.
380contain the <code style="white-space: normal">typedef</code> for the polymorphic implementation
381of the standard text archive classes 
382<a target=text_iarchive_hpp href="../../../boost/archive/text_iarchive.hpp"><code style="white-space: normal">text_iarchive.hpp</code></a>
383and
384<a target=text_oarchive_hpp href="../../../boost/archive/text_oarchive.hpp"><code style="white-space: normal">text_oarchive.hpp</code></a>
385respectively. All included polymorphic archives use the same naming scheme.
386<h4>Usage</h4>
387Polymorphic archives address the issues raised above regarding templated implementation.
388That is, there is no replicated code, and no recompilation for new archives.  This will
389result in smaller executables for program which use more than one type of archive, and
390smaller DLLS. There is a
391penalty for calling archive functions through a virtual function dispatch table and there
392is no possibility for a compiler to <code style="white-space: normal">inline</code> archive functions.  This will result
393in a detectable degradation in performance for saving and loading archives.
394<p>
395The main utility of polymorphic archives will be to permit the buiding of class DLLs that will
396include serialization code for all present and future archives with no redundant code.
397<hr>
398<p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
399Distributed under the Boost Software License, Version 1.0. (See
400accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
401</i></p>
402</body>
403</html>
Note: See TracBrowser for help on using the repository browser.