consign.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // consign.hpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_CONSIGN_HPP
  11. #define BOOST_ASIO_CONSIGN_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 <tuple>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. /// Completion token type used to specify that the completion handler should
  22. /// carry additional values along with it.
  23. /**
  24. * This completion token adapter is typically used to keep at least one copy of
  25. * an object, such as a smart pointer, alive until the completion handler is
  26. * called.
  27. */
  28. template <typename CompletionToken, typename... Values>
  29. class consign_t
  30. {
  31. public:
  32. /// Constructor.
  33. template <typename T, typename... V>
  34. constexpr explicit consign_t(T&& completion_token, V&&... values)
  35. : token_(static_cast<T&&>(completion_token)),
  36. values_(static_cast<V&&>(values)...)
  37. {
  38. }
  39. #if defined(GENERATING_DOCUMENTATION)
  40. private:
  41. #endif // defined(GENERATING_DOCUMENTATION)
  42. CompletionToken token_;
  43. std::tuple<Values...> values_;
  44. };
  45. /// Completion token adapter used to specify that the completion handler should
  46. /// carry additional values along with it.
  47. /**
  48. * This completion token adapter is typically used to keep at least one copy of
  49. * an object, such as a smart pointer, alive until the completion handler is
  50. * called.
  51. */
  52. template <typename CompletionToken, typename... Values>
  53. BOOST_ASIO_NODISCARD inline constexpr
  54. consign_t<decay_t<CompletionToken>, decay_t<Values>...>
  55. consign(CompletionToken&& completion_token, Values&&... values)
  56. {
  57. return consign_t<decay_t<CompletionToken>, decay_t<Values>...>(
  58. static_cast<CompletionToken&&>(completion_token),
  59. static_cast<Values&&>(values)...);
  60. }
  61. } // namespace asio
  62. } // namespace boost
  63. #include <boost/asio/detail/pop_options.hpp>
  64. #include <boost/asio/impl/consign.hpp>
  65. #endif // BOOST_ASIO_CONSIGN_HPP