require_concept.hpp 10 KB

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