File: shared_ptr2.html

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (103 lines) | stat: -rw-r--r-- 4,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
(C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com . 
Use, modification and distribution is subject to the Boost Software
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../../boost.css">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Template serialization - shared_ptr</title>
</head>
<body link="#0000ff" vlink="#800080">
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary="header">
  <tr> 
    <td valign="top" width="300"> 
      <h3><a href="../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../boost.png" border="0"></a></h3>
    </td>
    <td valign="top"> 
      <h1 align="center">Serialization</h1>
      <h2 align="center"><code style="white-space: normal">shared_ptr&lt;class T&gt;</code> Revisted</h2>
    </td>
  </tr>
</table>
<hr>
The previously described serialization of <code style="white-space: normal">shared_ptr</code>
illustrates the straight forward way of serializing a moderatly compiicated class structure.
Unfortunately, this way of doing it suffered from some undesirable features
<ul>
  <li>It was dependent on the Boost implementation of <code style="white-space: normal">shared_ptr</code>.
  The <code style="white-space: normal">shared_ptr</code> interface has been included
  in <code style="white-space: normal">std::tr1</code> and may someday be included in the standard
  C++ library. An implementation which depends only on the public interface can be guarenteed to
  function with any other future implementation of <code style="white-space: normal">shared_ptr</code>.
  <li>It required extra macros for export.
</ul>

<pre><code>
template&lt;class Archive, class T&gt;
inline void save(
    Archive & ar,
    const boost::shared_ptr&ltT&gt; &t,
    const unsigned int /* file_version */
){
    const T * t_ptr = t.get();
    // just seriailize the underlying raw pointer
    ar &lt;&lt: boost::serialization::make_nvp("px", t_ptr);
}

template&lt;class Archive, class T&gt
inline void load(
    Archive & ar,
    boost::shared_ptr&lt;T&gt; &t,
    const unsigned int file_version
){
    T* r;
    // recover the underlying raw poiter
    ar >> boost::serialization::make_nvp("px", r);

    // To Do - match up with other shared pointers which 
    // use this same raw pointer.
    ...
}
</code></pre>

In priniciple this is very much simpler than the original implementation.  Completion of
this code requires:

<ol>
  <li>Filling in the "To Do".  This required making an extra map for
  <code style="white-space: normal">shared_ptr</code> instances.
  <li>A method for identifing pointers to the same objects from pointers to their base classes.
  <li>Backward compatibility with pointers serialized by the previous method.  This exploits
  the serialization class versioning.
  <li>Proper handling of <code style="white-space: normal">weak_ptr</code>.
</ol>

The result of this effort can be found in 
<a target = serialization_shared_ptr href="../../../boost/serialization/shared_ptr.hpp">
<code style="white-space: normal">boost::serialization::shared_ptr.hpp</code>
</a>
<p>
Note that if your code needs to read archives created under boost version 1.32, you will
have to include the following

<pre><code>
#include &lt;boost/serialization/shared_ptr_132.hpp&gt;
#include &lt;boost/serialization/shared_ptr.hpp&gt;
</code></pre>
rather than just
<pre><code>
#include &lt;boost/serialization/shared_ptr.hpp&gt;
</code></pre>

<hr>
<p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004. 
Distributed under the Boost Software License, Version 1.0. (See
accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
</i></p>
</body>
</html>