Skip to content

Commit e2fe13b

Browse files
docs: Add documentation and expected results for example files
1 parent 05f68b5 commit e2fe13b

8 files changed

Lines changed: 229 additions & 0 deletions

examples/ex01_default_arithmetic.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
* Example: ex01_default_arithmetic
3+
*
4+
* Purpose:
5+
* Demonstrate the default primitive aliases and operator pipeline for
6+
* arithmetic operations without custom policy overrides.
7+
*
8+
* Expected results:
9+
* - All four operations (+, -, *, /) succeed through dispatcher.
10+
* - The printed values match: sum=42, diff=38, prod=80, quot=20.
11+
* - Program exits with code 0; otherwise prints an error and exits non-zero.
12+
*/
13+
114
#include <cstdint>
215
#include <iostream>
316

examples/ex02_type_policy.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*
2+
* Example: ex02_type_policy
3+
*
4+
* Purpose:
5+
* Compare type negotiation behavior between strict and compatible policies.
6+
*
7+
* Expected results:
8+
* - strict policy rejects mixed reps (int + long long) at compile-time
9+
* negotiation, yielding common_rep=void.
10+
* - compatible policy accepts mixed arithmetic reps and negotiates
11+
* common_rep=long long.
12+
* - Program prints a success message and exits with code 0.
13+
*/
14+
115
#include <iostream>
216
#include <type_traits>
317

examples/ex03_value_policy.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*
2+
* Example: ex03_value_policy
3+
*
4+
* Purpose:
5+
* Show how checked, unchecked, and saturating value policies behave under
6+
* the same overflow input.
7+
*
8+
* Expected results:
9+
* - checked: reports overflow as an error.
10+
* - unchecked: wraps according to native arithmetic semantics.
11+
* - saturating: clamps to the representable upper bound.
12+
* - Program prints observed values and exits with code 0.
13+
*/
14+
115
#include <cstdint>
216
#include <iostream>
317

examples/ex04_error_policy.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*
2+
* Example: ex04_error_policy
3+
*
4+
* Purpose:
5+
* Compare error handling styles (expected vs throwing) under divide-by-zero.
6+
*
7+
* Expected results:
8+
* - expected policy returns error::kind::divide_by_zero.
9+
* - throwing policy throws std::runtime_error.
10+
* - Program validates both paths and exits with code 0.
11+
*/
12+
113
#include <iostream>
214
#include <stdexcept>
315

examples/ex05_concurrency_policy.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
* Example: ex05_concurrency_policy
3+
*
4+
* Purpose:
5+
* Demonstrate the atomic concurrency policy path under multi-threaded
6+
* repeated dispatch.
7+
*
8+
* Expected results:
9+
* - Concurrent add operations consistently produce value 42.
10+
* - mismatch_count remains zero after all worker threads join.
11+
* - Program prints a success message and exits with code 0.
12+
*/
13+
114
#include <atomic>
215
#include <iostream>
316
#include <thread>

examples/ex06_custom_underlying.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1+
/*
2+
* Example: ex06_custom_underlying
3+
*
4+
* Purpose:
5+
* Demonstrate custom underlying integration, including rep bridge,
6+
* rep validation, and extensible common_rep negotiation.
7+
*
8+
* Expected results:
9+
* - UserInteger path succeeds and computes 40 + 2 = 42.
10+
* - NonNegativeInt path rejects invalid rep and returns domain_error.
11+
* - TaggedLhs/TaggedRhs path uses custom common_rep_traits to negotiate
12+
* TaggedCommonRep, then computes 40 + 2 = 42.
13+
* - Program prints a success message and exits with code 0.
14+
*/
15+
116
#include <iostream>
17+
#include <type_traits>
218

319
import mcpplibs.primitives;
420

@@ -13,6 +29,55 @@ struct NonNegativeInt {
1329
int value;
1430
};
1531

32+
struct TaggedLhs {
33+
int value;
34+
35+
constexpr explicit operator long long() const noexcept {
36+
return static_cast<long long>(value);
37+
}
38+
};
39+
40+
struct TaggedRhs {
41+
int value;
42+
43+
constexpr explicit operator long long() const noexcept {
44+
return static_cast<long long>(value);
45+
}
46+
};
47+
48+
struct TaggedCommonRep {
49+
long long value;
50+
51+
friend constexpr auto operator+(TaggedCommonRep lhs,
52+
TaggedCommonRep rhs) noexcept
53+
-> TaggedCommonRep {
54+
return TaggedCommonRep{lhs.value + rhs.value};
55+
}
56+
57+
friend constexpr auto operator-(TaggedCommonRep lhs,
58+
TaggedCommonRep rhs) noexcept
59+
-> TaggedCommonRep {
60+
return TaggedCommonRep{lhs.value - rhs.value};
61+
}
62+
63+
friend constexpr auto operator*(TaggedCommonRep lhs,
64+
TaggedCommonRep rhs) noexcept
65+
-> TaggedCommonRep {
66+
return TaggedCommonRep{lhs.value * rhs.value};
67+
}
68+
69+
friend constexpr auto operator/(TaggedCommonRep lhs,
70+
TaggedCommonRep rhs) noexcept
71+
-> TaggedCommonRep {
72+
return TaggedCommonRep{lhs.value / rhs.value};
73+
}
74+
75+
friend constexpr auto operator==(TaggedCommonRep lhs,
76+
TaggedCommonRep rhs) noexcept -> bool {
77+
return lhs.value == rhs.value;
78+
}
79+
};
80+
1681
// Point 6 / Step 2: Register underlying::traits for UserInteger.
1782
// This type has a full int bridge and accepts all reps.
1883
template <> struct mcpplibs::primitives::underlying::traits<UserInteger> {
@@ -55,6 +120,56 @@ template <> struct mcpplibs::primitives::underlying::traits<NonNegativeInt> {
55120
}
56121
};
57122

