require_concept.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // require_concept.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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;
  97. using asio::decay;
  98. using asio::declval;
  99. using asio::enable_if;
  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. ASIO_STATIC_CONSTEXPR(overload_type, overload = ill_formed);
  117. ASIO_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. typename enable_if<
  123. is_applicable_property<
  124. typename decay<T>::type,
  125. typename decay<Property>::type
  126. >::value
  127. >::type,
  128. typename enable_if<
  129. decay<Property>::type::is_requirable_concept
  130. >::type,
  131. typename enable_if<
  132. static_require_concept<T, Property>::is_valid
  133. >::type>
  134. {
  135. ASIO_STATIC_CONSTEXPR(overload_type, overload = identity);
  136. ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
  137. typedef ASIO_MOVE_ARG(T) result_type;
  138. };
  139. template <typename Impl, typename T, typename Property>
  140. struct call_traits<Impl, T, void(Property),
  141. typename enable_if<
  142. is_applicable_property<
  143. typename decay<T>::type,
  144. typename decay<Property>::type
  145. >::value
  146. >::type,
  147. typename enable_if<
  148. decay<Property>::type::is_requirable_concept
  149. >::type,
  150. typename enable_if<
  151. !static_require_concept<T, Property>::is_valid
  152. >::type,
  153. typename enable_if<
  154. require_concept_member<
  155. typename Impl::template proxy<T>::type,
  156. Property
  157. >::is_valid
  158. >::type> :
  159. require_concept_member<
  160. typename Impl::template proxy<T>::type,
  161. Property
  162. >
  163. {
  164. ASIO_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. typename enable_if<
  169. is_applicable_property<
  170. typename decay<T>::type,
  171. typename decay<Property>::type
  172. >::value
  173. >::type,
  174. typename enable_if<
  175. decay<Property>::type::is_requirable_concept
  176. >::type,
  177. typename enable_if<
  178. !static_require_concept<T, Property>::is_valid
  179. >::type,
  180. typename enable_if<
  181. !require_concept_member<
  182. typename Impl::template proxy<T>::type,
  183. Property
  184. >::is_valid
  185. >::type,
  186. typename enable_if<
  187. require_concept_free<T, Property>::is_valid
  188. >::type> :
  189. require_concept_free<T, Property>
  190. {
  191. ASIO_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(ASIO_MOVE_ARG(P) p)
  203. noexcept(
  204. noexcept(
  205. declval<typename conditional<true, T, P>::type>().require_concept(
  206. ASIO_MOVE_CAST(P)(p))
  207. )
  208. )
  209. -> decltype(
  210. declval<typename conditional<true, T, P>::type>().require_concept(
  211. ASIO_MOVE_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 ASIO_CONSTEXPR typename enable_if<
  220. call_traits<impl, T, void(Property)>::overload == identity,
  221. typename call_traits<impl, T, void(Property)>::result_type
  222. >::type
  223. operator()(
  224. ASIO_MOVE_ARG(T) t,
  225. ASIO_MOVE_ARG(Property)) const
  226. ASIO_NOEXCEPT_IF((
  227. call_traits<impl, T, void(Property)>::is_noexcept))
  228. {
  229. return ASIO_MOVE_CAST(T)(t);
  230. }
  231. template <typename T, typename Property>
  232. ASIO_NODISCARD ASIO_CONSTEXPR typename enable_if<
  233. call_traits<impl, T, void(Property)>::overload == call_member,
  234. typename call_traits<impl, T, void(Property)>::result_type
  235. >::type
  236. operator()(
  237. ASIO_MOVE_ARG(T) t,
  238. ASIO_MOVE_ARG(Property) p) const
  239. ASIO_NOEXCEPT_IF((
  240. call_traits<impl, T, void(Property)>::is_noexcept))
  241. {
  242. return ASIO_MOVE_CAST(T)(t).require_concept(
  243. ASIO_MOVE_CAST(Property)(p));
  244. }
  245. template <typename T, typename Property>
  246. ASIO_NODISCARD ASIO_CONSTEXPR typename enable_if<
  247. call_traits<impl, T, void(Property)>::overload == call_free,
  248. typename call_traits<impl, T, void(Property)>::result_type
  249. >::type
  250. operator()(
  251. ASIO_MOVE_ARG(T) t,
  252. ASIO_MOVE_ARG(Property) p) const
  253. ASIO_NOEXCEPT_IF((
  254. call_traits<impl, T, void(Property)>::is_noexcept))
  255. {
  256. return require_concept(
  257. ASIO_MOVE_CAST(T)(t),
  258. ASIO_MOVE_CAST(Property)(p));
  259. }
  260. };
  261. template <typename T = impl>
  262. struct static_instance
  263. {
  264. static const T instance;
  265. };
  266. template <typename T>
  267. const T static_instance<T>::instance = {};
  268. } // namespace asio_require_concept_fn
  269. namespace asio {
  270. namespace {
  271. static ASIO_CONSTEXPR const asio_require_concept_fn::impl&
  272. require_concept = asio_require_concept_fn::static_instance<>::instance;
  273. } // namespace
  274. typedef asio_require_concept_fn::impl require_concept_t;
  275. template <typename T, typename Property>
  276. struct can_require_concept :
  277. integral_constant<bool,
  278. asio_require_concept_fn::call_traits<
  279. require_concept_t, T, void(Property)>::overload !=
  280. asio_require_concept_fn::ill_formed>
  281. {
  282. };
  283. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  284. template <typename T, typename Property>
  285. constexpr bool can_require_concept_v
  286. = can_require_concept<T, Property>::value;
  287. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  288. template <typename T, typename Property>
  289. struct is_nothrow_require_concept :
  290. integral_constant<bool,
  291. asio_require_concept_fn::call_traits<
  292. require_concept_t, T, void(Property)>::is_noexcept>
  293. {
  294. };
  295. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  296. template <typename T, typename Property>
  297. constexpr bool is_nothrow_require_concept_v
  298. = is_nothrow_require_concept<T, Property>::value;
  299. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  300. template <typename T, typename Property>
  301. struct require_concept_result
  302. {
  303. typedef typename asio_require_concept_fn::call_traits<
  304. require_concept_t, T, void(Property)>::result_type type;
  305. };
  306. } // namespace asio
  307. #endif // defined(GENERATING_DOCUMENTATION)
  308. #include "asio/detail/pop_options.hpp"
  309. #endif // ASIO_REQUIRE_CONCEPT_HPP