Add execution::context_as property adapter.

This commit is contained in:
Christopher Kohlhoff 2020-06-16 22:05:00 +10:00
parent b975aa75c8
commit ed4530ef79
8 changed files with 358 additions and 0 deletions

View File

@ -298,6 +298,7 @@ nobase_include_HEADERS = \
asio/execution/blocking_adaptation.hpp \
asio/execution/bulk_guarantee.hpp \
asio/execution/context.hpp \
asio/execution/context_as.hpp \
asio/execution/execute.hpp \
asio/execution/executor.hpp \
asio/execution/impl/bad_executor.ipp \

View File

@ -59,6 +59,7 @@
#include "asio/execution/blocking_adaptation.hpp"
#include "asio/execution/bulk_guarantee.hpp"
#include "asio/execution/context.hpp"
#include "asio/execution/context_as.hpp"
#include "asio/execution/execute.hpp"
#include "asio/execution/executor.hpp"
#include "asio/execution/invocable_archetype.hpp"

View File

@ -64,6 +64,7 @@ using std::is_destructible;
using std::is_function;
using std::is_nothrow_copy_constructible;
using std::is_nothrow_destructible;
using std::is_reference;
using std::is_same;
using std::is_scalar;
using std::remove_pointer;
@ -98,6 +99,7 @@ template <typename T>
struct is_nothrow_copy_constructible : boost::has_nothrow_copy<T> {};
template <typename T>
struct is_nothrow_destructible : boost::has_nothrow_destructor<T> {};
using boost::is_reference;
using boost::is_same;
using boost::is_scalar;
using boost::remove_pointer;

View File

@ -0,0 +1,192 @@
//
// execution/context_as.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 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 ASIO_EXECUTION_CONTEXT_AS_HPP
#define ASIO_EXECUTION_CONTEXT_AS_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include "asio/detail/type_traits.hpp"
#include "asio/execution/context.hpp"
#include "asio/execution/executor.hpp"
#include "asio/is_applicable_property.hpp"
#include "asio/query.hpp"
#include "asio/traits/query_static_constexpr_member.hpp"
#include "asio/traits/static_query.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
#if defined(GENERATING_DOCUMENTATION)
namespace execution {
/// A property that is used to obtain the execution context that is associated
/// with an executor.
template <typename U>
struct context_as_t
{
/// The context_t property applies to executors.
template <typename T>
static constexpr bool is_applicable_property_v = is_executor_v<T>;
/// The context_t property cannot be required.
static constexpr bool is_requirable = false;
/// The context_t property cannot be preferred.
static constexpr bool is_preferable = false;
/// The type returned by queries against an @c any_executor.
typedef T polymorphic_query_result_type;
};
/// A special value used for accessing the context_as_t property.
template <typename U>
constexpr context_as_t context_as;
} // namespace execution
#else // defined(GENERATING_DOCUMENTATION)
namespace execution {
template <typename T>
struct context_as_t
{
#if defined(ASIO_HAS_VARIABLE_TEMPLATES)
template <typename U>
ASIO_STATIC_CONSTEXPR(bool,
is_applicable_property_v = is_executor<U>::value);
#endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
ASIO_STATIC_CONSTEXPR(bool, is_requirable = false);
ASIO_STATIC_CONSTEXPR(bool, is_preferable = false);
typedef T polymorphic_query_result_type;
ASIO_CONSTEXPR context_as_t()
{
}
ASIO_CONSTEXPR context_as_t(context_t)
{
}
#if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
&& defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename E>
static ASIO_CONSTEXPR
typename traits::query_static_constexpr_member<E, context_t>::result_type
static_query()
ASIO_NOEXCEPT_IF((
traits::query_static_constexpr_member<E, context_t>::is_noexcept))
{
return traits::query_static_constexpr_member<E, context_t>::value();
}
template <typename E, typename U = decltype(context_as_t::static_query<E>())>
static ASIO_CONSTEXPR const U static_query_v
= context_as_t::static_query<E>();
#endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
// && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename Executor, typename U>
friend ASIO_CONSTEXPR U query(
const Executor& ex, const context_as_t<U>&,
typename enable_if<
is_same<T, U>::value
&& can_query<const Executor&, const context_t&>::value
>::type* = 0)
#if defined(_MSC_VER) // Visual C++ wants the type to be qualified.
ASIO_NOEXCEPT_IF((
is_nothrow_query<const Executor&, const context_t&>::value))
#elif !defined(__clang__) // Clang crashes if noexcept is used here.
ASIO_NOEXCEPT_IF((
is_nothrow_query<const Executor&, const context_t&>::value))
#endif // !defined(__clang__)
{
return asio::query(ex, context);
}
};
#if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
&& defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename T> template <typename E, typename U>
const U context_as_t<T>::static_query_v;
#endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
// && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
#if (defined(ASIO_HAS_VARIABLE_TEMPLATES) \
&& defined(ASIO_HAS_CONSTEXPR)) \
|| defined(GENERATING_DOCUMENTATION)
template <typename T>
constexpr context_as_t<T> context_as{};
#endif // (defined(ASIO_HAS_VARIABLE_TEMPLATES)
// && defined(ASIO_HAS_CONSTEXPR))
// || defined(GENERATING_DOCUMENTATION)
} // namespace execution
#if !defined(ASIO_HAS_VARIABLE_TEMPLATES)
template <typename T, typename U>
struct is_applicable_property<T, execution::context_as_t<U> >
: execution::is_executor<T>
{
};
#endif // !defined(ASIO_HAS_VARIABLE_TEMPLATES)
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
|| !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename T, typename U>
struct static_query<T, execution::context_as_t<U>,
typename enable_if<
static_query<T, execution::context_t>::is_valid
>::type> : static_query<T, execution::context_t>
{
};
#endif // !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
// || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
#if !defined(ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
template <typename T, typename U>
struct query_free<T, execution::context_as_t<U>,
typename enable_if<
can_query<const T&, const execution::context_t&>::value
>::type>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
(is_nothrow_query<const T&, const execution::context_t&>::value));
typedef U result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
} // namespace traits
#endif // defined(GENERATING_DOCUMENTATION)
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_EXECUTION_CONTEXT_AS_HPP

