Skip to content

Commit 6020dbb

Browse files
docs: Add API documentation for primitives module in English and Chinese
Signed-off-by: FrozenlemonTee <1115306170@qq.com>
1 parent 2ee202a commit 6020dbb

File tree

2 files changed

+492
-0
lines changed

2 files changed

+492
-0
lines changed

docs/api/en/README.md

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
# primitives API Documentation
2+
3+
> Module entry: `import mcpplibs.primitives;`
4+
5+
## Module Entry
6+
7+
Top-level import:
8+
9+
```cpp
10+
import mcpplibs.primitives;
11+
```
12+
13+
This module exports:
14+
15+
- `mcpplibs.primitives.underlying`
16+
- `mcpplibs.primitives.policy`
17+
- `mcpplibs.primitives.primitive`
18+
- `mcpplibs.primitives.operations`
19+
20+
## Namespace Overview
21+
22+
- `mcpplibs::primitives`: core types, concepts, and `primitive`.
23+
- `mcpplibs::primitives::underlying`: underlying traits and categories.
24+
- `mcpplibs::primitives::policy`: policy tags, defaults, and protocols.
25+
- `mcpplibs::primitives::operations`: functional dispatch API.
26+
- `mcpplibs::primitives::operators`: operator overload entry.
27+
- `mcpplibs::primitives::meta`: primitive metadata traits.
28+
- `mcpplibs::primitives::types`: convenience aliases for common underlying types.
29+
30+
## Core Concepts and Metadata
31+
32+
### underlying concepts
33+
34+
- `underlying_type<T>`
35+
- `boolean_underlying_type<T>`
36+
- `character_underlying_type<T>`
37+
- `integer_underlying_type<T>`
38+
- `floating_underlying_type<T>`
39+
- `numeric_underlying_type<T>`
40+
- `has_common_rep<LhsRep, RhsRep>`
41+
- `common_rep_t<LhsRep, RhsRep>`
42+
43+
### policy concepts
44+
45+
- `policy::policy_type<P>`
46+
- `policy::value_policy<P>`
47+
- `policy::type_policy<P>`
48+
- `policy::error_policy<P>`
49+
- `policy::concurrency_policy<P>`
50+
- `policy::resolve_policy_t<category, Policies...>`
51+
52+
### primitive metadata
53+
54+
- `meta::traits<primitive<...>>`
55+
exposes `value_type / policies / value_policy / type_policy / error_policy / concurrency_policy`
56+
- `meta::make_primitive_t<T, PoliciesTuple>`
57+
58+
## `primitive<T, Policies...>`
59+
60+
```cpp
61+
template <underlying_type T, policy::policy_type... Policies>
62+
class primitive;
63+
```
64+
65+
### Member types
66+
67+
- `value_type = T`
68+
- `policies = std::tuple<Policies...>`
69+
70+
### Construction and assignment
71+
72+
- `explicit primitive(T)`: same-underlying construction.
73+
- `explicit primitive(U)`: cross-underlying construction (subject to type policy).
74+
- Copy/move construction and assignment are supported for the same policy set.
75+
- Cross-underlying primitive construction/assignment is supported when allowed by type policy.
76+
77+
### Value and concurrency access APIs
78+
79+
- `value()` / `value() const`
80+
- `explicit operator value_type() const`
81+
- `load() -> value_type`
82+
- `store(desired)`
83+
- `compare_exchange(expected, desired) -> bool`
84+
85+
Notes:
86+
87+
- `store/compare_exchange` always support same-underlying types.
88+
- Cross-underlying `store/compare_exchange` are available only when type policy allows.
89+
- Access APIs are provided by `policy::concurrency::handler<..., void, ...>`.
90+
91+
### Type policy impact on cross-underlying behavior
92+
93+
- `policy::type::strict`: only identical types are allowed.
94+
- `policy::type::compatible`: requires same underlying category and a valid `common_rep`.
95+
- `policy::type::transparent`: only requires a valid `common_rep`.
96+
97+
### Convenience aliases (`types`)
98+
99+
- Integers: `U8/U16/U32/U64`, `I8/I16/I32/I64`
100+
- Floating-point: `F32/F64/F80`
101+
- Bool/chars: `Bool/UChar/Char8/Char16/Char32/WChar`
102+
103+
Example:
104+
105+
```cpp
106+
using value_t = mcpplibs::primitives::types::I32<
107+
mcpplibs::primitives::policy::value::checked,
108+
mcpplibs::primitives::policy::error::expected>;
109+
```
110+
111+
## `operations` API
112+
113+
The `operations` namespace provides function-style APIs with unified `std::expected` results.
114+
115+
### Common result aliases
116+
117+
- `primitive_dispatch_result_t<OpTag, Lhs, Rhs, ErrorPayload>`
118+
- `mixed_primitive_dispatch_result_t<OpTag, Primitive, Underlying, ErrorPayload>`
119+
- `three_way_dispatch_result_t<Lhs, Rhs, ErrorPayload>`
120+
121+
### Unary operations
122+
123+
- `increment`
124+
- `decrement`
125+
- `bit_not`
126+
- `unary_plus`
127+
- `unary_minus`
128+
129+
### Binary arithmetic
130+
131+
- `add`
132+
- `sub`
133+
- `mul`
134+
- `div`
135+
- `mod`
136+
137+
### Binary bitwise
138+
139+
- `shift_left`
140+
- `shift_right`
141+
- `bit_and`
142+
- `bit_or`
143+
- `bit_xor`
144+
145+
### Comparisons
146+
147+
- `equal`
148+
- `not_equal`
149+
- `three_way_compare`
150+
151+
### Compound assignments
152+
153+
- `add_assign/sub_assign/mul_assign/div_assign/mod_assign`
154+
- `shift_left_assign/shift_right_assign`
155+
- `bit_and_assign/bit_or_assign/bit_xor_assign`
156+
157+
### Mixed operand support
158+
159+
Most binary operations support:
160+
161+
- `primitive op primitive`
162+
- `primitive op underlying`
163+
- `underlying op primitive`
164+
165+
## `operators` overloads
166+
167+
To use `+ - * / % ...` syntax, import:
168+
169+
```cpp
170+
using namespace mcpplibs::primitives::operators;
171+
```
172+
173+
Operator results are still `std::expected<...>`, not raw values.
174+
175+
## policy API
176+
177+
### Built-in policy tags
178+
179+
- Value: `policy::value::{checked, unchecked, saturating}`
180+
- Type: `policy::type::{strict, compatible, transparent}`
181+
- Error: `policy::error::{throwing, expected, terminate}`
182+
- Concurrency: `policy::concurrency::{none, fenced, fenced_relaxed, fenced_acq_rel, fenced_seq_cst}`
183+
184+
### Defaults
185+
186+
- `policy::defaults::value = policy::value::checked`
187+
- `policy::defaults::type = policy::type::strict`
188+
- `policy::defaults::error = policy::error::throwing`
189+
- `policy::defaults::concurrency = policy::concurrency::none`
190+
191+
### Error kinds
192+
193+
`policy::error::kind`:
194+
195+
- `none`
196+
- `invalid_type_combination`
197+
- `overflow`
198+
- `underflow`
199+
- `divide_by_zero`
200+
- `domain_error`
201+
- `unspecified`
202+
203+
## underlying Extension Points
204+
205+
### 1) Register `underlying::traits<T>`
206+
207+
Required members:
208+
209+
- `value_type`
210+
- `rep_type`
211+
- `enabled`
212+
- `kind`
213+
- `to_rep(value)`
214+
- `from_rep(rep)`
215+
- `is_valid_rep(rep)`
216+
217+
### 2) Customize common-rep negotiation
218+
219+
Specialize `underlying::common_rep_traits<LhsRep, RhsRep>` to override the default `std::common_type_t` behavior.
220+
221+
## Result and Error Model
222+
223+
- Most operations/operators APIs return `std::expected<...>`.
224+
- With `policy::error::expected`, failures are returned as `unexpected(policy::error::kind)`.
225+
- With `policy::error::throwing`, runtime failures throw exceptions (while API signatures still use `std::expected`).
226+
227+
## Minimal Example
228+
229+
```cpp
230+
import std;
231+
import mcpplibs.primitives;
232+
233+
using namespace mcpplibs::primitives;
234+
using namespace mcpplibs::primitives::operators;
235+
236+
int main() {
237+
using value_t =
238+
primitive<int, policy::value::checked, policy::error::expected>;
239+
240+
auto const out = value_t{40} + value_t{2};
241+
if (!out.has_value()) {
242+
return 1;
243+
}
244+
return out->value() == 42 ? 0 : 1;
245+
}
246+
```

0 commit comments

Comments
 (0)