-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpl.cppm
More file actions
313 lines (264 loc) · 10.5 KB
/
impl.cppm
File metadata and controls
313 lines (264 loc) · 10.5 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
module;
#include <cstddef>
#include <cstdint>
#include <tuple>
#include <type_traits>
export module mcpplibs.primitives.primitive.impl;
import mcpplibs.primitives.conversion.underlying;
import mcpplibs.primitives.underlying.traits;
import mcpplibs.primitives.policy.traits;
import mcpplibs.primitives.policy.handler;
import mcpplibs.primitives.policy.impl;
import mcpplibs.primitives.policy.utility;
export namespace mcpplibs::primitives {
template <underlying_type T, policy::policy_type... Policies> class primitive {
public:
using value_type = T;
using policies = std::tuple<Policies...>;
private:
using type_policy_t =
policy::resolve_policy_t<policy::category::type, Policies...>;
using value_rep_type = underlying::traits<value_type>::rep_type;
template <underlying_type Source, underlying_type Target>
static constexpr bool policy_allows_underlying_bridge_v = [] {
using source_rep_type =
underlying::traits<std::remove_cv_t<Source>>::rep_type;
using target_rep_type =
underlying::traits<std::remove_cv_t<Target>>::rep_type;
if constexpr (std::same_as<type_policy_t, policy::type::strict>) {
return false;
} else if constexpr (std::same_as<type_policy_t, policy::type::compatible>) {
return underlying::traits<std::remove_cv_t<Source>>::kind ==
underlying::traits<std::remove_cv_t<Target>>::kind &&
has_common_rep<source_rep_type, target_rep_type> &&
!std::same_as<common_rep_t<source_rep_type, target_rep_type>, void>;
} else if constexpr (std::same_as<type_policy_t,
policy::type::transparent>) {
return has_common_rep<source_rep_type, target_rep_type> &&
!std::same_as<common_rep_t<source_rep_type, target_rep_type>, void>;
} else {
return false;
}
}();
template <underlying_type U>
static constexpr bool cross_underlying_constructible_v =
policy_allows_underlying_bridge_v<U, value_type>;
template <underlying_type U>
static constexpr bool cross_underlying_exchangeable_v =
cross_underlying_constructible_v<U> &&
policy_allows_underlying_bridge_v<value_type, U>;
template <underlying_type Target, underlying_type Source>
static constexpr auto convert_underlying_(Source source) noexcept -> Target {
using source_value_type = std::remove_cv_t<Source>;
using source_rep_type = underlying::traits<source_value_type>::rep_type;
using target_rep_type =
underlying::traits<std::remove_cv_t<Target>>::rep_type;
auto const source_rep = underlying::traits<source_value_type>::to_rep(source);
auto const target_rep =
conversion::saturating_cast<target_rep_type>(
static_cast<source_rep_type>(source_rep));
return underlying::traits<std::remove_cv_t<Target>>::from_rep(target_rep);
}
public:
template <underlying_type U>
requires std::same_as<U, value_type>
constexpr explicit primitive(U u) noexcept : value_(u) {}
template <underlying_type U>
requires(!std::same_as<U, value_type> &&
cross_underlying_constructible_v<U>)
constexpr explicit primitive(U u) noexcept
: value_(convert_underlying_<value_type>(u)) {}
constexpr primitive(primitive const &other) noexcept {
if consteval {
value_ = other.value_;
} else {
value_ = other.load();
}
}
template <underlying_type U>
requires(!std::same_as<U, value_type>)
explicit constexpr primitive(primitive<U, Policies...> const &other) noexcept
requires(cross_underlying_constructible_v<U>)
: value_(convert_underlying_<value_type>(other.load())) {}
constexpr auto operator=(primitive const &other) noexcept -> primitive & {
if (this == &other) {
return *this;
}
if consteval {
value_ = other.value_;
} else {
store(other.load());
}
return *this;
}
template <underlying_type U>
requires(!std::same_as<U, value_type>)
constexpr auto
operator=(primitive<U, Policies...> const &other) noexcept -> primitive &
requires(cross_underlying_constructible_v<U>) {
store(convert_underlying_<value_type>(other.load()));
return *this;
}
constexpr primitive(primitive &&other) noexcept {
if consteval {
value_ = other.value_;
} else {
value_ = other.load();
}
}
template <underlying_type U>
requires(!std::same_as<U, value_type>)
explicit constexpr primitive(primitive<U, Policies...> &&other) noexcept
requires(cross_underlying_constructible_v<U>)
: value_(convert_underlying_<value_type>(other.load())) {}
constexpr auto operator=(primitive &&other) noexcept -> primitive & {
if (this == &other) {
return *this;
}
if consteval {
value_ = other.value_;
} else {
store(other.load());
}
return *this;
}
template <underlying_type U>
requires(!std::same_as<U, value_type>)
constexpr auto operator=(primitive<U, Policies...> &&other) noexcept
-> primitive &
requires(cross_underlying_constructible_v<U>) {
store(convert_underlying_<value_type>(other.load()));
return *this;
}
constexpr value_type &value() noexcept { return value_; }
[[nodiscard]] constexpr value_type const &value() const noexcept {
return value_;
}
constexpr explicit operator value_type() const noexcept { return value_; }
[[nodiscard]] constexpr auto load() const noexcept -> value_type {
if consteval {
return value_;
}
require_access_handler_();
return access_handler_t::load(value_);
}
template <underlying_type U>
requires std::same_as<U, value_type>
constexpr auto store(U desired) noexcept -> void {
if consteval {
value_ = desired;
} else {
require_access_handler_();
access_handler_t::store(value_, desired);
}
}
template <underlying_type U>
requires(!std::same_as<U, value_type> &&
cross_underlying_constructible_v<U>)
constexpr auto store(U desired) noexcept -> void {
store(convert_underlying_<value_type>(desired));
}
template <underlying_type U>
requires std::same_as<U, value_type>
constexpr auto compare_exchange(U &expected, U desired) noexcept -> bool {
if consteval {
if (value_ != expected) {
expected = value_;
return false;
}
value_ = desired;
return true;
}
require_access_handler_();
return access_handler_t::compare_exchange(value_, expected, desired);
}
template <underlying_type U>
requires(!std::same_as<U, value_type> && cross_underlying_exchangeable_v<U>)
constexpr auto compare_exchange(U &expected, U desired) noexcept -> bool {
auto expected_native = convert_underlying_<value_type>(expected);
auto const desired_native = convert_underlying_<value_type>(desired);
auto const exchanged = compare_exchange(expected_native, desired_native);
if (!exchanged) {
expected = convert_underlying_<U>(expected_native);
}
return exchanged;
}
private:
using concurrency_policy_t = policy::resolve_policy_t<
policy::category::concurrency, Policies...>;
using access_handler_t =
policy::concurrency::handler<concurrency_policy_t, void, value_type,
policy::error::kind>;
static constexpr auto require_access_handler_() noexcept -> void {
static_assert(
policy::concurrency::handler_access_available<concurrency_policy_t,
value_type>,
"Selected concurrency policy does not provide primitive "
"load/store/CAS support");
}
value_type value_;
};
template <policy::policy_type... Policies, underlying_type T>
constexpr auto with(T value) noexcept(
noexcept(primitive<std::remove_cv_t<T>, Policies...>{value}))
-> primitive<std::remove_cv_t<T>, Policies...> {
return primitive<std::remove_cv_t<T>, Policies...>{value};
}
template <policy::policy_type... Policies, underlying_type T>
constexpr auto with(std::tuple<Policies...>, T value) noexcept(
noexcept(primitive<std::remove_cv_t<T>, Policies...>{value}))
-> primitive<std::remove_cv_t<T>, Policies...> {
return primitive<std::remove_cv_t<T>, Policies...>{value};
}
namespace types {
template <policy::policy_type... Policies>
using Bool = primitive<bool, Policies...>;
template <policy::policy_type... Policies>
using UChar = primitive<unsigned char, Policies...>;
template <policy::policy_type... Policies>
using Char8 = primitive<char8_t, Policies...>;
template <policy::policy_type... Policies>
using Char16 = primitive<char16_t, Policies...>;
template <policy::policy_type... Policies>
using Char32 = primitive<char32_t, Policies...>;
template <policy::policy_type... Policies>
using WChar = primitive<wchar_t, Policies...>;
template <policy::policy_type... Policies>
using U8 = primitive<std::uint8_t, Policies...>;
template <policy::policy_type... Policies>
using U16 = primitive<std::uint16_t, Policies...>;
template <policy::policy_type... Policies>
using U32 = primitive<std::uint32_t, Policies...>;
template <policy::policy_type... Policies>
using U64 = primitive<std::uint64_t, Policies...>;
template <policy::policy_type... Policies>
using Size = primitive<std::size_t, Policies...>;
template <policy::policy_type... Policies>
using Diff = primitive<std::ptrdiff_t, Policies...>;
template <policy::policy_type... Policies>
using I8 = primitive<std::int8_t, Policies...>;
template <policy::policy_type... Policies>
using I16 = primitive<std::int16_t, Policies...>;
template <policy::policy_type... Policies>
using I32 = primitive<std::int32_t, Policies...>;
template <policy::policy_type... Policies>
using I64 = primitive<std::int64_t, Policies...>;
template <policy::policy_type... Policies>
using F32 = primitive<float, Policies...>;
template <policy::policy_type... Policies>
using F64 = primitive<double, Policies...>;
template <policy::policy_type... Policies>
using F80 = primitive<long double, Policies...>;
} // namespace types
} // namespace mcpplibs::primitives
export template <mcpplibs::primitives::underlying_type T, mcpplibs::primitives::policy::policy_type... Policies>
struct mcpplibs::primitives::underlying::traits<mcpplibs::primitives::primitive<T, Policies...>>
{
using value_type = void;
using rep_type = void;
static constexpr bool enabled = false;
static constexpr auto kind = static_cast<category>(-1);
template <typename U> static constexpr rep_type to_rep(U) noexcept = delete;
template <typename U> static constexpr value_type from_rep(U) noexcept = delete;
template <typename U> static constexpr bool is_valid_rep(U) noexcept = delete;
};