disposition.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // disposition.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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_DISPOSITION_HPP
  11. #define BOOST_ASIO_DISPOSITION_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/throw_exception.hpp>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/system/error_code.hpp>
  19. #include <boost/system/system_error.hpp>
  20. #include <exception>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /// Traits type to adapt arbitrary error types as dispositions.
  25. /**
  26. * This type may be specialised for user-defined types, to allow them to be
  27. * treated as a disposition by asio.
  28. *
  29. * The primary trait is not defined.
  30. */
  31. #if defined(GENERATING_DOCUMENTATION)
  32. template <typename T>
  33. struct disposition_traits
  34. {
  35. /// Determine whether a disposition represents no error.
  36. static bool not_an_error(const T& d) noexcept;
  37. /// Throw an exception if the disposition represents an error.
  38. static void throw_exception(T d);
  39. /// Convert a disposition into an @c exception_ptr.
  40. static std::exception_ptr to_exception_ptr(T d) noexcept;
  41. };
  42. #else // defined(GENERATING_DOCUMENTATION)
  43. template <typename T>
  44. struct disposition_traits;
  45. #endif // defined(GENERATING_DOCUMENTATION)
  46. namespace detail {
  47. template <typename T, typename = void, typename = void,
  48. typename = void, typename = void, typename = void, typename = void>
  49. struct is_disposition_impl : false_type
  50. {
  51. };
  52. template <typename T>
  53. struct is_disposition_impl<T,
  54. enable_if_t<
  55. is_nothrow_default_constructible<T>::value
  56. >,
  57. enable_if_t<
  58. is_nothrow_move_constructible<T>::value
  59. >,
  60. enable_if_t<
  61. is_nothrow_move_assignable<T>::value
  62. >,
  63. enable_if_t<
  64. is_same<
  65. decltype(disposition_traits<T>::not_an_error(declval<const T&>())),
  66. bool
  67. >::value
  68. >,
  69. void_t<
  70. decltype(disposition_traits<T>::throw_exception(declval<T>()))
  71. >,
  72. enable_if_t<
  73. is_same<
  74. decltype(disposition_traits<T>::to_exception_ptr(declval<T>())),
  75. std::exception_ptr
  76. >::value
  77. >> : true_type
  78. {
  79. };
  80. } // namespace detail
  81. /// Trait used for testing whether a type satisfies the requirements of a
  82. /// disposition.
  83. /**
  84. * To be a valid disposition, a type must be nothrow default-constructible,
  85. * nothrow move-constructible, nothrow move-assignable, and there must be a
  86. * specialisation of the disposition_traits template for the type that provides
  87. * the following static member functions:
  88. * @li @c not_an_error: Takes an argument of type <tt>const T&</tt> and returns
  89. * a @c bool.
  90. * @li @c throw_exception: Takes an argument of type <tt>T</tt>. The
  91. * caller of this function must not pass a disposition value for which
  92. * @c not_an_error returns true. This function must not return.
  93. * @li @c to_exception_ptr: Takes an argument of type <tt>T</tt> and returns a
  94. * value of type @c std::exception_ptr.
  95. */
  96. template <typename T>
  97. struct is_disposition :
  98. #if defined(GENERATING_DOCUMENTATION)
  99. integral_constant<bool, automatically_determined>
  100. #else // defined(GENERATING_DOCUMENTATION)
  101. detail::is_disposition_impl<T>
  102. #endif // defined(GENERATING_DOCUMENTATION)
  103. {
  104. };
  105. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  106. template <typename T>
  107. constexpr const bool is_disposition_v = is_disposition<T>::value;
  108. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  109. #if defined(BOOST_ASIO_HAS_CONCEPTS)
  110. template <typename T>
  111. BOOST_ASIO_CONCEPT disposition = is_disposition<T>::value;
  112. #define BOOST_ASIO_DISPOSITION ::boost::asio::disposition
  113. #else // defined(BOOST_ASIO_HAS_CONCEPTS)
  114. #define BOOST_ASIO_DISPOSITION typename
  115. #endif // defined(BOOST_ASIO_HAS_CONCEPTS)
  116. /// Specialisation of @c disposition_traits for @c error_code.
  117. template <>
  118. struct disposition_traits<boost::system::error_code>
  119. {
  120. static bool not_an_error(const boost::system::error_code& ec) noexcept
  121. {
  122. return !ec;
  123. }
  124. static void throw_exception(const boost::system::error_code& ec)
  125. {
  126. detail::throw_exception(boost::system::system_error(ec));
  127. }
  128. static std::exception_ptr to_exception_ptr(
  129. const boost::system::error_code& ec) noexcept
  130. {
  131. return ec
  132. ? std::make_exception_ptr(boost::system::system_error(ec))
  133. : nullptr;
  134. }
  135. };
  136. /// Specialisation of @c disposition_traits for @c std::exception_ptr.
  137. template <>
  138. struct disposition_traits<std::exception_ptr>
  139. {
  140. static bool not_an_error(const std::exception_ptr& e) noexcept
  141. {
  142. return !e;
  143. }
  144. static void throw_exception(std::exception_ptr e)
  145. {
  146. std::rethrow_exception(static_cast<std::exception_ptr&&>(e));
  147. }
  148. static std::exception_ptr to_exception_ptr(std::exception_ptr e) noexcept
  149. {
  150. return e;
  151. }
  152. };
  153. /// A tag type used to indicate the absence of an error.
  154. struct no_error_t
  155. {
  156. /// Default constructor.
  157. constexpr no_error_t()
  158. {
  159. }
  160. /// Equality operator.
  161. friend constexpr bool operator==(
  162. const no_error_t&, const no_error_t&) noexcept
  163. {
  164. return true;
  165. }
  166. /// Inequality operator.
  167. friend constexpr bool operator!=(
  168. const no_error_t&, const no_error_t&) noexcept
  169. {
  170. return false;
  171. }
  172. /// Equality operator, returns true if the disposition does not contain an
  173. /// error.
  174. template <BOOST_ASIO_DISPOSITION Disposition>
  175. friend constexpr constraint_t<is_disposition<Disposition>::value, bool>
  176. operator==(const no_error_t&, const Disposition& d) noexcept
  177. {
  178. return disposition_traits<Disposition>::not_an_error(d);
  179. }
  180. /// Equality operator, returns true if the disposition does not contain an
  181. /// error.
  182. template <BOOST_ASIO_DISPOSITION Disposition>
  183. friend constexpr constraint_t<is_disposition<Disposition>::value, bool>
  184. operator==(const Disposition& d, const no_error_t&) noexcept
  185. {
  186. return disposition_traits<Disposition>::not_an_error(d);
  187. }
  188. /// Inequality operator, returns true if the disposition contains an error.
  189. template <BOOST_ASIO_DISPOSITION Disposition>
  190. friend constexpr constraint_t<is_disposition<Disposition>::value, bool>
  191. operator!=(const no_error_t&, const Disposition& d) noexcept
  192. {
  193. return !disposition_traits<Disposition>::not_an_error(d);
  194. }
  195. /// Inequality operator, returns true if the disposition contains an error.
  196. template <BOOST_ASIO_DISPOSITION Disposition>
  197. friend constexpr constraint_t<is_disposition<Disposition>::value, bool>
  198. operator!=(const Disposition& d, const no_error_t&) noexcept
  199. {
  200. return !disposition_traits<Disposition>::not_an_error(d);
  201. }
  202. };
  203. /// A special value used to indicate the absence of an error.
  204. BOOST_ASIO_INLINE_VARIABLE constexpr no_error_t no_error;
  205. /// Specialisation of @c disposition_traits for @c no_error_t.
  206. template <>
  207. struct disposition_traits<no_error_t>
  208. {
  209. static bool not_an_error(no_error_t) noexcept
  210. {
  211. return true;
  212. }
  213. static void throw_exception(no_error_t)
  214. {
  215. }
  216. static std::exception_ptr to_exception_ptr(no_error_t) noexcept
  217. {
  218. return std::exception_ptr();
  219. }
  220. };
  221. /// Helper function to throw an exception arising from a disposition.
  222. template <typename Disposition>
  223. inline void throw_exception(Disposition&& d,
  224. constraint_t<is_disposition<decay_t<Disposition>>::value> = 0)
  225. {
  226. disposition_traits<decay_t<Disposition>>::throw_exception(
  227. static_cast<Disposition&&>(d));
  228. }
  229. /// Helper function to convert a disposition to an @c exception_ptr.
  230. template <typename Disposition>
  231. inline std::exception_ptr to_exception_ptr(Disposition&& d,
  232. constraint_t<is_disposition<decay_t<Disposition>>::value> = 0) noexcept
  233. {
  234. return disposition_traits<decay_t<Disposition>>::to_exception_ptr(
  235. static_cast<Disposition&&>(d));
  236. }
  237. } // namespace asio
  238. } // namespace boost
  239. #include <boost/asio/detail/pop_options.hpp>
  240. #endif // BOOST_ASIO_DISPOSITION_HPP