diff --git a/example/cpp17/lazy/lazy_1.cpp b/example/cpp17/lazy/lazy_1.cpp new file mode 100644 index 00000000..5212743c --- /dev/null +++ b/example/cpp17/lazy/lazy_1.cpp @@ -0,0 +1,23 @@ +// +// lazy_1.cpp +// ~~~~~~~~~~ +// +// Copyright (c) 2003-2019 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) +// + +#include +#include +#include + +int main() +{ + boost::asio::io_context ctx; + boost::asio::steady_timer timer(ctx); + timer.expires_after(std::chrono::seconds(1)); + auto x = timer.async_wait(boost::asio::lazy); + x([](auto){ std::cout << "timer done\n"; }); + ctx.run(); +} diff --git a/example/cpp17/lazy/lazy_2.cpp b/example/cpp17/lazy/lazy_2.cpp new file mode 100644 index 00000000..71750f2d --- /dev/null +++ b/example/cpp17/lazy/lazy_2.cpp @@ -0,0 +1,37 @@ +// +// lazy_2.cpp +// ~~~~~~~~~~ +// +// Copyright (c) 2003-2019 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) +// + +#include +#include +#include + +template CompletionToken> +auto async_foo(int i, CompletionToken&& token) +{ + return boost::asio::async_initiate( + [](auto&& handler, int i) + { + auto ex = boost::asio::get_associated_executor(handler); + auto alloc = boost::asio::get_associated_allocator(handler); + int result = i * 2; + ex.post( + [handler = std::move(handler), result]() mutable + { + handler(result); + }, alloc); + }, token, i); +} + +int main() +{ + auto x = async_foo(21, boost::asio::lazy); + x([](auto result){ std::cout << "result is " << result << "\n"; }); + boost::asio::system_executor().context().join(); +} diff --git a/include/boost/asio/lazy.hpp b/include/boost/asio/lazy.hpp new file mode 100644 index 00000000..11bc50f7 --- /dev/null +++ b/include/boost/asio/lazy.hpp @@ -0,0 +1,68 @@ +// +// lazy.hpp +// ~~~~~~~~ +// +// Copyright (c) 2003-2019 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_LAZY_HPP +#define BOOST_ASIO_LAZY_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) + +#include + +#include +#include +#include + +#include + +namespace boost { +namespace asio { + +struct lazy_t {}; + +template +class async_result +{ +public: + template + static auto initiate(Initiation initiation, lazy_t, InitArgs... init_args) + { + return [ + initiation = std::move(initiation), + init_arg_pack = std::make_tuple(std::move(init_args)...) + ](auto&& token) mutable + { + return std::apply( + [&](auto&&... args) + { + return async_initiate( + std::move(initiation), token, + std::forward(args)...); + }, + std::move(init_arg_pack) + ); + }; + } +}; + +/// A completion token object that indicates that an Awaitable should be returned. +#if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION) +constexpr lazy_t lazy; +#elif defined(BOOST_ASIO_MSVC) +__declspec(selectany) lazy_t lazy; +#endif + +} // namespace asio +} // namespace boost + +#include + +#endif // BOOST_ASIO_LAZY_HPP