-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex06_custom_underlying.cpp
More file actions
295 lines (235 loc) · 8.95 KB
/
ex06_custom_underlying.cpp
File metadata and controls
295 lines (235 loc) · 8.95 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
/*
* Example: ex06_custom_underlying
*
* Purpose:
* Demonstrate custom underlying integration, including rep bridge,
* rep validation, and extensible common_rep negotiation.
*
* Expected results:
* - UserInteger path succeeds and computes 40 + 2 = 42.
* - NonNegativeInt path rejects invalid rep and returns domain_error.
* - TaggedLhs/TaggedRhs path uses custom common_rep_traits to negotiate
* TaggedCommonRep, then computes 40 + 2 = 42.
* - Program prints a success message and exits with code 0.
*/
#include <iostream>
#include <type_traits>
import mcpplibs.primitives;
using namespace mcpplibs::primitives;
// Point 6 / Step 1: Define custom domain value types.
struct UserInteger {
int value;
};
struct NonNegativeInt {
int value;
};
struct TaggedLhs {
int value;
friend constexpr auto operator+(TaggedLhs lhs, TaggedLhs rhs) noexcept
-> TaggedLhs {
return TaggedLhs{lhs.value + rhs.value};
}
friend constexpr auto operator-(TaggedLhs lhs, TaggedLhs rhs) noexcept
-> TaggedLhs {
return TaggedLhs{lhs.value - rhs.value};
}
friend constexpr auto operator*(TaggedLhs lhs, TaggedLhs rhs) noexcept
-> TaggedLhs {
return TaggedLhs{lhs.value * rhs.value};
}
friend constexpr auto operator/(TaggedLhs lhs, TaggedLhs rhs) noexcept
-> TaggedLhs {
return TaggedLhs{lhs.value / rhs.value};
}
friend constexpr auto operator==(TaggedLhs lhs, TaggedLhs rhs) noexcept
-> bool {
return lhs.value == rhs.value;
}
constexpr explicit operator long long() const noexcept {
return static_cast<long long>(value);
}
};
struct TaggedRhs {
int value;
friend constexpr auto operator+(TaggedRhs lhs, TaggedRhs rhs) noexcept
-> TaggedRhs {
return TaggedRhs{lhs.value + rhs.value};
}
friend constexpr auto operator-(TaggedRhs lhs, TaggedRhs rhs) noexcept
-> TaggedRhs {
return TaggedRhs{lhs.value - rhs.value};
}
friend constexpr auto operator*(TaggedRhs lhs, TaggedRhs rhs) noexcept
-> TaggedRhs {
return TaggedRhs{lhs.value * rhs.value};
}
friend constexpr auto operator/(TaggedRhs lhs, TaggedRhs rhs) noexcept
-> TaggedRhs {
return TaggedRhs{lhs.value / rhs.value};
}
friend constexpr auto operator==(TaggedRhs lhs, TaggedRhs rhs) noexcept
-> bool {
return lhs.value == rhs.value;
}
constexpr explicit operator long long() const noexcept {
return static_cast<long long>(value);
}
};
struct TaggedCommonRep {
long long value;
constexpr TaggedCommonRep() noexcept = default;
constexpr explicit TaggedCommonRep(long long v) noexcept : value(v) {}
constexpr explicit TaggedCommonRep(TaggedLhs v) noexcept : value(v.value) {}
constexpr explicit TaggedCommonRep(TaggedRhs v) noexcept : value(v.value) {}
friend constexpr auto operator+(TaggedCommonRep lhs,
TaggedCommonRep rhs) noexcept
-> TaggedCommonRep {
return TaggedCommonRep{lhs.value + rhs.value};
}
friend constexpr auto operator-(TaggedCommonRep lhs,
TaggedCommonRep rhs) noexcept
-> TaggedCommonRep {
return TaggedCommonRep{lhs.value - rhs.value};
}
friend constexpr auto operator*(TaggedCommonRep lhs,
TaggedCommonRep rhs) noexcept
-> TaggedCommonRep {
return TaggedCommonRep{lhs.value * rhs.value};
}
friend constexpr auto operator/(TaggedCommonRep lhs,
TaggedCommonRep rhs) noexcept
-> TaggedCommonRep {
return TaggedCommonRep{lhs.value / rhs.value};
}
friend constexpr auto operator==(TaggedCommonRep lhs,
TaggedCommonRep rhs) noexcept -> bool {
return lhs.value == rhs.value;
}
};
// Point 6 / Step 2: Register underlying::traits for UserInteger.
// This type has a full int bridge and accepts all reps.
template <> struct mcpplibs::primitives::underlying::traits<UserInteger> {
using value_type = UserInteger;
using rep_type = int;
static constexpr bool enabled = true;
static constexpr auto kind = category::integer;
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
return value.value;
}
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
return UserInteger{value};
}
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
};
// Point 6 / Step 3: Register underlying::traits for NonNegativeInt.
// This one demonstrates custom rep validation (rep must be non-negative).
template <> struct mcpplibs::primitives::underlying::traits<NonNegativeInt> {
using value_type = NonNegativeInt;
using rep_type = int;
static constexpr bool enabled = true;
static constexpr auto kind = category::integer;
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
return value.value;
}
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
return NonNegativeInt{value};
}
static constexpr auto is_valid_rep(rep_type value) noexcept -> bool {
return value >= 0;
}
};
template <> struct mcpplibs::primitives::underlying::traits<TaggedLhs> {
using value_type = TaggedLhs;
using rep_type = TaggedLhs;
static constexpr bool enabled = true;
static constexpr auto kind = category::integer;
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
return value;
}
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
return value;
}
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
};
template <> struct mcpplibs::primitives::underlying::traits<TaggedRhs> {
using value_type = TaggedRhs;
using rep_type = TaggedRhs;
static constexpr bool enabled = true;
static constexpr auto kind = category::integer;
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
return value;
}
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
return value;
}
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
};
template <> struct mcpplibs::primitives::underlying::traits<TaggedCommonRep> {
using value_type = TaggedCommonRep;
using rep_type = TaggedCommonRep;
static constexpr bool enabled = true;
static constexpr auto kind = category::integer;
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
return value;
}
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
return value;
}
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
};
template <>
struct mcpplibs::primitives::underlying::common_rep_traits<TaggedLhs,
TaggedRhs> {
using type = TaggedCommonRep;
static constexpr bool enabled = true;
};
template <>
struct mcpplibs::primitives::underlying::common_rep_traits<TaggedRhs,
TaggedLhs> {
using type = TaggedCommonRep;
static constexpr bool enabled = true;
};
int main() {
// Point 6 / Step 4A: Call operations on UserInteger custom underlying.
using user_t =
primitive<UserInteger, policy::value::checked, policy::error::expected>;
auto const ua = user_t{UserInteger{40}};
auto const ub = user_t{UserInteger{2}};
auto const user_result = operations::add(ua, ub);
if (!user_result.has_value() || user_result->value() != 42) {
std::cerr << "custom UserInteger underlying failed\n";
return 1;
}
// Point 6 / Step 4B: Demonstrate dispatcher rejecting invalid reps.
// Here -1 fails is_valid_rep and is mapped to domain_error.
using nonneg_t = primitive<NonNegativeInt, policy::value::checked,
policy::error::expected>;
auto const na = nonneg_t{NonNegativeInt{-1}};
auto const nb = nonneg_t{NonNegativeInt{2}};
auto const nonneg_result = operations::add(na, nb);
if (nonneg_result.has_value() ||
nonneg_result.error() != policy::error::kind::domain_error) {
std::cerr << "invalid rep should be rejected by dispatcher\n";
return 1;
}
// Point 6 / Step 4C: Demonstrate extensible common_rep negotiation.
using lhs_t = primitive<TaggedLhs, policy::value::checked,
policy::type::transparent, policy::error::expected>;
using rhs_t = primitive<TaggedRhs, policy::value::checked,
policy::type::transparent, policy::error::expected>;
using transparent_handler =
policy::type::handler<policy::type::transparent, operations::Addition,
TaggedLhs, TaggedRhs>;
static_assert(transparent_handler::allowed);
static_assert(
std::same_as<typename transparent_handler::common_rep, TaggedCommonRep>);
auto const lhs = lhs_t{TaggedLhs{40}};
auto const rhs = rhs_t{TaggedRhs{2}};
auto const mixed_result = operations::add(lhs, rhs);
if (!mixed_result.has_value() || mixed_result->value().value != 42) {
std::cerr << "custom common_rep negotiation failed\n";
return 1;
}
std::cout << "custom underlying demo passed\n";
return 0;
}