Skip to content

Commit e3222e6

Browse files
docs: Add user and API documentation guides in English and Chinese
Signed-off-by: FrozenlemonTee <1115306170@qq.com>
1 parent 32b5300 commit e3222e6

File tree

11 files changed

+684
-0
lines changed

11 files changed

+684
-0
lines changed

docs/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Documentation
2+
3+
## Guide
4+
5+
- [中文用户文档](./guide/zh/README.md)
6+
- [English User Guide](./guide/en/README.md)
7+
8+
## API
9+
10+
- [中文 API 文档](./api/zh/README.md)
11+
- [English API Documentation](./api/en/README.md)
12+

docs/api/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# API Documentation
2+
3+
- [中文 API 文档](./zh/README.md)
4+
- [English API Documentation](./en/README.md)
5+

docs/guide/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# User Guide
2+
3+
- [中文用户文档](./zh/README.md)
4+
- [English User Guide](./en/README.md)
5+

docs/guide/en/concurrency.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Concurrency Guide
2+
3+
## Concurrency Policies
4+
5+
Built-in concurrency policies:
6+
7+
- `policy::concurrency::none`
8+
- `policy::concurrency::fenced`
9+
- `policy::concurrency::fenced_relaxed`
10+
- `policy::concurrency::fenced_acq_rel`
11+
- `policy::concurrency::fenced_seq_cst`
12+
13+
General guidance:
14+
15+
- Use `none` for single-threaded paths.
16+
- Use `fenced` or `fenced_seq_cst` first for simple correctness.
17+
- Use relaxed/acq_rel variants only when you have clear memory-order requirements.
18+
19+
## Shared State with `primitive`
20+
21+
```cpp
22+
using shared_t = mcpplibs::primitives::primitive<
23+
int,
24+
mcpplibs::primitives::policy::value::checked,
25+
mcpplibs::primitives::policy::concurrency::fenced,
26+
mcpplibs::primitives::policy::error::expected>;
27+
28+
shared_t counter{0};
29+
```
30+
31+
Use:
32+
33+
- `counter.load()`
34+
- `counter.store(v)`
35+
- `counter.compare_exchange(expected, desired)`
36+
37+
## CAS Loop Pattern
38+
39+
```cpp
40+
auto expected = counter.load();
41+
while (!counter.compare_exchange(expected, expected + 1)) {
42+
}
43+
```
44+
45+
This is the typical lock-free increment style used in multi-threaded updates.
46+
47+
## Mixed Read/Write Workload
48+
49+
For practical concurrent workloads, combine:
50+
51+
- writer threads updating operands through `store`
52+
- reader threads calling `operations::add` / `operations::sub`
53+
- optional sink updates via `store` and checkpoint `compare_exchange`
54+
55+
See `examples/ex05_concurrency_policy.cpp` for a representative end-to-end workload.
56+
57+
## Pitfalls
58+
59+
### 1) Atomic-ref compatibility
60+
61+
Fenced access handlers require underlying representations to satisfy atomic-ref constraints (trivially copyable and alignment-compatible).
62+
63+
### 2) Treating operations as implicit synchronization
64+
65+
Operation dispatch consistency and concurrency fences are policy-driven. Do not assume synchronization semantics unless explicitly configured.
66+
67+
### 3) Ignoring error paths
68+
69+
Operation results are `std::expected<...>`. Always handle `has_value()`.
70+
71+
## Next
72+
73+
- Extension guide: [./extension.md](./extension.md)
74+
- API concurrency details: [../../api/en/README.md](../../api/en/README.md)
75+

