sl@0
|
1 |
#ifndef BOOST_SERIALIZATION_SHARED_PTR_HPP
|
sl@0
|
2 |
#define BOOST_SERIALIZATION_SHARED_PTR_HPP
|
sl@0
|
3 |
|
sl@0
|
4 |
// MS compatible compilers support #pragma once
|
sl@0
|
5 |
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
sl@0
|
6 |
# pragma once
|
sl@0
|
7 |
#endif
|
sl@0
|
8 |
|
sl@0
|
9 |
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
sl@0
|
10 |
// shared_ptr.hpp: serialization for boost shared pointer
|
sl@0
|
11 |
|
sl@0
|
12 |
// (C) Copyright 2004 Robert Ramey and Martin Ecker
|
sl@0
|
13 |
// Use, modification and distribution is subject to the Boost Software
|
sl@0
|
14 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
sl@0
|
15 |
// http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
16 |
|
sl@0
|
17 |
// See http://www.boost.org for updates, documentation, and revision history.
|
sl@0
|
18 |
|
sl@0
|
19 |
#include <map>
|
sl@0
|
20 |
|
sl@0
|
21 |
#include <boost/config.hpp>
|
sl@0
|
22 |
#include <boost/mpl/integral_c.hpp>
|
sl@0
|
23 |
#include <boost/mpl/integral_c_tag.hpp>
|
sl@0
|
24 |
|
sl@0
|
25 |
#include <boost/detail/workaround.hpp>
|
sl@0
|
26 |
#include <boost/shared_ptr.hpp>
|
sl@0
|
27 |
#include <boost/throw_exception.hpp>
|
sl@0
|
28 |
|
sl@0
|
29 |
#include <boost/archive/archive_exception.hpp>
|
sl@0
|
30 |
|
sl@0
|
31 |
#include <boost/serialization/type_info_implementation.hpp>
|
sl@0
|
32 |
#include <boost/serialization/split_free.hpp>
|
sl@0
|
33 |
#include <boost/serialization/nvp.hpp>
|
sl@0
|
34 |
#include <boost/serialization/version.hpp>
|
sl@0
|
35 |
#include <boost/serialization/tracking.hpp>
|
sl@0
|
36 |
#include <boost/static_assert.hpp>
|
sl@0
|
37 |
|
sl@0
|
38 |
#include <boost/serialization/void_cast_fwd.hpp>
|
sl@0
|
39 |
|
sl@0
|
40 |
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
sl@0
|
41 |
// shared_ptr serialization traits
|
sl@0
|
42 |
// version 1 to distinguish from boost 1.32 version. Note: we can only do this
|
sl@0
|
43 |
// for a template when the compiler supports partial template specialization
|
sl@0
|
44 |
|
sl@0
|
45 |
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
sl@0
|
46 |
namespace boost {
|
sl@0
|
47 |
namespace serialization{
|
sl@0
|
48 |
template<class T>
|
sl@0
|
49 |
struct version< ::boost::shared_ptr<T> > {
|
sl@0
|
50 |
typedef mpl::integral_c_tag tag;
|
sl@0
|
51 |
#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
|
sl@0
|
52 |
typedef BOOST_DEDUCED_TYPENAME mpl::int_<1> type;
|
sl@0
|
53 |
#else
|
sl@0
|
54 |
typedef mpl::int_<1> type;
|
sl@0
|
55 |
#endif
|
sl@0
|
56 |
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
|
sl@0
|
57 |
BOOST_STATIC_CONSTANT(unsigned int, value = 1);
|
sl@0
|
58 |
#else
|
sl@0
|
59 |
BOOST_STATIC_CONSTANT(unsigned int, value = type::value);
|
sl@0
|
60 |
#endif
|
sl@0
|
61 |
};
|
sl@0
|
62 |
// don't track shared pointers
|
sl@0
|
63 |
template<class T>
|
sl@0
|
64 |
struct tracking_level< ::boost::shared_ptr<T> > {
|
sl@0
|
65 |
typedef mpl::integral_c_tag tag;
|
sl@0
|
66 |
#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
|
sl@0
|
67 |
typedef BOOST_DEDUCED_TYPENAME mpl::int_< ::boost::serialization::track_never> type;
|
sl@0
|
68 |
#else
|
sl@0
|
69 |
typedef mpl::int_< ::boost::serialization::track_never> type;
|
sl@0
|
70 |
#endif
|
sl@0
|
71 |
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
|
sl@0
|
72 |
BOOST_STATIC_CONSTANT(int, value = ::boost::serialization::track_never);
|
sl@0
|
73 |
#else
|
sl@0
|
74 |
BOOST_STATIC_CONSTANT(int, value = type::value);
|
sl@0
|
75 |
#endif
|
sl@0
|
76 |
};
|
sl@0
|
77 |
}}
|
sl@0
|
78 |
#define BOOST_SERIALIZATION_SHARED_PTR(T)
|
sl@0
|
79 |
#else
|
sl@0
|
80 |
// define macro to let users of these compilers do this
|
sl@0
|
81 |
#define BOOST_SERIALIZATION_SHARED_PTR(T) \
|
sl@0
|
82 |
BOOST_CLASS_VERSION( \
|
sl@0
|
83 |
::boost::shared_ptr< T >, \
|
sl@0
|
84 |
1 \
|
sl@0
|
85 |
) \
|
sl@0
|
86 |
BOOST_CLASS_TRACKING( \
|
sl@0
|
87 |
::boost::shared_ptr< T >, \
|
sl@0
|
88 |
::boost::serialization::track_never \
|
sl@0
|
89 |
) \
|
sl@0
|
90 |
/**/
|
sl@0
|
91 |
#endif
|
sl@0
|
92 |
|
sl@0
|
93 |
namespace boost {
|
sl@0
|
94 |
namespace serialization{
|
sl@0
|
95 |
|
sl@0
|
96 |
class extended_type_info;
|
sl@0
|
97 |
|
sl@0
|
98 |
namespace detail {
|
sl@0
|
99 |
|
sl@0
|
100 |
struct null_deleter {
|
sl@0
|
101 |
void operator()(void const *) const {}
|
sl@0
|
102 |
};
|
sl@0
|
103 |
|
sl@0
|
104 |
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
sl@0
|
105 |
// a common class for holding various types of shared pointers
|
sl@0
|
106 |
|
sl@0
|
107 |
class shared_ptr_helper {
|
sl@0
|
108 |
typedef std::map<void*, shared_ptr<void> > collection_type;
|
sl@0
|
109 |
typedef collection_type::const_iterator iterator_type;
|
sl@0
|
110 |
// list of shared_pointers create accessable by raw pointer. This
|
sl@0
|
111 |
// is used to "match up" shared pointers loaded at diferent
|
sl@0
|
112 |
// points in the archive
|
sl@0
|
113 |
collection_type m_pointers;
|
sl@0
|
114 |
// return a void pointer to the most derived type
|
sl@0
|
115 |
template<class T>
|
sl@0
|
116 |
void * object_identifier(T * t) const {
|
sl@0
|
117 |
const extended_type_info * true_type
|
sl@0
|
118 |
= type_info_implementation<T>::type::get_derived_extended_type_info(*t);
|
sl@0
|
119 |
// note:if this exception is thrown, be sure that derived pointer
|
sl@0
|
120 |
// is either regsitered or exported.
|
sl@0
|
121 |
if(NULL == true_type)
|
sl@0
|
122 |
boost::throw_exception(
|
sl@0
|
123 |
boost::archive::archive_exception(
|
sl@0
|
124 |
boost::archive::archive_exception::unregistered_class
|
sl@0
|
125 |
)
|
sl@0
|
126 |
);
|
sl@0
|
127 |
const boost::serialization::extended_type_info * this_type
|
sl@0
|
128 |
= boost::serialization::type_info_implementation<T>::type::get_instance();
|
sl@0
|
129 |
void * vp = void_downcast(*true_type, *this_type, t);
|
sl@0
|
130 |
return vp;
|
sl@0
|
131 |
}
|
sl@0
|
132 |
public:
|
sl@0
|
133 |
template<class T>
|
sl@0
|
134 |
void reset(shared_ptr<T> & s, T * r){
|
sl@0
|
135 |
if(NULL == r){
|
sl@0
|
136 |
s.reset();
|
sl@0
|
137 |
return;
|
sl@0
|
138 |
}
|
sl@0
|
139 |
// get pointer to the most derived object. This is effectively
|
sl@0
|
140 |
// the object identifer
|
sl@0
|
141 |
void * od = object_identifier(r);
|
sl@0
|
142 |
|
sl@0
|
143 |
iterator_type it = m_pointers.find(od);
|
sl@0
|
144 |
|
sl@0
|
145 |
if(it == m_pointers.end()){
|
sl@0
|
146 |
s.reset(r);
|
sl@0
|
147 |
m_pointers.insert(collection_type::value_type(od,s));
|
sl@0
|
148 |
}
|
sl@0
|
149 |
else{
|
sl@0
|
150 |
s = static_pointer_cast<T>((*it).second);
|
sl@0
|
151 |
}
|
sl@0
|
152 |
}
|
sl@0
|
153 |
virtual ~shared_ptr_helper(){}
|
sl@0
|
154 |
};
|
sl@0
|
155 |
|
sl@0
|
156 |
} // namespace detail
|
sl@0
|
157 |
|
sl@0
|
158 |
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
sl@0
|
159 |
// utility function for creating/getting a helper - could be useful in general
|
sl@0
|
160 |
// but shared_ptr is the only class (so far that needs it) and I don't have a
|
sl@0
|
161 |
// convenient header to place it into.
|
sl@0
|
162 |
template<class Archive, class H>
|
sl@0
|
163 |
H &
|
sl@0
|
164 |
get_helper(Archive & ar){
|
sl@0
|
165 |
extended_type_info * eti = type_info_implementation<H>::type::get_instance();
|
sl@0
|
166 |
shared_ptr<void> sph;
|
sl@0
|
167 |
ar.lookup_helper(eti, sph);
|
sl@0
|
168 |
if(NULL == sph.get()){
|
sl@0
|
169 |
sph = shared_ptr<H>(new H);
|
sl@0
|
170 |
ar.insert_helper(eti, sph);
|
sl@0
|
171 |
}
|
sl@0
|
172 |
return * static_cast<H *>(sph.get());
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
sl@0
|
176 |
// serialization for shared_ptr
|
sl@0
|
177 |
|
sl@0
|
178 |
template<class Archive, class T>
|
sl@0
|
179 |
inline void save(
|
sl@0
|
180 |
Archive & ar,
|
sl@0
|
181 |
const boost::shared_ptr<T> &t,
|
sl@0
|
182 |
const unsigned int /* file_version */
|
sl@0
|
183 |
){
|
sl@0
|
184 |
// The most common cause of trapping here would be serializing
|
sl@0
|
185 |
// something like shared_ptr<int>. This occurs because int
|
sl@0
|
186 |
// is never tracked by default. Wrap int in a trackable type
|
sl@0
|
187 |
BOOST_STATIC_ASSERT((tracking_level<T>::value != track_never));
|
sl@0
|
188 |
const T * t_ptr = t.get();
|
sl@0
|
189 |
ar << boost::serialization::make_nvp("px", t_ptr);
|
sl@0
|
190 |
}
|
sl@0
|
191 |
|
sl@0
|
192 |
template<class Archive, class T>
|
sl@0
|
193 |
inline void load(
|
sl@0
|
194 |
Archive & ar,
|
sl@0
|
195 |
boost::shared_ptr<T> &t,
|
sl@0
|
196 |
const unsigned int file_version
|
sl@0
|
197 |
){
|
sl@0
|
198 |
// The most common cause of trapping here would be serializing
|
sl@0
|
199 |
// something like shared_ptr<int>. This occurs because int
|
sl@0
|
200 |
// is never tracked by default. Wrap int in a trackable type
|
sl@0
|
201 |
BOOST_STATIC_ASSERT((tracking_level<T>::value != track_never));
|
sl@0
|
202 |
T* r;
|
sl@0
|
203 |
#ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
|
sl@0
|
204 |
if(file_version < 1){
|
sl@0
|
205 |
ar.register_type(static_cast<
|
sl@0
|
206 |
boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter<T> > *
|
sl@0
|
207 |
>(NULL));
|
sl@0
|
208 |
boost_132::shared_ptr<T> sp;
|
sl@0
|
209 |
ar >> boost::serialization::make_nvp("px", sp.px);
|
sl@0
|
210 |
ar >> boost::serialization::make_nvp("pn", sp.pn);
|
sl@0
|
211 |
// got to keep the sps around so the sp.pns don't disappear
|
sl@0
|
212 |
get_helper<Archive, boost_132::serialization::detail::shared_ptr_helper>(ar).append(sp);
|
sl@0
|
213 |
r = sp.get();
|
sl@0
|
214 |
}
|
sl@0
|
215 |
else
|
sl@0
|
216 |
#endif
|
sl@0
|
217 |
{
|
sl@0
|
218 |
ar >> boost::serialization::make_nvp("px", r);
|
sl@0
|
219 |
}
|
sl@0
|
220 |
get_helper<Archive, detail::shared_ptr_helper >(ar).reset(t,r);
|
sl@0
|
221 |
}
|
sl@0
|
222 |
|
sl@0
|
223 |
template<class Archive, class T>
|
sl@0
|
224 |
inline void serialize(
|
sl@0
|
225 |
Archive & ar,
|
sl@0
|
226 |
boost::shared_ptr<T> &t,
|
sl@0
|
227 |
const unsigned int file_version
|
sl@0
|
228 |
){
|
sl@0
|
229 |
// correct shared_ptr serialization depends upon object tracking
|
sl@0
|
230 |
// being used.
|
sl@0
|
231 |
BOOST_STATIC_ASSERT(
|
sl@0
|
232 |
boost::serialization::tracking_level<T>::value
|
sl@0
|
233 |
!= boost::serialization::track_never
|
sl@0
|
234 |
);
|
sl@0
|
235 |
boost::serialization::split_free(ar, t, file_version);
|
sl@0
|
236 |
}
|
sl@0
|
237 |
|
sl@0
|
238 |
} // namespace serialization
|
sl@0
|
239 |
} // namespace boost
|
sl@0
|
240 |
|
sl@0
|
241 |
#endif // BOOST_SERIALIZATION_SHARED_PTR_HPP
|