Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / asio / detail / winrt_async_manager.hpp @ 2486

History | View | Annotate | Download (8.5 kB)

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