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 - PIMPL</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">PIMPL</h2> |
---|
24 | </td> |
---|
25 | </tr> |
---|
26 | </table> |
---|
27 | <hr> |
---|
28 | PIMPL is a C++ programming idiom described by Herb Sutter <a href="bibliography.html#10">[10]</a> |
---|
29 | which stands for "Private Implementation". It is also referred to as |
---|
30 | the "Handle Body Idiom". Included in this library is a program called |
---|
31 | <a href="../example/demo_pimpl.cpp" target="demo_impl_cpp">demo_pimpl.cpp</a> |
---|
32 | which illustrates how this is used. The file |
---|
33 | <a href="../example/demo_pimpl_A.cpp" target="demo_impl__Acpp">demo_pimpl_A.hpp</a> |
---|
34 | contains the declaration of the a class that hides its implementation |
---|
35 | by including a pointer to struct B that is only defined as a pointer. |
---|
36 | <pre><code> |
---|
37 | // class whose declaration is hidden by a pointer |
---|
38 | struct B; |
---|
39 | |
---|
40 | struct A { |
---|
41 | // class a contains a pointer to a "hidden" declaration |
---|
42 | B *pimpl; |
---|
43 | template<class Archive> |
---|
44 | void serialize(Archive & ar, const unsigned int file_version); |
---|
45 | A(); |
---|
46 | }; |
---|
47 | </code></pre> |
---|
48 | Serialization of A requires access to the definition of B in order so it |
---|
49 | is defined in the separately compiled module: |
---|
50 | <a href="../example/demo_pimpl_A.cpp" target="demo_impl_A_cpp">demo_pimpl_A.cpp</a> |
---|
51 | by: |
---|
52 | <pre><code> |
---|
53 | #include "demo_pimpl_A.hpp" |
---|
54 | |
---|
55 | // "hidden" definition of class B |
---|
56 | struct B { |
---|
57 | int b; |
---|
58 | template<class Archive> |
---|
59 | void serialize(Archive & ar, const unsigned int file_version){ |
---|
60 | ar & b; |
---|
61 | } |
---|
62 | }; |
---|
63 | |
---|
64 | A::A() : |
---|
65 | pimpl(new B) |
---|
66 | {} |
---|
67 | |
---|
68 | A::~A(){ |
---|
69 | delete pimpl; |
---|
70 | } |
---|
71 | |
---|
72 | // now we can define the serialization for class A |
---|
73 | template<class Archive> |
---|
74 | void A::serialize(Archive & ar, const unsigned int file_version){ |
---|
75 | ar & pimpl; |
---|
76 | } |
---|
77 | </code></pre> |
---|
78 | As described in <a href="bibliography.html#10">[10]</a> this brings the |
---|
79 | following advantages: |
---|
80 | <ul> |
---|
81 | <li>type "B" can be used without using its header. |
---|
82 | <li>implementation of B can be changed without generating a |
---|
83 | cascade of recompilations. |
---|
84 | </ul> |
---|
85 | So, we compile the modules and everything is fine. However when we |
---|
86 | link, we get an error. Two symbols are undefined: |
---|
87 | <pre><code> |
---|
88 | void A::serialize(boost::archive::text_oarchive & ar, const unsigned int file_version); |
---|
89 | void A::serialize(boost::archive::text_iarchive & ar, const unsigned int file_version); |
---|
90 | </code></pre> |
---|
91 | The problem is that when compiling the above code, |
---|
92 | there is no instantiation of the <code style="white-space: normal">serialize</code> template. |
---|
93 | There can't be as its not "known" what types of archives |
---|
94 | the serialization is going to be used with. So these functions are "missing" |
---|
95 | when an attempt to link is made. The solution is to explicitly instantiate |
---|
96 | serialization code for those archives which are going to be used. In this |
---|
97 | example, including the the following code in any *.cpp file does just that: |
---|
98 | <pre></code> |
---|
99 | #include <boost/archive/text_iarchive.hpp> |
---|
100 | #include <boost/archive/text_oarchive.hpp> |
---|
101 | |
---|
102 | template void A::serialize<boost::archive::text_iarchive>( |
---|
103 | boost::archive::text_iarchive & ar, |
---|
104 | const unsigned int file_version |
---|
105 | ); |
---|
106 | template void A::serialize<boost::archive::text_oarchive>( |
---|
107 | boost::archive::text_oarchive & ar, |
---|
108 | const unsigned int file_version |
---|
109 | ); |
---|
110 | </code></pre> |
---|
111 | The program should now link as well as compile. |
---|
112 | <p> |
---|
113 | The downside of this is that one has to know which archives are going |
---|
114 | to be used with hidden serializations. This is an effect of using template |
---|
115 | driven code. One can invoke explicity instantiation for all known templates and |
---|
116 | presume that the linker will exclude unreferenced code. This should |
---|
117 | work but is platform dependent. |
---|
118 | <hr> |
---|
119 | <p><i>© Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004. |
---|
120 | Distributed under the Boost Software License, Version 1.0. (See |
---|
121 | accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
122 | </i></p> |
---|
123 | </body> |
---|
124 | </html> |
---|