mirror.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef BOOST_CONTRACT_DETAIL_MIRROR_HPP_
  2. #define BOOST_CONTRACT_DETAIL_MIRROR_HPP_
  3. // Copyright (C) 2008-2018 Lorenzo Caminiti
  4. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  5. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  6. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  7. #include <boost/contract/detail/name.hpp>
  8. #include <boost/function_types/member_function_pointer.hpp>
  9. #include <boost/function_types/function_pointer.hpp>
  10. #include <boost/function_types/property_tags.hpp>
  11. #include <boost/mpl/push_front.hpp>
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/preprocessor/control/iif.hpp>
  14. #include <boost/preprocessor/tuple/rem.hpp>
  15. #include <boost/preprocessor/tuple/eat.hpp>
  16. // NOTE: Unfortunately, it is not possible to use Boost.TTI because it not
  17. // always works on MSVC (e.g., when the mirror meta-function is invoked
  18. // multiple times, MSVC 2010 gives an internal compiler error). This is a
  19. // simpler mirror implementation that seems to work better on MSVC.
  20. /* PRIVATE */
  21. #define BOOST_CONTRACT_DETAIL_MIRROR_END_(tparam) \
  22. template<typename> \
  23. static boost::contract::detail::mirror::no& apply(...); \
  24. public: \
  25. static bool const value = sizeof(apply<tparam>(0)) == \
  26. sizeof(boost::contract::detail::mirror::yes); \
  27. typedef boost::mpl::bool_<value> type;
  28. #define BOOST_CONTRACT_DETAIL_MIRROR_HAS_MEMBER_FUNCTION_(is_static, \
  29. trait, func_name) \
  30. template< \
  31. typename BOOST_CONTRACT_DETAIL_NAME1(T), \
  32. typename BOOST_CONTRACT_DETAIL_NAME1(R), \
  33. class BOOST_CONTRACT_DETAIL_NAME1(P), \
  34. class BOOST_CONTRACT_DETAIL_NAME1(G) = boost::function_types::null_tag \
  35. > \
  36. class trait { \
  37. template<class BOOST_CONTRACT_DETAIL_NAME1(C)> \
  38. static boost::contract::detail::mirror::yes& apply( \
  39. boost::contract::detail::mirror::check_function< \
  40. typename \
  41. BOOST_PP_IIF(is_static, \
  42. boost::function_types::function_pointer \
  43. , \
  44. boost::function_types::member_function_pointer \
  45. ) \
  46. < \
  47. typename boost::mpl::push_front< \
  48. BOOST_PP_IIF(is_static, \
  49. BOOST_CONTRACT_DETAIL_NAME1(P) \
  50. BOOST_PP_TUPLE_EAT(2) \
  51. , \
  52. BOOST_PP_TUPLE_REM(2) \
  53. )( \
  54. typename boost::mpl::push_front< \
  55. BOOST_CONTRACT_DETAIL_NAME1(P), \
  56. BOOST_CONTRACT_DETAIL_NAME1(C) \
  57. >::type \
  58. ) \
  59. , BOOST_CONTRACT_DETAIL_NAME1(R) \
  60. >::type, \
  61. BOOST_CONTRACT_DETAIL_NAME1(G) \
  62. >::type, \
  63. &BOOST_CONTRACT_DETAIL_NAME1(C)::func_name \
  64. >* \
  65. ); \
  66. BOOST_CONTRACT_DETAIL_MIRROR_END_( \
  67. BOOST_CONTRACT_DETAIL_NAME1(T)) \
  68. };
  69. /* PUBLIC */
  70. #define BOOST_CONTRACT_DETAIL_MIRROR_HAS_TYPE(trait, type_name)\
  71. template<typename BOOST_CONTRACT_DETAIL_NAME1(T)> \
  72. class trait { \
  73. template<class BOOST_CONTRACT_DETAIL_NAME1(C)> \
  74. static boost::contract::detail::mirror::yes& apply( \
  75. typename BOOST_CONTRACT_DETAIL_NAME1(C)::type_name*); \
  76. BOOST_CONTRACT_DETAIL_MIRROR_END_( \
  77. BOOST_CONTRACT_DETAIL_NAME1(T)) \
  78. };
  79. #define BOOST_CONTRACT_DETAIL_MIRROR_HAS_MEMBER_FUNCTION( \
  80. trait, func_name) \
  81. BOOST_CONTRACT_DETAIL_MIRROR_HAS_MEMBER_FUNCTION_( \
  82. /* is_static = */ 0, trait, func_name)
  83. #define BOOST_CONTRACT_DETAIL_MIRROR_HAS_STATIC_MEMBER_FUNCTION(trait, \
  84. func_name) \
  85. BOOST_CONTRACT_DETAIL_MIRROR_HAS_MEMBER_FUNCTION_( \
  86. /* is_static = */ 1, trait, func_name)
  87. /* CODE */
  88. namespace boost { namespace contract { namespace detail { namespace mirror {
  89. typedef class {} yes;
  90. typedef yes no[2];
  91. template<typename F, F> class check_function;
  92. } } } } // namespace
  93. #endif // #include guard