use_promise.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // experimental/use_promise.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2021-2023 Klemens D. Morgenstern
  6. // (klemens dot morgenstern at gmx dot net)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_EXPERIMENTAL_USE_PROMISE_HPP
  12. #define BOOST_ASIO_EXPERIMENTAL_USE_PROMISE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #include <memory>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace experimental {
  23. template <typename Allocator = std::allocator<void>>
  24. struct use_promise_t
  25. {
  26. /// The allocator type. The allocator is used when constructing the
  27. /// @c promise object for a given asynchronous operation.
  28. typedef Allocator allocator_type;
  29. /// Construct using default-constructed allocator.
  30. constexpr use_promise_t()
  31. {
  32. }
  33. /// Construct using specified allocator.
  34. explicit use_promise_t(const Allocator& allocator)
  35. : allocator_(allocator)
  36. {
  37. }
  38. /// Obtain allocator.
  39. allocator_type get_allocator() const noexcept
  40. {
  41. return allocator_;
  42. }
  43. /// Adapts an executor to add the @c use_promise_t completion token as the
  44. /// default.
  45. template <typename InnerExecutor>
  46. struct executor_with_default : InnerExecutor
  47. {
  48. /// Specify @c use_promise_t as the default completion token type.
  49. typedef use_promise_t<Allocator> default_completion_token_type;
  50. /// Construct the adapted executor from the inner executor type.
  51. executor_with_default(const InnerExecutor& ex) noexcept
  52. : InnerExecutor(ex)
  53. {
  54. }
  55. /// Convert the specified executor to the inner executor type, then use
  56. /// that to construct the adapted executor.
  57. template <typename OtherExecutor>
  58. executor_with_default(const OtherExecutor& ex,
  59. constraint_t<
  60. is_convertible<OtherExecutor, InnerExecutor>::value
  61. > = 0) noexcept
  62. : InnerExecutor(ex)
  63. {
  64. }
  65. };
  66. /// Function helper to adapt an I/O object to use @c use_promise_t as its
  67. /// default completion token type.
  68. template <typename T>
  69. static typename decay_t<T>::template rebind_executor<
  70. executor_with_default<typename decay_t<T>::executor_type>
  71. >::other
  72. as_default_on(T&& object)
  73. {
  74. return typename decay_t<T>::template rebind_executor<
  75. executor_with_default<typename decay_t<T>::executor_type>
  76. >::other(static_cast<T&&>(object));
  77. }
  78. /// Specify an alternate allocator.
  79. template <typename OtherAllocator>
  80. use_promise_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
  81. {
  82. return use_promise_t<OtherAllocator>(allocator);
  83. }
  84. private:
  85. Allocator allocator_;
  86. };
  87. BOOST_ASIO_INLINE_VARIABLE constexpr use_promise_t<> use_promise;
  88. } // namespace experimental
  89. } // namespace asio
  90. } // namespace boost
  91. #include <boost/asio/detail/pop_options.hpp>
  92. #include <boost/asio/experimental/impl/use_promise.hpp>
  93. #endif // BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP