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="#strong_type"><code style="white-space: normal">BOOST_STRONG_TYPEDEF</code></a> |
---|
31 | <dt><a href="#nvp">Name-Value Pairs</a> |
---|
32 | <dt><a href="#composition">Composition</a> |
---|
33 | </dl> |
---|
34 | Sometimes it convenient to create a temporary object just to support serialization |
---|
35 | of some underlying data. This permits an archive class to define special |
---|
36 | handling of this type. The library includes several such types for varying |
---|
37 | purposes. |
---|
38 | <p> |
---|
39 | |
---|
40 | <h3><a name="binaryobjects">Binary Objects</a></h3> |
---|
41 | A binary object is just an sequence of bytes stored as raw |
---|
42 | binary data. This would most likely be used for a large amount |
---|
43 | of "light weight" data such as a pixel map or embedded binary file. |
---|
44 | The header file |
---|
45 | <a href="../../../boost/serialization/binary_object.hpp" target="binary_object_hpp"> |
---|
46 | binary_object.hpp |
---|
47 | </a> |
---|
48 | includes the constructors: |
---|
49 | <pre><code> |
---|
50 | boost::serialization::binary_object(void * t, size_t size); |
---|
51 | boost::serialization::make_binary_object(void * t, size_t size); |
---|
52 | </code></pre> |
---|
53 | which will construct a temporary binary object that can be serialized just like any other object. |
---|
54 | Its default serialization is to use archive class primitives |
---|
55 | <code style="white-space: normal">save_binary</code> and <code>load_binary</code>. |
---|
56 | Note that it doesn't allocated any storage or create any objects. |
---|
57 | Its sole purpose is to pass the data size and address as pair to the archive class. |
---|
58 | |
---|
59 | <h3><a name="strong_type"><code style="white-space: normal">BOOST_STRONG_TYPEDEF</code></h3> |
---|
60 | Another example of a serialization wrapper is the |
---|
61 | <a href="strong_typedef.html"><code style="white-space: normal">BOOST_STRONG_TYPEDEF</code></a> template. |
---|
62 | The serialization libraries uses these to pass particular kinds of integers such |
---|
63 | as object_id, version, etc. to an archive class. Given that these integers |
---|
64 | are now distinguishable according to their type, XML archives can apply |
---|
65 | special handling to these types. For example, a version number is rendered |
---|
66 | as an XML attribute in the form "version=12". In the absence of any specific override, |
---|
67 | these types are automatically converted to the underlying integer type so the |
---|
68 | special overrides used for XML archives aren't needed for other archives. |
---|
69 | |
---|
70 | <h3><a name="nvp">Name-Value Pairs</h3> |
---|
71 | XML archives present a somewhat special case. XML format has a nested |
---|
72 | structure that maps well to the "recursive class member visitor" pattern used |
---|
73 | by the serialization system. However, XML differs from other formats |
---|
74 | in that it requires a name for each class data member. Our goal is to |
---|
75 | add this information to the class serialization specification while |
---|
76 | still permiting the the serialization code to be used with any archive. |
---|
77 | <p> |
---|
78 | Our solution is to wrap class members to be serialized in a |
---|
79 | <strong>name-value-pair</strong>. This structure is defined in |
---|
80 | <a href="../../../boost/serialization/nvp.hpp" target="nvp_hpp">nvp.hpp</a>. |
---|
81 | It is just a reference to the data member coupled with a pointer to |
---|
82 | to a <code style="white-space: normal">const char *</code> which |
---|
83 | corresponds to the XML name. It implements the default |
---|
84 | serialization functions for a name-value pair. This default |
---|
85 | action is to just ignore the item name and serialize the |
---|
86 | data value in the normal manner. For archive classes that |
---|
87 | don't make any special provision for name-value pairs, this |
---|
88 | is the action which will be invoked when the name-value pair |
---|
89 | is serialized. Hence, wrapping a data value into a name-value |
---|
90 | pair will have no effect when used with archives which |
---|
91 | make no special provision for this wrapper. |
---|
92 | <p> |
---|
93 | The xml archive classes contain code similar to: |
---|
94 | <pre><code> |
---|
95 | // special treatment for name-value pairs. |
---|
96 | template<class T> |
---|
97 | xml_oarchive & operator&(const boost::serialization::nvp<T> & t) |
---|
98 | { |
---|
99 | // write an xml start tag |
---|
100 | start_tag(t.name()); |
---|
101 | |
---|
102 | // serialize the data as usual |
---|
103 | *this & t.value(); |
---|
104 | |
---|
105 | // write an xml end tag |
---|
106 | end_tag(t.name()); |
---|
107 | } |
---|
108 | </code></pre> |
---|
109 | The most obvious and convient name to assign to as the XML data item name |
---|
110 | is - surpise! - the name of the C++ class data member. So our serialization |
---|
111 | code will look like: |
---|
112 | <pre><code> |
---|
113 | ar & make_nvp("my_variable", my_variable); |
---|
114 | </code></pre> |
---|
115 | To simplify typing and enhance readability a macro is defined so we can write: |
---|
116 | <pre><code> |
---|
117 | ar & BOOST_SERIALIZATION_NVP(my_variable); |
---|
118 | </code></pre> |
---|
119 | Similarly there exists a macro definition that permits us to write: |
---|
120 | <pre><code> |
---|
121 | BOOST_SERIALIZATION_BASE_OBJECT_NVP(my_base_class) |
---|
122 | </code></pre> |
---|
123 | <p> |
---|
124 | Included is |
---|
125 | <a href="../example/demo_xml.hpp" target="demo_xml_hpp">demo_xml.hpp<a> |
---|
126 | which renders it's data members as <strong>name-value-pair</strong>s and |
---|
127 | <a href="../example/demo_xml.cpp" target="demo_xml_cpp">demo_xml.cpp<a> |
---|
128 | which saves and loads data to an XML archive. |
---|
129 | <a href="../example/demo_save.xml" target="demo_save_xml">Here</a> |
---|
130 | is example of the XML Archive corresponding to our tutorial example. |
---|
131 | |
---|
132 | <h3><a name="composition">Composition</h3> |
---|
133 | Wrappers should be designed so that they can be composed as necessary. |
---|
134 | For example, to pass binary data as a name value pair use: |
---|
135 | <pre><code> |
---|
136 | ar & make_nvp("named_binary_object", make_binary_object(address, size)); |
---|
137 | </code></pre> |
---|
138 | </html> |
---|
139 | <hr> |
---|
140 | <p><i>© Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004. |
---|
141 | Distributed under the Boost Software License, Version 1.0. (See |
---|
142 | accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
143 | </i></p> |
---|
144 | </body> |
---|
145 | </html> |
---|
146 | |
---|