-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinspect.cpp
More file actions
199 lines (177 loc) · 6.76 KB
/
inspect.cpp
File metadata and controls
199 lines (177 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// examples/inspectc.pp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <iostream>
#include <ostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <tuple>
#include <variant>
#ifdef BEMAN_HAS_MODULES
import beman.execution;
#else
#include <beman/execution/execution.hpp>
#endif
namespace ex = beman::execution;
// ----------------------------------------------------------------------------
namespace meta {
// The code in this namespace is a fairly basic way to print types. It can
// almost certainly be done better, in particular using reflection. However,
// that's beside the point of this example. The important part for the
// example is that meta::type<T>::name() yields a string representing the
// type T in some reasonable way
template <template <typename...> class, typename...>
struct list {
static auto name() { return "unknown-list"; }
};
template <>
struct list<::beman::execution::completion_signatures> {
static auto name() { return "ex::completion_signatures"; }
};
template <typename T>
struct type {
static auto name() { return typeid(T).name(); }
};
template <typename T>
struct type<T&> {
static auto name() { return typeid(T).name() + std::string("&"); }
};
template <typename T>
struct type<T&&> {
static auto name() { return typeid(T).name() + std::string("&&"); }
};
template <typename T>
struct type<T*> {
static auto name() { return typeid(T).name() + std::string("*"); }
};
template <typename T>
struct type<const T> {
static auto name() { return typeid(T).name() + std::string("const"); }
};
template <typename T>
struct type<volatile T> {
static auto name() { return typeid(T).name() + std::string("volatile"); }
};
template <typename T>
struct type<const volatile T> {
static auto name() { return typeid(T).name() + std::string("const volatile"); }
};
// NOLINTBEGIN(hicpp-avoid-c-arrays)
template <typename T, std::size_t N>
struct type<T[N]> {
static auto name() { return typeid(T).name() + std::string("[") + std::to_string(N) + "]"; }
};
template <typename T, std::size_t N>
struct type<T (&)[N]> {
static auto name() { return typeid(T).name() + std::string("(&)[") + std::to_string(N) + "]"; }
};
template <typename T, std::size_t N>
struct type<T (*)[N]> {
static auto name() { return typeid(T).name() + std::string("(*)[") + std::to_string(N) + "]"; }
};
// NOLINTEND(hicpp-avoid-c-arrays)
template <>
struct type<::beman::execution::set_value_t> {
static auto name() { return "ex::set_value_t"; }
};
template <>
struct type<::beman::execution::set_error_t> {
static auto name() { return "ex::set_error_t"; }
};
template <>
struct type<::beman::execution::set_stopped_t> {
static auto name() { return "ex::set_stopped_t"; }
};
template <typename T>
struct type<T()> {
static auto name() { return type<T>::name() + std::string("()"); }
};
template <typename T, typename A, typename... B>
struct type<T(A, B...)> {
static auto name() {
return type<T>::name() + std::string("(") + (type<A>::name() + ... + (std::string(", ") + type<B>::name())) +
")";
}
};
template <typename T>
struct type<T (*)()> {
static auto name() { return type<T>::name() + std::string("(*)()"); }
};
template <typename T, typename A, typename... B>
struct type<T (*)(A, B...)> {
static auto name() {
return type<T>::name() + std::string("(*)(") +
(type<A>::name() + ... + (std::string(", ") + type<B>::name())) + ")";
}
};
template <template <typename...> class L>
struct type<L<>> {
static auto name() { return list<L>::name() + std::string("<>"); }
};
template <template <typename...> class L, typename T, typename... S>
struct type<L<T, S...>> {
static auto name() {
return list<L>::name() + std::string("<") + (type<T>::name() + ... + (std::string(", ") + type<S>::name())) +
">";
}
};
template <typename T>
inline std::ostream& operator<<(std::ostream& out, const type<T>& t) {
return out << t.name();
}
} // namespace meta
// ----------------------------------------------------------------------------
namespace {
struct logger_t {
template <ex::sender Sndr, ex::receiver Rcvr, typename Log>
struct state {
using operation_state_concept = ex::operation_state_tag;
using inner_t = decltype(ex::connect(std::declval<Sndr>(), std::declval<Rcvr>()));
inner_t inner;
std::remove_cvref_t<Log> log;
state(Sndr&& s, Rcvr&& r, Log&& l)
: inner(ex::connect(std::forward<Sndr>(s), std::forward<Rcvr>(r))), log(std::forward<Log>(l)) {}
auto start() & noexcept -> void {
this->log(meta::type<
decltype(ex::get_completion_signatures<Sndr, decltype(ex::get_env(std::declval<Rcvr>()))>())>::
name());
ex::start(this->inner);
}
};
template <ex::sender Sndr, typename Log>
struct sender {
using sender_concept = ex::sender_tag;
Sndr sndr;
Log log;
template <typename, typename... Env>
static consteval auto get_completion_signatures() noexcept {
return ex::get_completion_signatures<Sndr, Env...>();
}
template <ex::receiver Receiver>
auto connect(Receiver&& receiver) && noexcept(noexcept(ex::connect(std::move(this->sndr),
std::forward<Receiver>(receiver)))) {
return state<Sndr, Receiver, Log>(
std::move(this->sndr), std::forward<Receiver>(receiver), std::move(this->log));
}
};
template <ex::sender Sndr, typename Log>
auto operator()(Sndr&& sndr, Log&& log) const {
return sender<std::remove_cvref_t<Sndr>, std::remove_cvref_t<Log>>{std::forward<Sndr>(sndr),
std::forward<Log>(log)};
}
};
inline constexpr logger_t logger{};
} // namespace
// ----------------------------------------------------------------------------
int main() {
auto log = [](std::string_view name) {
return [name](std::string_view msg) { std::cout << name << " message='" << msg << "'\n"; };
};
ex::sync_wait(logger(ex::just(), log("just()")));
ex::sync_wait(logger(ex::just() | ex::then([]() {}), log("just() | then(...)")));
ex::sync_wait(logger(ex::just() | ex::then([]() noexcept {}), log("just() | then(...)")));
ex::sync_wait(logger(ex::just(0, 1), log("just(0, 1)")));
ex::sync_wait(logger(ex::just(0, 1, 2), log("just(0, 1, 2)")));
ex::sync_wait(logger(ex::just_error(0), log("just_error(0)")) | ex::upon_error([](auto) {}));
ex::sync_wait(logger(ex::just_stopped(), log("just_stopped()")) | ex::upon_stopped([]() {}));
}