View File

@ -151,6 +151,7 @@ UNIT_TEST_EXES = \
tests\unit\execution\blocking.exe \
tests\unit\execution\blocking_adaptation.exe \
tests\unit\execution\bulk_guarantee.exe \
tests\unit\execution\context_as.exe \
tests\unit\execution\execute.exe \
tests\unit\execution\invocable_archetype.exe \
tests\unit\execution\mapping.exe \

View File

@ -47,6 +47,7 @@ check_PROGRAMS = \
unit/execution/blocking \
unit/execution/blocking_adaptation \
unit/execution/bulk_guarantee \
unit/execution/context_as \
unit/execution/execute \
unit/execution/invocable_archetype \
unit/execution/mapping \
@ -198,6 +199,7 @@ TESTS = \
unit/execution/blocking \
unit/execution/blocking_adaptation \
unit/execution/bulk_guarantee \
unit/execution/context_as \
unit/execution/execute \
unit/execution/invocable_archetype \
unit/execution/mapping \
@ -349,6 +351,7 @@ unit_execution_any_executor_SOURCES = unit/execution/any_executor.cpp
unit_execution_blocking_SOURCES = unit/execution/blocking.cpp
unit_execution_blocking_adaptation_SOURCES = unit/execution/blocking_adaptation.cpp
unit_execution_bulk_guarantee_SOURCES = unit/execution/bulk_guarantee.cpp
unit_execution_context_as_SOURCES = unit/execution/context_as.cpp
unit_execution_execute_SOURCES = unit/execution/execute.cpp
unit_execution_invocable_archetype_SOURCES = unit/execution/invocable_archetype.cpp
unit_execution_mapping_SOURCES = unit/execution/mapping.cpp

