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.

173 lines
4.3KB

  1. //
  2. // impl/use_future.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_IMPL_USE_FUTURE_HPP
  11. #define ASIO_IMPL_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 <future>
  17. #include "asio/async_result.hpp"
  18. #include "asio/error_code.hpp"
  19. #include "asio/handler_type.hpp"
  20. #include "asio/system_error.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. // Completion handler to adapt a promise as a completion handler.
  25. template <typename T>
  26. class promise_handler
  27. {
  28. public:
  29. // Construct from use_future special value.
  30. template <typename Allocator>
  31. promise_handler(use_future_t<Allocator> uf)
  32. : promise_(std::allocate_shared<std::promise<T> >(
  33. uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
  34. {
  35. }
  36. void operator()(T t)
  37. {
  38. promise_->set_value(t);
  39. }
  40. void operator()(const asio::error_code& ec, T t)
  41. {
  42. if (ec)
  43. promise_->set_exception(
  44. std::make_exception_ptr(
  45. asio::system_error(ec)));
  46. else
  47. promise_->set_value(t);
  48. }
  49. //private:
  50. std::shared_ptr<std::promise<T> > promise_;
  51. };
  52. // Completion handler to adapt a void promise as a completion handler.
  53. template <>
  54. class promise_handler<void>
  55. {
  56. public:
  57. // Construct from use_future special value. Used during rebinding.
  58. template <typename Allocator>
  59. promise_handler(use_future_t<Allocator> uf)
  60. : promise_(std::allocate_shared<std::promise<void> >(
  61. uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
  62. {
  63. }
  64. void operator()()
  65. {
  66. promise_->set_value();
  67. }
  68. void operator()(const asio::error_code& ec)
  69. {
  70. if (ec)
  71. promise_->set_exception(
  72. std::make_exception_ptr(
  73. asio::system_error(ec)));
  74. else
  75. promise_->set_value();
  76. }
  77. //private:
  78. std::shared_ptr<std::promise<void> > promise_;
  79. };
  80. // Ensure any exceptions thrown from the handler are propagated back to the
  81. // caller via the future.
  82. template <typename Function, typename T>
  83. void asio_handler_invoke(Function f, promise_handler<T>* h)
  84. {
  85. std::shared_ptr<std::promise<T> > p(h->promise_);
  86. try
  87. {
  88. f();
  89. }
  90. catch (...)
  91. {
  92. p->set_exception(std::current_exception());
  93. }
  94. }
  95. } // namespace detail
  96. #if !defined(GENERATING_DOCUMENTATION)
  97. // Handler traits specialisation for promise_handler.
  98. template <typename T>
  99. class async_result<detail::promise_handler<T> >
  100. {
  101. public:
  102. // The initiating function will return a future.
  103. typedef std::future<T> type;
  104. // Constructor creates a new promise for the async operation, and obtains the
  105. // corresponding future.
  106. explicit async_result(detail::promise_handler<T>& h)
  107. {
  108. value_ = h.promise_->get_future();
  109. }
  110. // Obtain the future to be returned from the initiating function.
  111. type get() { return std::move(value_); }
  112. private:
  113. type value_;
  114. };
  115. // Handler type specialisation for use_future.
  116. template <typename Allocator, typename ReturnType>
  117. struct handler_type<use_future_t<Allocator>, ReturnType()>
  118. {
  119. typedef detail::promise_handler<void> type;
  120. };
  121. // Handler type specialisation for use_future.
  122. template <typename Allocator, typename ReturnType, typename Arg1>
  123. struct handler_type<use_future_t<Allocator>, ReturnType(Arg1)>
  124. {
  125. typedef detail::promise_handler<Arg1> type;
  126. };
  127. // Handler type specialisation for use_future.
  128. template <typename Allocator, typename ReturnType>
  129. struct handler_type<use_future_t<Allocator>,
  130. ReturnType(asio::error_code)>
  131. {
  132. typedef detail::promise_handler<void> type;
  133. };
  134. // Handler type specialisation for use_future.
  135. template <typename Allocator, typename ReturnType, typename Arg2>
  136. struct handler_type<use_future_t<Allocator>,
  137. ReturnType(asio::error_code, Arg2)>
  138. {
  139. typedef detail::promise_handler<Arg2> type;
  140. };
  141. #endif // !defined(GENERATING_DOCUMENTATION)
  142. } // namespace asio
  143. #include "asio/detail/pop_options.hpp"
  144. #endif // ASIO_IMPL_USE_FUTURE_HPP