Chris@16: #ifndef BOOST_SERIALIZATION_SHARED_PTR_132_HPP Chris@16: #define BOOST_SERIALIZATION_SHARED_PTR_132_HPP Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@101: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // shared_ptr.hpp: serialization for boost shared pointer Chris@16: Chris@16: // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org for updates, documentation, and revision history. Chris@16: Chris@16: // note: totally unadvised hack to gain access to private variables Chris@16: // in shared_ptr and shared_count. Unfortunately its the only way to Chris@16: // do this without changing shared_ptr and shared_count Chris@16: // the best we can do is to detect a conflict here Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include // NULL Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // mark base class as an (uncreatable) base class Chris@16: #include Chris@16: Chris@16: ///////////////////////////////////////////////////////////// Chris@16: // Maintain a couple of lists of loaded shared pointers of the old previous Chris@16: // version (1.32) Chris@16: Chris@16: namespace boost_132 { Chris@16: namespace serialization { Chris@16: namespace detail { Chris@16: Chris@16: struct null_deleter { Chris@16: void operator()(void const *) const {} Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace serialization Chris@16: } // namespace boost_132 Chris@16: Chris@16: ///////////////////////////////////////////////////////////// Chris@16: // sp_counted_base_impl serialization Chris@16: Chris@16: namespace boost { Chris@16: namespace serialization { Chris@16: Chris@16: template Chris@16: inline void serialize( Chris@16: Archive & /* ar */, Chris@16: boost_132::detail::sp_counted_base_impl & /* t */, Chris@16: const unsigned int /*file_version*/ Chris@16: ){ Chris@16: // register the relationship between each derived class Chris@16: // its polymorphic base Chris@16: boost::serialization::void_cast_register< Chris@16: boost_132::detail::sp_counted_base_impl, Chris@16: boost_132::detail::sp_counted_base Chris@16: >( Chris@16: static_cast *>(NULL), Chris@16: static_cast(NULL) Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void save_construct_data( Chris@16: Archive & ar, Chris@16: const Chris@16: boost_132::detail::sp_counted_base_impl *t, Chris@16: const BOOST_PFTO unsigned int /* file_version */ Chris@16: ){ Chris@16: // variables used for construction Chris@16: ar << boost::serialization::make_nvp("ptr", t->ptr); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void load_construct_data( Chris@16: Archive & ar, Chris@16: boost_132::detail::sp_counted_base_impl * t, Chris@16: const unsigned int /* file_version */ Chris@16: ){ Chris@16: P ptr_; Chris@16: ar >> boost::serialization::make_nvp("ptr", ptr_); Chris@16: // ::new(t)boost_132::detail::sp_counted_base_impl(ptr_, D()); Chris@16: // placement Chris@16: // note: the original ::new... above is replaced by the one here. This one Chris@16: // creates all new objects with a null_deleter so that after the archive Chris@16: // is finished loading and the shared_ptrs are destroyed - the underlying Chris@16: // raw pointers are NOT deleted. This is necessary as they are used by the Chris@16: // new system as well. Chris@16: ::new(t)boost_132::detail::sp_counted_base_impl< Chris@16: P, Chris@16: boost_132::serialization::detail::null_deleter Chris@16: >( Chris@16: ptr_, boost_132::serialization::detail::null_deleter() Chris@16: ); // placement new Chris@16: // compensate for that fact that a new shared count always is Chris@16: // initialized with one. the add_ref_copy below will increment it Chris@16: // every time its serialized so without this adjustment Chris@16: // the use and weak counts will be off by one. Chris@16: t->use_count_ = 0; Chris@16: } Chris@16: Chris@16: } // serialization Chris@16: } // namespace boost Chris@16: Chris@16: ///////////////////////////////////////////////////////////// Chris@16: // shared_count serialization Chris@16: Chris@16: namespace boost { Chris@16: namespace serialization { Chris@16: Chris@16: template Chris@16: inline void save( Chris@16: Archive & ar, Chris@16: const boost_132::detail::shared_count &t, Chris@16: const unsigned int /* file_version */ Chris@16: ){ Chris@16: ar << boost::serialization::make_nvp("pi", t.pi_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void load( Chris@16: Archive & ar, Chris@16: boost_132::detail::shared_count &t, Chris@16: const unsigned int /* file_version */ Chris@16: ){ Chris@16: ar >> boost::serialization::make_nvp("pi", t.pi_); Chris@16: if(NULL != t.pi_) Chris@16: t.pi_->add_ref_copy(); Chris@16: } Chris@16: Chris@16: } // serialization Chris@16: } // namespace boost Chris@16: Chris@16: BOOST_SERIALIZATION_SPLIT_FREE(boost_132::detail::shared_count) Chris@16: Chris@16: ///////////////////////////////////////////////////////////// Chris@16: // implement serialization for shared_ptr< T > Chris@16: Chris@16: namespace boost { Chris@16: namespace serialization { Chris@16: Chris@16: template Chris@16: inline void save( Chris@16: Archive & ar, Chris@16: const boost_132::shared_ptr< T > &t, Chris@16: const unsigned int /* file_version */ Chris@16: ){ Chris@16: // only the raw pointer has to be saved Chris@16: // the ref count is maintained automatically as shared pointers are loaded Chris@16: ar.register_type(static_cast< Chris@16: boost_132::detail::sp_counted_base_impl > * Chris@16: >(NULL)); Chris@16: ar << boost::serialization::make_nvp("px", t.px); Chris@16: ar << boost::serialization::make_nvp("pn", t.pn); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void load( Chris@16: Archive & ar, Chris@16: boost_132::shared_ptr< T > &t, Chris@16: const unsigned int /* file_version */ Chris@16: ){ Chris@16: // only the raw pointer has to be saved Chris@16: // the ref count is maintained automatically as shared pointers are loaded Chris@16: ar.register_type(static_cast< Chris@16: boost_132::detail::sp_counted_base_impl > * Chris@16: >(NULL)); Chris@16: ar >> boost::serialization::make_nvp("px", t.px); Chris@16: ar >> boost::serialization::make_nvp("pn", t.pn); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void serialize( Chris@16: Archive & ar, Chris@16: boost_132::shared_ptr< T > &t, Chris@16: const unsigned int file_version Chris@16: ){ Chris@16: // correct shared_ptr serialization depends upon object tracking Chris@16: // being used. Chris@16: BOOST_STATIC_ASSERT( Chris@16: boost::serialization::tracking_level< T >::value Chris@16: != boost::serialization::track_never Chris@16: ); Chris@16: boost::serialization::split_free(ar, t, file_version); Chris@16: } Chris@16: Chris@16: } // serialization Chris@16: } // namespace boost Chris@16: Chris@16: // note: change below uses null_deleter Chris@16: // This macro is used to export GUIDS for shared pointers to allow Chris@16: // the serialization system to export them properly. David Tonge Chris@16: #define BOOST_SHARED_POINTER_EXPORT_GUID(T, K) \ Chris@16: typedef boost_132::detail::sp_counted_base_impl< \ Chris@16: T *, \ Chris@16: boost::checked_deleter< T > \ Chris@16: > __shared_ptr_ ## T; \ Chris@16: BOOST_CLASS_EXPORT_GUID(__shared_ptr_ ## T, "__shared_ptr_" K) \ Chris@16: BOOST_CLASS_EXPORT_GUID(T, K) \ Chris@16: /**/ Chris@16: Chris@16: #define BOOST_SHARED_POINTER_EXPORT(T) \ Chris@16: BOOST_SHARED_POINTER_EXPORT_GUID( \ Chris@16: T, \ Chris@16: BOOST_PP_STRINGIZE(T) \ Chris@16: ) \ Chris@16: /**/ Chris@16: Chris@16: #endif // BOOST_SERIALIZATION_SHARED_PTR_132_HPP