View File

@ -11,6 +11,7 @@ any_executor
blocking
blocking_adaptation
bulk_guarantee
context_as
execute
invocable_archetype
mapping

View File

@ -0,0 +1,157 @@
//
// context_as.cpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 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)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include "asio/execution/context_as.hpp"
#include "asio/execution/any_executor.hpp"
#include "asio/io_context.hpp"
#include "asio/static_thread_pool.hpp"
#include "../unit_test.hpp"
#if defined(ASIO_HAS_BOOST_BIND)
# include <boost/bind/bind.hpp>
#else // defined(ASIO_HAS_BOOST_BIND)
# include <functional>
#endif // defined(ASIO_HAS_BOOST_BIND)
using namespace asio;
#if defined(ASIO_HAS_BOOST_BIND)
namespace bindns = boost;
#else // defined(ASIO_HAS_BOOST_BIND)
namespace bindns = std;
#endif
void context_as_executor_query_test()
{
static_thread_pool pool(1);
ASIO_CHECK(
&asio::query(pool.executor(),
execution::context_as_t<static_thread_pool&>())
== &pool);
execution::any_executor<
execution::context_as_t<static_thread_pool&>
> ex1 = pool.executor();
ASIO_CHECK(
&asio::query(ex1,
execution::context_as_t<static_thread_pool&>())
== &pool);
ASIO_CHECK(
&asio::query(ex1, execution::context)
== &pool);
ASIO_CHECK(
&asio::query(pool.executor(),
execution::context_as_t<const static_thread_pool&>())
== &pool);
execution::any_executor<
execution::context_as_t<const static_thread_pool&>
> ex2 = pool.executor();
ASIO_CHECK(
&asio::query(ex2,
execution::context_as_t<const static_thread_pool&>())
== &pool);
ASIO_CHECK(
&asio::query(ex2, execution::context)
== &pool);
io_context io_ctx;
ASIO_CHECK(
&asio::query(io_ctx.get_executor(),
execution::context_as_t<io_context&>())
== &io_ctx);
execution::any_executor<
execution::context_as_t<io_context&>
> ex3 = io_ctx.get_executor();
ASIO_CHECK(
&asio::query(ex3,
execution::context_as_t<io_context&>())
== &io_ctx);
ASIO_CHECK(
&asio::query(ex3, execution::context)
== &io_ctx);
ASIO_CHECK(
&asio::query(io_ctx.get_executor(),
execution::context_as_t<const io_context&>())
== &io_ctx);
execution::any_executor<
execution::context_as_t<const io_context&>
> ex4 = io_ctx.get_executor();
ASIO_CHECK(
&asio::query(ex4,
execution::context_as_t<const io_context&>())
== &io_ctx);
ASIO_CHECK(
&asio::query(ex4, execution::context)
== &io_ctx);
ASIO_CHECK(
&asio::query(io_ctx.get_executor(),
execution::context_as_t<execution_context&>())
== &io_ctx);
execution::any_executor<
execution::context_as_t<execution_context&>
> ex5 = io_ctx.get_executor();
ASIO_CHECK(
&asio::query(ex5,
execution::context_as_t<execution_context&>())
== &io_ctx);
ASIO_CHECK(
&asio::query(ex5, execution::context)
== &io_ctx);
ASIO_CHECK(
&asio::query(io_ctx.get_executor(),
execution::context_as_t<const execution_context&>())
== &io_ctx);
execution::any_executor<
execution::context_as_t<const execution_context&>
> ex6 = io_ctx.get_executor();
ASIO_CHECK(
&asio::query(ex6,
execution::context_as_t<const execution_context&>())
== &io_ctx);
ASIO_CHECK(
&asio::query(ex6, execution::context)
== &io_ctx);
}
ASIO_TEST_SUITE
(
"context_as",
ASIO_TEST_CASE(context_as_executor_query_test)
)