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 "asio/execution/context_as.hpp"
18 
19 #include "asio/execution/any_executor.hpp"
20 #include "asio/io_context.hpp"
21 #include "asio/static_thread_pool.hpp"
22 #include "../unit_test.hpp"
23 
24 #if defined(ASIO_HAS_BOOST_BIND)
25 # include <boost/bind/bind.hpp>
26 #else // defined(ASIO_HAS_BOOST_BIND)
27 # include <functional>
28 #endif // defined(ASIO_HAS_BOOST_BIND)
29 
30 using namespace asio;
31 
32 #if defined(ASIO_HAS_BOOST_BIND)
33 namespace bindns = boost;
34 #else // defined(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   ASIO_CHECK(
43       &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   ASIO_CHECK(
52       &asio::query(ex1,
53         execution::context_as_t<static_thread_pool&>())
54         == &pool);
55 
56   ASIO_CHECK(
57       &asio::query(ex1, execution::context)
58         == &pool);
59 
60   ASIO_CHECK(
61       &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   ASIO_CHECK(
70       &asio::query(ex2,
71         execution::context_as_t<const static_thread_pool&>())
72         == &pool);
73 
74   ASIO_CHECK(
75       &asio::query(ex2, execution::context)
76         == &pool);
77 
78   io_context io_ctx;
79 
80   ASIO_CHECK(
81       &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   ASIO_CHECK(
90       &asio::query(ex3,
91         execution::context_as_t<io_context&>())
92         == &io_ctx);
93 
94   ASIO_CHECK(
95       &asio::query(ex3, execution::context)
96         == &io_ctx);
97 
98   ASIO_CHECK(
99       &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   ASIO_CHECK(
108       &asio::query(ex4,
109         execution::context_as_t<const io_context&>())
110         == &io_ctx);
111 
112   ASIO_CHECK(
113       &asio::query(ex4, execution::context)
114         == &io_ctx);
115 
116   ASIO_CHECK(
117       &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   ASIO_CHECK(
126       &asio::query(ex5,
127         execution::context_as_t<execution_context&>())
128         == &io_ctx);
129 
130   ASIO_CHECK(
131       &asio::query(ex5, execution::context)
132         == &io_ctx);
133 
134   ASIO_CHECK(
135       &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   ASIO_CHECK(
144       &asio::query(ex6,
145         execution::context_as_t<const execution_context&>())
146         == &io_ctx);
147 
148   ASIO_CHECK(
149       &asio::query(ex6, execution::context)
150         == &io_ctx);
151 }
152 
153 ASIO_TEST_SUITE
154 (
155   "context_as",
156   ASIO_TEST_CASE(context_as_executor_query_test)
157 )
158