redirect_error.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // redirect_error.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_REDIRECT_ERROR_HPP
  11. #define ASIO_REDIRECT_ERROR_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/error_code.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. /// A @ref completion_token adapter used to specify that an error produced by an
  21. /// asynchronous operation is captured to an error_code variable.
  22. /**
  23. * The redirect_error_t class is used to indicate that any error_code produced
  24. * by an asynchronous operation is captured to a specified variable.
  25. */
  26. template <typename CompletionToken>
  27. class redirect_error_t
  28. {
  29. public:
  30. /// Constructor.
  31. template <typename T>
  32. redirect_error_t(T&& completion_token, asio::error_code& ec)
  33. : token_(static_cast<T&&>(completion_token)),
  34. ec_(ec)
  35. {
  36. }
  37. //private:
  38. CompletionToken token_;
  39. asio::error_code& ec_;
  40. };
  41. /// Adapt a @ref completion_token to capture error_code values to a variable.
  42. template <typename CompletionToken>
  43. inline redirect_error_t<decay_t<CompletionToken>> redirect_error(
  44. CompletionToken&& completion_token, asio::error_code& ec)
  45. {
  46. return redirect_error_t<decay_t<CompletionToken>>(
  47. static_cast<CompletionToken&&>(completion_token), ec);
  48. }
  49. } // namespace asio
  50. #include "asio/detail/pop_options.hpp"
  51. #include "asio/impl/redirect_error.hpp"
  52. #endif // ASIO_REDIRECT_ERROR_HPP