source: NonGTP/Boost/boost/serialization/shared_ptr_132.hpp @ 857

Revision 857, 8.3 KB checked in by igarcia, 18 years ago (diff)
Line 
1#ifndef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
2#define BOOST_SERIALIZATION_SHARED_PTR_132_HPP
3
4// MS compatible compilers support #pragma once
5#if defined(_MSC_VER) && (_MSC_VER >= 1020)
6# pragma once
7#endif
8
9/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10// shared_ptr.hpp: serialization for boost shared pointer
11
12// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
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)
16
17//  See http://www.boost.org for updates, documentation, and revision history.
18
19// note: totally unadvised hack to gain access to private variables
20// in shared_ptr and shared_count. Unfortunately its the only way to
21// do this without changing shared_ptr and shared_count
22// the best we can do is to detect a conflict here
23#include <boost/config.hpp>
24
25#include <list>
26
27#include <boost/serialization/detail/shared_ptr_132.hpp>
28
29#include <boost/serialization/is_abstract.hpp>
30#include <boost/serialization/split_free.hpp>
31#include <boost/serialization/nvp.hpp>
32#include <boost/serialization/tracking.hpp>
33#include <boost/serialization/void_cast.hpp>
34
35// mark base class as an (uncreatable) base class
36BOOST_IS_ABSTRACT(boost_132::detail::sp_counted_base)
37
38/////////////////////////////////////////////////////////////
39// Maintain a couple of lists of loaded shared pointers of the old previous
40// version (1.32)
41
42namespace boost_132 {
43namespace serialization {
44namespace detail {
45
46struct null_deleter {
47    void operator()(void const *) const {}
48};
49
50class shared_ptr_helper{
51    typedef std::list<shared_ptr<void> > collection_type;
52    typedef collection_type::iterator iterator_type;
53    // list of loaded pointers.  This is used to be sure that the pointers
54    // stay around long enough to be "matched" with other pointers loaded
55    // by the same archive.  These are created with a "null_deleter" so that
56    // when this list is destroyed - the underlaying raw pointers are not
57    // destroyed.  This has to be done because the pointers are also held by
58    // new system which is disjoint from this set.  This is implemented
59    // by a change in load_construct_data below.  It makes this file suitable
60    // only for loading pointers into a 1.33 or later boost system.
61    collection_type m_pointers;
62public:
63    void append(const boost_132::shared_ptr<void> & t){
64        m_pointers.push_back(t);
65    }
66    virtual ~shared_ptr_helper(){}
67};
68
69} // namespace detail
70} // namespace serialization
71} // namespace boost_132
72
73/////////////////////////////////////////////////////////////
74// sp_counted_base_impl serialization
75
76namespace boost {
77namespace serialization {
78
79template<class Archive, class P, class D>
80inline void serialize(
81    Archive & /* ar */,
82    boost_132::detail::sp_counted_base_impl<P, D> & /* t */,
83    const unsigned int /*file_version*/
84){
85    // register the relationship between each derived class
86    // its polymorphic base
87    boost::serialization::void_cast_register<
88        boost_132::detail::sp_counted_base_impl<P, D>,
89        boost_132::detail::sp_counted_base
90    >(
91        static_cast<boost_132::detail::sp_counted_base_impl<P, D> *>(NULL),
92        static_cast<boost_132::detail::sp_counted_base *>(NULL)
93    );
94}
95
96template<class Archive, class P, class D>
97inline void save_construct_data(
98    Archive & ar,
99    const boost_132::detail::sp_counted_base_impl<P, D> *t,
100    const unsigned int /* file_version */
101){
102    // variables used for construction
103    ar << boost::serialization::make_nvp("ptr", t->ptr);
104}
105
106template<class Archive, class P, class D>
107inline void load_construct_data(
108    Archive & ar,
109    boost_132::detail::sp_counted_base_impl<P, D> * t,
110    const unsigned int /* file_version */
111){
112    P ptr_;
113    ar >> boost::serialization::make_nvp("ptr", ptr_);
114//    ::new(t)boost_132::detail::sp_counted_base_impl<P, D>(ptr_,  D()); // placement
115    // note: the original ::new... above is replaced by the one here.  This one
116    // creates all new objects with a null_deleter so that after the archive
117    // is finished loading and the shared_ptrs are destroyed - the underlying
118    // raw pointers are NOT deleted.  This is necessary as they are used by the
119    // new system as well.
120    ::new(t)boost_132::detail::sp_counted_base_impl<
121        P,
122        boost_132::serialization::detail::null_deleter
123    >(
124        ptr_,  boost_132::serialization::detail::null_deleter()
125    ); // placement new
126    // compensate for that fact that a new shared count always is
127    // initialized with one. the add_ref_copy below will increment it
128    // every time its serialized so without this adjustment
129    // the use and weak counts will be off by one.
130    t->use_count_ = 0;
131}
132
133} // serialization
134} // namespace boost
135
136/////////////////////////////////////////////////////////////
137// shared_count serialization
138
139namespace boost {
140namespace serialization {
141
142template<class Archive>
143inline void save(
144    Archive & ar,
145    const boost_132::detail::shared_count &t,
146    const unsigned int /* file_version */
147){
148    ar << boost::serialization::make_nvp("pi", t.pi_);
149}
150
151template<class Archive>
152inline void load(
153    Archive & ar,
154    boost_132::detail::shared_count &t,
155    const unsigned int /* file_version */
156){
157    ar >> boost::serialization::make_nvp("pi", t.pi_);
158    if(NULL != t.pi_)
159        t.pi_->add_ref_copy();
160}
161
162} // serialization
163} // namespace boost
164
165BOOST_SERIALIZATION_SPLIT_FREE(boost_132::detail::shared_count)
166
167/////////////////////////////////////////////////////////////
168// implement serialization for shared_ptr<T>
169
170namespace boost {
171namespace serialization {
172
173template<class Archive, class T>
174inline void save(
175    Archive & ar,
176    const boost_132::shared_ptr<T> &t,
177    const unsigned int /* file_version */
178){
179    // only the raw pointer has to be saved
180    // the ref count is maintained automatically as shared pointers are loaded
181    ar.register_type(static_cast<
182        boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter<T> > *
183    >(NULL));
184    ar << boost::serialization::make_nvp("px", t.px);
185    ar << boost::serialization::make_nvp("pn", t.pn);
186}
187
188template<class Archive, class T>
189inline void load(
190    Archive & ar,
191    boost_132::shared_ptr<T> &t,
192    const unsigned int /* file_version */
193){
194    // only the raw pointer has to be saved
195    // the ref count is maintained automatically as shared pointers are loaded
196    ar.register_type(static_cast<
197        boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter<T> > *
198    >(NULL));
199    ar >> boost::serialization::make_nvp("px", t.px);
200    ar >> boost::serialization::make_nvp("pn", t.pn);
201}
202
203template<class Archive, class T>
204inline void serialize(
205    Archive & ar,
206    boost_132::shared_ptr<T> &t,
207    const unsigned int file_version
208){
209    // correct shared_ptr serialization depends upon object tracking
210    // being used.
211    BOOST_STATIC_ASSERT(
212        boost::serialization::tracking_level<T>::value
213        != boost::serialization::track_never
214    );
215    boost::serialization::split_free(ar, t, file_version);
216}
217
218} // serialization
219} // namespace boost
220
221// note: change below uses null_deleter
222// This macro is used to export GUIDS for shared pointers to allow
223// the serialization system to export them properly. David Tonge
224#define BOOST_SHARED_POINTER_EXPORT_GUID(T, K)                     \
225    typedef boost_132::detail::sp_counted_base_impl<               \
226        T *,                                                       \
227        boost::checked_deleter< T >                                \
228    > __shared_ptr_ ## T;                                          \
229    BOOST_CLASS_EXPORT_GUID(__shared_ptr_ ## T, "__shared_ptr_" K) \
230    BOOST_CLASS_EXPORT_GUID(T, K)                                  \
231    /**/
232
233#define BOOST_SHARED_POINTER_EXPORT(T)                             \
234    BOOST_SHARED_POINTER_EXPORT_GUID(                              \
235        T,                                                         \
236        BOOST_PP_STRINGIZE(T)                                      \
237    )                                                              \
238    /**/
239
240#endif // BOOST_SERIALIZATION_SHARED_PTR_132_HPP
Note: See TracBrowser for help on using the repository browser.