archives.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <link rel="stylesheet" type="text/css" href="../../../boost.css">
  6. <link rel="stylesheet" type="text/css" href="style.css">
  7. <title>Serialization - Archives</title>
  8. </head>
  9. <body link="#0000ff" vlink="#800080">
  10. <table border="0" cellpadding="7" cellspacing="0" width="100%" summary="header">
  11. <tr>
  12. <td valign="top" width="300">
  13. <h3><a href="../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../boost.png" border="0"></a></h3>
  14. </td>
  15. <td valign="top">
  16. <h1 align="center">Serialization</h1>
  17. <h2 align="center">Archive Concepts</h2>
  18. </td>
  19. </tr>
  20. </table>
  21. <hr>
  22. <dl class="page-index">
  23. <dt><a href="#saving_interface">Saving Archive Concept</a>
  24. <dt><a href="#loading_interface">Loading Archive Concept</a>
  25. <dt><a href="#archive_models">Models</a>
  26. <dt><a href="#exceptions">Exceptions</a>
  27. <dt><a href="#charactersets">Character Sets</a>
  28. </dl>
  29. <h4>Notation</h4>
  30. In the following descriptions
  31. <ul>
  32. <li><code>SA</code> is an type modeling the <a href="#saving_interface">Saving Archive Concept</a>.
  33. <li><code>sa</code> is an instance of type SA.
  34. <li><code>LA</code> is an type modeling the <a href="#loading_interface">Loading Archive Concept</a>.
  35. <li><code>la</code> is an instance of type LA.
  36. <li><code>T</code> is an <a href="serialization.html"><strong>Serializable</strong></a> Type.
  37. <li><code>x</code> is an instance of type T Type.
  38. <li><code>u,v</code> is a pointer to a an instance of type T.
  39. <li><code>count</code> is an instance of a type that can be converted to <code>std::size_t</code>.
  40. </ul>
  41. <h4><a name="saving_interface">Saving Archive Concept</a></h4>
  42. <h4>Associated Types</h4>
  43. Intuitively, a type modeling this concept will generate a sequence of bytes
  44. corresponding to an arbitrary set of C++ data structures. Each type modeling the
  45. Saving Archive concept (SA) may be associated with another type modeling the
  46. <a href="#loading_interface">Loading Archive Concept</a>(LA).
  47. This associated type will perform the inverse operation.
  48. That is, given a sequence of bytes generated by SA, it will generate a set of
  49. C++ data structures that is equivalent to the original.
  50. The notion of equivalence is defined by the implementations of the pair of archives and the
  51. way the data are rendered <a href="serialization.html">serializable</a>.
  52. <p>
  53. <h4>Valid Expressions</h4>
  54. <dl>
  55. <dt><h4><code>
  56. SA::is_saving
  57. </code></h4></dt>
  58. <dd>
  59. Returns the Boost MPL Integral Constant type boost::mpl::bool_&lt;true&gt;
  60. </dd>
  61. <dt><h4><code>
  62. SA::is_loading
  63. </code></h4></dt>
  64. <dd>
  65. Returns the Boost MPL Integral Constant type boost::mpl::bool_&lt;false&gt;
  66. </dd>
  67. <dt><h4><code>
  68. sa &lt;&lt; x
  69. <br>
  70. sa &amp; x
  71. </code></h4></dt>
  72. <dd>
  73. These expressions must perform exactly the same function. They append the
  74. value of <code style="white-space: normal">x</code> along with other information to <code>sa</code>.
  75. This other information is defined by the implementation of the archive.
  76. Typically this information is that which is required by a corresponding
  77. Loading Archive type to properly restore the value of <code>x</code>.
  78. <p>
  79. Returns a reference to <code>sa</code>.
  80. </dd>
  81. <dt><h4><code>
  82. sa.save_binary(u, count)
  83. </code></h4></dt>
  84. <dd>
  85. Appends to the archive <code style="white-space: normal">size_t(count)</code> bytes found at
  86. <code style="white-space: normal">u</code>.
  87. </dd>
  88. <dt><h4><code>
  89. sa.register_type&lt;T&gt;()
  90. <br>
  91. sa.register_type(u)
  92. </code></h4></dt>
  93. <dd>
  94. Appends information about class T to the archive. This information is used to
  95. construct the correct class when a derived pointer is loaded by a corresponding
  96. Loading Archive type.
  97. Invocation of this member function is referred to as "class registration".
  98. This is explained in detail in
  99. <a href="special.html#derivedpointers">Special Considerations - Derived Pointers</a>.
  100. The second syntax is included to permit this function to be called on non-conforming
  101. compilers when <code style="white-space: normal">sa</code> is a template argument.
  102. For more information, see <a target="detail" href="implementation.html#tempatesyntax">Template Invocation syntax</a>
  103. </dd>
  104. <dt><h4><code>
  105. sa.get_library_version()
  106. </code></h4></dt>
  107. <dd>
  108. Returns an unsigned integer containing the current version number of the serialization
  109. library. This number will be incremented each time the library is altered in such a
  110. way that serialization could be altered for some type. For example, suppose the type
  111. used for a count of collection members is changed. The code that loads collections
  112. might be conditioned on the library version to make sure that libraries created by
  113. previous versions of the library can still be read.
  114. </dd>
  115. <dt><h4><code>
  116. sa.get_helper&lt;Helper&gt;(void * const helper_instance_id = 0)
  117. </code></h4></dt>
  118. <dd>
  119. See <code>la.get_helper&lt;Helper&gt;(void * const helper_instance_id = 0)</code>
  120. below.
  121. </dd>
  122. </dl>
  123. <h4><a name="loading_interface">Loading Archive Concept</a></h4>
  124. <h4>Associated Types</h4>
  125. Each model of this concept presumes the
  126. existence of a corresponding type modeling the
  127. <a href="#saving_interface">Saving Archive Concept</a>.
  128. The purpose of an instance of this concept is to convert a sequence of bytes
  129. generated by this corresponding type to a set of C++ data structures
  130. equivalent to the original.
  131. <h4>Valid Expressions</h4>
  132. <dl>
  133. <dt><h4><code>
  134. LA::is_saving
  135. </code></h4></dt>
  136. <dd>
  137. Returns the Boost MPL Integral Constant type boost::mpl::bool_&lt;false&gt;
  138. </dd>
  139. <dt><h4><code>
  140. LA::is_loading
  141. </code></h4></dt>
  142. <dd>
  143. Returns the Boost MPL Integral Constant type boost::mpl::bool_&lt;true&gt;
  144. </dd>
  145. <dt><h4><code>
  146. la &gt;&gt; x
  147. <br>
  148. la &amp; x
  149. </code></h4></dt>
  150. <dd>
  151. These expressions must perform exactly the same function.
  152. Sets <code>x</code> to a value retrieved from <code>la</code>.
  153. <p>
  154. Returns a reference to <code>la</code>.
  155. </dd>
  156. <dt><h4><code>
  157. la.load_binary(u, count)
  158. </code></h4></dt>
  159. <dd>
  160. Retrieves from <code style="white-space: normal">la</code> <code style="white-space: normal">size_t(count)</code> bytes and stores
  161. them in memory starting at <code style="white-space: normal">u</code>.
  162. </dd>
  163. <dt>
  164. <dt><h4><code>
  165. la.register_type&lt;T&gt;()
  166. <br>
  167. la.register_type(u)
  168. </code></h4></dt>
  169. <dd>
  170. Retrieves information about class T from the archive. This information is used to
  171. construct the correct class when loading a pointer to a derived class not
  172. otherwise referred to in the program by name.
  173. Invocation of this member function is referred to as "class registration".
  174. This is explained in detail in
  175. <a href="special.html#derivedpointers">Special Considerations - Derived Pointers</a>.
  176. The second syntax is included to permit this function to be called on non-conforming
  177. compilers when <code style="white-space: normal">la</code> is a template argument.
  178. For more information, see <a target="detail" href="implementation.html#tempatesyntax">Template Invocation syntax</a>
  179. </dd>
  180. <dt><h4><code>
  181. la.get_library_version()
  182. </code></h4></dt>
  183. <dd>
  184. Returns an unsigned integer containing the version number of the serialization
  185. library that created the archive. This number will be incremented each time the
  186. library is altered in such a way that serialization could be altered for some type.
  187. For example, suppose the type used for a count of collection members is changed.
  188. The code that loads collections might be conditioned on the library version to make
  189. sure that libraries created by previous versions of the library can still be read.
  190. </dd>
  191. <dt><h4><code>
  192. la.get_helper&lt;Helper&gt;(void * const helper_instance_id)
  193. </code></h4></dt>
  194. <dd>
  195. Some otherwise unserializable types can be made serializable by inclusion of
  196. a helper object. The iconic example of this is shared_ptr which needs this
  197. helper object to keep track of previously loaded shared_ptr instances so they
  198. can be "matched up" with subsequently loaded ones.
  199. The first time <code style="white-space: normal">la.get_helper&lt;Helper&gt;(void * const helper_instance_id)</code>
  200. is invoked for a given helper_instance_id, <code style="white-space: normal">Helper</code>, a default-constructed
  201. <code style="white-space: normal">Helper</code> object is created, attached to
  202. <code style="white-space: normal">la</code> and a reference to it is returned. Subsequent
  203. invocations of <code style="white-space: normal">la.get_helper&lt;Helper&gt;(void * const helper_instance_id)</code> with the same id value return
  204. a reference to the formerly constructed object. All objects created in this manner are
  205. destroyed upon <code style="white-space: normal">la</code> destruction time. The purpose
  206. of helper objects is discussed in
  207. <a href="special.html#helpersupport">Special Considerations - Helper Support</a>.
  208. </dd>
  209. <dt><h4><code>
  210. la.reset_object_address(v, u)
  211. </code></h4></dt>
  212. <dd>
  213. Communicates to the archive that the object originally at address u has been
  214. moved to address v.
  215. <p>
  216. When an object is loaded to a temporary variable and later moved to another location,
  217. this function must be called in order communicate this fact. This permits the
  218. archive to properly implement object tracking. Object tracking is required in order
  219. to correctly implement serialization of pointers to instances of derived classes.
  220. </dd>
  221. <dt><h4><code>
  222. la.delete_created_pointers()
  223. </code></h4></dt>
  224. <dd>
  225. Deletes all objects created by the loading of pointers. This can be used to
  226. avoid memory leaks that might otherwise occur if pointers are being loaded
  227. and the archive load encounters an exception.
  228. </dd>
  229. </dl>
  230. There are archives based on text, binary and XML file
  231. formats but all have the above interface. Given that all archives present
  232. the same public interface, specifcation of serialization is exactly the same
  233. for all archives. Archive classes have other members not mentioned here.
  234. However they are related to the internal functioning of the library and
  235. are not meant to be called by users of an archive. Implementation of new
  236. archives is discussed in
  237. <a href="archive_reference.html#implementation">New Archives - Implementation</a>.
  238. <p>
  239. The existence of the <code style="white-space: normal">&lt;&lt;</code>
  240. and <code style="white-space: normal">&gt;&gt;</code> suggests
  241. a relationship between archives and C++ i/o streams. <strong>Archives are not
  242. C++ i/o streams</strong>. All the archives included with this system take a stream
  243. as an argument in the constructor and that stream is used for output or input.
  244. However, this is not a requirement of the serialization functions or the
  245. archive interface. It just turns out that the archives written so far have
  246. found it useful to base their implementation on streams.
  247. <h3><a name="archive_models">Archive Models</a></h3>
  248. This library includes various implementations of the Archive concept.
  249. An archive is defined by two complementary classes. One is for saving data while
  250. the other is for loading it.
  251. This library includes a number of archive implementations that are "ready to go" for the
  252. most common requirements. These classes implement the archive concept for differing data formats.
  253. They can be used "as is" or as a basis for developing one's own particular type of archive.
  254. An archive is defined by two complementary classes. One is for saving data while the other is for loading it.
  255. To invoke serialization using one of
  256. these archives, one or more of the following header files must be
  257. included in the code module containing the serialization code.
  258. <pre><code>
  259. // a portable text archive</a>
  260. <a href="../../../boost/archive/text_oarchive.hpp" target="text_oarchive_cpp">boost::archive::text_oarchive</a> // saving
  261. <a href="../../../boost/archive/text_iarchive.hpp" target="text_iarchive_cpp">boost::archive::text_iarchive</a> // loading
  262. // a portable text archive using a wide character stream</a>
  263. <a href="../../../boost/archive/text_woarchive.hpp">boost::archive::text_woarchive</a> // saving
  264. <a href="../../../boost/archive/text_wiarchive.hpp">boost::archive::text_wiarchive</a> // loading
  265. // a portable XML archive</a>
  266. <a href="../../../boost/archive/xml_oarchive.hpp" target="xml_oarchive_cpp">boost::archive::xml_oarchive</a> // saving
  267. <a href="../../../boost/archive/xml_iarchive.hpp" target="xml_iarchive_cpp">boost::archive::xml_iarchive</a> // loading
  268. // a portable XML archive which uses wide characters - use for utf-8 output</a>
  269. <a href="../../../boost/archive/xml_woarchive.hpp" target="xml_woarchive_cpp">boost::archive::xml_woarchive</a> // saving
  270. <a href="../../../boost/archive/xml_wiarchive.hpp" target="xml_wiarchive_cpp">boost::archive::xml_wiarchive</a> // loading
  271. // a non-portable native binary archive</a>
  272. <a href="../../../boost/archive/binary_oarchive.hpp" target="binary_oarchive_cpp">boost::archive::binary_oarchive</a> // saving
  273. <a href="../../../boost/archive/binary_iarchive.hpp" target="binary_iarchive_cpp">boost::archive::binary_iarchive</a> // loading
  274. <!--
  275. // a non-portable native binary archive which use wide character streams
  276. <a href="../../../boost/archive/binary_woarchive.hpp">boost::archive::binary_woarchive</a> // saving
  277. <a href="../../../boost/archive/binary_wiarchive.hpp">boost::archive::binary_wiarchive</a> // loading
  278. -->
  279. </code></pre>
  280. All of these archives implement the same interface. Hence, it should suffice to describe only one
  281. of them in detail. For this purpose we will use the text archive.
  282. <pre><code>
  283. namespace boost {
  284. namespace archive {
  285. enum archive_flags {
  286. no_header = 1, // suppress archive header info
  287. no_codecvt = 2, // suppress alteration of codecvt facet
  288. no_xml_tag_checking = 4 // suppress checking of xml tags - igored on saving
  289. };
  290. } // archive
  291. } // boost
  292. </code></pre>
  293. <pre><code>
  294. namespace boost {
  295. namespace archive {
  296. class text_oarchive : ...
  297. {
  298. ...
  299. public:
  300. ... // implementation of the <strong>Saving Archive</strong> concept
  301. text_oarchive(std::ostream & os, unsigned int flags = 0);
  302. ~text_oarchive();
  303. };
  304. } // archive
  305. } // boost
  306. </code></pre>
  307. <dl>
  308. <dt><h4><code>
  309. text_oarchive(std::ostream & os, unsigned int flags = 0);
  310. </code></h4></dt>
  311. <dd>
  312. Constructs an archive given an open <code style="white-space: normal">stream</code> as
  313. an argument and optional flags. For most applications there will be no need to use flags.
  314. Flags are defined by <code style="white-space: normal">enum archive_flags</code> enumerator.
  315. Multiple flags can be combined with the <code style="white-space: normal">|</code> operator.
  316. By default, archives prepend
  317. output with initial data which helps identify them as archives produced by this system.
  318. This permits a more graceful handling of the case where an attempt is made to load an archive
  319. from an invalid file format. In addition to this, each type of archive might have
  320. its own information. For example, native binary archives include information about
  321. sizes of native types and endianess to gracefully handle the case where it has been
  322. erroneously assumed that such an archive is portable across platforms. In some cases,
  323. where this extra overhead might be considered objectionable, it can be suppressed with the
  324. <code style="white-space: normal">no_header</code> flag.
  325. <p>
  326. In some cases, an archive may alter (and later restore)
  327. the codecvt facet of the stream locale. To suppress this action,
  328. include the <code style="white-space: normal">no_codecvt</code> flag.
  329. <p>
  330. XML archives contain nested tags signifying the start and end of data fields.
  331. These tags are normally checked for agreement with the object name when
  332. data is loaded. If a mismatch occurs an exception is thrown. It's possible
  333. that this may not be desired behavior. To suppress this checking of XML
  334. tags, use <code style="white-space: normal">no_xml_tag_checking</code> flag.
  335. </dd>
  336. <dt><h4><code>
  337. ~text_oarchive();
  338. </code></h4></dt>
  339. <dd>
  340. Destructor for an archive. This should be called before the stream is
  341. closed. It restores any altered stream facets to their state before the
  342. archive was opened.
  343. </dd>
  344. </dl>
  345. <pre><code>
  346. namespace boost {
  347. namespace archive {
  348. class text_iarchive : ...
  349. {
  350. ...
  351. public:
  352. ... // implementation of the <strong>Loading Archive</strong> concept
  353. text_iarchive(std::istream & is, unsigned int flags = 0);
  354. ~text_iarchive();
  355. };
  356. } //namespace archive
  357. ) //namespace boost
  358. </code></pre>
  359. <dl>
  360. <dt><h4><code>
  361. text_iarchive(std::istream & is, unsigned int flags = 0);
  362. </code></h4></dt>
  363. <dd>
  364. Contructs an archive given an open <code style="white-space: normal">stream</code> as
  365. an argument and optional flags. If flags are used, they should be the same
  366. as those used when the archive was created. Function and usage of flags is described
  367. above.
  368. </dd>
  369. <dt><h4><code>
  370. ~text_iarchive();
  371. </code></h4></dt>
  372. <dd>
  373. Destructor for an archive. This should be called before the stream is
  374. closed. It restores any altered stream facets to their state before the
  375. the archive was opened.
  376. </dd>
  377. </dl>
  378. <p>
  379. The <code style="white-space: normal">binary_oarchive</code> and
  380. <code style="white-space: normal">binary_iarchive</code> classes are
  381. implemented in terms of the more basic
  382. <code style="white-space: normal">std::streambuf</code>. So, in addition
  383. to the common class interface described above, they include the following
  384. constructors:
  385. <dl>
  386. <dt><h4><code>
  387. binary_oarchive(std::streambuf & bsb, unsigned int flags = 0);
  388. </code></h4></dt>
  389. and
  390. <dt><h4><code>
  391. binary_iarchive(std::streambuf & bsb, unsigned int flags = 0);
  392. </code></h4></dt>
  393. </dl>
  394. <h3><a name="exceptions">Exceptions</h3>
  395. All of the archive classes included may throw exceptions. The list of exceptions that might
  396. be thrown can be found in section <a target="detail" href="exceptions.html">Archive Exceptions</a>
  397. of this documentation.
  398. <h3><a name="charactersets">Character Sets</h3>
  399. This library includes two archive classes for XML. The wide character
  400. version (<code style="white-space: normal">xml_w?archive</code>) renders its output as UTF-8 which can
  401. handle any wide character without loss of information.
  402. <code style="white-space: normal">std::string</code> data is converted from multi-byte format to wide
  403. character format using the current <code style="white-space: normal">
  404. locale</code>. Hence this version should give a fair rendering of all
  405. C++ data for all cases. This could result in some unexpected behavior.
  406. Suppose an <code style="white-space: normal">std::string</code>
  407. is created with the <code style="white-space: normal">locale</code> character
  408. set to hebrew characters. On output this is converted to wide characters.
  409. On input however, there could be a problem if the <code style="white-space: normal">locale</code> is
  410. not set the same as when the archive is created.
  411. <p>
  412. The normal character version (<code style="white-space: normal">xml_?archive</code>) renders
  413. <code style="white-space: normal">std::string</code> output without any conversion. Though this may work
  414. fine for serialization, it may create difficulties if the XML archive is used
  415. for some other purpose.
  416. <hr>
  417. <p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
  418. Distributed under the Boost Software License, Version 1.0. (See
  419. accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  420. </i></p>
  421. </body>
  422. </html>