282 lines
9.8 KiB
C++
282 lines
9.8 KiB
C++
#ifndef ACTIV_BOOST_SERIALIZATION_SHARED_PTR_HPP
|
|
#define ACTIV_BOOST_SERIALIZATION_SHARED_PTR_HPP
|
|
|
|
// MS compatible compilers support #pragma once
|
|
#if defined(_MSC_VER)
|
|
# pragma once
|
|
#endif
|
|
|
|
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
|
// shared_ptr.hpp: serialization for boost shared pointer
|
|
|
|
// (C) Copyright 2004 Robert Ramey and Martin Ecker
|
|
// Use, modification and distribution is subject to the Boost Software
|
|
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
// See http://www.boost.org for updates, documentation, and revision history.
|
|
|
|
#include <cstddef> // NULL
|
|
#include <memory>
|
|
|
|
#include <ActivBoost/config.hpp>
|
|
#include <ActivBoost/mpl/integral_c.hpp>
|
|
#include <ActivBoost/mpl/integral_c_tag.hpp>
|
|
|
|
#include <ActivBoost/detail/workaround.hpp>
|
|
#include <ActivBoost/shared_ptr.hpp>
|
|
|
|
#include <ActivBoost/serialization/shared_ptr_helper.hpp>
|
|
#include <ActivBoost/serialization/split_free.hpp>
|
|
#include <ActivBoost/serialization/nvp.hpp>
|
|
#include <ActivBoost/serialization/version.hpp>
|
|
#include <ActivBoost/serialization/tracking.hpp>
|
|
|
|
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
|
// Boost:: shared_ptr serialization traits
|
|
// version 1 to distinguish from boost 1.32 version. Note: we can only do this
|
|
// for a template when the compiler supports partial template specialization
|
|
|
|
#ifndef ACTIV_BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
|
namespace Boost {
|
|
namespace serialization{
|
|
template<class T>
|
|
struct version< ::Boost::shared_ptr< T > > {
|
|
typedef mpl::integral_c_tag tag;
|
|
#if ACTIV_BOOST_WORKAROUND(__MWERKS__, ACTIV_BOOST_TESTED_AT(0x3206))
|
|
typedef typename mpl::int_<1> type;
|
|
#else
|
|
typedef mpl::int_<1> type;
|
|
#endif
|
|
ACTIV_BOOST_STATIC_CONSTANT(int, value = type::value);
|
|
};
|
|
// don't track shared pointers
|
|
template<class T>
|
|
struct tracking_level< ::Boost::shared_ptr< T > > {
|
|
typedef mpl::integral_c_tag tag;
|
|
#if ACTIV_BOOST_WORKAROUND(__MWERKS__, ACTIV_BOOST_TESTED_AT(0x3206))
|
|
typedef typename mpl::int_< ::Boost::serialization::track_never> type;
|
|
#else
|
|
typedef mpl::int_< ::Boost::serialization::track_never> type;
|
|
#endif
|
|
ACTIV_BOOST_STATIC_CONSTANT(int, value = type::value);
|
|
};
|
|
}}
|
|
#define ACTIV_BOOST_SERIALIZATION_SHARED_PTR(T)
|
|
#else
|
|
// define macro to let users of these compilers do this
|
|
#define ACTIV_BOOST_SERIALIZATION_SHARED_PTR(T) \
|
|
ACTIV_BOOST_CLASS_VERSION( \
|
|
::Boost::shared_ptr< T >, \
|
|
1 \
|
|
) \
|
|
ACTIV_BOOST_CLASS_TRACKING( \
|
|
::Boost::shared_ptr< T >, \
|
|
::Boost::serialization::track_never \
|
|
) \
|
|
/**/
|
|
#endif
|
|
|
|
namespace Boost {
|
|
namespace serialization{
|
|
|
|
struct null_deleter {
|
|
void operator()(void const *) const {}
|
|
};
|
|
|
|
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
|
// serialization for Boost::shared_ptr
|
|
|
|
// Using a constant means that all shared pointers are held in the same set.
|
|
// Thus we detect handle multiple pointers to the same value instances
|
|
// in the archive.
|
|
void * const shared_ptr_helper_id = 0;
|
|
|
|
template<class Archive, class T>
|
|
inline void save(
|
|
Archive & ar,
|
|
const Boost::shared_ptr< T > &t,
|
|
const unsigned int /* file_version */
|
|
){
|
|
// The most common cause of trapping here would be serializing
|
|
// something like shared_ptr<int>. This occurs because int
|
|
// is never tracked by default. Wrap int in a trackable type
|
|
ACTIV_BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
|
const T * t_ptr = t.get();
|
|
ar << Boost::serialization::make_nvp("px", t_ptr);
|
|
}
|
|
|
|
#ifdef ACTIV_BOOST_SERIALIZATION_SHARED_PTR_132_HPP
|
|
template<class Archive, class T>
|
|
inline void load(
|
|
Archive & ar,
|
|
Boost::shared_ptr< T > &t,
|
|
const unsigned int file_version
|
|
){
|
|
// something like shared_ptr<int>. This occurs because int
|
|
// is never tracked by default. Wrap int in a trackable type
|
|
ACTIV_BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
|
T* r;
|
|
if(file_version < 1){
|
|
ar.register_type(static_cast<
|
|
Boost_132::detail::sp_counted_base_impl<T *, null_deleter > *
|
|
>(NULL));
|
|
Boost_132::shared_ptr< T > sp;
|
|
ar >> Boost::serialization::make_nvp("px", sp.px);
|
|
ar >> Boost::serialization::make_nvp("pn", sp.pn);
|
|
// got to keep the sps around so the sp.pns don't disappear
|
|
Boost::serialization::shared_ptr_helper<Boost::shared_ptr> & h =
|
|
ar.template get_helper< shared_ptr_helper<Boost::shared_ptr> >(
|
|
shared_ptr_helper_id
|
|
);
|
|
h.append(sp);
|
|
r = sp.get();
|
|
}
|
|
else{
|
|
ar >> Boost::serialization::make_nvp("px", r);
|
|
}
|
|
shared_ptr_helper<Boost::shared_ptr> & h =
|
|
ar.template get_helper<shared_ptr_helper<Boost::shared_ptr> >(
|
|
shared_ptr_helper_id
|
|
);
|
|
h.reset(t,r);
|
|
}
|
|
#else
|
|
|
|
template<class Archive, class T>
|
|
inline void load(
|
|
Archive & ar,
|
|
Boost::shared_ptr< T > &t,
|
|
const unsigned int /*file_version*/
|
|
){
|
|
// The most common cause of trapping here would be serializing
|
|
// something like shared_ptr<int>. This occurs because int
|
|
// is never tracked by default. Wrap int in a trackable type
|
|
ACTIV_BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
|
T* r;
|
|
ar >> Boost::serialization::make_nvp("px", r);
|
|
|
|
Boost::serialization::shared_ptr_helper<Boost::shared_ptr> & h =
|
|
ar.template get_helper<shared_ptr_helper<Boost::shared_ptr> >(
|
|
shared_ptr_helper_id
|
|
);
|
|
h.reset(t,r);
|
|
}
|
|
#endif
|
|
|
|
template<class Archive, class T>
|
|
inline void serialize(
|
|
Archive & ar,
|
|
Boost::shared_ptr< T > &t,
|
|
const unsigned int file_version
|
|
){
|
|
// correct shared_ptr serialization depends upon object tracking
|
|
// being used.
|
|
ACTIV_BOOST_STATIC_ASSERT(
|
|
Boost::serialization::tracking_level< T >::value
|
|
!= Boost::serialization::track_never
|
|
);
|
|
Boost::serialization::split_free(ar, t, file_version);
|
|
}
|
|
|
|
} // namespace serialization
|
|
} // namespace Boost
|
|
|
|
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
|
// std::shared_ptr serialization traits
|
|
// version 1 to distinguish from boost 1.32 version. Note: we can only do this
|
|
// for a template when the compiler supports partial template specialization
|
|
|
|
#ifndef ACTIV_BOOST_NO_CXX11_SMART_PTR
|
|
#include <ActivBoost/static_assert.hpp>
|
|
|
|
// note: we presume that any compiler/library which supports C++11
|
|
// std::pointers also supports template partial specialization
|
|
// trap here if such presumption were to turn out to wrong!!!
|
|
#ifdef ACTIV_BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
|
ACTIV_BOOST_STATIC_ASSERT(false);
|
|
#endif
|
|
|
|
namespace Boost {
|
|
namespace serialization{
|
|
template<class T>
|
|
struct version< ::std::shared_ptr< T > > {
|
|
typedef mpl::integral_c_tag tag;
|
|
typedef mpl::int_<1> type;
|
|
ACTIV_BOOST_STATIC_CONSTANT(int, value = type::value);
|
|
};
|
|
// don't track shared pointers
|
|
template<class T>
|
|
struct tracking_level< ::std::shared_ptr< T > > {
|
|
typedef mpl::integral_c_tag tag;
|
|
typedef mpl::int_< ::Boost::serialization::track_never> type;
|
|
ACTIV_BOOST_STATIC_CONSTANT(int, value = type::value);
|
|
};
|
|
}}
|
|
// the following just keeps older programs from breaking
|
|
#define ACTIV_BOOST_SERIALIZATION_SHARED_PTR(T)
|
|
|
|
namespace Boost {
|
|
namespace serialization{
|
|
|
|
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
|
// serialization for std::shared_ptr
|
|
|
|
template<class Archive, class T>
|
|
inline void save(
|
|
Archive & ar,
|
|
const std::shared_ptr< T > &t,
|
|
const unsigned int /* file_version */
|
|
){
|
|
// The most common cause of trapping here would be serializing
|
|
// something like shared_ptr<int>. This occurs because int
|
|
// is never tracked by default. Wrap int in a trackable type
|
|
ACTIV_BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
|
const T * t_ptr = t.get();
|
|
ar << Boost::serialization::make_nvp("px", t_ptr);
|
|
}
|
|
|
|
template<class Archive, class T>
|
|
inline void load(
|
|
Archive & ar,
|
|
std::shared_ptr< T > &t,
|
|
const unsigned int /*file_version*/
|
|
){
|
|
// The most common cause of trapping here would be serializing
|
|
// something like shared_ptr<int>. This occurs because int
|
|
// is never tracked by default. Wrap int in a trackable type
|
|
ACTIV_BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
|
T* r;
|
|
ar >> Boost::serialization::make_nvp("px", r);
|
|
//void (* const id)(Archive &, std::shared_ptr< T > &, const unsigned int) = & load;
|
|
Boost::serialization::shared_ptr_helper<std::shared_ptr> & h =
|
|
ar.template get_helper<
|
|
shared_ptr_helper<std::shared_ptr>
|
|
>(
|
|
shared_ptr_helper_id
|
|
);
|
|
h.reset(t,r);
|
|
}
|
|
|
|
template<class Archive, class T>
|
|
inline void serialize(
|
|
Archive & ar,
|
|
std::shared_ptr< T > &t,
|
|
const unsigned int file_version
|
|
){
|
|
// correct shared_ptr serialization depends upon object tracking
|
|
// being used.
|
|
ACTIV_BOOST_STATIC_ASSERT(
|
|
Boost::serialization::tracking_level< T >::value
|
|
!= Boost::serialization::track_never
|
|
);
|
|
Boost::serialization::split_free(ar, t, file_version);
|
|
}
|
|
|
|
} // namespace serialization
|
|
} // namespace Boost
|
|
|
|
#endif // ACTIV_BOOST_NO_CXX11_SMART_PTR
|
|
|
|
#endif // ACTIV_BOOST_SERIALIZATION_SHARED_PTR_HPP
|