override.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef BOOST_CONTRACT_OVERRIDE_HPP_
  2. #define BOOST_CONTRACT_OVERRIDE_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. /** @file
  8. Handle public function overrides (for subcontracting).
  9. */
  10. // IMPORTANT: Included by contract_macro.hpp so must #if-guard all its includes.
  11. #include <boost/contract/core/config.hpp>
  12. #include <boost/preprocessor/cat.hpp>
  13. #include <boost/preprocessor/config/config.hpp>
  14. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  15. /**
  16. Declare an override type trait with an arbitrary name.
  17. Declare the override type trait named @c type_name to pass as an explicit
  18. template parameter to @RefFunc{boost::contract::public_function} for public
  19. function overrides.
  20. @see @RefSect{advanced.named_overrides, Named Overrides}
  21. @param type_name Name of the override type trait this macro will declare.
  22. (This is not a variadic macro parameter but it should
  23. never contain commas because it is an identifier.)
  24. @param func_name Function name of the public function override.
  25. This macro is called just once even if the function name
  26. is overloaded (the same override type trait is used for
  27. all overloaded functions with the same name, see
  28. @RefSect{advanced.function_overloads,
  29. Function Overloads}).
  30. (This is not a variadic macro parameter but it should
  31. never contain commas because it is an identifier.)
  32. */
  33. #define BOOST_CONTRACT_NAMED_OVERRIDE(type_name, func_name)
  34. #elif !defined(BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS)
  35. #include <boost/contract/core/virtual.hpp>
  36. #include <boost/contract/detail/type_traits/mirror.hpp>
  37. #include <boost/contract/detail/tvariadic.hpp>
  38. #include <boost/contract/detail/none.hpp>
  39. #include <boost/contract/detail/name.hpp>
  40. /* PRIVATE */
  41. #define BOOST_CONTRACT_OVERRIDE_CALL_BASE_(z, arity, arity_compl, \
  42. func_name) \
  43. template< \
  44. class BOOST_CONTRACT_DETAIL_NAME1(B), \
  45. class BOOST_CONTRACT_DETAIL_NAME1(C) \
  46. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(arity) \
  47. BOOST_CONTRACT_DETAIL_TVARIADIC_TPARAMS_Z(z, arity, \
  48. BOOST_CONTRACT_DETAIL_NAME1(Args)) \
  49. > \
  50. static void BOOST_CONTRACT_DETAIL_NAME1(call_base)( \
  51. boost::contract::virtual_* BOOST_CONTRACT_DETAIL_NAME1(v), \
  52. BOOST_CONTRACT_DETAIL_NAME1(C)* BOOST_CONTRACT_DETAIL_NAME1(obj) \
  53. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(arity) \
  54. BOOST_CONTRACT_DETAIL_TVARIADIC_FPARAMS_Z(z, arity, \
  55. BOOST_CONTRACT_DETAIL_NAME1(Args), \
  56. &, \
  57. BOOST_CONTRACT_DETAIL_NAME1(args) \
  58. ) \
  59. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_COMMA(arity_compl) \
  60. BOOST_CONTRACT_DETAIL_NO_TVARIADIC_ENUM_Z(z, arity_compl, \
  61. boost::contract::detail::none&) \
  62. ) { \
  63. BOOST_CONTRACT_DETAIL_NAME1(obj)-> \
  64. BOOST_CONTRACT_DETAIL_NAME1(B)::func_name( \
  65. BOOST_CONTRACT_DETAIL_TVARIADIC_ARGS_Z(z, arity, \
  66. BOOST_CONTRACT_DETAIL_NAME1(args)) \
  67. BOOST_CONTRACT_DETAIL_TVARIADIC_COMMA(arity) \
  68. BOOST_CONTRACT_DETAIL_NAME1(v) \
  69. ); \
  70. }
  71. #if BOOST_CONTRACT_DETAIL_TVARIADIC
  72. #define BOOST_CONTRACT_OVERRIDE_CALL_BASE_DECL_(func_name) \
  73. BOOST_CONTRACT_OVERRIDE_CALL_BASE_(1, ~, ~, func_name)
  74. #else
  75. #include <boost/preprocessor/repetition/repeat.hpp>
  76. #include <boost/preprocessor/arithmetic/inc.hpp>
  77. #include <boost/preprocessor/arithmetic/sub.hpp>
  78. #define BOOST_CONTRACT_OVERRIDE_CALL_BASE_DECL_(func_name) \
  79. BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_CONTRACT_MAX_ARGS), \
  80. BOOST_CONTRACT_OVERRIDE_CALL_BASE_ARITY_, func_name) \
  81. #define BOOST_CONTRACT_OVERRIDE_CALL_BASE_ARITY_(z, arity, func_name) \
  82. BOOST_CONTRACT_OVERRIDE_CALL_BASE_(z, arity, \
  83. BOOST_PP_SUB(BOOST_CONTRACT_MAX_ARGS, arity), func_name)
  84. #endif
  85. /* PUBLIC */
  86. #define BOOST_CONTRACT_NAMED_OVERRIDE(type_name, func_name) \
  87. struct type_name { \
  88. BOOST_CONTRACT_DETAIL_MIRROR_HAS_MEMBER_FUNCTION( \
  89. BOOST_CONTRACT_DETAIL_NAME1(has_member_function), \
  90. func_name \
  91. ) \
  92. BOOST_CONTRACT_OVERRIDE_CALL_BASE_DECL_(func_name) \
  93. };
  94. #else
  95. #define BOOST_CONTRACT_NAMED_OVERRIDE(type_name, func_name) \
  96. struct type_name {}; /* empty (not used), just to compile */
  97. #endif
  98. /* PUBLIC */
  99. /**
  100. Declare an override type trait named <c>override_<i>func_name</i></c>.
  101. Declare the override type trait named <c>override_<i>func_name</i></c> to pass
  102. as an explicit template parameter to @RefFunc{boost::contract::public_function}
  103. for public function overrides.
  104. Use @RefMacro{BOOST_CONTRACT_NAMED_OVERRIDE} to generate an override type trait
  105. with a name different than <c>override_<i>func_name</i></c> (usually not
  106. needed).
  107. @see @RefSect{tutorial.public_function_overrides__subcontracting_,
  108. Public Function Overrides}
  109. @param func_name Function name of the public function override.
  110. This macro is called just once even if the function name is
  111. overloaded (the same override type trait is used for all
  112. overloaded functions with the same name, see
  113. @RefSect{advanced.function_overloads, Function Overloads}).
  114. (This is not a variadic macro parameter but it should never
  115. contain any comma because it is an identifier.)
  116. */
  117. #define BOOST_CONTRACT_OVERRIDE(func_name) \
  118. BOOST_CONTRACT_NAMED_OVERRIDE(BOOST_PP_CAT(override_, func_name), func_name)
  119. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  120. /**
  121. Declare multiple override type traits at once naming them
  122. <c>override_...</c> (for convenience).
  123. This variadic macro is provided for convenience as
  124. <c>BOOST_CONTRACT_OVERRIDES(f_1, f_2, ..., f_n)</c> expands to code
  125. equivalent to:
  126. @code
  127. BOOST_CONTRACT_OVERRIDE(f_1)
  128. BOOST_CONTRACT_OVERRIDE(f_2)
  129. ...
  130. BOOST_CONTRACT_OVERRIDE(f_n)
  131. @endcode
  132. On compilers that do not support variadic macros,
  133. the override type traits can be equivalently programmed one-by-one calling
  134. @RefMacro{BOOST_CONTRACT_OVERRIDE} for each function name as shown above.
  135. @see @RefSect{tutorial.public_function_overrides__subcontracting_,
  136. Public Function Overrides}
  137. @param ... A comma separated list of one or more function names of public
  138. function overrides.
  139. (Each function name should never contain commas because it is an
  140. identifier.)
  141. */
  142. #define BOOST_CONTRACT_OVERRIDES(...)
  143. #elif BOOST_PP_VARIADICS
  144. #include <boost/preprocessor/seq/for_each.hpp>
  145. #include <boost/preprocessor/variadic/to_seq.hpp>
  146. /* PRIVATE */
  147. #define BOOST_CONTRACT_OVERRIDES_SEQ_(r, unused, func_name) \
  148. BOOST_CONTRACT_OVERRIDE(func_name)
  149. /* PUBLIC */
  150. #define BOOST_CONTRACT_OVERRIDES(...) \
  151. BOOST_PP_SEQ_FOR_EACH(BOOST_CONTRACT_OVERRIDES_SEQ_, ~, \
  152. BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
  153. #else
  154. #define BOOST_CONTRACT_OVERRIDES \
  155. BOOST_CONTRACT_ERROR_macro_OVERRIDES_requires_variadic_macros_otherwise_manually_repeat_OVERRIDE_macro
  156. #endif
  157. #endif // #include guard