Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use_future.hpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // use_future.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_USE_FUTURE_HPP
  11. #define ASIO_USE_FUTURE_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/future.hpp"
  17. #if defined(ASIO_HAS_STD_FUTURE_CLASS) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <memory>
  20. #include "asio/detail/type_traits.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. template <typename Function, typename Allocator>
  25. class packaged_token;
  26. template <typename Function, typename Allocator, typename Result>
  27. class packaged_handler;
  28. } // namespace detail
  29. /// Class used to specify that an asynchronous operation should return a future.
  30. /**
  31. * The use_future_t class is used to indicate that an asynchronous operation
  32. * should return a std::future object. A use_future_t object may be passed as a
  33. * handler to an asynchronous operation, typically using the special value @c
  34. * asio::use_future. For example:
  35. *
  36. * @code std::future<std::size_t> my_future
  37. * = my_socket.async_read_some(my_buffer, asio::use_future); @endcode
  38. *
  39. * The initiating function (async_read_some in the above example) returns a
  40. * future that will receive the result of the operation. If the operation
  41. * completes with an error_code indicating failure, it is converted into a
  42. * system_error and passed back to the caller via the future.
  43. */
  44. template <typename Allocator = std::allocator<void> >
  45. class use_future_t
  46. {
  47. public:
  48. /// The allocator type. The allocator is used when constructing the
  49. /// @c std::promise object for a given asynchronous operation.
  50. typedef Allocator allocator_type;
  51. /// Construct using default-constructed allocator.
  52. ASIO_CONSTEXPR use_future_t()
  53. {
  54. }
  55. /// Construct using specified allocator.
  56. explicit use_future_t(const Allocator& allocator)
  57. : allocator_(allocator)
  58. {
  59. }
  60. #if !defined(ASIO_NO_DEPRECATED)
  61. /// (Deprecated: Use rebind().) Specify an alternate allocator.
  62. template <typename OtherAllocator>
  63. use_future_t<OtherAllocator> operator[](const OtherAllocator& allocator) const
  64. {
  65. return use_future_t<OtherAllocator>(allocator);
  66. }
  67. #endif // !defined(ASIO_NO_DEPRECATED)
  68. /// Specify an alternate allocator.
  69. template <typename OtherAllocator>
  70. use_future_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
  71. {
  72. return use_future_t<OtherAllocator>(allocator);
  73. }
  74. /// Obtain allocator.
  75. allocator_type get_allocator() const
  76. {
  77. return allocator_;
  78. }
  79. /// Wrap a function object in a packaged task.
  80. /**
  81. * The @c package function is used to adapt a function object as a packaged
  82. * task. When this adapter is passed as a completion token to an asynchronous
  83. * operation, the result of the function object is retuned via a std::future.
  84. *
  85. * @par Example
  86. *
  87. * @code std::future<std::size_t> fut =
  88. * my_socket.async_read_some(buffer,
  89. * use_future([](asio::error_code ec, std::size_t n)
  90. * {
  91. * return ec ? 0 : n;
  92. * }));
  93. * ...
  94. * std::size_t n = fut.get(); @endcode
  95. */
  96. template <typename Function>
  97. #if defined(GENERATING_DOCUMENTATION)
  98. unspecified
  99. #else // defined(GENERATING_DOCUMENTATION)
  100. detail::packaged_token<typename decay<Function>::type, Allocator>
  101. #endif // defined(GENERATING_DOCUMENTATION)
  102. operator()(ASIO_MOVE_ARG(Function) f) const;
  103. private:
  104. // Helper type to ensure that use_future can be constexpr default-constructed
  105. // even when std::allocator<void> can't be.
  106. struct std_allocator_void
  107. {
  108. ASIO_CONSTEXPR std_allocator_void()
  109. {
  110. }
  111. operator std::allocator<void>() const
  112. {
  113. return std::allocator<void>();
  114. }
  115. };
  116. typename conditional<
  117. is_same<std::allocator<void>, Allocator>::value,
  118. std_allocator_void, Allocator>::type allocator_;
  119. };
  120. /// A special value, similar to std::nothrow.
  121. /**
  122. * See the documentation for asio::use_future_t for a usage example.
  123. */
  124. #if defined(ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  125. constexpr use_future_t<> use_future;
  126. #elif defined(ASIO_MSVC)
  127. __declspec(selectany) use_future_t<> use_future;
  128. #endif
  129. } // namespace asio
  130. #include "asio/detail/pop_options.hpp"
  131. #include "asio/impl/use_future.hpp"
  132. #endif // defined(ASIO_HAS_STD_FUTURE_CLASS)
  133. // || defined(GENERATING_DOCUMENTATION)
  134. #endif // ASIO_USE_FUTURE_HPP