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.

winrt_async_manager.hpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // detail/winrt_async_manager.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_DETAIL_WINRT_ASYNC_MANAGER_HPP
  11. #define ASIO_DETAIL_WINRT_ASYNC_MANAGER_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. #if defined(ASIO_WINDOWS_RUNTIME)
  17. #include <future>
  18. #include "asio/detail/atomic_count.hpp"
  19. #include "asio/detail/winrt_async_op.hpp"
  20. #include "asio/error.hpp"
  21. #include "asio/io_context.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. class winrt_async_manager
  26. : public asio::detail::service_base<winrt_async_manager>
  27. {
  28. public:
  29. // Constructor.
  30. winrt_async_manager(asio::io_context& io_context)
  31. : asio::detail::service_base<winrt_async_manager>(io_context),
  32. io_context_(use_service<io_context_impl>(io_context)),
  33. outstanding_ops_(1)
  34. {
  35. }
  36. // Destructor.
  37. ~winrt_async_manager()
  38. {
  39. }
  40. // Destroy all user-defined handler objects owned by the service.
  41. void shutdown()
  42. {
  43. if (--outstanding_ops_ > 0)
  44. {
  45. // Block until last operation is complete.
  46. std::future<void> f = promise_.get_future();
  47. f.wait();
  48. }
  49. }
  50. void sync(Windows::Foundation::IAsyncAction^ action,
  51. asio::error_code& ec)
  52. {
  53. using namespace Windows::Foundation;
  54. using Windows::Foundation::AsyncStatus;
  55. auto promise = std::make_shared<std::promise<asio::error_code>>();
  56. auto future = promise->get_future();
  57. action->Completed = ref new AsyncActionCompletedHandler(
  58. [promise](IAsyncAction^ action, AsyncStatus status)
  59. {
  60. switch (status)
  61. {
  62. case AsyncStatus::Canceled:
  63. promise->set_value(asio::error::operation_aborted);
  64. break;
  65. case AsyncStatus::Error:
  66. case AsyncStatus::Completed:
  67. default:
  68. asio::error_code ec(
  69. action->ErrorCode.Value,
  70. asio::system_category());
  71. promise->set_value(ec);
  72. break;
  73. }
  74. });
  75. ec = future.get();
  76. }
  77. template <typename TResult>
  78. TResult sync(Windows::Foundation::IAsyncOperation<TResult>^ operation,
  79. asio::error_code& ec)
  80. {
  81. using namespace Windows::Foundation;
  82. using Windows::Foundation::AsyncStatus;
  83. auto promise = std::make_shared<std::promise<asio::error_code>>();
  84. auto future = promise->get_future();
  85. operation->Completed = ref new AsyncOperationCompletedHandler<TResult>(
  86. [promise](IAsyncOperation<TResult>^ operation, AsyncStatus status)
  87. {
  88. switch (status)
  89. {
  90. case AsyncStatus::Canceled:
  91. promise->set_value(asio::error::operation_aborted);
  92. break;
  93. case AsyncStatus::Error:
  94. case AsyncStatus::Completed:
  95. default:
  96. asio::error_code ec(
  97. operation->ErrorCode.Value,
  98. asio::system_category());
  99. promise->set_value(ec);
  100. break;
  101. }
  102. });
  103. ec = future.get();
  104. return operation->GetResults();
  105. }
  106. template <typename TResult, typename TProgress>
  107. TResult sync(
  108. Windows::Foundation::IAsyncOperationWithProgress<
  109. TResult, TProgress>^ operation,
  110. asio::error_code& ec)
  111. {
  112. using namespace Windows::Foundation;
  113. using Windows::Foundation::AsyncStatus;
  114. auto promise = std::make_shared<std::promise<asio::error_code>>();
  115. auto future = promise->get_future();
  116. operation->Completed
  117. = ref new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>(
  118. [promise](IAsyncOperationWithProgress<TResult, TProgress>^ operation,
  119. AsyncStatus status)
  120. {
  121. switch (status)
  122. {
  123. case AsyncStatus::Canceled:
  124. promise->set_value(asio::error::operation_aborted);
  125. break;
  126. case AsyncStatus::Started:
  127. break;
  128. case AsyncStatus::Error:
  129. case AsyncStatus::Completed:
  130. default:
  131. asio::error_code ec(
  132. operation->ErrorCode.Value,
  133. asio::system_category());
  134. promise->set_value(ec);
  135. break;
  136. }
  137. });
  138. ec = future.get();
  139. return operation->GetResults();
  140. }
  141. void async(Windows::Foundation::IAsyncAction^ action,
  142. winrt_async_op<void>* handler)
  143. {
  144. using namespace Windows::Foundation;
  145. using Windows::Foundation::AsyncStatus;
  146. auto on_completed = ref new AsyncActionCompletedHandler(
  147. [this, handler](IAsyncAction^ action, AsyncStatus status)
  148. {
  149. switch (status)
  150. {
  151. case AsyncStatus::Canceled:
  152. handler->ec_ = asio::error::operation_aborted;
  153. break;
  154. case AsyncStatus::Started:
  155. return;
  156. case AsyncStatus::Completed:
  157. case AsyncStatus::Error:
  158. default:
  159. handler->ec_ = asio::error_code(
  160. action->ErrorCode.Value,
  161. asio::system_category());
  162. break;
  163. }
  164. io_context_.post_deferred_completion(handler);
  165. if (--outstanding_ops_ == 0)
  166. promise_.set_value();
  167. });
  168. io_context_.work_started();
  169. ++outstanding_ops_;
  170. action->Completed = on_completed;
  171. }
  172. template <typename TResult>
  173. void async(Windows::Foundation::IAsyncOperation<TResult>^ operation,
  174. winrt_async_op<TResult>* handler)
  175. {
  176. using namespace Windows::Foundation;
  177. using Windows::Foundation::AsyncStatus;
  178. auto on_completed = ref new AsyncOperationCompletedHandler<TResult>(
  179. [this, handler](IAsyncOperation<TResult>^ operation, AsyncStatus status)
  180. {
  181. switch (status)
  182. {
  183. case AsyncStatus::Canceled:
  184. handler->ec_ = asio::error::operation_aborted;
  185. break;
  186. case AsyncStatus::Started:
  187. return;
  188. case AsyncStatus::Completed:
  189. handler->result_ = operation->GetResults();
  190. // Fall through.
  191. case AsyncStatus::Error:
  192. default:
  193. handler->ec_ = asio::error_code(
  194. operation->ErrorCode.Value,
  195. asio::system_category());
  196. break;
  197. }
  198. io_context_.post_deferred_completion(handler);
  199. if (--outstanding_ops_ == 0)
  200. promise_.set_value();
  201. });
  202. io_context_.work_started();
  203. ++outstanding_ops_;
  204. operation->Completed = on_completed;
  205. }
  206. template <typename TResult, typename TProgress>
  207. void async(
  208. Windows::Foundation::IAsyncOperationWithProgress<
  209. TResult, TProgress>^ operation,
  210. winrt_async_op<TResult>* handler)
  211. {
  212. using namespace Windows::Foundation;
  213. using Windows::Foundation::AsyncStatus;
  214. auto on_completed
  215. = ref new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>(
  216. [this, handler](IAsyncOperationWithProgress<
  217. TResult, TProgress>^ operation, AsyncStatus status)
  218. {
  219. switch (status)
  220. {
  221. case AsyncStatus::Canceled:
  222. handler->ec_ = asio::error::operation_aborted;
  223. break;
  224. case AsyncStatus::Started:
  225. return;
  226. case AsyncStatus::Completed:
  227. handler->result_ = operation->GetResults();
  228. // Fall through.
  229. case AsyncStatus::Error:
  230. default:
  231. handler->ec_ = asio::error_code(
  232. operation->ErrorCode.Value,
  233. asio::system_category());
  234. break;
  235. }
  236. io_context_.post_deferred_completion(handler);
  237. if (--outstanding_ops_ == 0)
  238. promise_.set_value();
  239. });
  240. io_context_.work_started();
  241. ++outstanding_ops_;
  242. operation->Completed = on_completed;
  243. }
  244. private:
  245. // The io_context implementation used to post completed handlers.
  246. io_context_impl& io_context_;
  247. // Count of outstanding operations.
  248. atomic_count outstanding_ops_;
  249. // Used to keep wait for outstanding operations to complete.
  250. std::promise<void> promise_;
  251. };
  252. } // namespace detail
  253. } // namespace asio
  254. #include "asio/detail/pop_options.hpp"
  255. #endif // defined(ASIO_WINDOWS_RUNTIME)
  256. #endif // ASIO_DETAIL_WINRT_ASYNC_MANAGER_HPP