Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
1 #ifndef BOOST_ARCHIVE_DETAIL_ISERIALIZER_HPP
2 #define BOOST_ARCHIVE_DETAIL_ISERIALIZER_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
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 // iserializer.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 <new> // for placement new
26 #include <memory> // for auto_ptr
27 #include <cstddef> // size_t
29 #include <boost/config.hpp>
30 #include <boost/detail/workaround.hpp>
31 #if defined(BOOST_NO_STDC_NAMESPACE)
36 #include <boost/throw_exception.hpp>
37 #include <boost/smart_cast.hpp>
38 #include <boost/static_assert.hpp>
39 #include <boost/static_warning.hpp>
40 #include <boost/detail/no_exceptions_support.hpp>
42 #include <boost/type_traits/is_pointer.hpp>
43 #include <boost/type_traits/is_fundamental.hpp>
44 #include <boost/type_traits/is_enum.hpp>
45 #include <boost/type_traits/is_const.hpp>
46 #include <boost/type_traits/remove_const.hpp>
47 #include <boost/serialization/is_abstract.hpp>
49 #include <boost/mpl/eval_if.hpp>
50 #include <boost/mpl/if.hpp>
51 #include <boost/mpl/identity.hpp>
52 #include <boost/mpl/or.hpp>
53 #include <boost/mpl/and.hpp>
54 #include <boost/mpl/less.hpp>
55 #include <boost/mpl/greater_equal.hpp>
56 #include <boost/mpl/int.hpp>
57 #include <boost/mpl/list.hpp>
58 #include <boost/mpl/empty.hpp>
59 #include <boost/mpl/not.hpp>
61 #ifndef BOOST_SERIALIZATION_DEFAULT_TYPE_INFO
62 #include <boost/serialization/extended_type_info_typeid.hpp>
64 // the following is need only for dynamic cast of polymorphic pointers
65 #include <boost/archive/detail/basic_iarchive.hpp>
66 #include <boost/archive/detail/basic_iserializer.hpp>
67 #include <boost/archive/detail/archive_pointer_iserializer.hpp>
68 #include <boost/archive/archive_exception.hpp>
70 #include <boost/serialization/force_include.hpp>
71 #include <boost/serialization/serialization.hpp>
72 #include <boost/serialization/version.hpp>
73 #include <boost/serialization/level.hpp>
74 #include <boost/serialization/tracking.hpp>
75 #include <boost/serialization/type_info_implementation.hpp>
76 #include <boost/serialization/nvp.hpp>
77 #include <boost/serialization/binary_object.hpp>
78 #include <boost/serialization/void_cast.hpp>
82 namespace serialization {
83 class extended_type_info;
84 } // namespace serialization
88 // an accessor to permit friend access to archives. Needed because
89 // some compilers don't handle friend templates completely
92 template<class Archive, class T>
93 static void load_primitive(Archive &ar, T &t){
100 template<class Archive, class T>
101 class iserializer : public basic_iserializer
104 virtual void destroy(/*const*/ void *address) const {
105 boost::serialization::access::destroy(static_cast<T *>(address));
107 // private constructor to inhibit any existence other than the
109 explicit iserializer() :
111 * boost::serialization::type_info_implementation<T>::type::get_instance()
115 virtual BOOST_DLLEXPORT void load_object_data(
118 const unsigned int file_version
120 virtual bool class_info() const {
121 return boost::serialization::implementation_level<T>::value
122 >= boost::serialization::object_class_info;
124 virtual bool tracking(const unsigned int flags) const {
125 // if(0 != (flags & no_tracking))
127 return boost::serialization::tracking_level<T>::value
128 == boost::serialization::track_always
129 || boost::serialization::tracking_level<T>::value
130 == boost::serialization::track_selectivly
131 && serialized_as_pointer();
133 virtual unsigned int version() const {
134 return ::boost::serialization::version<T>::value;
136 virtual bool is_polymorphic() const {
137 typedef BOOST_DEDUCED_TYPENAME
138 boost::serialization::type_info_implementation<
140 >::type::is_polymorphic::type typex;
143 static iserializer & instantiate(){
144 static iserializer instance;
147 virtual ~iserializer(){};
150 template<class Archive, class T>
151 BOOST_DLLEXPORT void iserializer<Archive, T>::load_object_data(
154 const unsigned int file_version
156 // make sure call is routed through the higest interface that might
157 // be specialized by the user.
158 boost::serialization::serialize_adl(
159 boost::smart_cast_reference<Archive &>(ar),
160 * static_cast<T *>(x),
165 // instantiation of this template creates a static object. Note inversion of
166 // normal argument order to workaround bizarre error in MSVC 6.0 which only
167 // manifests iftself during compiler time.
168 template<class T, class Archive>
169 class pointer_iserializer : public archive_pointer_iserializer<Archive>
172 virtual const basic_iserializer & get_basic_serializer() const {
173 return iserializer<Archive, T>::instantiate();
175 virtual BOOST_DLLEXPORT void load_object_ptr(
178 const unsigned int file_version
180 #if defined(__GNUC__) || ( defined(BOOST_MSVC) && (_MSC_VER <= 1300) )
183 // private constructor to inhibit any existence other than the
184 // static one. Note GCC doesn't permit constructor to be private
185 explicit BOOST_DLLEXPORT pointer_iserializer() BOOST_USED;
186 static const pointer_iserializer instance;
188 // at least one compiler (CW) seems to require that serialize_adl
189 // be explicitly instantiated. Still under investigation.
190 #if ! BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
191 void (* const m)(Archive &, T &, const unsigned);
192 boost::serialization::extended_type_info * (* e)();
194 static BOOST_DLLEXPORT const pointer_iserializer & instantiate() BOOST_USED;
195 virtual ~pointer_iserializer(){};
198 template<class T, class Archive>
199 BOOST_DLLEXPORT const pointer_iserializer<T, Archive> &
200 pointer_iserializer<T, Archive>::instantiate() {
204 // note: instances of this template to be constructed before the main
205 // is called in order for things to be initialized properly. For this
206 // reason, hiding the instance in a static function as was done above
207 // won't work here so we created a free instance here.
208 template<class T, class Archive>
209 const pointer_iserializer<T, Archive> pointer_iserializer<T, Archive>::instance;
211 // note trick to be sure that operator new is using class specific
212 // version if such exists. Due to Peter Dimov.
213 // note: the following fails if T has no default constructor.
214 // otherwise it would have been ideal
215 //struct heap_allocator : public T
218 // return ::new(sizeof(T));
222 // note: this should really be a member of the load_ptr function
223 // below but some compilers still complain about this.
225 struct heap_allocator
228 // note: this fails on msvc 7.0 and gcc 3.2
229 template <class U, U x> struct test;
233 yes has_op_new(U*, test<void* (*)(std::size_t), &U::operator new>* = 0);
239 T * new_operator(yes){
240 return (T::operator new)(sizeof(T));
242 T * new_operator(no){
243 return static_cast<T *>(operator new(sizeof(T)));
246 return new_operator(has_op_new(static_cast<T *>(NULL)));
249 // while this doesn't handle operator new overload for class T
251 return static_cast<T *>(operator new(sizeof(T)));
256 // due to Martin Ecker
257 template <typename T>
258 class auto_ptr_with_deleter
261 explicit auto_ptr_with_deleter(T* p) :
264 ~auto_ptr_with_deleter(){
266 boost::serialization::access::destroy(m_p);
281 template<class T, class Archive>
282 BOOST_DLLEXPORT void pointer_iserializer<T, Archive>::load_object_ptr(
285 const unsigned int file_version
287 Archive & ar_impl = boost::smart_cast_reference<Archive &>(ar);
289 // if(0 != (ar.get_flags() & no_object_creation)){
290 // ar_impl >> boost::serialization::make_nvp(NULL, * static_cast<T *>(x));
294 auto_ptr_with_deleter<T> ap(heap_allocator<T>::invoke());
296 boost::throw_exception(std::bad_alloc()) ;
301 // catch exception during load_construct_data so that we don't
302 // automatically delete the t which is most likely not fully
305 // this addresses an obscure situtation that occurs when
306 // load_constructor de-serializes something through a pointer.
307 ar.next_object_pointer(t);
308 boost::serialization::load_construct_data_adl<Archive, T>(
319 ar_impl >> boost::serialization::make_nvp(NULL, * t);
323 template<class T, class Archive>
324 #if ! BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
325 BOOST_DLLEXPORT pointer_iserializer<T, Archive>::pointer_iserializer() :
326 archive_pointer_iserializer<Archive>(
327 * boost::serialization::type_info_implementation<T>::type::get_instance()
329 m(boost::serialization::serialize_adl<Archive, T>),
330 e(boost::serialization::type_info_implementation<T>::type::get_instance)
332 BOOST_DLLEXPORT pointer_iserializer<T, Archive>::pointer_iserializer() :
333 archive_pointer_iserializer<Archive>(
334 * boost::serialization::type_info_implementation<T>::type::get_instance()
338 iserializer<Archive, T> & bis = iserializer<Archive, T>::instantiate();
342 template<class Archive, class T>
343 struct load_non_pointer_type {
344 // note this bounces the call right back to the archive
345 // with no runtime overhead
346 struct load_primitive {
347 static void invoke(Archive & ar, T & t){
348 load_access::load_primitive(ar, t);
351 // note this bounces the call right back to the archive
352 // with no runtime overhead
354 static void invoke(Archive & ar, T & t){
355 // short cut to user's serializer
356 // make sure call is routed through the higest interface that might
357 // be specialized by the user.
358 boost::serialization::serialize_adl(
359 ar, t, boost::serialization::version<T>::value
364 // note this save class information including version
365 // and serialization level to the archive
366 struct load_standard {
367 static void invoke(Archive &ar, T &t){
368 //BOOST_STATIC_ASSERT(! boost::is_const<T>::value);
369 // borland - for some reason T is const here - even though
370 // its not called that way - so fix it her
371 typedef BOOST_DEDUCED_TYPENAME boost::remove_const<T>::type typex;
372 void * x = & const_cast<typex &>(t);
373 ar.load_object(x, iserializer<Archive, T>::instantiate());
377 struct load_conditional {
378 static void invoke(Archive &ar, T &t){
379 //if(0 == (ar.get_flags() & no_tracking))
380 load_standard::invoke(ar, t);
382 // load_only::invoke(ar, t);
386 typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
389 boost::serialization::implementation_level<T>,
390 mpl::int_<boost::serialization::primitive_type>
392 mpl::identity<load_primitive>,
394 BOOST_DEDUCED_TYPENAME mpl::eval_if<
395 // class info / version
397 boost::serialization::implementation_level<T>,
398 mpl::int_<boost::serialization::object_class_info>
401 mpl::identity<load_standard>,
403 BOOST_DEDUCED_TYPENAME mpl::eval_if<
406 boost::serialization::tracking_level<T>,
407 mpl::int_<boost::serialization::track_never>
410 mpl::identity<load_only>,
412 // do a fast load only tracking is turned off
413 mpl::identity<load_conditional>
416 static void invoke(Archive & ar, T &t){
417 BOOST_STATIC_ASSERT((
419 boost::serialization::implementation_level<T>,
420 mpl::int_<boost::serialization::primitive_type>
423 typex::invoke(ar, t);
427 template<class Archive, class Tptr>
428 struct load_pointer_type {
432 static const basic_pointer_iserializer * register_type(Archive & /* ar */){
433 #if ! defined(__BORLANDC__)
434 typedef BOOST_DEDUCED_TYPENAME
435 boost::serialization::type_info_implementation<T>::type::is_polymorphic typex;
436 // it has? to be polymorphic
437 BOOST_STATIC_ASSERT(typex::value);
439 return static_cast<basic_pointer_iserializer *>(NULL);
446 static const basic_pointer_iserializer * register_type(Archive & ar){
447 return ar.register_type(static_cast<T *>(NULL));
452 static const basic_pointer_iserializer * register_type(Archive &ar, T & /*t*/){
453 // there should never be any need to load an abstract polymorphic
454 // class pointer. Inhibiting code generation for this
455 // permits abstract base classes to be used - note: exception
456 // virtual serialize functions used for plug-ins
457 typedef BOOST_DEDUCED_TYPENAME
459 serialization::is_abstract<T>,
460 mpl::identity<abstract<T> >,
461 mpl::identity<non_abstract<T> >
463 return typex::register_type(ar);
467 static T * pointer_tweak(
468 const boost::serialization::extended_type_info & eti,
472 // tweak the pointer back to the base class
473 return static_cast<T *>(
474 boost::serialization::void_upcast(
476 * boost::serialization::type_info_implementation<T>::type::get_instance(),
482 static void invoke(Archive & ar, Tptr & t){
483 const basic_pointer_iserializer * bpis_ptr = register_type(ar, *t);
484 const basic_pointer_iserializer * newbpis_ptr = ar.load_pointer(
485 * reinterpret_cast<void **>(&t),
487 archive_pointer_iserializer<Archive>::find
489 // if the pointer isn't that of the base class
490 if(newbpis_ptr != bpis_ptr){
491 t = pointer_tweak(newbpis_ptr->get_eti(), t, *t);
496 template<class Archive, class T>
497 struct load_enum_type {
498 static void invoke(Archive &ar, T &t){
499 // convert integers to correct enum to load
501 ar >> boost::serialization::make_nvp(NULL, i);
502 t = static_cast<T>(i);
506 template<class Archive, class T>
507 struct load_array_type {
508 static void invoke(Archive &ar, T &t){
509 // convert integers to correct enum to load
510 int current_count = sizeof(t) / (
511 static_cast<char *>(static_cast<void *>(&t[1]))
512 - static_cast<char *>(static_cast<void *>(&t[0]))
515 ar >> BOOST_SERIALIZATION_NVP(count);
516 if(count > current_count)
517 boost::throw_exception(archive::archive_exception(
518 boost::archive::archive_exception::array_size_too_short
521 for(i = 0; i < count; ++i)
522 ar >> boost::serialization::make_nvp("item", t[i]);
526 // note bogus arguments to workaround msvc 6 silent runtime failure
527 template<class Archive, class T>
529 inline const basic_pointer_iserializer &
530 instantiate_pointer_iserializer(
531 Archive * /* ar = NULL */,
535 template<class Archive, class T>
537 inline const basic_pointer_iserializer &
538 instantiate_pointer_iserializer(
539 Archive * /* ar = NULL */,
542 // note: reversal of order of arguments to work around msvc 6.0 bug
543 // that manifests itself while trying to link.
544 return pointer_iserializer<T, Archive>::instantiate();
549 template<class Archive, class T>
550 inline void load(Archive &ar, T &t){
551 // if this assertion trips. It means we're trying to load a
552 // const object with a compiler that doesn't have correct
553 // funtion template ordering. On other compilers, this is
555 BOOST_STATIC_ASSERT(! boost::is_const<T>::value);
557 BOOST_DEDUCED_TYPENAME mpl::eval_if<is_pointer<T>,
558 mpl::identity<detail::load_pointer_type<Archive, T> >
560 BOOST_DEDUCED_TYPENAME mpl::eval_if<is_array<T>,
561 mpl::identity<detail::load_array_type<Archive, T> >
563 BOOST_DEDUCED_TYPENAME mpl::eval_if<is_enum<T>,
564 mpl::identity<detail::load_enum_type<Archive, T> >
566 mpl::identity<detail::load_non_pointer_type<Archive, T> >
570 typex::invoke(ar, t);
574 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x560))
575 // borland has a couple fo problems
576 // a) if function is partiall specialized - see below
577 // const paramters are transformed to non-const ones
578 // b) implementation of base_object can't be made to work
579 // correctly which results in all base_object s being const.
580 // So, strip off the const for borland. This breaks the trap
581 // for loading const objects - but I see no alternative
582 template<class Archive, class T>
583 inline void load(Archive &ar, const T & t){
584 load(ar, const_cast<T &>(t));
588 // let wrappers through. (Someday implement is_wrapper)
589 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
590 template<class Archive, class T>
591 inline void load(Archive &ar, const serialization::nvp<T> &t){
592 boost::archive::load(ar, const_cast<serialization::nvp<T> &>(t));
594 template<class Archive>
595 inline void load(Archive &ar, const serialization::binary_object &t){
596 boost::archive::load(ar, const_cast<serialization::binary_object &>(t));
599 //template<class Archive, class T>
600 //inline void load(Archive &ar, const serialization::binary_object &t){
601 // load(ar, const_cast<binary_object &>(t));
605 } // namespace archive
608 #endif // BOOST_ARCHIVE_DETAIL_ISERIALIZER_HPP