shared_ptr2.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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>Template serialization - shared_ptr</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"><code style="white-space: normal">shared_ptr&lt;class T&gt;</code> Revisited</h2>
  24. </td>
  25. </tr>
  26. </table>
  27. <hr>
  28. The previously described serialization of <code style="white-space: normal">shared_ptr</code>
  29. illustrates the straightforward way of serializing a moderately complicated class structure.
  30. Unfortunately, this way of doing it suffered from some undesirable features
  31. <ul>
  32. <li>It was dependent on the Boost implementation of <code style="white-space: normal">shared_ptr</code>.
  33. The <code style="white-space: normal">shared_ptr</code> interface has been included
  34. in <code style="white-space: normal">std::tr1</code> and may someday be included in the standard
  35. C++ library. An implementation which depends only on the public interface can be guaranteed to
  36. function with any other future implementation of <code style="white-space: normal">shared_ptr</code>.
  37. <li>It required extra macros for export.
  38. </ul>
  39. <pre><code>
  40. template&lt;class Archive, class T&gt;
  41. inline void save(
  42. Archive & ar,
  43. const boost::shared_ptr&ltT&gt; &t,
  44. const unsigned int /* file_version */
  45. ){
  46. const T * t_ptr = t.get();
  47. // just serialize the underlying raw pointer
  48. ar &lt;&lt: boost::serialization::make_nvp("px", t_ptr);
  49. }
  50. template&lt;class Archive, class T&gt
  51. inline void load(
  52. Archive & ar,
  53. boost::shared_ptr&lt;T&gt; &t,
  54. const unsigned int file_version
  55. ){
  56. T* r;
  57. // recover the underlying raw pointer
  58. ar >> boost::serialization::make_nvp("px", r);
  59. // To Do - match up with other shared pointers which
  60. // use this same raw pointer.
  61. ...
  62. }
  63. </code></pre>
  64. In principle, this is very much simpler than the original implementation. Completion of
  65. this code requires:
  66. <ol>
  67. <li>Filling in the "To Do". This required making an extra map for
  68. <code style="white-space: normal">shared_ptr</code> instances.
  69. <li>A method for identifying pointers to the same objects from pointers to their base classes.
  70. <li>Backward compatibility with pointers serialized by the previous method. This exploits
  71. the serialization class versioning.
  72. <li>Proper handling of <code style="white-space: normal">weak_ptr</code>.
  73. </ol>
  74. The result of this effort can be found in
  75. <a target = serialization_shared_ptr href="../../../boost/serialization/shared_ptr.hpp">
  76. <code style="white-space: normal">boost::serialization::shared_ptr.hpp</code>
  77. </a>
  78. <p>
  79. Note that if your code needs to read archives created under boost version 1.32, you will
  80. have to include the following
  81. <pre><code>
  82. #include &lt;boost/serialization/shared_ptr_132.hpp&gt;
  83. #include &lt;boost/serialization/shared_ptr.hpp&gt;
  84. </code></pre>
  85. rather than just
  86. <pre><code>
  87. #include &lt;boost/serialization/shared_ptr.hpp&gt;
  88. </code></pre>
  89. <hr>
  90. <p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
  91. Distributed under the Boost Software License, Version 1.0. (See
  92. accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  93. </i></p>
  94. </body>
  95. </html>