wrappers.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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=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 - Serialization Wrappers</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">Serialization Wrappers</h2>
  24. </td>
  25. </tr>
  26. </table>
  27. <hr>
  28. <dl class="page-index">
  29. <dt><a href="#binaryobjects">Binary Objects</a>
  30. <dt><a href="#arrays">Arrays</a>
  31. <dt><a href="#strong_type"><code style="white-space: normal">BOOST_STRONG_TYPEDEF</code></a>
  32. <dt><a href="#collection_size_type">Collection Sizes</a>
  33. <dt><a href="#nvp">Name-Value Pairs</a>
  34. <dt><a href="#composition">Composition</a>
  35. </dl>
  36. Sometimes it convenient to create a temporary object just to support serialization
  37. of some underlying data. This permits an archive class to define special
  38. handling of this type. The library includes several such types for varying
  39. purposes.
  40. <p>
  41. Wrappers need to be treated in a special way by some archives, and hence
  42. the <A href="traits.html#wrappers"><code>is_wrapper</code></a> trait for
  43. these wrapper classes is set to true.
  44. <h3><a name="binaryobjects">Binary Objects</a></h3>
  45. A binary object is just a sequence of bytes stored as raw
  46. binary data. This would most likely be used for a large amount
  47. of "light weight" data such as a pixel map or embedded binary file.
  48. The header file
  49. <a href="../../../boost/serialization/binary_object.hpp" target="binary_object_hpp">
  50. binary_object.hpp
  51. </a>
  52. includes the constructors:
  53. <pre><code>
  54. boost::serialization::binary_object(void * t, size_t size);
  55. boost::serialization::make_binary_object(void * t, size_t size);
  56. </code></pre>
  57. which will construct a temporary binary object that can be serialized just like any other object.
  58. Its default serialization is to use archive class primitives
  59. <code style="white-space: normal">save_binary</code> and <code>load_binary</code>.
  60. Note that it doesn't allocated any storage or create any objects.
  61. Its sole purpose is to pass the data size and address as a pair to the archive class.
  62. <h3><a name="arrays">Arrays</a></h3>
  63. An array is a contiguous sequence of homogeneous data types, such as a builtin
  64. C-array, a <code>boost::array&lt;T></code> or a <code>std::vector&lt;T></code>.
  65. The purpose of this wrapper is to support archive types (such as binary
  66. archives) that provide optimized serialization for contiguous sequences of
  67. objects of the same type.
  68. The header file
  69. <a href="../../../boost/serialization/array.hpp" target="array_hpp">
  70. array.hpp
  71. </a>
  72. includes the function
  73. <pre><code>
  74. template &lt;T>
  75. boost::serialization::make_array(T* t, std::size_t size);
  76. </code></pre>
  77. which will construct a temporary <code>array</code> object
  78. <pre><code>
  79. template<class T>
  80. class array
  81. {
  82. public:
  83. typedef T value_type;
  84. array(value_type* t, std::size_t s);
  85. value_type* address() const;
  86. std::size_t count() const;
  87. };
  88. </code></pre>
  89. that can be serialized just like any other object.
  90. Its default serialization is to serialize each array element.
  91. Note that it doesn't allocated any storage or create any objects.
  92. Its sole purpose is to pass the data type, size and address to the archive class.
  93. Archive types that can provide optimized implementations for contiguous
  94. arrays of homogeneous data types should overload the serialization of
  95. <code>array</code>.
  96. <h3><a name="strong_type"><code style="white-space: normal">BOOST_STRONG_TYPEDEF</code></h3>
  97. Another example of a serialization wrapper is the
  98. <a href="strong_typedef.html"><code style="white-space: normal">BOOST_STRONG_TYPEDEF</code></a> template.
  99. The serialization libraries uses these to pass particular kinds of integers such
  100. as object_id, version, etc. to an archive class. Given that these integers
  101. are now distinguishable according to their type, XML archives can apply
  102. special handling to these types. For example, a version number is rendered
  103. as an XML attribute in the form "version=12". In the absence of any specific override,
  104. these types are automatically converted to the underlying integer type so the
  105. special overrides used for XML archives aren't needed for other archives.
  106. <h3><a name="collection_size_type">Collection Sizes</h3>
  107. An example of a strong typedef is the <code>collection_size_type</code> in the
  108. header file
  109. <a href="../../../boost/serialization/collection_size_type.hpp" target="collection_size_type_hpp">
  110. collection_size_type.hpp
  111. </a>. This type should be used for serializaing the size of a C++ collection, so
  112. that the archive can pick the best integral representation for the serialization
  113. of collection sizes. This is necessary since, although <code>std::size_t</code>
  114. is guaranteed to be an integral type large enough to represent the size of
  115. a collection on a specific platform, the archive might want to serialize
  116. the size differently than this type. For example, the <code>collection_size_type</code>
  117. might be serialized as a variable length integer in a portable binary archive.
  118. <h3><a name="nvp">Name-Value Pairs</h3>
  119. XML archives present a somewhat special case. XML format has a nested
  120. structure that maps well to the "recursive class member visitor" pattern used
  121. by the serialization system. However, XML differs from other formats
  122. in that it requires a name for each class data member. Our goal is to
  123. add this information to the class serialization specification while
  124. still permiting the the serialization code to be used with any archive.
  125. <p>
  126. Our solution is to wrap class members to be serialized in a
  127. <strong>name-value-pair</strong>. This structure is defined in
  128. <a href="../../../boost/serialization/nvp.hpp" target="nvp_hpp">nvp.hpp</a>.
  129. It is just a reference to the data member coupled with a pointer to
  130. to a <code style="white-space: normal">const char *</code> which
  131. corresponds to the XML name. It implements the default
  132. serialization functions for a name-value pair. This default
  133. action is to just ignore the item name and serialize the
  134. data value in the normal manner. For archive classes that
  135. don't make any special provision for name-value pairs, this
  136. is the action which will be invoked when the name-value pair
  137. is serialized. Hence, wrapping a data value into a name-value
  138. pair will have no effect when used with archives which
  139. make no special provision for this wrapper.
  140. <p>
  141. The xml archive classes contain code similar to:
  142. <pre><code>
  143. // special treatment for name-value pairs.
  144. template&lt;class T&gt;
  145. xml_oarchive & operator&(const boost::serialization::nvp<T> & t)
  146. {
  147. // write an xml start tag
  148. start_tag(t.name());
  149. // serialize the data as usual
  150. *this & t.value();
  151. // write an xml end tag
  152. end_tag(t.name());
  153. }
  154. </code></pre>
  155. The most obvious and convient name to assign to as the XML data item name
  156. is - surprise! - the name of the C++ class data member. So our serialization
  157. code will look like:
  158. <pre><code>
  159. ar & make_nvp("my_variable", my_variable);
  160. </code></pre>
  161. To simplify typing and enhance readability a macro is defined so we can write:
  162. <pre><code>
  163. ar & BOOST_SERIALIZATION_NVP(my_variable);
  164. </code></pre>
  165. Similarly there exists a macro definition that permits us to write:
  166. <pre><code>
  167. BOOST_SERIALIZATION_BASE_OBJECT_NVP(my_base_class)
  168. </code></pre>
  169. Note that these macros must be used in the namespace of the class,
  170. and without qualifying the namespace in the argument.
  171. <p>
  172. <a href="../example/demo_gps.hpp" target="demo_gps_hpp">demo_gps.hpp<a>
  173. includes NVP wrappers or all data members.
  174. <a href="../example/demo_xml.cpp" target="demo_xml_cpp">demo_xml.cpp<a>
  175. saves and loads data to an XML archive.
  176. <a href="../example/demo_save.xml" target="demo_save_xml">Here</a>
  177. is example of the XML Archive corresponding to our tutorial example.
  178. <h3><a name="composition">Composition</h3>
  179. Wrappers should be designed so that they can be composed as necessary.
  180. For example, to pass binary data as a name value pair use:
  181. <pre><code>
  182. ar & make_nvp("named_binary_object", make_binary_object(address, size));
  183. </code></pre>
  184. </html>
  185. <hr>
  186. <p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
  187. Distributed under the Boost Software License, Version 1.0. (See
  188. accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  189. </i></p>
  190. </body>
  191. </html>