| 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 .  | 
|---|
| 5 | Use, modification and distribution is subject to the Boost Software | 
|---|
| 6 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | 
|---|
| 7 | http://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> | 
|---|
| 35 | The <a href="archives.html"><strong>Archive</strong></a> concept specifies the functions that a | 
|---|
| 36 | class must implement to in order to be used to serialize  | 
|---|
| 37 | <a href="serialization.html"><strong>Serializable</strong></a> types.  | 
|---|
| 38 | The library implements a family of archives appropriate for different purposes.   | 
|---|
| 39 | This section describes how they have been implemented and how one can implement his own archive class. | 
|---|
| 40 | Our discussion will focus on archives used for loading as the hierarchy is exactly analogous | 
|---|
| 41 | for archives used for saving data. | 
|---|
| 42 | <p> | 
|---|
| 43 | Our archives have been factored in to a tree of classes in order to minimize | 
|---|
| 44 | repetition of code.  This is shown in the accompanying  | 
|---|
| 45 | <a target="class_diagram" href="class_diagram.html">class diagram</a>. | 
|---|
| 46 | All input archives should be derived from the following template: | 
|---|
| 47 | <pre><code> | 
|---|
| 48 | template<class Archive> | 
|---|
| 49 | detail::common_iarchive; | 
|---|
| 50 | </code></pre> | 
|---|
| 51 | <h4>Minimum Requirments</h4> | 
|---|
| 52 | An instance of the this template handles all the "bookkeeping" associated | 
|---|
| 53 | with serialization. In order to be a functional only the following additional | 
|---|
| 54 | functions <strong>must</strong> be defined: | 
|---|
| 55 | <dl> | 
|---|
| 56 | <dt><h4><code>void load(T &t);</code></h4></dt> | 
|---|
| 57 | <dd> | 
|---|
| 58 | This function must be implemented for all primitive data types.  This can be | 
|---|
| 59 | accomplished through the use of a member template or explict declarations | 
|---|
| 60 | for all prmititive types. | 
|---|
| 61 | </dd> | 
|---|
| 62 |  | 
|---|
| 63 | <dt><h4><code>void load_binary(void *address, std::size_t size);</code></h4></dt> | 
|---|
| 64 | <dd> | 
|---|
| 65 | This function should <code style="white-space: normal">size</code> bytes from the archive, and  | 
|---|
| 66 | copy 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> | 
|---|
| 71 | In addition, such a class <strong>must</strong> provide a the following | 
|---|
| 72 | friend declaration grant access to the core serialization library to the functions | 
|---|
| 73 | of this class. | 
|---|
| 74 | </dd> | 
|---|
| 75 |  | 
|---|
| 76 | </dl> | 
|---|
| 77 |  | 
|---|
| 78 | So, 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 <boost/archive/detail/common_iarchive.hpp> | 
|---|
| 83 | </a> | 
|---|
| 84 |  | 
|---|
| 85 | ///////////////////////////////////////////////////////////////////////// | 
|---|
| 86 | // class trivial_iarchive - read serialized objects from a input text stream | 
|---|
| 87 | class trivial_iarchive :  | 
|---|
| 88 |     public boost::archive::detail::common_iarchive<trivial_iarchive> | 
|---|
| 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<class T> | 
|---|
| 97 |     void load(T & t); | 
|---|
| 98 |  | 
|---|
| 99 | public: | 
|---|
| 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> | 
|---|
| 109 | The simplest possible output archive class is exactly analogous to the above. | 
|---|
| 110 | In the following discussion, only input archives will be addressed. | 
|---|
| 111 | Output archives are exactly symmetrical to input archives. | 
|---|
| 112 | <p> | 
|---|
| 113 | Given a suitable definitions of <code style="white-space: normal">load</code> | 
|---|
| 114 | and <code style="white-space: normal">load_binary</code>,  | 
|---|
| 115 | any program using serialization with a conforming C++ compiler should compile  | 
|---|
| 116 | and run with this archive class. | 
|---|
| 117 |  | 
|---|
| 118 | <h4>Optional Overrides</h4> | 
|---|
| 119 |  | 
|---|
| 120 | The <code style="white-space: normal">detail::common_iarchive</code> class contains | 
|---|
| 121 | a number of functions that are used by various parts of the serialization library | 
|---|
| 122 | to 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 | 
|---|
| 130 | by XML archive to inject "<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 | 
|---|
| 138 | by XML archive to inject "</name>" 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.  | 
|---|
| 146 | Its not called when archive book keeping data is saved.  This is used by XML archives  | 
|---|
| 147 | to determine  when to inject a ">" character at end of XML header. XML output archives  | 
|---|
| 148 | keep their own internal flag indicating that data being written is header data. This | 
|---|
| 149 | internal 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 | 
|---|
| 151 | a ">" character is appended to the output and the internal flag is reset. The default | 
|---|
| 152 | implementation for <code style="white-space: normal">void end_preamble()</code> is a no-op there by permitting it | 
|---|
| 153 | to be optimised away for archive classes that don't use it. | 
|---|
| 154 | </dd> | 
|---|
| 155 | <p> | 
|---|
| 156 | <dt><h4><code> | 
|---|
| 157 | template<class T> | 
|---|
| 158 | void 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> | 
|---|
| 162 | This 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  | 
|---|
| 164 | to the archive in some special way.  For example, XML archives implement special handling for | 
|---|
| 165 | name-value pairs by overriding this function template for name-value pairs.   | 
|---|
| 166 | This replaces the default name-value pair handling, which is just to throw away the name, | 
|---|
| 167 | with one appropriate for XML which writes out the start of an XML tag with the correct object name. | 
|---|
| 168 | <p> | 
|---|
| 169 | The second argument must be part of the function signature even this it is not used. | 
|---|
| 170 | Its purpose is to be sure that code is portable to compilers which fail to correctly | 
|---|
| 171 | implement 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> | 
|---|
| 178 | The serialization library injects bookkeeping data into the serialization archive. | 
|---|
| 179 | This data includes things like object ids, version numbers, class names etc.  Each | 
|---|
| 180 | of these objects is included in a wrapper so that the archive class can override the | 
|---|
| 181 | implementation of <code style="white-space: normal">void load_override(T & t, int);</code>. | 
|---|
| 182 | For 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> | 
|---|
| 197 | All of these are associated with a default serialization defined in terms of primitive types | 
|---|
| 198 | so it isn't a requirement to define <code style="white-space: normal">load_override</code>  | 
|---|
| 199 | for these types. | 
|---|
| 200 | <p> | 
|---|
| 201 | These 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>. | 
|---|
| 203 | All 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.  | 
|---|
| 206 | So 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>. | 
|---|
| 208 | which relies upon the default.  However, in some cases, overrides will have to be | 
|---|
| 209 | explicitly 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> | 
|---|
| 212 | In real practice, we probably won't be quite done.  | 
|---|
| 213 | One 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 <name ... >...</name> 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> | 
|---|
| 230 | The attached <a target="class_diagram" href="class_diagram.html">class diagram</a> | 
|---|
| 231 | shows the relationships between classes used to implement the serialization library. | 
|---|
| 232 | <p> | 
|---|
| 233 | A close examination of the archives included with the library illustrate | 
|---|
| 234 | what it takes to make a portable archive that covers all data types. | 
|---|
| 235 | <h3><a name="usage">Usage</a></h3> | 
|---|
| 236 | The newly created archive will usually be stored in its own header module.  All | 
|---|
| 237 | that is necessary is to include the header and construct an instance of the new archive. | 
|---|
| 238 | EXCEPT 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> | 
|---|
| 245 | The problem here is that BOOT_CLASS_EXPORT only generates code for those archives | 
|---|
| 246 | included with the library - not those added subsequently.  To generate code for | 
|---|
| 247 | newly 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> | 
|---|
| 252 | before <code style="white-space: normal">BOOST_CLASS_EXPORT</code> is invoked for any serializable class. | 
|---|
| 253 | Failure to do this will not inhibit the program from compiling, linking | 
|---|
| 254 | and executing properly - except in one case.  If an instance of a derived | 
|---|
| 255 | class is serialized through a pointer to its base class, the program | 
|---|
| 256 | will throw an | 
|---|
| 257 | <a href="exceptions.html#unregistered_class"><code style="white-space: normal">unregistered_class</code></a> | 
|---|
| 258 | exception. | 
|---|
| 259 | <p> | 
|---|
| 260 | Only one of the above statements is permitted, However, any number of new archive | 
|---|
| 261 | classes can be specified as list separated by commas. | 
|---|
| 262 |  | 
|---|
| 263 | <h4><a name="testing">Testing</h4> | 
|---|
| 264 | Exhaustive testing of the library requires testing the different aspects of object | 
|---|
| 265 | serialization with each archive.  There are 36 different tests that can run with any archive.  There are | 
|---|
| 266 | 5 "standard archives" included with the system. (3 in systems which don't support wide | 
|---|
| 267 | charactor i/o).  | 
|---|
| 268 | <p> | 
|---|
| 269 | In addition, there are 22 other tests which aren't related to any particular archive | 
|---|
| 270 | class. | 
|---|
| 271 | <p> | 
|---|
| 272 | The default <code style="white-space: normal">bjam</code> testing setup will run all | 
|---|
| 273 | the above described tests.  This will result in as many as 39 archive tests * 5  | 
|---|
| 274 | standard archives + 25 general tests = 220 tests. Note that a complete test of the | 
|---|
| 275 | library would include DLL vs static library, release vs debug so the actual total  | 
|---|
| 276 | would be closer to 880 tests. | 
|---|
| 277 | <p> | 
|---|
| 278 | For each archive there is a header file in the test directory similar to the one below. | 
|---|
| 279 | The name of this archive is passed to the test program by setting the | 
|---|
| 280 | environmental variable <code style="white-space: normal">BOOST_ARCHIVE_TEST</code> | 
|---|
| 281 | to the name of the header.  Here is the header file  | 
|---|
| 282 | <code style="white-space: normal">test_archive.hpp</code> . Test header files for | 
|---|
| 283 | other archives are similar. | 
|---|
| 284 | <pre><code> | 
|---|
| 285 | // text_archive test header | 
|---|
| 286 | // include output archive header | 
|---|
| 287 | #include <boost/archive/text_oarchive.hpp> | 
|---|
| 288 | // set name of test output archive | 
|---|
| 289 | typedef boost::archive::text_oarchive test_oarchive; | 
|---|
| 290 | // set name of test output stream | 
|---|
| 291 | typedef std::ofstream test_ostream; | 
|---|
| 292 |  | 
|---|
| 293 | // repeat the above for input archive | 
|---|
| 294 | #include <boost/archive/text_iarchive.hpp> | 
|---|
| 295 | typedef boost::archive::text_iarchive test_iarchive; | 
|---|
| 296 | typedef 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 |  | 
|---|
| 303 | To test a new archive, for example, portable binary archives, make a | 
|---|
| 304 | header file <code style="white-space: normal">portable_binary_archive.hpp</code> | 
|---|
| 305 | and invoke <code style="white-space: normal">bjam</code> with | 
|---|
| 306 | <pre><code>  | 
|---|
| 307 | -sBOOST_ARCHIVE_LIST=portable_binary_archive.hpp | 
|---|
| 308 | </code></pre> | 
|---|
| 309 | This process in encapsulated in the shell script  | 
|---|
| 310 | <code style="white-space: normal">run_archive_test</code> whose command line is | 
|---|
| 311 | <pre><code> | 
|---|
| 312 | run_archive_test <test header file> <toolset> [<boost root>] [<target directory>] | 
|---|
| 313 | </code></pre> | 
|---|
| 314 | <h3><a name="polymorphic">Polymorphic Archives</a></h3> | 
|---|
| 315 |  | 
|---|
| 316 | <h4>Motivation</h4> | 
|---|
| 317 |  | 
|---|
| 318 | All archives described so far are implemented as templates.  Code to save and load | 
|---|
| 319 | data to archives is regenerated for each combination of archive class and data type. | 
|---|
| 320 | Under these cirumstances, a good optimizing compiler that can expand  | 
|---|
| 321 | <code>inline</code> functions to enough depth will generate fast code.   | 
|---|
| 322 | However: | 
|---|
| 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  | 
|---|
| 327 | each 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> | 
|---|
| 341 | The solution is the the pair <code>polymorphic_oarchive</code> | 
|---|
| 342 | and <code>polymorphic_iarchive</code>.  They present a common interface of virtual | 
|---|
| 343 | functions - no templates - that is equivalent to the standard templated one. | 
|---|
| 344 |  | 
|---|
| 345 | This is shown in the accompanying  | 
|---|
| 346 | <a target="class_diagram" href="class_diagram.html">class diagram</a> | 
|---|
| 347 | <p> | 
|---|
| 348 | The 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> | 
|---|
| 353 | show 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> | 
|---|
| 357 | contain no templates and no reference to any specific archive implementation.  That is, they will | 
|---|
| 358 | only have to be compiled once for all archive implementations.  The even applies to archives classes | 
|---|
| 359 | created 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> | 
|---|
| 361 | specifies a specific archive implementation.   | 
|---|
| 362 | </ul> | 
|---|
| 363 | As can be seen in the | 
|---|
| 364 | <a target="class_diagram" href="class_diagram.html">class diagram</a> | 
|---|
| 365 | and the header files, this implementation is just a composition of the polymorphic | 
|---|
| 366 | interface and the standard template driven implementation.  This composition is | 
|---|
| 367 | accomplished 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> | 
|---|
| 369 | and | 
|---|
| 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>. | 
|---|
| 371 | As these contain no code specific to the particular implementation archive, they can be used to create | 
|---|
| 372 | a polymorphic archive implementation from any functioning templated archive implementation. | 
|---|
| 373 | <p> | 
|---|
| 374 | As a convenience, small header files have been included which contain  | 
|---|
| 375 | <code style="white-space: normal">typedef</code> for polymorphic implementation for each corresponding | 
|---|
| 376 | templated 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> | 
|---|
| 378 | and | 
|---|
| 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>. | 
|---|
| 380 | contain the <code style="white-space: normal">typedef</code> for the polymorphic implementation | 
|---|
| 381 | of 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> | 
|---|
| 383 | and | 
|---|
| 384 | <a target=text_oarchive_hpp href="../../../boost/archive/text_oarchive.hpp"><code style="white-space: normal">text_oarchive.hpp</code></a> | 
|---|
| 385 | respectively. All included polymorphic archives use the same naming scheme. | 
|---|
| 386 | <h4>Usage</h4> | 
|---|
| 387 | Polymorphic archives address the issues raised above regarding templated implementation. | 
|---|
| 388 | That is, there is no replicated code, and no recompilation for new archives.  This will | 
|---|
| 389 | result in smaller executables for program which use more than one type of archive, and  | 
|---|
| 390 | smaller DLLS. There is a | 
|---|
| 391 | penalty for calling archive functions through a virtual function dispatch table and there | 
|---|
| 392 | is no possibility for a compiler to <code style="white-space: normal">inline</code> archive functions.  This will result | 
|---|
| 393 | in a detectable degradation in performance for saving and loading archives. | 
|---|
| 394 | <p> | 
|---|
| 395 | The main utility of polymorphic archives will be to permit the buiding of class DLLs that will | 
|---|
| 396 | include serialization code for all present and future archives with no redundant code. | 
|---|
| 397 | <hr> | 
|---|
| 398 | <p><i>© Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.  | 
|---|
| 399 | Distributed under the Boost Software License, Version 1.0. (See | 
|---|
| 400 | accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
|---|
| 401 | </i></p> | 
|---|
| 402 | </body> | 
|---|
| 403 | </html> | 
|---|