1 #ifndef BOOST_SERIALIZATION_EXPORT_HPP
2 #define BOOST_SERIALIZATION_EXPORT_HPP
4 // MS compatible compilers support #pragma once
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // export.hpp: set traits of classes to be serialized
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)
17 // See http://www.boost.org for updates, documentation, and revision history.
19 // (C) Copyright 2006 David Abrahams - http://www.boost.org.
20 // implementation of class export functionality. This is an alternative to
21 // "forward declaration" method to provoke instantiation of derived classes
22 // that are to be serialized through pointers.
25 #include <cstddef> // NULL
27 #include <boost/config.hpp>
28 #include <boost/static_assert.hpp>
29 #include <boost/preprocessor/stringize.hpp>
30 #include <boost/type_traits/is_polymorphic.hpp>
32 #include <boost/mpl/assert.hpp>
33 #include <boost/mpl/and.hpp>
34 #include <boost/mpl/not.hpp>
35 #include <boost/mpl/bool_fwd.hpp>
37 #include <boost/serialization/extended_type_info.hpp> // for guid_defined only
38 #include <boost/serialization/static_warning.hpp>
39 #include <boost/serialization/assume_abstract.hpp>
40 #include <boost/serialization/force_include.hpp>
41 #include <boost/serialization/singleton.hpp>
43 #include <boost/archive/detail/register_archive.hpp>
51 class basic_pointer_iserializer;
52 class basic_pointer_oserializer;
54 template<class Archive, class T>
55 class pointer_iserializer;
56 template<class Archive, class T>
57 class pointer_oserializer;
59 template <class Archive, class Serializable>
62 static const basic_pointer_iserializer &
63 enable_load(mpl::true_){
64 return boost::serialization::singleton<
65 pointer_iserializer<Archive, Serializable>
66 >::get_const_instance();
69 static const basic_pointer_oserializer &
70 enable_save(mpl::true_){
71 return boost::serialization::singleton<
72 pointer_oserializer<Archive, Serializable>
73 >::get_const_instance();
75 inline static void enable_load(mpl::false_) {}
76 inline static void enable_save(mpl::false_) {}
79 // On many platforms, naming a specialization of this template is
80 // enough to cause its argument to be instantiated.
82 struct instantiate_function {};
84 template <class Archive, class Serializable>
85 struct ptr_serialization_support
87 # if defined(BOOST_MSVC) || defined(__SUNPRO_CC)
88 virtual BOOST_DLLEXPORT void instantiate() BOOST_USED;
90 static BOOST_DLLEXPORT void instantiate() BOOST_USED;
91 typedef instantiate_function<
92 &ptr_serialization_support::instantiate
97 template <class Archive, class Serializable>
99 ptr_serialization_support<Archive,Serializable>::instantiate()
101 export_impl<Archive,Serializable>::enable_save(
102 typename Archive::is_saving()
105 export_impl<Archive,Serializable>::enable_load(
106 typename Archive::is_loading()
110 // Note INTENTIONAL usage of anonymous namespace in header.
111 // This was made this way so that export.hpp could be included
112 // in other headers. This is still under study.
114 namespace extra_detail {
117 struct guid_initializer
119 void export_guid(mpl::false_) const {
120 // generates the statically-initialized objects whose constructors
121 // register the information allowing serialization of T objects
122 // through pointers to their base classes.
123 instantiate_ptr_serialization((T*)0, 0, adl_tag());
125 void export_guid(mpl::true_) const {
127 guid_initializer const & export_guid() const {
128 BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value);
129 // note: exporting an abstract base class will have no effect
130 // and cannot be used to instantitiate serialization code
131 // (one might be using this in a DLL to instantiate code)
132 //BOOST_STATIC_WARNING(! boost::serialization::is_abstract< T >::value);
133 export_guid(boost::serialization::is_abstract< T >());
142 } // namespace detail
143 } // namespace archive
146 #define BOOST_CLASS_EXPORT_IMPLEMENT(T) \
148 namespace archive { \
150 namespace extra_detail { \
152 struct init_guid< T > { \
153 static guid_initializer< T > const & g; \
155 guid_initializer< T > const & init_guid< T >::g = \
156 ::boost::serialization::singleton< \
157 guid_initializer< T > \
158 >::get_mutable_instance().export_guid(); \
162 #define BOOST_CLASS_EXPORT_KEY2(T, K) \
164 namespace serialization { \
166 struct guid_defined< T > : boost::mpl::true_ {}; \
168 inline const char * guid< T >(){ \
171 } /* serialization */ \
175 #define BOOST_CLASS_EXPORT_KEY(T) \
176 BOOST_CLASS_EXPORT_KEY2(T, BOOST_PP_STRINGIZE(T)) \
179 #define BOOST_CLASS_EXPORT_GUID(T, K) \
180 BOOST_CLASS_EXPORT_KEY2(T, K) \
181 BOOST_CLASS_EXPORT_IMPLEMENT(T) \
184 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
186 // CodeWarrior fails to construct static members of class templates
187 // when they are instantiated from within templates, so on that
188 // compiler we ask users to specifically register base/derived class
189 // relationships for exported classes. On all other compilers, use of
190 // this macro is entirely optional.
191 # define BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(Base,Derived) \
193 static int BOOST_PP_CAT(boost_serialization_mwerks_init_, __LINE__) = \
194 (::boost::archive::detail::instantiate_ptr_serialization((Derived*)0,0), 3); \
195 static int BOOST_PP_CAT(boost_serialization_mwerks_init2_, __LINE__) = ( \
196 ::boost::serialization::void_cast_register((Derived*)0,(Base*)0) \
202 # define BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(Base,Derived)
206 // check for unnecessary export. T isn't polymorphic so there is no
207 // need to export it.
208 #define BOOST_CLASS_EXPORT_CHECK(T) \
209 BOOST_STATIC_WARNING( \
210 boost::is_polymorphic<U>::value \
214 // the default exportable class identifier is the class name
215 // the default list of archives types for which code id generated
216 // are the originally included with this serialization system
217 #define BOOST_CLASS_EXPORT(T) \
218 BOOST_CLASS_EXPORT_GUID( \
220 BOOST_PP_STRINGIZE(T) \
224 #endif // BOOST_SERIALIZATION_EXPORT_HPP