123+
template <> struct mcpplibs::primitives::underlying::traits<TaggedLhs> {
124+
using value_type = TaggedLhs;
125+
using rep_type = TaggedLhs;
126+
127+
static constexpr bool enabled = true;
128+
static constexpr auto kind = category::integer;
129+
130+
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
131+
return value;
132+
}
133+
134+
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
135+
return value;
136+
}
137+
138+
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
139+
};
140+
141+
template <> struct mcpplibs::primitives::underlying::traits<TaggedRhs> {
142+
using value_type = TaggedRhs;
143+
using rep_type = TaggedRhs;
144+
145+
static constexpr bool enabled = true;
146+
static constexpr auto kind = category::integer;
147+
148+
static constexpr auto to_rep(value_type value) noexcept -> rep_type {
149+
return value;
150+
}
151+
152+
static constexpr auto from_rep(rep_type value) noexcept -> value_type {
153+
return value;
154+
}
155+
156+
static constexpr auto is_valid_rep(rep_type) noexcept -> bool { return true; }
157+
};
158+
159+
template <>
160+
struct mcpplibs::primitives::underlying::common_rep_traits<TaggedLhs,
161+
TaggedRhs> {
162+
using type = TaggedCommonRep;
163+
static constexpr bool enabled = true;
164+
};
165+
166+
template <>
167+
struct mcpplibs::primitives::underlying::common_rep_traits<TaggedRhs,
168+
TaggedLhs> {
169+
using type = TaggedCommonRep;
170+
static constexpr bool enabled = true;
171+
};
172+
58173
int main() {
59174
// Point 6 / Step 4A: Call operations on UserInteger custom underlying.
60175
using user_t =
@@ -81,6 +196,27 @@ int main() {
81196
return 1;
82197
}
83198

199+
// Point 6 / Step 4C: Demonstrate extensible common_rep negotiation.
200+
using lhs_t = primitive<TaggedLhs, policy::value::checked,
201+
policy::type::transparent, policy::error::expected>;
202+
using rhs_t = primitive<TaggedRhs, policy::value::checked,
203+
policy::type::transparent, policy::error::expected>;
204+
205+
using transparent_handler =
206+
policy::type::handler<policy::type::transparent, operations::Addition,
207+
TaggedLhs, TaggedRhs>;
208+
static_assert(transparent_handler::allowed);
209+
static_assert(
210+
std::same_as<typename transparent_handler::common_rep, TaggedCommonRep>);
211+
212+
auto const lhs = lhs_t{TaggedLhs{40}};
213+
auto const rhs = rhs_t{TaggedRhs{2}};
214+
auto const mixed_result = operations::add(lhs, rhs);
215+
if (!mixed_result.has_value() || mixed_result->value().value != 42) {
216+
std::cerr << "custom common_rep negotiation failed\n";
217+
return 1;
218+
}
219+
84220
std::cout << "custom underlying demo passed\n";
85221
return 0;
86222
}

examples/ex07_custom_policy.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*
2+
* Example: ex07_custom_policy
3+
*
4+
* Purpose:
5+
* Show end-to-end customization of all policy dimensions (type, value,
6+
* error, concurrency) and custom operation binding integration.
7+
*
8+
* Expected results:
9+
* - Custom policy traits are recognized and selected by dispatcher.
10+
* - Custom value finalize step adjusts addition result by +1.
11+
* - Input 20 and 21 produces final value 42.
12+
* - Program prints a success message and exits with code 0.
13+
*/
14+
115
#include <expected>
216
#include <iostream>
317
#include <type_traits>

examples/ex08_custom_operation.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
* Example: ex08_custom_operation
3+
*
4+
* Purpose:
5+
* Demonstrate how to register new operation tags, declare operation traits,
6+
* and provide runtime op_binding specializations.
7+
*
8+
* Expected results:
9+
* - Custom operations Average, GreaterThan, and BitAnd dispatch successfully.
10+
* - For inputs 10 and 6, outputs are avg=8, gt=1, bitand=2.
11+
* - Program prints computed values and exits with code 0.
12+
*/
13+
114
#include <iostream>
215

316
import mcpplibs.primitives;

0 commit comments

Comments
 (0)