check.hpp 5.3 KB

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