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 - Derivation from an Existing Archive</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">Derivation from an Existing Archive</h2> |
---|
24 | </td> |
---|
25 | </tr> |
---|
26 | </table> |
---|
27 | <hr> |
---|
28 | <dl class="page-index"> |
---|
29 | <dt><a target="detail" href="#portable_archives">Portable Binary Archives</a> |
---|
30 | <dt><a target="detail" href="#fast_archives">Fast Binary Archives</a> |
---|
31 | </dl> |
---|
32 | |
---|
33 | <a name=portable_archives> |
---|
34 | <h3>Portable Binary Archives</h3> |
---|
35 | It may happen that one wants to create a new archive class by derivation from one |
---|
36 | of the included ones. Included is a sample program that shows how to derive a |
---|
37 | new archive from one of the ones included with the library. The first example is |
---|
38 | <a href="../example/demo_portable_archive.cpp" target="demo_portable_archive_cpp">demo_portable_archive.cpp</a>. |
---|
39 | This binary archive save/loads integers in a portable format. To this end |
---|
40 | it is derived from the native binary archive and the save/load functions for |
---|
41 | integers are overridden with special versions which convert to big endian |
---|
42 | format if necessary. It also implements an exception for the case where an |
---|
43 | integer saved on one platform is too large for the platform which is loading |
---|
44 | the archive. This example doesn't address floating point types. |
---|
45 | This examples illustrates several issues that have to be addressed when doing |
---|
46 | something like this. The discussion below refers to the output archive only but it |
---|
47 | applies equally to input archives as well. |
---|
48 | <ol> |
---|
49 | <li><i>It is derived from</i> <code style="white-space: normal">binary_oarchive_impl<portable_binary_oarchive></code> |
---|
50 | <b>NOT</b> <code style="white-space: normal">binary_oarchive</code> <br> |
---|
51 | As described in the comments in |
---|
52 | <a href="../../../boost/archive/binary_oarchive.hpp" target="binary_oarchive_hpp">binary_oarchive.hpp</a>. |
---|
53 | <code style="white-space: normal">binary_oarchive</code> really a shorthand name for |
---|
54 | <code style="white-space: normal">binary_oarchive_impl<binary_oarchive></code>. So we should derive |
---|
55 | from <code style="white-space: normal">binary_oarchive_impl<portable_binary_oarchive></code> rather |
---|
56 | than <code style="white-space: normal">binary_oarchive</code>. |
---|
57 | <pre><code> |
---|
58 | class portable_binary_oarchive : |
---|
59 | // don't derive from binary_oarchive !!! |
---|
60 | public binary_oarchive_impl<portable_binary_oarchive> |
---|
61 | { |
---|
62 | ... |
---|
63 | </code></pre> |
---|
64 | <li><i>Note the</i> <code style="white-space: normal">portable_binary_oarchive</code> <i>between the</i> <> |
---|
65 | This is required so that base classes can downcast their <code style="white-space: normal">this</code> pointer |
---|
66 | to the most derived class. This is referred to as <b>C</b>uriously <b>R</b>ecurring |
---|
67 | <b>T</b>emplate <b>P</b>attern (<b>CRTP</b>) <a href="bibliography.html#11">[11]</a>. |
---|
68 | It is used to implement static polymorphism. |
---|
69 | <li><i>Base classes need to be explicitly given access to the derived class.</i> |
---|
70 | This can be done by making members public or by including friend declarations for |
---|
71 | the base classes. |
---|
72 | <pre><code> |
---|
73 | typedef portable_binary_oarchive derived_t; |
---|
74 | friend class detail::common_oarchive<derived_t>; |
---|
75 | friend class basic_binary_oarchive<derived_t>; |
---|
76 | friend class basic_binary_oprimitive< |
---|
77 | derived_t, |
---|
78 | std::ostream::char_type, |
---|
79 | std::ostream::traits_type |
---|
80 | >; |
---|
81 | friend class boost::serialization::save_access; |
---|
82 | </code></pre> |
---|
83 | <li><i>Base class functions will usually need to be explicitly invoked</i> |
---|
84 | We commonly overload the function name <code style="white-space: normal">save</code> for saving primitives. |
---|
85 | This is very convenient. Usage of a function name in a derived class |
---|
86 | "hides" similarly named functions of the base class. That is, |
---|
87 | function name overloading doesn't automatically |
---|
88 | include base classes. To address this, we can use: |
---|
89 | <pre><code> |
---|
90 | using binary_oarchive_impl<derived_t>::save; |
---|
91 | void save(const unsigned int t); |
---|
92 | ... |
---|
93 | </code></pre> |
---|
94 | which should work on conforming compilers. However, I have found |
---|
95 | that the following equivalent works on more compilers. |
---|
96 | <pre><code> |
---|
97 | // default fall through for any types not specified here |
---|
98 | template<class T> |
---|
99 | void save(const T & t){ |
---|
100 | binary_oarchive_impl<derived_t>::save(t); |
---|
101 | } |
---|
102 | void save(const unsigned int t); |
---|
103 | ... |
---|
104 | </code></pre> |
---|
105 | so it's what I use. |
---|
106 | <li><i>Template definitions of base classes may have to be included.</i> |
---|
107 | The demo includes |
---|
108 | <pre><code> |
---|
109 | // explicitly instantiate for this type of binary stream |
---|
110 | #include <boost/archive/basic_binary_oprimitive.ipp> |
---|
111 | </code></pre> |
---|
112 | for just this purpose. Failure to include required template definitions |
---|
113 | will result in undefined symbol errors when the program is linked. |
---|
114 | <li><i>Without alteration, this class cannot be further derived from</i><br> |
---|
115 | Base classes using <b>CRTP</b> must be templates with a parameter corresponding to |
---|
116 | the most derived class. As presented here, this class doesn't qualify, so |
---|
117 | it cannot be used as a base class. In order to derive further from this class, |
---|
118 | it would have to be reorganized along the lines of the original <code style="white-space: normal">binary_oarchive</code>. |
---|
119 | Specifically, it would look something like: |
---|
120 | <pre><code> |
---|
121 | template<class Archive> |
---|
122 | class portable_binary_oarchive_impl : |
---|
123 | // don't derive from binary_oarchive !!! |
---|
124 | public binary_oarchive_impl<Archive> |
---|
125 | { |
---|
126 | ... |
---|
127 | ); |
---|
128 | |
---|
129 | // do not derived from this class !!! |
---|
130 | class portable_binary_oarchive : |
---|
131 | public portable_binary_oarchive_impl<portable_binary_oarchive> |
---|
132 | { |
---|
133 | public: |
---|
134 | portable_binary_oarchive(std::ostream & os, unsigned int flags = 0) : |
---|
135 | portable_binary_oarchive_impl<binary_oarchive>(os, flags) |
---|
136 | {} |
---|
137 | }; |
---|
138 | </code></pre> |
---|
139 | |
---|
140 | </ol> |
---|
141 | |
---|
142 | <a name=fast_archives> |
---|
143 | <h3>Fast Binary Archives</h3> |
---|
144 | The second example |
---|
145 | <a href="../example/demo_fast_archive.cpp" target="demo_fast_archive_cpp">demo_fast_archive.cpp</a>. |
---|
146 | is similar to the first one. The difference is that it intercepts the serialization before |
---|
147 | the default serialization is invoked. In this case we want to replace the default |
---|
148 | serialization of C arrays of integers with a faster one. The default version |
---|
149 | will invoke serialization of each element of the array. If its an array of |
---|
150 | integers, and we're not concerned with the archive being portable to another platform |
---|
151 | we can just save/load the whole array as a binary string of bytes. This should |
---|
152 | be faster than the default element by element method. |
---|
153 | <p> |
---|
154 | The same considerations that applied when overriding the the save/load of primitives |
---|
155 | above apply here, and the code is very similar. |
---|
156 | <hr> |
---|
157 | <p><i>© Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004. |
---|
158 | Distributed under the Boost Software License, Version 1.0. (See |
---|
159 | accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
160 | </i></p> |
---|
161 | </body> |
---|
162 | </html> |
---|