2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-19 04:02:09 +00:00

Add asio::lazy.

This commit is contained in:
Christopher Kohlhoff
2019-09-03 22:07:43 +10:00
parent fae562d7c9
commit f63b07ec6a
3 changed files with 128 additions and 0 deletions

View File

@@ -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 <boost/asio/ts/net.hpp>
#include <boost/asio/lazy.hpp>
#include <iostream>
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();
}

View File

@@ -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 <boost/asio/ts/net.hpp>
#include <boost/asio/lazy.hpp>
#include <iostream>
template <boost::asio::completion_token_for<void(int)> CompletionToken>
auto async_foo(int i, CompletionToken&& token)
{
return boost::asio::async_initiate<CompletionToken, void(int)>(
[](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();
}

View File

@@ -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 <boost/asio/detail/config.hpp>
#include <tuple>
#include <utility>
#include <boost/asio/async_result.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
struct lazy_t {};
template <BOOST_ASIO_COMPLETION_SIGNATURE Signature>
class async_result<lazy_t, Signature>
{
public:
template <typename Initiation, typename... InitArgs>
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<decltype(token), Signature>(
std::move(initiation), token,
std::forward<decltype(args)>(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 <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_LAZY_HPP