1 //
2 // context_as.cpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 // Disable autolinking for unit tests.
12 #if !defined(BOOST_ALL_NO_LIB)
13 #define BOOST_ALL_NO_LIB 1
14 #endif // !defined(BOOST_ALL_NO_LIB)
15
16 // Test that header file is self-contained.
17 #include <boost/asio/execution/context_as.hpp>
18
19 #include <boost/asio/execution/any_executor.hpp>
20 #include <boost/asio/io_context.hpp>
21 #include <boost/asio/static_thread_pool.hpp>
22 #include "../unit_test.hpp"
23
24 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
25 # include <boost/bind/bind.hpp>
26 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
27 # include <functional>
28 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
29
30 using namespace boost::asio;
31
32 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
33 namespace bindns = boost;
34 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
35 namespace bindns = std;
36 #endif
37
context_as_executor_query_test()38 void context_as_executor_query_test()
39 {
40 static_thread_pool pool(1);
41
42 BOOST_ASIO_CHECK(
43 &boost::asio::query(pool.executor(),
44 execution::context_as_t<static_thread_pool&>())
45 == &pool);
46
47 execution::any_executor<
48 execution::context_as_t<static_thread_pool&>
49 > ex1 = pool.executor();
50
51 BOOST_ASIO_CHECK(
52 &boost::asio::query(ex1,
53 execution::context_as_t<static_thread_pool&>())
54 == &pool);
55
56 BOOST_ASIO_CHECK(
57 &boost::asio::query(ex1, execution::context)
58 == &pool);
59
60 BOOST_ASIO_CHECK(
61 &boost::asio::query(pool.executor(),
62 execution::context_as_t<const static_thread_pool&>())
63 == &pool);
64
65 execution::any_executor<
66 execution::context_as_t<const static_thread_pool&>
67 > ex2 = pool.executor();
68
69 BOOST_ASIO_CHECK(
70 &boost::asio::query(ex2,
71 execution::context_as_t<const static_thread_pool&>())
72 == &pool);
73
74 BOOST_ASIO_CHECK(
75 &boost::asio::query(ex2, execution::context)
76 == &pool);
77
78 io_context io_ctx;
79
80 BOOST_ASIO_CHECK(
81 &boost::asio::query(io_ctx.get_executor(),
82 execution::context_as_t<io_context&>())
83 == &io_ctx);
84
85 execution::any_executor<
86 execution::context_as_t<io_context&>
87 > ex3 = io_ctx.get_executor();
88
89 BOOST_ASIO_CHECK(
90 &boost::asio::query(ex3,
91 execution::context_as_t<io_context&>())
92 == &io_ctx);
93
94 BOOST_ASIO_CHECK(
95 &boost::asio::query(ex3, execution::context)
96 == &io_ctx);
97
98 BOOST_ASIO_CHECK(
99 &boost::asio::query(io_ctx.get_executor(),
100 execution::context_as_t<const io_context&>())
101 == &io_ctx);
102
103 execution::any_executor<
104 execution::context_as_t<const io_context&>
105 > ex4 = io_ctx.get_executor();
106
107 BOOST_ASIO_CHECK(
108 &boost::asio::query(ex4,
109 execution::context_as_t<const io_context&>())
110 == &io_ctx);
111
112 BOOST_ASIO_CHECK(
113 &boost::asio::query(ex4, execution::context)
114 == &io_ctx);
115
116 BOOST_ASIO_CHECK(
117 &boost::asio::query(io_ctx.get_executor(),
118 execution::context_as_t<execution_context&>())
119 == &io_ctx);
120
121 execution::any_executor<
122 execution::context_as_t<execution_context&>
123 > ex5 = io_ctx.get_executor();
124
125 BOOST_ASIO_CHECK(
126 &boost::asio::query(ex5,
127 execution::context_as_t<execution_context&>())
128 == &io_ctx);
129
130 BOOST_ASIO_CHECK(
131 &boost::asio::query(ex5, execution::context)
132 == &io_ctx);
133
134 BOOST_ASIO_CHECK(
135 &boost::asio::query(io_ctx.get_executor(),
136 execution::context_as_t<const execution_context&>())
137 == &io_ctx);
138
139 execution::any_executor<
140 execution::context_as_t<const execution_context&>
141 > ex6 = io_ctx.get_executor();
142
143 BOOST_ASIO_CHECK(
144 &boost::asio::query(ex6,
145 execution::context_as_t<const execution_context&>())
146 == &io_ctx);
147
148 BOOST_ASIO_CHECK(
149 &boost::asio::query(ex6, execution::context)
150 == &io_ctx);
151 }
152
153 BOOST_ASIO_TEST_SUITE
154 (
155 "context_as",
156 BOOST_ASIO_TEST_CASE(context_as_executor_query_test)
157 )
158