1 #ifndef BOOST_ARCHIVE_OSERIALIZER_HPP
2 #define BOOST_ARCHIVE_OSERIALIZER_HPP
4 // MS compatible compilers support #pragma once
7 #pragma inline_depth(511)
8 #pragma inline_recursion(on)
11 #if defined(__MWERKS__)
12 #pragma inline_depth(511)
15 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
16 // oserializer.hpp: interface for serialization system.
18 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
19 // Use, modification and distribution is subject to the Boost Software
20 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
21 // http://www.boost.org/LICENSE_1_0.txt)
23 // See http://www.boost.org for updates, documentation, and revision history.
25 #include <boost/assert.hpp>
26 #include <cstddef> // NULL
28 #include <boost/config.hpp>
30 #include <boost/static_assert.hpp>
31 #include <boost/detail/workaround.hpp>
33 #include <boost/mpl/eval_if.hpp>
34 #include <boost/mpl/equal_to.hpp>
35 #include <boost/mpl/greater_equal.hpp>
36 #include <boost/mpl/identity.hpp>
37 #include <boost/mpl/bool_fwd.hpp>
39 #ifndef BOOST_SERIALIZATION_DEFAULT_TYPE_INFO
40 #include <boost/serialization/extended_type_info_typeid.hpp>
42 #include <boost/serialization/throw_exception.hpp>
43 #include <boost/serialization/smart_cast.hpp>
44 #include <boost/serialization/assume_abstract.hpp>
45 #include <boost/serialization/static_warning.hpp>
47 #include <boost/type_traits/is_pointer.hpp>
48 #include <boost/type_traits/is_enum.hpp>
49 #include <boost/type_traits/is_const.hpp>
50 #include <boost/type_traits/is_polymorphic.hpp>
51 #include <boost/type_traits/remove_extent.hpp>
53 #include <boost/serialization/serialization.hpp>
54 #include <boost/serialization/version.hpp>
55 #include <boost/serialization/level.hpp>
56 #include <boost/serialization/tracking.hpp>
57 #include <boost/serialization/type_info_implementation.hpp>
58 #include <boost/serialization/nvp.hpp>
59 #include <boost/serialization/void_cast.hpp>
60 #include <boost/serialization/collection_size_type.hpp>
61 #include <boost/serialization/array_wrapper.hpp>
63 #include <boost/serialization/singleton.hpp>
65 #include <boost/archive/archive_exception.hpp>
66 #include <boost/archive/detail/basic_oarchive.hpp>
67 #include <boost/archive/detail/basic_oserializer.hpp>
68 #include <boost/archive/detail/basic_pointer_oserializer.hpp>
69 #include <boost/archive/detail/archive_serializer_map.hpp>
70 #include <boost/archive/detail/check.hpp>
72 #include <boost/core/addressof.hpp>
76 namespace serialization {
77 class extended_type_info;
78 } // namespace serialization
82 // an accessor to permit friend access to archives. Needed because
83 // some compilers don't handle friend templates completely
86 template<class Archive>
87 static void end_preamble(Archive & ar){
90 template<class Archive, class T>
91 static void save_primitive(Archive & ar, const T & t){
100 # pragma warning(push)
101 # pragma warning(disable : 4511 4512)
104 template<class Archive, class T>
105 class oserializer : public basic_oserializer
108 // private constructor to inhibit any existence other than the
111 explicit BOOST_DLLEXPORT oserializer() :
113 boost::serialization::singleton<
115 boost::serialization::type_info_implementation< T >::type
116 >::get_const_instance()
119 virtual BOOST_DLLEXPORT void save_object_data(
123 virtual bool class_info() const {
124 return boost::serialization::implementation_level< T >::value
125 >= boost::serialization::object_class_info;
127 virtual bool tracking(const unsigned int /* flags */) const {
128 return boost::serialization::tracking_level< T >::value == boost::serialization::track_always
129 || (boost::serialization::tracking_level< T >::value == boost::serialization::track_selectively
130 && serialized_as_pointer());
132 virtual version_type version() const {
133 return version_type(::boost::serialization::version< T >::value);
135 virtual bool is_polymorphic() const {
136 return boost::is_polymorphic< T >::value;
138 virtual ~oserializer(){}
142 # pragma warning(pop)
145 template<class Archive, class T>
146 BOOST_DLLEXPORT void oserializer<Archive, T>::save_object_data(
150 // make sure call is routed through the highest interface that might
151 // be specialized by the user.
152 BOOST_STATIC_ASSERT(boost::is_const< T >::value == false);
153 boost::serialization::serialize_adl(
154 boost::serialization::smart_cast_reference<Archive &>(ar),
155 * static_cast<T *>(const_cast<void *>(x)),
161 # pragma warning(push)
162 # pragma warning(disable : 4511 4512)
165 template<class Archive, class T>
166 class pointer_oserializer :
167 public basic_pointer_oserializer
170 const basic_oserializer &
171 get_basic_serializer() const {
172 return boost::serialization::singleton<
173 oserializer<Archive, T>
174 >::get_const_instance();
176 virtual BOOST_DLLEXPORT void save_object_ptr(
181 pointer_oserializer();
182 ~pointer_oserializer();
186 # pragma warning(pop)
189 template<class Archive, class T>
190 BOOST_DLLEXPORT void pointer_oserializer<Archive, T>::save_object_ptr(
194 BOOST_ASSERT(NULL != x);
195 // make sure call is routed through the highest interface that might
196 // be specialized by the user.
197 T * t = static_cast<T *>(const_cast<void *>(x));
198 const unsigned int file_version = boost::serialization::version< T >::value;
200 = boost::serialization::smart_cast_reference<Archive &>(ar);
201 boost::serialization::save_construct_data_adl<Archive, T>(
206 ar_impl << boost::serialization::make_nvp(NULL, * t);
209 template<class Archive, class T>
210 pointer_oserializer<Archive, T>::pointer_oserializer() :
211 basic_pointer_oserializer(
212 boost::serialization::singleton<
214 boost::serialization::type_info_implementation< T >::type
215 >::get_const_instance()
218 // make sure appropriate member function is instantiated
219 boost::serialization::singleton<
220 oserializer<Archive, T>
221 >::get_mutable_instance().set_bpos(this);
222 archive_serializer_map<Archive>::insert(this);
225 template<class Archive, class T>
226 pointer_oserializer<Archive, T>::~pointer_oserializer(){
227 archive_serializer_map<Archive>::erase(this);
230 template<class Archive>
231 struct save_non_pointer_type {
232 // note this bounces the call right back to the archive
233 // with no runtime overhead
234 struct save_primitive {
236 static void invoke(Archive & ar, const T & t){
237 save_access::save_primitive(ar, t);
240 // same as above but passes through serialization
243 static void invoke(Archive & ar, const T & t){
244 // make sure call is routed through the highest interface that might
245 // be specialized by the user.
246 boost::serialization::serialize_adl(
249 ::boost::serialization::version< T >::value
253 // adds class information to the archive. This includes
254 // serialization level and class version
255 struct save_standard {
257 static void invoke(Archive &ar, const T & t){
260 boost::serialization::singleton<
261 oserializer<Archive, T>
262 >::get_const_instance()
269 // adds class information to the archive. This includes
270 // serialization level and class version
271 struct save_conditional {
273 static void invoke(Archive &ar, const T &t){
274 //if(0 == (ar.get_flags() & no_tracking))
275 save_standard::invoke(ar, t);
277 // save_only::invoke(ar, t);
283 static void invoke(Archive & ar, const T & t){
285 typename mpl::eval_if<
288 boost::serialization::implementation_level< T >,
289 mpl::int_<boost::serialization::primitive_type>
291 mpl::identity<save_primitive>,
293 typename mpl::eval_if<
294 // class info / version
296 boost::serialization::implementation_level< T >,
297 mpl::int_<boost::serialization::object_class_info>
300 mpl::identity<save_standard>,
302 typename mpl::eval_if<
305 boost::serialization::tracking_level< T >,
306 mpl::int_<boost::serialization::track_never>
309 mpl::identity<save_only>,
311 // do a fast save only tracking is turned off
312 mpl::identity<save_conditional>
314 check_object_versioning< T >();
315 typex::invoke(ar, t);
318 static void invoke(Archive & ar, T & t){
319 check_object_level< T >();
320 check_object_tracking< T >();
321 invoke(ar, const_cast<const T &>(t));
325 template<class Archive>
326 struct save_pointer_type {
330 static const basic_pointer_oserializer * register_type(Archive & /* ar */){
331 // it has? to be polymorphic
332 BOOST_STATIC_ASSERT(boost::is_polymorphic< T >::value);
340 static const basic_pointer_oserializer * register_type(Archive & ar){
341 return ar.register_type(static_cast<T *>(NULL));
346 static const basic_pointer_oserializer * register_type(Archive &ar, T* const /*t*/){
347 // there should never be any need to save an abstract polymorphic
348 // class pointer. Inhibiting code generation for this
349 // permits abstract base classes to be used - note: exception
350 // virtual serialize functions used for plug-ins
352 typename mpl::eval_if<
353 boost::serialization::is_abstract< T >,
354 mpl::identity<abstract>,
355 mpl::identity<non_abstract>
357 return typex::template register_type< T >(ar);
360 struct non_polymorphic
367 const basic_pointer_oserializer & bpos =
368 boost::serialization::singleton<
369 pointer_oserializer<Archive, T>
370 >::get_const_instance();
371 // save the requested pointer type
372 ar.save_pointer(& t, & bpos);
384 boost::serialization::type_info_implementation< T >::type const
385 & i = boost::serialization::singleton<
387 boost::serialization::type_info_implementation< T >::type
388 >::get_const_instance();
390 boost::serialization::extended_type_info const * const this_type = & i;
392 // retrieve the true type of the object pointed to
393 // if this assertion fails its an error in this library
394 BOOST_ASSERT(NULL != this_type);
396 const boost::serialization::extended_type_info * true_type =
397 i.get_derived_extended_type_info(t);
399 // note:if this exception is thrown, be sure that derived pointer
400 // is either registered or exported.
401 if(NULL == true_type){
402 boost::serialization::throw_exception(
404 archive_exception::unregistered_class,
405 "derived class not registered or exported"
410 // if its not a pointer to a more derived type
411 const void *vp = static_cast<const void *>(&t);
412 if(*this_type == *true_type){
413 const basic_pointer_oserializer * bpos = register_type(ar, &t);
414 ar.save_pointer(vp, bpos);
417 // convert pointer to more derived type. if this is thrown
418 // it means that the base/derived relationship hasn't be registered
419 vp = serialization::void_downcast(
422 static_cast<const void *>(&t)
425 boost::serialization::throw_exception(
427 archive_exception::unregistered_cast,
428 true_type->get_debug_info(),
429 this_type->get_debug_info()
434 // since true_type is valid, and this only gets made if the
435 // pointer oserializer object has been created, this should never
437 const basic_pointer_oserializer * bpos
438 = static_cast<const basic_pointer_oserializer *>(
439 boost::serialization::singleton<
440 archive_serializer_map<Archive>
441 >::get_const_instance().find(*true_type)
443 BOOST_ASSERT(NULL != bpos);
445 boost::serialization::throw_exception(
447 archive_exception::unregistered_class,
448 "derived class not registered or exported"
451 ar.save_pointer(vp, bpos);
460 check_pointer_level< T >();
461 check_pointer_tracking< T >();
462 typedef typename mpl::eval_if<
464 mpl::identity<polymorphic>,
465 mpl::identity<non_polymorphic>
467 type::save(ar, const_cast<T &>(t));
471 static void invoke(Archive &ar, const TPtr t){
472 register_type(ar, t);
475 = boost::serialization::smart_cast_reference<basic_oarchive &>(ar);
476 boa.save_null_pointer();
477 save_access::end_preamble(ar);
484 template<class Archive>
485 struct save_enum_type
488 static void invoke(Archive &ar, const T &t){
489 // convert enum to integers on save
490 const int i = static_cast<int>(t);
491 ar << boost::serialization::make_nvp(NULL, i);
495 template<class Archive>
496 struct save_array_type
499 static void invoke(Archive &ar, const T &t){
500 typedef typename boost::remove_extent< T >::type value_type;
502 save_access::end_preamble(ar);
503 // consider alignment
504 std::size_t c = sizeof(t) / (
505 static_cast<const char *>(static_cast<const void *>(&t[1]))
506 - static_cast<const char *>(static_cast<const void *>(&t[0]))
508 boost::serialization::collection_size_type count(c);
509 ar << BOOST_SERIALIZATION_NVP(count);
510 // explict template arguments to pass intel C++ compiler
511 ar << serialization::make_array<
513 boost::serialization::collection_size_type
515 static_cast<const value_type *>(&t[0]),
523 template<class Archive, class T>
524 inline void save(Archive & ar, /*const*/ T &t){
526 typename mpl::eval_if<is_pointer< T >,
527 mpl::identity<detail::save_pointer_type<Archive> >,
529 typename mpl::eval_if<is_enum< T >,
530 mpl::identity<detail::save_enum_type<Archive> >,
532 typename mpl::eval_if<is_array< T >,
533 mpl::identity<detail::save_array_type<Archive> >,
535 mpl::identity<detail::save_non_pointer_type<Archive> >
539 typex::invoke(ar, t);
542 } // namespace archive
545 #endif // BOOST_ARCHIVE_OSERIALIZER_HPP