check.hpp 5.3 KB

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