fix doc example typo
[boost.git] / boost / serialization / shared_ptr.hpp
blob43452da5feb75527f2c72ccb0273ebd345230767
1 #ifndef BOOST_SERIALIZATION_SHARED_PTR_HPP
2 #define BOOST_SERIALIZATION_SHARED_PTR_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // shared_ptr.hpp: serialization for boost shared pointer
12 // (C) Copyright 2004 Robert Ramey and Martin Ecker
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 // See http://www.boost.org for updates, documentation, and revision history.
19 #include <map>
20 #include <cstddef> // NULL
22 #include <boost/config.hpp>
23 #include <boost/mpl/integral_c.hpp>
24 #include <boost/mpl/integral_c_tag.hpp>
26 #include <boost/detail/workaround.hpp>
27 #include <boost/shared_ptr.hpp>
29 #include <boost/serialization/split_free.hpp>
30 #include <boost/serialization/nvp.hpp>
31 #include <boost/serialization/version.hpp>
32 #include <boost/serialization/tracking.hpp>
34 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
35 // shared_ptr serialization traits
36 // version 1 to distinguish from boost 1.32 version. Note: we can only do this
37 // for a template when the compiler supports partial template specialization
39 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
40 namespace boost {
41 namespace serialization{
42 template<class T>
43 struct version< ::boost::shared_ptr<T> > {
44 typedef mpl::integral_c_tag tag;
45 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
46 typedef BOOST_DEDUCED_TYPENAME mpl::int_<1> type;
47 #else
48 typedef mpl::int_<1> type;
49 #endif
50 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
51 BOOST_STATIC_CONSTANT(unsigned int, value = 1);
52 #else
53 BOOST_STATIC_CONSTANT(unsigned int, value = type::value);
54 #endif
56 // don't track shared pointers
57 template<class T>
58 struct tracking_level< ::boost::shared_ptr<T> > {
59 typedef mpl::integral_c_tag tag;
60 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
61 typedef BOOST_DEDUCED_TYPENAME mpl::int_< ::boost::serialization::track_never> type;
62 #else
63 typedef mpl::int_< ::boost::serialization::track_never> type;
64 #endif
65 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
66 BOOST_STATIC_CONSTANT(int, value = ::boost::serialization::track_never);
67 #else
68 BOOST_STATIC_CONSTANT(int, value = type::value);
69 #endif
72 #define BOOST_SERIALIZATION_SHARED_PTR(T)
73 #else
74 // define macro to let users of these compilers do this
75 #define BOOST_SERIALIZATION_SHARED_PTR(T) \
76 BOOST_CLASS_VERSION( \
77 ::boost::shared_ptr< T >, \
78 1 \
79 ) \
80 BOOST_CLASS_TRACKING( \
81 ::boost::shared_ptr< T >, \
82 ::boost::serialization::track_never \
83 ) \
84 /**/
85 #endif
87 namespace boost {
88 namespace serialization{
90 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
91 // serialization for shared_ptr
93 template<class Archive, class T>
94 inline void save(
95 Archive & ar,
96 const boost::shared_ptr<T> &t,
97 const unsigned int /* file_version */
99 // The most common cause of trapping here would be serializing
100 // something like shared_ptr<int>. This occurs because int
101 // is never tracked by default. Wrap int in a trackable type
102 BOOST_STATIC_ASSERT((tracking_level<T>::value != track_never));
103 const T * t_ptr = t.get();
104 ar << boost::serialization::make_nvp("px", t_ptr);
107 template<class Archive, class T>
108 inline void load(
109 Archive & ar,
110 boost::shared_ptr<T> &t,
111 const unsigned int file_version
113 // The most common cause of trapping here would be serializing
114 // something like shared_ptr<int>. This occurs because int
115 // is never tracked by default. Wrap int in a trackable type
116 BOOST_STATIC_ASSERT((tracking_level<T>::value != track_never));
117 T* r;
118 #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
119 if(file_version < 1){
120 //ar.register_type(static_cast<
121 // boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter<T> > *
122 //>(NULL));
123 ar.register_type(static_cast<
124 boost_132::detail::sp_counted_base_impl<T *, boost::archive::detail::null_deleter > *
125 >(NULL));
126 boost_132::shared_ptr<T> sp;
127 ar >> boost::serialization::make_nvp("px", sp.px);
128 ar >> boost::serialization::make_nvp("pn", sp.pn);
129 // got to keep the sps around so the sp.pns don't disappear
130 ar.append(sp);
131 r = sp.get();
133 else
134 #endif
136 ar >> boost::serialization::make_nvp("px", r);
138 ar.reset(t,r);
141 template<class Archive, class T>
142 inline void serialize(
143 Archive & ar,
144 boost::shared_ptr<T> &t,
145 const unsigned int file_version
147 // correct shared_ptr serialization depends upon object tracking
148 // being used.
149 BOOST_STATIC_ASSERT(
150 boost::serialization::tracking_level<T>::value
151 != boost::serialization::track_never
153 boost::serialization::split_free(ar, t, file_version);
156 } // namespace serialization
157 } // namespace boost
159 #endif // BOOST_SERIALIZATION_SHARED_PTR_HPP