Realistic 3D camera system
3D camera system components
use_future.hpp
Go to the documentation of this file.
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 
11 #ifndef ASIO_IMPL_USE_FUTURE_HPP
12 #define ASIO_IMPL_USE_FUTURE_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 #include <future>
20 #include "asio/async_result.hpp"
21 #include "asio/error_code.hpp"
22 #include "asio/handler_type.hpp"
23 #include "asio/system_error.hpp"
24 
26 
27 namespace asio {
28 namespace detail {
29 
30  // Completion handler to adapt a promise as a completion handler.
31  template <typename T>
33  {
34  public:
35  // Construct from use_future special value.
36  template <typename Allocator>
38  : promise_(std::allocate_shared<std::promise<T> >(
39  uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
40  {
41  }
42 
43  void operator()(T t)
44  {
45  promise_->set_value(t);
46  }
47 
48  void operator()(const asio::error_code& ec, T t)
49  {
50  if (ec)
51  promise_->set_exception(
52  std::make_exception_ptr(
53  asio::system_error(ec)));
54  else
55  promise_->set_value(t);
56  }
57 
58  //private:
59  std::shared_ptr<std::promise<T> > promise_;
60  };
61 
62  // Completion handler to adapt a void promise as a completion handler.
63  template <>
64  class promise_handler<void>
65  {
66  public:
67  // Construct from use_future special value. Used during rebinding.
68  template <typename Allocator>
70  : promise_(std::allocate_shared<std::promise<void> >(
71  uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
72  {
73  }
74 
75  void operator()()
76  {
77  promise_->set_value();
78  }
79 
80  void operator()(const asio::error_code& ec)
81  {
82  if (ec)
83  promise_->set_exception(
84  std::make_exception_ptr(
85  asio::system_error(ec)));
86  else
87  promise_->set_value();
88  }
89 
90  //private:
91  std::shared_ptr<std::promise<void> > promise_;
92  };
93 
94  // Ensure any exceptions thrown from the handler are propagated back to the
95  // caller via the future.
96  template <typename Function, typename T>
98  {
99  std::shared_ptr<std::promise<T> > p(h->promise_);
100  try
101  {
102  f();
103  }
104  catch (...)
105  {
106  p->set_exception(std::current_exception());
107  }
108  }
109 
110 } // namespace detail
111 
112 #if !defined(GENERATING_DOCUMENTATION)
113 
114 // Handler traits specialisation for promise_handler.
115 template <typename T>
116 class async_result<detail::promise_handler<T> >
117 {
118 public:
119  // The initiating function will return a future.
120  typedef std::future<T> type;
121 
122  // Constructor creates a new promise for the async operation, and obtains the
123  // corresponding future.
125  {
126  value_ = h.promise_->get_future();
127  }
128 
129  // Obtain the future to be returned from the initiating function.
130  type get() { return std::move(value_); }
131 
132 private:
133  type value_;
134 };
135 
136 // Handler type specialisation for use_future.
137 template <typename Allocator, typename ReturnType>
138 struct handler_type<use_future_t<Allocator>, ReturnType()>
139 {
141 };
142 
143 // Handler type specialisation for use_future.
144 template <typename Allocator, typename ReturnType, typename Arg1>
145 struct handler_type<use_future_t<Allocator>, ReturnType(Arg1)>
146 {
148 };
149 
150 // Handler type specialisation for use_future.
151 template <typename Allocator, typename ReturnType>
152 struct handler_type<use_future_t<Allocator>,
153  ReturnType(asio::error_code)>
154 {
156 };
157 
158 // Handler type specialisation for use_future.
159 template <typename Allocator, typename ReturnType, typename Arg2>
160 struct handler_type<use_future_t<Allocator>,
161  ReturnType(asio::error_code, Arg2)>
162 {
164 };
165 
166 #endif // !defined(GENERATING_DOCUMENTATION)
167 
168 } // namespace asio
169 
171 
172 #endif // ASIO_IMPL_USE_FUTURE_HPP
An interface for customising the behaviour of an initiating function.
Class used to specify that an asynchronous operation should return a future.
Definition: use_future.hpp:41
STL namespace.
promise_handler(use_future_t< Allocator > uf)
Definition: use_future.hpp:69
void operator()(const asio::error_code &ec)
Definition: use_future.hpp:80
std::shared_ptr< std::promise< void > > promise_
Definition: use_future.hpp:91
std::shared_ptr< std::promise< T > > promise_
Definition: use_future.hpp:59
async_result(detail::promise_handler< T > &h)
Definition: use_future.hpp:124
void asio_handler_invoke(Function &function, binder1< Handler, Arg1 > *this_handler)
Class to represent an error code value.
Definition: error_code.hpp:80
promise_handler(use_future_t< Allocator > uf)
Definition: use_future.hpp:37
void operator()(const asio::error_code &ec, T t)
Definition: use_future.hpp:48
Default handler type traits provided for all handlers.