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>
49 class basic_pointer_iserializer;
50 class basic_pointer_oserializer;
52 template<class Archive, class T>
53 class pointer_iserializer;
54 template<class Archive, class T>
55 class pointer_oserializer;
57 template <class Archive, class Serializable>
60 static const basic_pointer_iserializer &
61 enable_load(mpl::true_){
62 return boost::serialization::singleton<
63 pointer_iserializer<Archive, Serializable>
64 >::get_const_instance();
67 static const basic_pointer_oserializer &
68 enable_save(mpl::true_){
69 return boost::serialization::singleton<
70 pointer_oserializer<Archive, Serializable>
71 >::get_const_instance();
73 inline static void enable_load(mpl::false_) {}
74 inline static void enable_save(mpl::false_) {}
77 // On many platforms, naming a specialization of this template is
78 // enough to cause its argument to be instantiated.
80 struct instantiate_function {};
82 template <class Archive, class Serializable>
83 struct ptr_serialization_support
85 # if defined(BOOST_MSVC) || defined(__SUNPRO_CC)
86 virtual BOOST_DLLEXPORT void instantiate() BOOST_USED;
88 static BOOST_DLLEXPORT void instantiate() BOOST_USED;
89 typedef instantiate_function<
90 &ptr_serialization_support::instantiate
95 template <class Archive, class Serializable>
97 ptr_serialization_support<Archive,Serializable>::instantiate()
99 export_impl<Archive,Serializable>::enable_save(
100 typename Archive::is_saving()
103 export_impl<Archive,Serializable>::enable_load(
104 typename Archive::is_loading()
108 // Note INTENTIONAL usage of anonymous namespace in header.
109 // This was made this way so that export.hpp could be included
110 // in other headers. This is still under study.
112 namespace extra_detail {
115 struct guid_initializer
117 void export_guid(mpl::false_) const {
118 // generates the statically-initialized objects whose constructors
119 // register the information allowing serialization of T objects
120 // through pointers to their base classes.
121 instantiate_ptr_serialization((T*)0, 0, adl_tag());
123 void export_guid(mpl::true_) const {
125 guid_initializer const & export_guid() const {
126 BOOST_STATIC_WARNING(boost::is_polymorphic< T >::value);
127 // note: exporting an abstract base class will have no effect
128 // and cannot be used to instantitiate serialization code
129 // (one might be using this in a DLL to instantiate code)
130 //BOOST_STATIC_WARNING(! boost::serialization::is_abstract< T >::value);
131 export_guid(boost::serialization::is_abstract< T >());
140 } // namespace detail
141 } // namespace archive
144 #define BOOST_CLASS_EXPORT_IMPLEMENT(T) \
146 namespace archive { \
148 namespace extra_detail { \
150 struct init_guid< T > { \
151 static guid_initializer< T > const & g; \
153 guid_initializer< T > const & init_guid< T >::g = \
154 ::boost::serialization::singleton< \
155 guid_initializer< T > \
156 >::get_mutable_instance().export_guid(); \
160 #define BOOST_CLASS_EXPORT_KEY2(T, K) \
162 namespace serialization { \
164 struct guid_defined< T > : boost::mpl::true_ {}; \
166 inline const char * guid< T >(){ \
169 } /* serialization */ \
173 #define BOOST_CLASS_EXPORT_KEY(T) \
174 BOOST_CLASS_EXPORT_KEY2(T, BOOST_PP_STRINGIZE(T)) \
177 #define BOOST_CLASS_EXPORT_GUID(T, K) \
178 BOOST_CLASS_EXPORT_KEY2(T, K) \
179 BOOST_CLASS_EXPORT_IMPLEMENT(T) \
182 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
184 // CodeWarrior fails to construct static members of class templates
185 // when they are instantiated from within templates, so on that
186 // compiler we ask users to specifically register base/derived class
187 // relationships for exported classes. On all other compilers, use of
188 // this macro is entirely optional.
189 # define BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(Base,Derived) \
191 static int BOOST_PP_CAT(boost_serialization_mwerks_init_, __LINE__) = \
192 (::boost::archive::detail::instantiate_ptr_serialization((Derived*)0,0), 3); \
193 static int BOOST_PP_CAT(boost_serialization_mwerks_init2_, __LINE__) = ( \
194 ::boost::serialization::void_cast_register((Derived*)0,(Base*)0) \
200 # define BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(Base,Derived)
204 // check for unnecessary export. T isn't polymorphic so there is no
205 // need to export it.
206 #define BOOST_CLASS_EXPORT_CHECK(T) \
207 BOOST_STATIC_WARNING( \
208 boost::is_polymorphic<U>::value \
212 // the default exportable class identifier is the class name
213 // the default list of archives types for which code id generated
214 // are the originally included with this serialization system
215 #define BOOST_CLASS_EXPORT(T) \
216 BOOST_CLASS_EXPORT_GUID( \
218 BOOST_PP_STRINGIZE(T) \
222 #endif // BOOST_SERIALIZATION_EXPORT_HPP