pimpl.html 4.8 KB

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