annotate DEPENDENCIES/generic/include/boost/asio/impl/use_future.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //
Chris@16 2 // impl/use_future.hpp
Chris@16 3 // ~~~~~~~~~~~~~~~~~~~
Chris@16 4 //
Chris@101 5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
Chris@16 6 //
Chris@16 7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
Chris@16 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 9 //
Chris@16 10
Chris@16 11 #ifndef BOOST_ASIO_IMPL_USE_FUTURE_HPP
Chris@16 12 #define BOOST_ASIO_IMPL_USE_FUTURE_HPP
Chris@16 13
Chris@16 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 15 # pragma once
Chris@16 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 17
Chris@16 18 #include <boost/asio/detail/config.hpp>
Chris@16 19 #include <future>
Chris@16 20 #include <boost/asio/async_result.hpp>
Chris@16 21 #include <boost/system/error_code.hpp>
Chris@16 22 #include <boost/asio/handler_type.hpp>
Chris@16 23 #include <boost/system/system_error.hpp>
Chris@16 24
Chris@16 25 #include <boost/asio/detail/push_options.hpp>
Chris@16 26
Chris@16 27 namespace boost {
Chris@16 28 namespace asio {
Chris@16 29 namespace detail {
Chris@16 30
Chris@16 31 // Completion handler to adapt a promise as a completion handler.
Chris@16 32 template <typename T>
Chris@16 33 class promise_handler
Chris@16 34 {
Chris@16 35 public:
Chris@16 36 // Construct from use_future special value.
Chris@16 37 template <typename Allocator>
Chris@16 38 promise_handler(use_future_t<Allocator> uf)
Chris@16 39 : promise_(std::allocate_shared<std::promise<T> >(
Chris@16 40 uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
Chris@16 41 {
Chris@16 42 }
Chris@16 43
Chris@16 44 void operator()(T t)
Chris@16 45 {
Chris@16 46 promise_->set_value(t);
Chris@16 47 }
Chris@16 48
Chris@16 49 void operator()(const boost::system::error_code& ec, T t)
Chris@16 50 {
Chris@16 51 if (ec)
Chris@16 52 promise_->set_exception(
Chris@16 53 std::make_exception_ptr(
Chris@16 54 boost::system::system_error(ec)));
Chris@16 55 else
Chris@16 56 promise_->set_value(t);
Chris@16 57 }
Chris@16 58
Chris@16 59 //private:
Chris@16 60 std::shared_ptr<std::promise<T> > promise_;
Chris@16 61 };
Chris@16 62
Chris@16 63 // Completion handler to adapt a void promise as a completion handler.
Chris@16 64 template <>
Chris@16 65 class promise_handler<void>
Chris@16 66 {
Chris@16 67 public:
Chris@16 68 // Construct from use_future special value. Used during rebinding.
Chris@16 69 template <typename Allocator>
Chris@16 70 promise_handler(use_future_t<Allocator> uf)
Chris@16 71 : promise_(std::allocate_shared<std::promise<void> >(
Chris@16 72 uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
Chris@16 73 {
Chris@16 74 }
Chris@16 75
Chris@16 76 void operator()()
Chris@16 77 {
Chris@16 78 promise_->set_value();
Chris@16 79 }
Chris@16 80
Chris@16 81 void operator()(const boost::system::error_code& ec)
Chris@16 82 {
Chris@16 83 if (ec)
Chris@16 84 promise_->set_exception(
Chris@16 85 std::make_exception_ptr(
Chris@16 86 boost::system::system_error(ec)));
Chris@16 87 else
Chris@16 88 promise_->set_value();
Chris@16 89 }
Chris@16 90
Chris@16 91 //private:
Chris@16 92 std::shared_ptr<std::promise<void> > promise_;
Chris@16 93 };
Chris@16 94
Chris@16 95 // Ensure any exceptions thrown from the handler are propagated back to the
Chris@16 96 // caller via the future.
Chris@16 97 template <typename Function, typename T>
Chris@16 98 void asio_handler_invoke(Function f, promise_handler<T>* h)
Chris@16 99 {
Chris@16 100 std::shared_ptr<std::promise<T> > p(h->promise_);
Chris@16 101 try
Chris@16 102 {
Chris@16 103 f();
Chris@16 104 }
Chris@16 105 catch (...)
Chris@16 106 {
Chris@16 107 p->set_exception(std::current_exception());
Chris@16 108 }
Chris@16 109 }
Chris@16 110
Chris@16 111 } // namespace detail
Chris@16 112
Chris@16 113 #if !defined(GENERATING_DOCUMENTATION)
Chris@16 114
Chris@16 115 // Handler traits specialisation for promise_handler.
Chris@16 116 template <typename T>
Chris@16 117 class async_result<detail::promise_handler<T> >
Chris@16 118 {
Chris@16 119 public:
Chris@16 120 // The initiating function will return a future.
Chris@16 121 typedef std::future<T> type;
Chris@16 122
Chris@16 123 // Constructor creates a new promise for the async operation, and obtains the
Chris@16 124 // corresponding future.
Chris@16 125 explicit async_result(detail::promise_handler<T>& h)
Chris@16 126 {
Chris@16 127 value_ = h.promise_->get_future();
Chris@16 128 }
Chris@16 129
Chris@16 130 // Obtain the future to be returned from the initiating function.
Chris@16 131 type get() { return std::move(value_); }
Chris@16 132
Chris@16 133 private:
Chris@16 134 type value_;
Chris@16 135 };
Chris@16 136
Chris@16 137 // Handler type specialisation for use_future.
Chris@16 138 template <typename Allocator, typename ReturnType>
Chris@16 139 struct handler_type<use_future_t<Allocator>, ReturnType()>
Chris@16 140 {
Chris@16 141 typedef detail::promise_handler<void> type;
Chris@16 142 };
Chris@16 143
Chris@16 144 // Handler type specialisation for use_future.
Chris@16 145 template <typename Allocator, typename ReturnType, typename Arg1>
Chris@16 146 struct handler_type<use_future_t<Allocator>, ReturnType(Arg1)>
Chris@16 147 {
Chris@16 148 typedef detail::promise_handler<Arg1> type;
Chris@16 149 };
Chris@16 150
Chris@16 151 // Handler type specialisation for use_future.
Chris@16 152 template <typename Allocator, typename ReturnType>
Chris@16 153 struct handler_type<use_future_t<Allocator>,
Chris@16 154 ReturnType(boost::system::error_code)>
Chris@16 155 {
Chris@16 156 typedef detail::promise_handler<void> type;
Chris@16 157 };
Chris@16 158
Chris@16 159 // Handler type specialisation for use_future.
Chris@16 160 template <typename Allocator, typename ReturnType, typename Arg2>
Chris@16 161 struct handler_type<use_future_t<Allocator>,
Chris@16 162 ReturnType(boost::system::error_code, Arg2)>
Chris@16 163 {
Chris@16 164 typedef detail::promise_handler<Arg2> type;
Chris@16 165 };
Chris@16 166
Chris@16 167 #endif // !defined(GENERATING_DOCUMENTATION)
Chris@16 168
Chris@16 169 } // namespace asio
Chris@16 170 } // namespace boost
Chris@16 171
Chris@16 172 #include <boost/asio/detail/pop_options.hpp>
Chris@16 173
Chris@16 174 #endif // BOOST_ASIO_IMPL_USE_FUTURE_HPP