Brian Silverman | 8867871 | 2018-08-04 23:56:48 -0700 | [diff] [blame^] | 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<class T></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 | |
| 40 | <pre><code> |
| 41 | template<class Archive, class T> |
| 42 | inline void save( |
| 43 | Archive & ar, |
| 44 | const boost::shared_ptr<T> &t, |
| 45 | const unsigned int /* file_version */ |
| 46 | ){ |
| 47 | const T * t_ptr = t.get(); |
| 48 | // just serialize the underlying raw pointer |
| 49 | ar <<: boost::serialization::make_nvp("px", t_ptr); |
| 50 | } |
| 51 | |
| 52 | template<class Archive, class T> |
| 53 | inline void load( |
| 54 | Archive & ar, |
| 55 | boost::shared_ptr<T> &t, |
| 56 | const unsigned int file_version |
| 57 | ){ |
| 58 | T* r; |
| 59 | // recover the underlying raw pointer |
| 60 | ar >> boost::serialization::make_nvp("px", r); |
| 61 | |
| 62 | // To Do - match up with other shared pointers which |
| 63 | // use this same raw pointer. |
| 64 | ... |
| 65 | } |
| 66 | </code></pre> |
| 67 | |
| 68 | In principle, this is very much simpler than the original implementation. Completion of |
| 69 | this code requires: |
| 70 | |
| 71 | <ol> |
| 72 | <li>Filling in the "To Do". This required making an extra map for |
| 73 | <code style="white-space: normal">shared_ptr</code> instances. |
| 74 | <li>A method for identifying pointers to the same objects from pointers to their base classes. |
| 75 | <li>Backward compatibility with pointers serialized by the previous method. This exploits |
| 76 | the serialization class versioning. |
| 77 | <li>Proper handling of <code style="white-space: normal">weak_ptr</code>. |
| 78 | </ol> |
| 79 | |
| 80 | The result of this effort can be found in |
| 81 | <a target = serialization_shared_ptr href="../../../boost/serialization/shared_ptr.hpp"> |
| 82 | <code style="white-space: normal">boost::serialization::shared_ptr.hpp</code> |
| 83 | </a> |
| 84 | <p> |
| 85 | Note that if your code needs to read archives created under boost version 1.32, you will |
| 86 | have to include the following |
| 87 | |
| 88 | <pre><code> |
| 89 | #include <boost/serialization/shared_ptr_132.hpp> |
| 90 | #include <boost/serialization/shared_ptr.hpp> |
| 91 | </code></pre> |
| 92 | rather than just |
| 93 | <pre><code> |
| 94 | #include <boost/serialization/shared_ptr.hpp> |
| 95 | </code></pre> |
| 96 | |
| 97 | <hr> |
| 98 | <p><i>© Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004. |
| 99 | Distributed under the Boost Software License, Version 1.0. (See |
| 100 | accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 101 | </i></p> |
| 102 | </body> |
| 103 | </html> |