Brian Silverman | 8867871 | 2018-08-04 23:56:48 -0700 | [diff] [blame^] | 1 | #ifndef BOOST_ARCHIVE_DETAIL_CHECK_HPP |
| 2 | #define BOOST_ARCHIVE_DETAIL_CHECK_HPP |
| 3 | |
| 4 | // MS compatible compilers support #pragma once |
| 5 | #if defined(_MSC_VER) |
| 6 | # pragma once |
| 7 | #pragma inline_depth(511) |
| 8 | #pragma inline_recursion(on) |
| 9 | #endif |
| 10 | |
| 11 | #if defined(__MWERKS__) |
| 12 | #pragma inline_depth(511) |
| 13 | #endif |
| 14 | |
| 15 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 16 | // check.hpp: interface for serialization system. |
| 17 | |
| 18 | // (C) Copyright 2009 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) |
| 22 | |
| 23 | // See http://www.boost.org for updates, documentation, and revision history. |
| 24 | |
| 25 | #include <boost/config.hpp> |
| 26 | |
| 27 | #include <boost/static_assert.hpp> |
| 28 | #include <boost/type_traits/is_const.hpp> |
| 29 | |
| 30 | #include <boost/mpl/eval_if.hpp> |
| 31 | #include <boost/mpl/or.hpp> |
| 32 | #include <boost/mpl/equal_to.hpp> |
| 33 | #include <boost/mpl/int.hpp> |
| 34 | #include <boost/mpl/not.hpp> |
| 35 | #include <boost/mpl/greater.hpp> |
| 36 | #include <boost/mpl/assert.hpp> |
| 37 | |
| 38 | #include <boost/serialization/static_warning.hpp> |
| 39 | #include <boost/serialization/version.hpp> |
| 40 | #include <boost/serialization/level.hpp> |
| 41 | #include <boost/serialization/tracking.hpp> |
| 42 | #include <boost/serialization/wrapper.hpp> |
| 43 | |
| 44 | namespace boost { |
| 45 | namespace archive { |
| 46 | namespace detail { |
| 47 | |
| 48 | // checks for objects |
| 49 | |
| 50 | template<class T> |
| 51 | inline void check_object_level(){ |
| 52 | typedef |
| 53 | typename mpl::greater_equal< |
| 54 | serialization::implementation_level< T >, |
| 55 | mpl::int_<serialization::primitive_type> |
| 56 | >::type typex; |
| 57 | |
| 58 | // trap attempts to serialize objects marked |
| 59 | // not_serializable |
| 60 | BOOST_STATIC_ASSERT(typex::value); |
| 61 | } |
| 62 | |
| 63 | template<class T> |
| 64 | inline void check_object_versioning(){ |
| 65 | typedef |
| 66 | typename mpl::or_< |
| 67 | typename mpl::greater< |
| 68 | serialization::implementation_level< T >, |
| 69 | mpl::int_<serialization::object_serializable> |
| 70 | >, |
| 71 | typename mpl::equal_to< |
| 72 | serialization::version< T >, |
| 73 | mpl::int_<0> |
| 74 | > |
| 75 | > typex; |
| 76 | // trap attempts to serialize with objects that don't |
| 77 | // save class information in the archive with versioning. |
| 78 | BOOST_STATIC_ASSERT(typex::value); |
| 79 | } |
| 80 | |
| 81 | template<class T> |
| 82 | inline void check_object_tracking(){ |
| 83 | // presume it has already been determined that |
| 84 | // T is not a const |
| 85 | BOOST_STATIC_ASSERT(! boost::is_const< T >::value); |
| 86 | typedef typename mpl::equal_to< |
| 87 | serialization::tracking_level< T >, |
| 88 | mpl::int_<serialization::track_never> |
| 89 | >::type typex; |
| 90 | // saving an non-const object of a type not marked "track_never) |
| 91 | |
| 92 | // may be an indicator of an error usage of the |
| 93 | // serialization library and should be double checked. |
| 94 | // See documentation on object tracking. Also, see the |
| 95 | // "rationale" section of the documenation |
| 96 | // for motivation for this checking. |
| 97 | |
| 98 | BOOST_STATIC_WARNING(typex::value); |
| 99 | } |
| 100 | |
| 101 | // checks for pointers |
| 102 | |
| 103 | template<class T> |
| 104 | inline void check_pointer_level(){ |
| 105 | // we should only invoke this once we KNOW that T |
| 106 | // has been used as a pointer!! |
| 107 | typedef |
| 108 | typename mpl::or_< |
| 109 | typename mpl::greater< |
| 110 | serialization::implementation_level< T >, |
| 111 | mpl::int_<serialization::object_serializable> |
| 112 | >, |
| 113 | typename mpl::not_< |
| 114 | typename mpl::equal_to< |
| 115 | serialization::tracking_level< T >, |
| 116 | mpl::int_<serialization::track_selectively> |
| 117 | > |
| 118 | > |
| 119 | > typex; |
| 120 | // Address the following when serializing to a pointer: |
| 121 | |
| 122 | // a) This type doesn't save class information in the |
| 123 | // archive. That is, the serialization trait implementation |
| 124 | // level <= object_serializable. |
| 125 | // b) Tracking for this type is set to "track selectively" |
| 126 | |
| 127 | // in this case, indication that an object is tracked is |
| 128 | // not stored in the archive itself - see level == object_serializable |
| 129 | // but rather the existence of the operation ar >> T * is used to |
| 130 | // infer that an object of this type should be tracked. So, if |
| 131 | // you save via a pointer but don't load via a pointer the operation |
| 132 | // will fail on load without given any valid reason for the failure. |
| 133 | |
| 134 | // So if your program traps here, consider changing the |
| 135 | // tracking or implementation level traits - or not |
| 136 | // serializing via a pointer. |
| 137 | BOOST_STATIC_WARNING(typex::value); |
| 138 | } |
| 139 | |
| 140 | template<class T> |
| 141 | void inline check_pointer_tracking(){ |
| 142 | typedef typename mpl::greater< |
| 143 | serialization::tracking_level< T >, |
| 144 | mpl::int_<serialization::track_never> |
| 145 | >::type typex; |
| 146 | // serializing an object of a type marked "track_never" through a pointer |
| 147 | // could result in creating more objects than were saved! |
| 148 | BOOST_STATIC_WARNING(typex::value); |
| 149 | } |
| 150 | |
| 151 | template<class T> |
| 152 | inline void check_const_loading(){ |
| 153 | typedef |
| 154 | typename mpl::or_< |
| 155 | typename boost::serialization::is_wrapper< T >, |
| 156 | typename mpl::not_< |
| 157 | typename boost::is_const< T > |
| 158 | > |
| 159 | >::type typex; |
| 160 | // cannot load data into a "const" object unless it's a |
| 161 | // wrapper around some other non-const object. |
| 162 | BOOST_STATIC_ASSERT(typex::value); |
| 163 | } |
| 164 | |
| 165 | } // detail |
| 166 | } // archive |
| 167 | } // boost |
| 168 | |
| 169 | #endif // BOOST_ARCHIVE_DETAIL_CHECK_HPP |