docs/guide/en/extension.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Extension Guide
2+
3+
This page covers extensibility paths used by advanced users.
4+
5+
## 1) Custom Underlying Type
6+
7+
Define a domain type and specialize `underlying::traits<T>`.
8+
9+
Required members:
10+
11+
- `value_type`
12+
- `rep_type`
13+
- `static constexpr bool enabled`
14+
- `static constexpr underlying::category kind`
15+
- `to_rep(value)`
16+
- `from_rep(rep)`
17+
- `is_valid_rep(rep)`
18+
19+
After registration, your type can be used as `primitive<YourType, ...>` if it satisfies `underlying_type`.
20+
21+
Reference example: `examples/ex06_custom_underlying.cpp`.
22+
23+
## 2) Custom Common Rep Negotiation
24+
25+
If default `std::common_type_t` is not suitable, specialize:
26+
27+
```cpp
28+
template <>
29+
struct mcpplibs::primitives::underlying::common_rep_traits<LhsRep, RhsRep> {
30+
using type = YourCommonRep;
31+
static constexpr bool enabled = true;
32+
};
33+
```
34+
35+
This affects mixed dispatch and type negotiation.
36+
37+
## 3) Custom Policy Tags and Handlers
38+
39+
Custom policies require:
40+
41+
1. Registering tags via `policy::traits<YourPolicyTag>`.
42+
2. Providing protocol specializations in the correct namespaces:
43+
- `policy::type::handler`
44+
- `policy::value::handler`
45+
- `policy::error::handler`
46+
- `policy::concurrency::handler`
47+
48+
If your value policy needs operation runtime behavior, also provide `operations::runtime::op_binding` specializations.
49+
50+
Reference example: `examples/ex07_custom_policy.cpp`.
51+
52+
## 4) Custom Operation Tags
53+
54+
To add new operation tags:
55+
56+
1. Define operation tag type.
57+
2. Specialize `operations::traits<OpTag>` with:
58+
- `enabled = true`
59+
- `arity`
60+
- `capability_mask`
61+
3. Provide `operations::runtime::op_binding<OpTag, ValuePolicy, CommonRep>`.
62+
4. Invoke via `operations::apply<OpTag>(lhs, rhs)`.
63+
64+
Reference example: `examples/ex08_custom_operation.cpp`.
65+
66+
## Extension Checklist
67+
68+
- Policy groups are consistent across operands.
69+
- Operation capability metadata is valid.
70+
- Value policy + operation binding exists for runtime dispatch.
71+
- Error policy handler returns expected payload type.
72+
- Concurrency access handler exists if you need `load/store/CAS`.
73+
74+
## Next
75+
76+
- API details for protocol contracts: [../../api/en/README.md](../../api/en/README.md)
77+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Installation and Build
2+
3+
## Prerequisites
4+
5+
- C++23-capable compiler
6+
- GCC >= 14
7+
- Clang >= 17
8+
- MSVC >= 19.34 (MSVC version >= 1934)
9+
- Build tools
10+
- xmake (recommended)
11+
- or CMake + Ninja
12+
13+
## Build with xmake
14+
15+
Build the library:
16+
17+
```bash
18+
xmake build mcpplibs-primitives
19+
```
20+
21+
Run an example:
22+
23+
```bash
24+
xmake run ex01_default_arithmetic
25+
```
26+
27+
Run tests:
28+
29+
```bash
30+
xmake run primitives_test
31+
```
32+
33+
Compatibility alias:
34+
35+
```bash
36+
xmake run basic
37+
```
38+
39+
`basic` runs `ex01_default_arithmetic`.
40+
41+
## Build with CMake
42+
43+
Configure and build:
44+
45+
```bash
46+
cmake -B build -G Ninja
47+
cmake --build build --target mcpplibs-primitives
48+
```
49+
50+
Build an example and tests:
51+
52+
```bash
53+
cmake --build build --target ex01_default_arithmetic
54+
cmake --build build --target basic_tests
55+
ctest --test-dir build --output-on-failure
56+
```
57+
58+
## Example Targets
59+
60+
Examples are available as independent targets:
61+
62+
- `ex01_default_arithmetic`
63+
- `ex02_type_policy`
64+
- `ex03_value_policy`
65+
- `ex04_error_policy`
66+
- `ex05_concurrency_policy`
67+
- `ex06_custom_underlying`
68+
- `ex07_custom_policy`
69+
- `ex08_custom_operation`
70+
71+
## Common Build Issues
72+
73+
### 1) C++ standard or modules not enabled
74+
75+
Ensure C++23 is enabled and your compiler supports C++ modules.
76+
77+
### 2) Compiler version too old
78+
79+
Use the minimum versions listed above. For GCC/Clang/MSVC, older versions can fail early at configuration or module compilation.
80+
81+
### 3) Failing to find test dependencies
82+
83+
The test target depends on GoogleTest. Use xmake package resolution or ensure CMake can fetch/access the configured repository.
84+
85+
## Next
86+
87+
- Continue with [Quick Start](./quick-start.md)
88+
- API details: [../../api/en/README.md](../../api/en/README.md)
89+

docs/guide/en/quick-start.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Quick Start
2+
3+
## Minimal Program
4+
5+
```cpp
6+
import std;
7+
import mcpplibs.primitives;
8+
9+
using namespace mcpplibs::primitives;
10+
using namespace mcpplibs::primitives::operators;
11+
12+
int main() {
13+
using value_t =
14+
primitive<int, policy::value::checked, policy::error::expected>;
15+
16+
auto const out = value_t{40} + value_t{2};
17+
if (!out.has_value()) {
18+
return 1;
19+
}
20+
return out->value() == 42 ? 0 : 1;
21+
}
22+
```
23+
24+
Key point: arithmetic and operator APIs typically return `std::expected<...>`.
25+
26+
## First Policy Setup
27+
28+
Recommended starting combination:
29+
30+
- `policy::value::checked`
31+
- `policy::error::expected`
32+
- default `type::strict`
33+
- default `concurrency::none`
34+
35+
This gives safe arithmetic and explicit error handling without requiring exceptions.
36+
37+
## Learn by Examples
38+
39+
Run examples in this order:
40+
41+
1. `ex01_default_arithmetic`
42+
2. `ex02_type_policy`
43+
3. `ex03_value_policy`
44+
4. `ex04_error_policy`
45+
5. `ex05_concurrency_policy`
46+
6. `ex06_custom_underlying`
47+
7. `ex07_custom_policy`
48+
8. `ex08_custom_operation`
49+
50+
Command:
51+
52+
```bash
53+
xmake run ex03_value_policy
54+
```
55+
56+
## Common Usage Snippets
57+
58+
### Default primitive
59+
60+
```cpp
61+
using default_i32 = mcpplibs::primitives::primitive<int>;
62+
```
63+
64+
### Mixed primitive/underlying operation
65+
66+
```cpp
67+
using value_t = mcpplibs::primitives::primitive<
68+
int,
69+
mcpplibs::primitives::policy::type::compatible,
70+
mcpplibs::primitives::policy::error::expected>;
71+
72+
auto const lhs = value_t{40};
73+
short const rhs = 2;
74+
auto const out = mcpplibs::primitives::operations::add(lhs, rhs);
75+
```
76+
77+
### Operators namespace
78+
79+
```cpp
80+
using namespace mcpplibs::primitives::operators;
81+
```
82+
83+
Without this namespace, call function APIs via `mcpplibs::primitives::operations`.
84+
85+
## Next
86+
87+
- Concurrency usage: [./concurrency.md](./concurrency.md)
88+
- Extension guide: [./extension.md](./extension.md)
89+
- API reference: [../../api/en/README.md](../../api/en/README.md)
90+

0 commit comments

Comments
 (0)