-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.cppm
More file actions
279 lines (241 loc) · 9.54 KB
/
handler.cppm
File metadata and controls
279 lines (241 loc) · 9.54 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
module;
#include <atomic>
#include <concepts>
#include <expected>
#include <optional>
#include <string_view>
#include <type_traits>
#include <utility>
export module mcpplibs.primitives.policy.handler;
import mcpplibs.primitives.policy.traits;
import mcpplibs.primitives.operations.traits;
export namespace mcpplibs::primitives::policy {
namespace error {
// Unified runtime error kind carried across the policy chain.
enum class kind : unsigned char {
none = 0,
invalid_type_combination,
overflow,
underflow,
divide_by_zero,
domain_error,
unspecified,
};
template <typename CommonRep> struct request {
kind code = kind::none;
std::string_view reason{};
std::optional<CommonRep> lhs_value{};
std::optional<CommonRep> rhs_value{};
std::optional<CommonRep> fallback_value{};
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
struct handler {
static constexpr bool enabled = false;
using request_type = request<CommonRep>;
using result_type = std::expected<CommonRep, ErrorPayload>;
static constexpr auto resolve(request_type const &) -> result_type {
return std::unexpected(ErrorPayload{});
}
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
concept handler_protocol =
error_policy<Policy> && operations::operation<OpTag> &&
(!std::same_as<CommonRep, void>) && requires {
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::request_type;
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::result_type;
{
handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled
} -> std::convertible_to<bool>;
requires handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled;
requires std::same_as<typename handler<Policy, OpTag, CommonRep,
ErrorPayload>::request_type,
request<CommonRep>>;
requires std::same_as<
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::result_type,
std::expected<CommonRep, ErrorPayload>>;
{
handler<Policy, OpTag, CommonRep, ErrorPayload>::resolve(
std::declval<request<CommonRep> const &>())
} -> std::same_as<typename handler<Policy, OpTag, CommonRep,
ErrorPayload>::result_type>;
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
concept handler_available =
handler_protocol<Policy, OpTag, CommonRep, ErrorPayload> &&
handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled;
} // namespace error
namespace concurrency {
struct injection {
bool fence_before = false;
bool fence_after = false;
std::memory_order order_before = std::memory_order_seq_cst;
std::memory_order order_after = std::memory_order_seq_cst;
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
struct handler {
static constexpr bool enabled = false;
using injection_type = injection;
using result_type = std::expected<CommonRep, ErrorPayload>;
static constexpr auto inject() noexcept -> injection_type { return {}; }
static constexpr auto load(CommonRep const &) noexcept -> CommonRep {
return CommonRep{};
}
static constexpr auto store(CommonRep &, CommonRep) noexcept -> void {}
static constexpr auto compare_exchange(CommonRep &, CommonRep &,
CommonRep) noexcept -> bool {
return false;
}
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
concept handler_protocol = requires {
requires concurrency_policy<Policy>;
requires operations::operation<OpTag>;
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::injection_type;
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::result_type;
{
handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled
} -> std::convertible_to<bool>;
requires handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled;
requires std::same_as<
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::injection_type,
injection>;
requires std::same_as<
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::result_type,
std::expected<CommonRep, ErrorPayload>>;
{
handler<Policy, OpTag, CommonRep, ErrorPayload>::inject()
} noexcept -> std::same_as<
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::injection_type>;
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
concept handler_available = requires {
requires handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled;
requires handler_protocol<Policy, OpTag, CommonRep, ErrorPayload>;
};
template <typename Policy, typename CommonRep,
typename ErrorPayload = error::kind>
concept handler_access_protocol = requires {
requires concurrency_policy<Policy>;
{
handler<Policy, void, CommonRep, ErrorPayload>::enabled
} -> std::convertible_to<bool>;
requires handler<Policy, void, CommonRep, ErrorPayload>::enabled;
{
handler<Policy, void, CommonRep, ErrorPayload>::load(
std::declval<CommonRep const &>())
} noexcept -> std::same_as<CommonRep>;
{
handler<Policy, void, CommonRep, ErrorPayload>::store(
std::declval<CommonRep &>(), std::declval<CommonRep>())
} noexcept -> std::same_as<void>;
{
handler<Policy, void, CommonRep, ErrorPayload>::compare_exchange(
std::declval<CommonRep &>(), std::declval<CommonRep &>(),
std::declval<CommonRep>())
} noexcept -> std::same_as<bool>;
};
template <typename Policy, typename CommonRep,
typename ErrorPayload = error::kind>
concept handler_access_available = requires {
requires handler<Policy, void, CommonRep, ErrorPayload>::enabled;
requires handler_access_protocol<Policy, CommonRep, ErrorPayload>;
};
} // namespace concurrency
namespace type {
template <typename OpTag, typename LhsRep, typename RhsRep, typename CommonRep>
struct operation_context {
using op_tag = OpTag;
using lhs_rep = LhsRep;
using rhs_rep = RhsRep;
using common_rep = CommonRep;
};
// Compile-time type negotiation protocol.
template <typename Policy, typename OpTag, typename LhsRep, typename RhsRep>
struct handler {
static constexpr bool enabled = false;
static constexpr bool allowed = false;
static constexpr unsigned diagnostic_id = 0;
using common_rep = void;
};
template <typename Policy, typename OpTag, typename LhsRep, typename RhsRep>
concept handler_protocol = requires {
requires type_policy<Policy>;
requires operations::operation<OpTag>;
typename handler<Policy, OpTag, LhsRep, RhsRep>::common_rep;
{
handler<Policy, OpTag, LhsRep, RhsRep>::enabled
} -> std::convertible_to<bool>;
{
handler<Policy, OpTag, LhsRep, RhsRep>::allowed
} -> std::convertible_to<bool>;
{
handler<Policy, OpTag, LhsRep, RhsRep>::diagnostic_id
} -> std::convertible_to<unsigned>;
requires handler<Policy, OpTag, LhsRep, RhsRep>::enabled;
};
template <typename Policy, typename OpTag, typename LhsRep, typename RhsRep>
concept handler_available = requires {
requires handler<Policy, OpTag, LhsRep, RhsRep>::enabled;
requires handler_protocol<Policy, OpTag, LhsRep, RhsRep>;
};
} // namespace type
namespace value {
template <typename CommonRep> struct decision {
bool has_value = false;
CommonRep value{};
error::request<CommonRep> error{};
};
// Runtime value-check protocol.
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
struct handler {
static constexpr bool enabled = false;
static constexpr bool may_adjust_value = false;
using decision_type = decision<CommonRep>;
using result_type = std::expected<CommonRep, ErrorPayload>;
static constexpr auto finalize(decision_type decision,
concurrency::injection const &)
-> decision_type {
return decision;
}
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
concept handler_protocol =
value_policy<Policy> && operations::operation<OpTag> &&
(!std::same_as<CommonRep, void>) && requires {
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::decision_type;
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::result_type;
{
handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled
} -> std::convertible_to<bool>;
{
handler<Policy, OpTag, CommonRep, ErrorPayload>::may_adjust_value
} -> std::convertible_to<bool>;
requires handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled;
requires std::same_as<typename handler<Policy, OpTag, CommonRep,
ErrorPayload>::decision_type,
decision<CommonRep>>;
requires std::same_as<
typename handler<Policy, OpTag, CommonRep, ErrorPayload>::result_type,
std::expected<CommonRep, ErrorPayload>>;
{
handler<Policy, OpTag, CommonRep, ErrorPayload>::finalize(
std::declval<decision<CommonRep>>(),
std::declval<concurrency::injection const &>())
} -> std::same_as<typename handler<Policy, OpTag, CommonRep,
ErrorPayload>::decision_type>;
};
template <typename Policy, typename OpTag, typename CommonRep,
typename ErrorPayload>
concept handler_available =
handler_protocol<Policy, OpTag, CommonRep, ErrorPayload> &&
handler<Policy, OpTag, CommonRep, ErrorPayload>::enabled;
} // namespace value
} // namespace mcpplibs::primitives::policy