consign.hpp 2.1 KB

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