require_concept.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // require_concept.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_REQUIRE_CONCEPT_HPP
  11. #define ASIO_REQUIRE_CONCEPT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/detail/type_traits.hpp"
  17. #include "asio/is_applicable_property.hpp"
  18. #include "asio/traits/require_concept_member.hpp"
  19. #include "asio/traits/require_concept_free.hpp"
  20. #include "asio/traits/static_require_concept.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. #if defined(GENERATING_DOCUMENTATION)
  23. namespace asio {
  24. /// A customisation point that applies a concept-enforcing property to an
  25. /// object.
  26. /**
  27. * The name <tt>require_concept</tt> denotes a customization point object. The
  28. * expression <tt>asio::require_concept(E, P)</tt> for some
  29. * subexpressions <tt>E</tt> and <tt>P</tt> (with types <tt>T =
  30. * decay_t<decltype(E)></tt> and <tt>Prop = decay_t<decltype(P)></tt>) is
  31. * expression-equivalent to:
  32. *
  33. * @li If <tt>is_applicable_property_v<T, Prop> &&
  34. * Prop::is_requirable_concept</tt> is not a well-formed constant expression
  35. * with value <tt>true</tt>, <tt>asio::require_concept(E, P)</tt> is
  36. * ill-formed.
  37. *
  38. * @li Otherwise, <tt>E</tt> if the expression <tt>Prop::template
  39. * static_query_v<T> == Prop::value()</tt> is a well-formed constant
  40. * expression with value <tt>true</tt>.
  41. *
  42. * @li Otherwise, <tt>(E).require_concept(P)</tt> if the expression
  43. * <tt>(E).require_concept(P)</tt> is well-formed.
  44. *
  45. * @li Otherwise, <tt>require_concept(E, P)</tt> if the expression
  46. * <tt>require_concept(E, P)</tt> is a valid expression with overload
  47. * resolution performed in a context that does not include the declaration
  48. * of the <tt>require_concept</tt> customization point object.
  49. *
  50. * @li Otherwise, <tt>asio::require_concept(E, P)</tt> is ill-formed.
  51. */
  52. inline constexpr unspecified require_concept = unspecified;
  53. /// A type trait that determines whether a @c require_concept expression is
  54. /// well-formed.
  55. /**
  56. * Class template @c can_require_concept is a trait that is derived from
  57. * @c true_type if the expression
  58. * <tt>asio::require_concept(std::declval<T>(),
  59. * std::declval<Property>())</tt> is well formed; otherwise @c false_type.
  60. */
  61. template <typename T, typename Property>
  62. struct can_require_concept :
  63. integral_constant<bool, automatically_determined>
  64. {
  65. };
  66. /// A type trait that determines whether a @c require_concept expression will
  67. /// not throw.
  68. /**
  69. * Class template @c is_nothrow_require_concept is a trait that is derived from
  70. * @c true_type if the expression
  71. * <tt>asio::require_concept(std::declval<T>(),
  72. * std::declval<Property>())</tt> is @c noexcept; otherwise @c false_type.
  73. */
  74. template <typename T, typename Property>
  75. struct is_nothrow_require_concept :
  76. integral_constant<bool, automatically_determined>
  77. {
  78. };
  79. /// A type trait that determines the result type of a @c require_concept
  80. /// expression.
  81. /**
  82. * Class template @c require_concept_result is a trait that determines the
  83. * result type of the expression
  84. * <tt>asio::require_concept(std::declval<T>(),
  85. * std::declval<Property>())</tt>.
  86. */
  87. template <typename T, typename Property>
  88. struct require_concept_result
  89. {
  90. /// The result of the @c require_concept expression.
  91. typedef automatically_determined type;
  92. };
  93. } // namespace asio
  94. #else // defined(GENERATING_DOCUMENTATION)
  95. namespace asio_require_concept_fn {
  96. using asio::conditional_t;
  97. using asio::decay_t;
  98. using asio::declval;
  99. using asio::enable_if_t;
  100. using asio::is_applicable_property;
  101. using asio::traits::require_concept_free;
  102. using asio::traits::require_concept_member;
  103. using asio::traits::static_require_concept;
  104. void require_concept();
  105. enum overload_type
  106. {
  107. identity,
  108. call_member,
  109. call_free,
  110. ill_formed
  111. };
  112. template <typename Impl, typename T, typename Properties, typename = void,
  113. typename = void, typename = void, typename = void, typename = void>
  114. struct call_traits
  115. {
  116. static constexpr overload_type overload = ill_formed;
  117. static constexpr bool is_noexcept = false;
  118. typedef void result_type;
  119. };
  120. template <typename Impl, typename T, typename Property>
  121. struct call_traits<Impl, T, void(Property),
  122. enable_if_t<
  123. is_applicable_property<
  124. decay_t<T>,
  125. decay_t<Property>
  126. >::value
  127. >,
  128. enable_if_t<
  129. decay_t<Property>::is_requirable_concept
  130. >,
  131. enable_if_t<
  132. static_require_concept<T, Property>::is_valid
  133. >>
  134. {
  135. static constexpr overload_type overload = identity;
  136. static constexpr bool is_noexcept = true;
  137. typedef T&& result_type;
  138. };
  139. template <typename Impl, typename T, typename Property>
  140. struct call_traits<Impl, T, void(Property),
  141. enable_if_t<
  142. is_applicable_property<
  143. decay_t<T>,
  144. decay_t<Property>
  145. >::value
  146. >,
  147. enable_if_t<
  148. decay_t<Property>::is_requirable_concept
  149. >,
  150. enable_if_t<
  151. !static_require_concept<T, Property>::is_valid
  152. >,
  153. enable_if_t<
  154. require_concept_member<
  155. typename Impl::template proxy<T>::type,
  156. Property
  157. >::is_valid
  158. >> :
  159. require_concept_member<
  160. typename Impl::template proxy<T>::type,
  161. Property
  162. >
  163. {
  164. static constexpr overload_type overload = call_member;
  165. };
  166. template <typename Impl, typename T, typename Property>
  167. struct call_traits<Impl, T, void(Property),
  168. enable_if_t<
  169. is_applicable_property<
  170. decay_t<T>,
  171. decay_t<Property>
  172. >::value
  173. >,
  174. enable_if_t<
  175. decay_t<Property>::is_requirable_concept
  176. >,
  177. enable_if_t<
  178. !static_require_concept<T, Property>::is_valid
  179. >,
  180. enable_if_t<
  181. !require_concept_member<
  182. typename Impl::template proxy<T>::type,
  183. Property
  184. >::is_valid
  185. >,
  186. enable_if_t<
  187. require_concept_free<T, Property>::is_valid
  188. >> :
  189. require_concept_free<T, Property>
  190. {
  191. static constexpr overload_type overload = call_free;
  192. };
  193. struct impl
  194. {
  195. template <typename T>
  196. struct proxy
  197. {
  198. #if defined(ASIO_HAS_DEDUCED_REQUIRE_CONCEPT_MEMBER_TRAIT)
  199. struct type
  200. {
  201. template <typename P>
  202. auto require_concept(P&& p)
  203. noexcept(
  204. noexcept(
  205. declval<conditional_t<true, T, P>>().require_concept(
  206. static_cast<P&&>(p))
  207. )
  208. )
  209. -> decltype(
  210. declval<conditional_t<true, T, P>>().require_concept(
  211. static_cast<P&&>(p))
  212. );
  213. };
  214. #else // defined(ASIO_HAS_DEDUCED_REQUIRE_CONCEPT_MEMBER_TRAIT)
  215. typedef T type;
  216. #endif // defined(ASIO_HAS_DEDUCED_REQUIRE_CONCEPT_MEMBER_TRAIT)
  217. };
  218. template <typename T, typename Property>
  219. ASIO_NODISCARD constexpr enable_if_t<
  220. call_traits<impl, T, void(Property)>::overload == identity,
  221. typename call_traits<impl, T, void(Property)>::result_type
  222. >
  223. operator()(T&& t, Property&&) const
  224. noexcept(call_traits<impl, T, void(Property)>::is_noexcept)
  225. {
  226. return static_cast<T&&>(t);
  227. }
  228. template <typename T, typename Property>
  229. ASIO_NODISCARD constexpr enable_if_t<
  230. call_traits<impl, T, void(Property)>::overload == call_member,
  231. typename call_traits<impl, T, void(Property)>::result_type
  232. >
  233. operator()(T&& t, Property&& p) const
  234. noexcept(call_traits<impl, T, void(Property)>::is_noexcept)
  235. {
  236. return static_cast<T&&>(t).require_concept(static_cast<Property&&>(p));
  237. }
  238. template <typename T, typename Property>
  239. ASIO_NODISCARD constexpr enable_if_t<
  240. call_traits<impl, T, void(Property)>::overload == call_free,
  241. typename call_traits<impl, T, void(Property)>::result_type
  242. >
  243. operator()(T&& t, Property&& p) const
  244. noexcept(call_traits<impl, T, void(Property)>::is_noexcept)
  245. {
  246. return require_concept(static_cast<T&&>(t), static_cast<Property&&>(p));
  247. }
  248. };
  249. template <typename T = impl>
  250. struct static_instance
  251. {
  252. static const T instance;
  253. };
  254. template <typename T>
  255. const T static_instance<T>::instance = {};
  256. } // namespace asio_require_concept_fn
  257. namespace asio {
  258. namespace {
  259. static constexpr const asio_require_concept_fn::impl&
  260. require_concept = asio_require_concept_fn::static_instance<>::instance;
  261. } // namespace
  262. typedef asio_require_concept_fn::impl require_concept_t;
  263. template <typename T, typename Property>
  264. struct can_require_concept :
  265. integral_constant<bool,
  266. asio_require_concept_fn::call_traits<
  267. require_concept_t, T, void(Property)>::overload !=
  268. asio_require_concept_fn::ill_formed>
  269. {
  270. };
  271. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  272. template <typename T, typename Property>
  273. constexpr bool can_require_concept_v = can_require_concept<T, Property>::value;
  274. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  275. template <typename T, typename Property>
  276. struct is_nothrow_require_concept :
  277. integral_constant<bool,
  278. asio_require_concept_fn::call_traits<
  279. require_concept_t, T, void(Property)>::is_noexcept>
  280. {
  281. };
  282. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  283. template <typename T, typename Property>
  284. constexpr bool is_nothrow_require_concept_v
  285. = is_nothrow_require_concept<T, Property>::value;
  286. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  287. template <typename T, typename Property>
  288. struct require_concept_result
  289. {
  290. typedef typename asio_require_concept_fn::call_traits<
  291. require_concept_t, T, void(Property)>::result_type type;
  292. };
  293. template <typename T, typename Property>
  294. using require_concept_result_t =
  295. typename require_concept_result<T, Property>::type;
  296. } // namespace asio
  297. #endif // defined(GENERATING_DOCUMENTATION)
  298. #include "asio/detail/pop_options.hpp"
  299. #endif // ASIO_REQUIRE_CONCEPT_HPP