// // impl/use_future.hpp // ~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_ASIO_IMPL_USE_FUTURE_HPP #define BOOST_ASIO_IMPL_USE_FUTURE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include #include #include namespace boost { namespace asio { namespace detail { // Completion handler to adapt a promise as a completion handler. template class promise_handler { public: // Construct from use_future special value. template promise_handler(use_future_t uf) : promise_(std::allocate_shared >( typename Alloc::template rebind::other(uf.get_allocator()), std::allocator_arg, typename Alloc::template rebind::other(uf.get_allocator()))) { } void operator()(T t) { promise_->set_value(t); } void operator()(const boost::system::error_code& ec, T t) { if (ec) promise_->set_exception( std::make_exception_ptr( boost::system::system_error(ec))); else promise_->set_value(t); } //private: std::shared_ptr > promise_; }; // Completion handler to adapt a void promise as a completion handler. template <> class promise_handler { public: // Construct from use_future special value. Used during rebinding. template promise_handler(use_future_t uf) : promise_(std::allocate_shared >( typename Alloc::template rebind::other(uf.get_allocator()), std::allocator_arg, typename Alloc::template rebind::other(uf.get_allocator()))) { } void operator()() { promise_->set_value(); } void operator()(const boost::system::error_code& ec) { if (ec) promise_->set_exception( std::make_exception_ptr( boost::system::system_error(ec))); else promise_->set_value(); } //private: std::shared_ptr > promise_; }; // Ensure any exceptions thrown from the handler are propagated back to the // caller via the future. template void asio_handler_invoke(Function f, promise_handler* h) { std::shared_ptr > p(h->promise_); try { f(); } catch (...) { p->set_exception(std::current_exception()); } } } // namespace detail #if !defined(GENERATING_DOCUMENTATION) // Handler traits specialisation for promise_handler. template class async_result > { public: // The initiating function will return a future. typedef std::future type; // Constructor creates a new promise for the async operation, and obtains the // corresponding future. explicit async_result(detail::promise_handler& h) { value_ = h.promise_->get_future(); } // Obtain the future to be returned from the initiating function. type get() { return std::move(value_); } private: type value_; }; // Handler type specialisation for use_future. template struct handler_type, ReturnType()> { typedef detail::promise_handler type; }; // Handler type specialisation for use_future. template struct handler_type, ReturnType(Arg1)> { typedef detail::promise_handler type; }; // Handler type specialisation for use_future. template struct handler_type, ReturnType(boost::system::error_code)> { typedef detail::promise_handler type; }; // Handler type specialisation for use_future. template struct handler_type, ReturnType(boost::system::error_code, Arg2)> { typedef detail::promise_handler type; }; #endif // !defined(GENERATING_DOCUMENTATION) } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_IMPL_USE_FUTURE_HPP