1 #ifndef BOOST_SERIALIZATION_SHARED_PTR_HELPER_HPP
2 #define BOOST_SERIALIZATION_SHARED_PTR_HELPER_HPP
4 // MS compatible compilers support #pragma once
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // shared_ptr_helper.hpp: serialization for boost shared pointern
12 // (C) Copyright 2004-2009 Robert Ramey, Martin Ecker and Takatoshi Kondo
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.
22 #include <cstddef> // NULL
24 #include <boost/config.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/type_traits/is_polymorphic.hpp>
27 #include <boost/mpl/if.hpp>
29 #include <boost/serialization/singleton.hpp>
30 #include <boost/serialization/extended_type_info.hpp>
31 #include <boost/serialization/throw_exception.hpp>
32 #include <boost/serialization/type_info_implementation.hpp>
33 #include <boost/archive/archive_exception.hpp>
34 #include <boost/archive/detail/decl.hpp>
36 #include <boost/archive/detail/abi_prefix.hpp> // must be the last headern
39 template<class T> class shared_ptr;
42 namespace serialization {
44 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
45 template<class Archive, template<class U> class SPT >
49 const unsigned int file_version
53 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
54 // a common class for holding various types of shared pointers
56 template<template<class T> class SPT>
57 class shared_ptr_helper {
59 const void *, // address of object
60 SPT<const void> // address shared ptr to single instance
61 > object_shared_pointer_map;
63 // list of shared_pointers create accessable by raw pointer. This
64 // is used to "match up" shared pointers loaded at different
65 // points in the archive. Note, we delay construction until
66 // it is actually used since this is by default included as
67 // a "mix-in" even if shared_ptr isn't used.
68 object_shared_pointer_map * m_o_sp;
71 void operator()(void const *) const {}
74 #if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) \
75 || defined(BOOST_MSVC) \
76 || defined(__SUNPRO_CC)
79 template<class Archive, class U>
80 friend void boost::serialization::load(
83 const unsigned int file_version
87 #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
88 // list of loaded pointers. This is used to be sure that the pointers
89 // stay around long enough to be "matched" with other pointers loaded
90 // by the same archive. These are created with a "null_deleter" so that
91 // when this list is destroyed - the underlaying raw pointers are not
92 // destroyed. This has to be done because the pointers are also held by
93 // new system which is disjoint from this set. This is implemented
94 // by a change in load_construct_data below. It makes this file suitable
95 // only for loading pointers into a 1.33 or later boost system.
96 std::list<boost_132::shared_ptr<const void> > * m_pointers_132;
97 BOOST_ARCHIVE_DECL void
98 append(const boost_132::shared_ptr<const void> & t){
99 if(NULL == m_pointers_132)
100 m_pointers_132 = new std::list<boost_132::shared_ptr<const void> >;
101 m_pointers_132->push_back(t);
105 struct non_polymorphic {
107 static const boost::serialization::extended_type_info *
108 get_object_type(U & ){
109 return & boost::serialization::singleton<
111 boost::serialization::type_info_implementation< U >::type
112 >::get_const_instance();
117 static const boost::serialization::extended_type_info *
118 get_object_type(U & u){
119 return boost::serialization::singleton<
121 boost::serialization::type_info_implementation< U >::type
122 >::get_const_instance().get_derived_extended_type_info(u);
128 void reset(SPT< T > & s, T * t){
133 const boost::serialization::extended_type_info * this_type
134 = & boost::serialization::type_info_implementation< T >::type
135 ::get_const_instance();
137 // get pointer to the most derived object's eti. This is effectively
138 // the object type identifer
139 typedef typename mpl::if_<
145 const boost::serialization::extended_type_info * true_type
146 = type::get_object_type(*t);
148 // note:if this exception is thrown, be sure that derived pointern
149 // is either registered or exported.
150 if(NULL == true_type)
151 boost::serialization::throw_exception(
152 boost::archive::archive_exception(
153 boost::archive::archive_exception::unregistered_class,
154 this_type->get_debug_info()
157 // get void pointer to the most derived type
158 // this uniquely identifies the object referred to
159 // oid = "object identifier"
160 const void * oid = void_downcast(
166 boost::serialization::throw_exception(
167 boost::archive::archive_exception(
168 boost::archive::archive_exception::unregistered_cast,
169 true_type->get_debug_info(),
170 this_type->get_debug_info()
174 // make tracking array if necessary
176 m_o_sp = new object_shared_pointer_map;
178 typename object_shared_pointer_map::iterator i = m_o_sp->find(oid);
180 // if it's a new object
181 if(i == m_o_sp->end()){
183 std::pair<typename object_shared_pointer_map::iterator, bool> result;
184 result = m_o_sp->insert(std::make_pair(oid, s));
185 BOOST_ASSERT(result.second);
187 // if the object has already been seen
189 s = SPT<T>(i->second, t);
193 shared_ptr_helper() :
195 #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
196 , m_pointers_132(NULL)
199 virtual ~shared_ptr_helper(){
202 #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
203 if(NULL != m_pointers_132)
204 delete m_pointers_132;
209 } // namespace serialization
212 #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
214 #endif // BOOST_SERIALIZATION_SHARED_PTR_HELPER_HPP