Realistic 3D camera system
3D camera system components
winrt_async_manager.hpp
Go to the documentation of this file.
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 
11 #ifndef ASIO_DETAIL_WINRT_ASYNC_MANAGER_HPP
12 #define ASIO_DETAIL_WINRT_ASYNC_MANAGER_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 
20 #if defined(ASIO_WINDOWS_RUNTIME)
21 
22 #include <future>
25 #include "asio/error.hpp"
26 #include "asio/io_service.hpp"
27 
29 
30 namespace asio {
31 namespace detail {
32 
33 class winrt_async_manager
34  : public asio::detail::service_base<winrt_async_manager>
35 {
36 public:
37  // Constructor.
38  winrt_async_manager(asio::io_service& io_service)
39  : asio::detail::service_base<winrt_async_manager>(io_service),
40  io_service_(use_service<io_service_impl>(io_service)),
41  outstanding_ops_(1)
42  {
43  }
44 
45  // Destructor.
46  ~winrt_async_manager()
47  {
48  }
49 
50  // Destroy all user-defined handler objects owned by the service.
51  void shutdown_service()
52  {
53  if (--outstanding_ops_ > 0)
54  {
55  // Block until last operation is complete.
56  std::future<void> f = promise_.get_future();
57  f.wait();
58  }
59  }
60 
61  void sync(Windows::Foundation::IAsyncAction^ action,
62  asio::error_code& ec)
63  {
64  using namespace Windows::Foundation;
65  using Windows::Foundation::AsyncStatus;
66 
67  auto promise = std::make_shared<std::promise<asio::error_code>>();
68  auto future = promise->get_future();
69 
70  action->Completed = ref new AsyncActionCompletedHandler(
71  [promise](IAsyncAction^ action, AsyncStatus status)
72  {
73  switch (status)
74  {
75  case AsyncStatus::Canceled:
76  promise->set_value(asio::error::operation_aborted);
77  break;
78  case AsyncStatus::Error:
79  case AsyncStatus::Completed:
80  default:
82  action->ErrorCode.Value,
84  promise->set_value(ec);
85  break;
86  }
87  });
88 
89  ec = future.get();
90  }
91 
92  template <typename TResult>
93  TResult sync(Windows::Foundation::IAsyncOperation<TResult>^ operation,
94  asio::error_code& ec)
95  {
96  using namespace Windows::Foundation;
97  using Windows::Foundation::AsyncStatus;
98 
99  auto promise = std::make_shared<std::promise<asio::error_code>>();
100  auto future = promise->get_future();
101 
102  operation->Completed = ref new AsyncOperationCompletedHandler<TResult>(
103  [promise](IAsyncOperation<TResult>^ operation, AsyncStatus status)
104  {
105  switch (status)
106  {
107  case AsyncStatus::Canceled:
108  promise->set_value(asio::error::operation_aborted);
109  break;
110  case AsyncStatus::Error:
111  case AsyncStatus::Completed:
112  default:
113  asio::error_code ec(
114  operation->ErrorCode.Value,
116  promise->set_value(ec);
117  break;
118  }
119  });
120 
121  ec = future.get();
122  return operation->GetResults();
123  }
124 
125  template <typename TResult, typename TProgress>
126  TResult sync(
127  Windows::Foundation::IAsyncOperationWithProgress<
128  TResult, TProgress>^ operation,
129  asio::error_code& ec)
130  {
131  using namespace Windows::Foundation;
132  using Windows::Foundation::AsyncStatus;
133 
134  auto promise = std::make_shared<std::promise<asio::error_code>>();
135  auto future = promise->get_future();
136 
137  operation->Completed
138  = ref new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>(
139  [promise](IAsyncOperationWithProgress<TResult, TProgress>^ operation,
140  AsyncStatus status)
141  {
142  switch (status)
143  {
144  case AsyncStatus::Canceled:
145  promise->set_value(asio::error::operation_aborted);
146  break;
147  case AsyncStatus::Started:
148  break;
149  case AsyncStatus::Error:
150  case AsyncStatus::Completed:
151  default:
152  asio::error_code ec(
153  operation->ErrorCode.Value,
155  promise->set_value(ec);
156  break;
157  }
158  });
159 
160  ec = future.get();
161  return operation->GetResults();
162  }
163 
164  void async(Windows::Foundation::IAsyncAction^ action,
165  winrt_async_op<void>* handler)
166  {
167  using namespace Windows::Foundation;
168  using Windows::Foundation::AsyncStatus;
169 
170  auto on_completed = ref new AsyncActionCompletedHandler(
171  [this, handler](IAsyncAction^ action, AsyncStatus status)
172  {
173  switch (status)
174  {
175  case AsyncStatus::Canceled:
176  handler->ec_ = asio::error::operation_aborted;
177  break;
178  case AsyncStatus::Started:
179  return;
180  case AsyncStatus::Completed:
181  case AsyncStatus::Error:
182  default:
183  handler->ec_ = asio::error_code(
184  action->ErrorCode.Value,
186  break;
187  }
188  io_service_.post_deferred_completion(handler);
189  if (--outstanding_ops_ == 0)
190  promise_.set_value();
191  });
192 
193  io_service_.work_started();
194  ++outstanding_ops_;
195  action->Completed = on_completed;
196  }
197 
198  template <typename TResult>
199  void async(Windows::Foundation::IAsyncOperation<TResult>^ operation,
200  winrt_async_op<TResult>* handler)
201  {
202  using namespace Windows::Foundation;
203  using Windows::Foundation::AsyncStatus;
204 
205  auto on_completed = ref new AsyncOperationCompletedHandler<TResult>(
206  [this, handler](IAsyncOperation<TResult>^ operation, AsyncStatus status)
207  {
208  switch (status)
209  {
210  case AsyncStatus::Canceled:
211  handler->ec_ = asio::error::operation_aborted;
212  break;
213  case AsyncStatus::Started:
214  return;
215  case AsyncStatus::Completed:
216  handler->result_ = operation->GetResults();
217  // Fall through.
218  case AsyncStatus::Error:
219  default:
220  handler->ec_ = asio::error_code(
221  operation->ErrorCode.Value,
223  break;
224  }
225  io_service_.post_deferred_completion(handler);
226  if (--outstanding_ops_ == 0)
227  promise_.set_value();
228  });
229 
230  io_service_.work_started();
231  ++outstanding_ops_;
232  operation->Completed = on_completed;
233  }
234 
235  template <typename TResult, typename TProgress>
236  void async(
237  Windows::Foundation::IAsyncOperationWithProgress<
238  TResult, TProgress>^ operation,
239  winrt_async_op<TResult>* handler)
240  {
241  using namespace Windows::Foundation;
242  using Windows::Foundation::AsyncStatus;
243 
244  auto on_completed
245  = ref new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>(
246  [this, handler](IAsyncOperationWithProgress<
247  TResult, TProgress>^ operation, AsyncStatus status)
248  {
249  switch (status)
250  {
251  case AsyncStatus::Canceled:
252  handler->ec_ = asio::error::operation_aborted;
253  break;
254  case AsyncStatus::Started:
255  return;
256  case AsyncStatus::Completed:
257  handler->result_ = operation->GetResults();
258  // Fall through.
259  case AsyncStatus::Error:
260  default:
261  handler->ec_ = asio::error_code(
262  operation->ErrorCode.Value,
264  break;
265  }
266  io_service_.post_deferred_completion(handler);
267  if (--outstanding_ops_ == 0)
268  promise_.set_value();
269  });
270 
271  io_service_.work_started();
272  ++outstanding_ops_;
273  operation->Completed = on_completed;
274  }
275 
276 private:
277  // The io_service implementation used to post completed handlers.
278  io_service_impl& io_service_;
279 
280  // Count of outstanding operations.
281  atomic_count outstanding_ops_;
282 
283  // Used to keep wait for outstanding operations to complete.
284  std::promise<void> promise_;
285 };
286 
287 } // namespace detail
288 } // namespace asio
289 
291 
292 #endif // defined(ASIO_WINDOWS_RUNTIME)
293 
294 #endif // ASIO_DETAIL_WINRT_ASYNC_MANAGER_HPP
Operation cancelled.
Definition: error.hpp:161
Provides core I/O functionality.
Definition: io_service.hpp:184
Service & use_service(io_service &ios)
Definition: io_service.hpp:26
class task_io_service io_service_impl
Definition: io_service.hpp:48
Class to represent an error code value.
Definition: error_code.hpp:80
task_io_service_operation operation
Definition: operation.hpp:32
ASIO_DECL const error_category & system_category()
Returns the error category used for the system errors produced by asio.
Definition: error_code.ipp:118