fix doc example typo
[boost.git] / boost / serialization / export.hpp
blobb043f41590b2bf34369003c13f0235e69246b87d
1 #ifndef BOOST_SERIALIZATION_EXPORT_HPP
2 #define BOOST_SERIALIZATION_EXPORT_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
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.
24 #include <utility>
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 #ifndef BOOST_SERIALIZATION_DEFAULT_TYPE_INFO
33 #include <boost/serialization/extended_type_info_typeid.hpp>
34 #endif
35 #include <boost/serialization/static_warning.hpp>
36 #include <boost/serialization/type_info_implementation.hpp>
37 #include <boost/serialization/assume_abstract.hpp>
38 #include <boost/serialization/force_include.hpp>
39 #include <boost/serialization/singleton.hpp>
41 #include <boost/archive/detail/register_archive.hpp>
42 #include <boost/mpl/assert.hpp>
43 #include <boost/mpl/and.hpp>
44 #include <boost/mpl/not.hpp>
45 #include <boost/mpl/bool.hpp>
47 #include <iostream>
49 namespace boost {
50 namespace archive {
51 namespace detail {
53 class basic_pointer_iserializer;
54 class basic_pointer_oserializer;
56 template<class Archive, class T>
57 class pointer_iserializer;
58 template<class Archive, class T>
59 class pointer_oserializer;
61 template <class Archive, class Serializable>
62 struct export_impl
64 static const basic_pointer_iserializer &
65 enable_load(mpl::true_){
66 return boost::serialization::singleton<
67 pointer_iserializer<Archive, Serializable>
68 >::get_const_instance();
71 static const basic_pointer_oserializer &
72 enable_save(mpl::true_){
73 return boost::serialization::singleton<
74 pointer_oserializer<Archive, Serializable>
75 >::get_const_instance();
77 inline static void enable_load(mpl::false_) {}
78 inline static void enable_save(mpl::false_) {}
81 // On many platforms, naming a specialization of this template is
82 // enough to cause its argument to be instantiated.
83 template <void(*)()>
84 struct instantiate_function {};
86 template <class Archive, class Serializable>
87 struct ptr_serialization_support
89 # if defined(BOOST_MSVC) || defined(__SUNPRO_CC)
90 virtual BOOST_DLLEXPORT void instantiate() BOOST_USED;
91 # elif defined(__BORLANDC__)
92 static BOOST_DLLEXPORT void instantiate() BOOST_USED;
93 enum { x = sizeof(instantiate(),3) };
94 # else
95 static BOOST_DLLEXPORT void instantiate() BOOST_USED;
96 typedef instantiate_function<
97 &ptr_serialization_support::instantiate
98 > x;
99 # endif
102 template <class Archive, class Serializable>
103 BOOST_DLLEXPORT void
104 ptr_serialization_support<Archive,Serializable>::instantiate()
106 export_impl<Archive,Serializable>::enable_save(
107 #if ! defined(__BORLANDC__)
108 BOOST_DEDUCED_TYPENAME
109 #endif
110 Archive::is_saving()
113 export_impl<Archive,Serializable>::enable_load(
114 #if ! defined(__BORLANDC__)
115 BOOST_DEDUCED_TYPENAME
116 #endif
117 Archive::is_loading()
121 namespace {
123 template<class T>
124 struct guid_initializer
126 const guid_initializer & export_guid(char const* /* key */, mpl::false_){
127 // generates the statically-initialized objects whose constructors
128 // register the information allowing serialization of T objects
129 // through pointers to their base classes.
130 instantiate_ptr_serialization((T*)0, 0, adl_tag());
131 return *this;
133 const guid_initializer & export_guid(char const* /*key*/, mpl::true_){
134 return *this;
136 const guid_initializer & export_guid(char const* key){
137 BOOST_STATIC_WARNING(boost::is_polymorphic<T>::value);
138 assert(NULL != key);
139 boost::serialization::singleton<
140 BOOST_DEDUCED_TYPENAME
141 boost::serialization::type_info_implementation<T>::type
142 >::get_mutable_instance().key_register(key);
143 // note: exporting an abstract base class will have no effect
144 // and cannot be used to instantitiate serialization code
145 // (one might be using this in a DLL to instantiate code)
146 //BOOST_STATIC_WARNING(! boost::serialization::is_abstract<T>::value);
147 return export_guid(key, boost::serialization::is_abstract<T>());
151 template<typename T>
152 struct init_guid;
154 } // anonymous
155 } // namespace detail
156 } // namespace archive
157 } // namespace boost
159 #define BOOST_CLASS_EXPORT_GUID(T, K) \
160 namespace boost { \
161 namespace archive { \
162 namespace detail { \
163 namespace { \
164 template<> \
165 struct init_guid< T > { \
166 static ::boost::archive::detail::guid_initializer< T > const \
167 & guid_initializer; \
168 }; \
169 ::boost::archive::detail::guid_initializer< T > const & \
170 ::boost::archive::detail::init_guid< T >::guid_initializer = \
171 ::boost::serialization::singleton< \
172 ::boost::archive::detail::guid_initializer< T > \
173 >::get_mutable_instance().export_guid(K); \
174 }}}} \
175 /**/
177 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
179 // CodeWarrior fails to construct static members of class templates
180 // when they are instantiated from within templates, so on that
181 // compiler we ask users to specifically register base/derived class
182 // relationships for exported classes. On all other compilers, use of
183 // this macro is entirely optional.
184 # define BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(Base,Derived) \
185 namespace { \
186 static int BOOST_PP_CAT(boost_serialization_mwerks_init_, __LINE__) = \
187 (::boost::archive::detail::instantiate_ptr_serialization((Derived*)0,0), 3); \
188 static int BOOST_PP_CAT(boost_serialization_mwerks_init2_, __LINE__) = ( \
189 ::boost::serialization::void_cast_register((Derived*)0,(Base*)0) \
190 , 3); \
193 #else
195 # define BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(Base,Derived)
197 #endif
199 // check for unnecessary export. T isn't polymorphic so there is no
200 // need to export it.
201 #define BOOST_CLASS_EXPORT_CHECK(T) \
202 BOOST_STATIC_WARNING( \
203 boost::is_polymorphic<U>::value \
204 ); \
205 /**/
207 // the default exportable class identifier is the class name
208 // the default list of archives types for which code id generated
209 // are the originally included with this serialization system
210 #define BOOST_CLASS_EXPORT(T) \
211 BOOST_CLASS_EXPORT_GUID( \
212 T, \
213 BOOST_PP_STRINGIZE(T) \
215 /**/
217 #endif // BOOST_SERIALIZATION_EXPORT_HPP