Skip to content

Commit 81eecec

Browse files
docs: Update README and installation guide with new namespaces and features
Signed-off-by: FrozenlemonTee <1115306170@qq.com>
1 parent c71e282 commit 81eecec

7 files changed

Lines changed: 197 additions & 47 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| [中文](README.zh.md) - [English](README.md) - [Forum](https://mcpp.d2learn.org/forum) |
1010
|---------------------------------------------------------------------------------|
1111
| [用户文档](docs/guide/zh/README.md) - [User Documentation](docs/guide/en/README.md) |
12-
| [API文档](docs/guide/api/README.md) - [API Documentation](docs/api/en/README.md) |
12+
| [API文档](docs/api/zh/README.md) - [API Documentation](docs/api/en/README.md) |
1313

1414
This repository provides configurable `primitive` infrastructure (`underlying traits`, `policy`, and `operations/dispatcher`) to unify numeric behavior, error handling, and concurrency access semantics.
1515

@@ -161,7 +161,7 @@ xmake run ex05_concurrency_policy
161161
xmake run primitives_test
162162
```
163163

164-
**Using CMake**
164+
**Using CMake** (`CMake >= 3.31`)
165165

166166
```bash
167167
cmake -B build -G Ninja

README.zh.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
# mcpplibs primitives
22

3-
> C++23 模块化原语库 - `import mcpplibs.primitives;`
3+
> C++23 模块化 primitives 库 - `import mcpplibs.primitives;`
44
55
[![d2x](https://img.shields.io/badge/d2x-ok-green.svg)](https://github.com/d2learn/d2x)
66
[![Online-ebook](https://img.shields.io/badge/online-ebook-orange.svg)](https://github.com/d2learn/d2x)
77
[![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](LICENSE-CODE)
88

99
| [中文](README.zh.md) - [English](README.md) - [论坛](https://mcpp.d2learn.org/forum) |
1010
|----------------------------------------------------------------------------------|
11-
| [用户文档](docs/guide/zh/README.md) - [User Documentation](docs/guide/en/README.md) |
12-
| [API文档](docs/guide/api/README.md) - [API Documentation](docs/api/en/README.md) |
11+
| [用户文档](docs/guide/zh/README.md) - [User Documentation](docs/guide/en/README.md) |
12+
| [API文档](docs/api/zh/README.md) - [API Documentation](docs/api/en/README.md) |
1313

14-
本仓库提供可配置的 `primitive` 基础设施(`underlying traits``policy``operations/dispatcher`),用于统一约束数值计算、错误处理与并发访问语义。
14+
本仓库提供可配置的 `primitive` 基础设施(`underlying traits``policy``operations/dispatcher`),用于统一数值行为、错误处理与并发访问语义。
1515

1616
> [!WARNING]
17-
> 当前项目仍在快速演进中,API 可能发生变更
17+
> 项目仍在快速演进中,API 可能继续调整
1818
1919
## 特性
2020

21-
- **C++23 模块** `import mcpplibs.primitives;`
22-
- **双构建系统** 同时支持 xmake 和 CMake
23-
- **策略驱动行为** — 值/类型/错误/并发策略可组合配置
24-
- **混合运算支持**支持 `primitive``underlying` 的混合二元运算
25-
- **并发访问接口** `primitive::load/store/compare_exchange`
21+
- **C++23 模块**: `import mcpplibs.primitives;`
22+
- **双构建系统**: 同时支持 xmake 和 CMake
23+
- **策略驱动行为**: value/type/error/concurrency 策略可组合配置
24+
- **混合运算支持**: 支持 `primitive``underlying` 的二元运算
25+
- **并发访问接口**: `primitive::load/store/compare_exchange`
2626

2727
## Operators
2828

29-
该库为 `primitive` 提供了常见的一元、算术、位运算与比较操作。
30-
算术结果通过统一分发链路返回 `std::expected<..., policy::error::kind>`
29+
该库为 `primitive` 提供常见的一元、算术、位运算和比较操作。
30+
算术路径通过统一分发链路返回 `std::expected<..., policy::error::kind>`
3131

32-
- 值策略(`policy::value::checked` / `policy::value::saturating` / `policy::value::unchecked`决定溢出行为;
33-
- 错误策略(`policy::error::throwing` / `policy::error::expected` / `policy::error::terminate`决定错误传播方式
32+
- 值策略(`policy::value::checked` / `policy::value::saturating` / `policy::value::unchecked`定义溢出行为。
33+
- 错误策略(`policy::error::throwing` / `policy::error::expected` / `policy::error::terminate`定义错误传播方式
3434

3535
示例:
3636

@@ -53,24 +53,24 @@ auto maybe_overflow =
5353
5454
## Policy 协议命名空间
5555
56-
自定义 policy 时,协议入口按职责拆分到子命名空间
56+
实现自定义 policy 时,协议入口按职责拆分为以下命名空间
5757
5858
- `policy::type::handler` / `policy::type::handler_available`
5959
- `policy::concurrency::handler` / `policy::concurrency::injection`
6060
- `policy::value::handler` / `policy::value::decision`
6161
- `policy::error::handler` / `policy::error::request` / `policy::error::kind`
6262
63-
预设 policy 标签:
63+
内置 policy 标签:
6464
6565
- `policy::value::{checked, unchecked, saturating}`
6666
- `policy::type::{strict, compatible, transparent}`
6767
- `policy::error::{throwing, expected, terminate}`
6868
- `policy::concurrency::{none, fenced, fenced_relaxed, fenced_acq_rel, fenced_seq_cst}`
6969
70-
并发策略说明
70+
并发说明
7171
72-
- `fenced*` 系列是操作级并发语义,通过策略注入内存序 fence
73-
- `primitive` 存储仍保持统一、零额外存储抽象;
72+
- `fenced*` 系列提供操作级并发语义,并通过策略注入内存序 fence
73+
- `primitive` 存储保持统一的零额外存储抽象。
7474
- `primitive::load/store/compare_exchange` 由并发策略协议提供,若策略未实现会在编译期报错。
7575
7676
示例(并发访问 API):
@@ -98,16 +98,16 @@ if (v.compare_exchange(expected, 3)) {
9898

9999
## 示例程序
100100

101-
- `ex01_basic_usage`: 演示 literal 与 primitive 工厂函数联合使用,并覆盖更多内置操作符。
102-
- `ex02_type_policy`: 展示 `strict/compatible` 的类型协商差异,并包含 `underlying` 构造路径对 type 策略的影响。
103-
- `ex03_value_policy`: 展示 `checked/unchecked/saturating`,并包含与 `underlying` 的混合二元运算行为
104-
- `ex04_error_policy`: 展示不同 error 策略的处理方式
105-
- `ex05_concurrency_policy`: 读写组合并发场景(writer `store` + reader `add/sub` + `CAS`示例
106-
- `ex06_conversion`: 展示 underlying 与 primitive 之间的 checked/saturating/truncating/exact 转换
107-
- `ex07_algorithms`: 展示 limits、特殊数值与 hash 等 algorithms 辅助接口
108-
- `ex08_custom_underlying`: 自定义 underlying traits、rep 校验与 common rep 扩展。
109-
- `ex09_custom_policy`: 自定义策略协议实现示例
110-
- `ex10_custom_operation`: 自定义 operation 扩展示例
101+
- `ex01_basic_usage`: 展示 literal 与 primitive 工厂函数联合使用,并覆盖更多内置操作符。
102+
- `ex02_type_policy`: 展示 `strict/compatible` 的类型协商差异,以及 `underlying` 构造对 type 策略的影响。
103+
- `ex03_value_policy`: 展示 `checked/unchecked/saturating` 行为,以及与 `underlying` 的混合二元运算
104+
- `ex04_error_policy`: 展示不同 error 策略下的错误处理方式
105+
- `ex05_concurrency_policy`: 展示典型的读写混合并发场景(writer `store` + reader `add/sub` + `CAS`)。
106+
- `ex06_conversion`: 展示 underlying 与 primitive 之间的 `checked/saturating/truncating/exact` 转换辅助接口
107+
- `ex07_algorithms`: 展示 limits 元信息、特殊数值和哈希辅助接口
108+
- `ex08_custom_underlying`: 展示自定义 underlying traits、rep 校验和 common rep 扩展。
109+
- `ex09_custom_policy`: 展示自定义 policy 协议实现
110+
- `ex10_custom_operation`: 展示自定义 operation 扩展
111111

112112
## 项目结构
113113

@@ -120,7 +120,7 @@ mcpplibs-primitives/
120120
│ ├── operations/ # operation tags / dispatcher / operators
121121
│ └── underlying/ # underlying traits 与 common_rep
122122
├── examples/ # 示例程序
123-
├── tests/ # 测试入口与 basic 测试集
123+
├── tests/ # 测试入口与基础测试集
124124
├── xmake.lua # xmake 构建脚本
125125
├── CMakeLists.txt # CMake 构建脚本
126126
└── .xlings.json # xlings 包描述文件
@@ -153,15 +153,15 @@ xlings install
153153

154154
```bash
155155
xmake build mcpplibs-primitives
156-
xmake run basic # 等价于 ex01_basic_usage
156+
xmake run basic # ex01_basic_usage 的兼容别名
157157
xmake run ex01_basic_usage
158158
xmake run ex06_conversion
159159
xmake run ex07_algorithms
160160
xmake run ex05_concurrency_policy
161161
xmake run primitives_test
162162
```
163163

164-
**使用 CMake**
164+
**使用 CMake**`CMake >= 3.31`
165165

166166
```bash
167167
cmake -B build -G Ninja
@@ -192,8 +192,8 @@ target("myapp")
192192

193193
## 相关链接
194194

195-
- [mcpp-style-ref | 现代 C++ 编码/项目风格参考](https://github.com/mcpp-community/mcpp-style-ref)
196-
- [d2mystl | 从零实现一个迷你STL库](https://github.com/mcpp-community/d2mystl)
197-
- [mcpp 社区官网](https://mcpp.d2learn.org)
198-
- [mcpp | 现代 C++ 爱好者论坛](https://mcpp.d2learn.org/forum)
199-
- [入门教程: 动手学现代 C++](https://github.com/Sunrisepeak/mcpp-standard)
195+
- [mcpp-style-ref | 现代 C++ 编码与项目风格参考](https://github.com/mcpp-community/mcpp-style-ref)
196+
- [d2mystl | 从零实现一个迷你 STL](https://github.com/mcpp-community/d2mystl)
197+
- [mcpp 社区网站](https://mcpp.d2learn.org)
198+
- [mcpp 论坛](https://mcpp.d2learn.org/forum)
199+
- [入门教程:动手学习现代 C++](https://github.com/Sunrisepeak/mcpp-standard)

docs/api/en/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ This module exports:
1616
- `mcpplibs.primitives.policy`
1717
- `mcpplibs.primitives.primitive`
1818
- `mcpplibs.primitives.operations`
19+
- `mcpplibs.primitives.algorithms`
20+
- `mcpplibs.primitives.conversion`
1921

2022
## Namespace Overview
2123

2224
- `mcpplibs::primitives`: core types, concepts, and `primitive`.
2325
- `mcpplibs::primitives::underlying`: underlying traits and categories.
2426
- `mcpplibs::primitives::policy`: policy tags, defaults, and protocols.
2527
- `mcpplibs::primitives::operations`: functional dispatch API.
28+
- `mcpplibs::primitives::conversion`: numeric risk checks and cast helpers.
29+
- `mcpplibs::primitives::algorithms`: limits and hash helpers.
2630
- `mcpplibs::primitives::operators`: operator overload entry.
31+
- `mcpplibs::primitives::literals`: checked underlying literal suffixes.
2732
- `mcpplibs::primitives::meta`: primitive metadata traits.
2833
- `mcpplibs::primitives::types`: convenience aliases for common underlying types.
2934

@@ -96,7 +101,7 @@ Notes:
96101

97102
### Convenience aliases (`types`)
98103

99-
- Integers: `U8/U16/U32/U64`, `I8/I16/I32/I64`
104+
- Integers: `U8/U16/U32/U64`, `Size`, `Diff`, `I8/I16/I32/I64`
100105
- Floating-point: `F32/F64/F80`
101106
- Bool/chars: `Bool/UChar/Char8/Char16/Char32/WChar`
102107

@@ -108,6 +113,25 @@ using value_t = mcpplibs::primitives::types::I32<
108113
mcpplibs::primitives::policy::error::expected>;
109114
```
110115

116+
### Factory helpers and literals
117+
118+
- `with(value)` builds a `primitive` with default policies from an underlying value.
119+
- `with<Policies...>(value)` applies an explicit policy set during construction.
120+
- `with(policies_tuple, value)` reuses an existing policies tuple.
121+
- `mcpplibs::primitives::literals` provides checked suffixes such as
122+
`_u8/_u16/_u32/_u64/_size/_diff`, `_i8/_i16/_i32/_i64`,
123+
`_f32/_f32e/_f64/_f64e/_f80/_f80e`, and character suffixes
124+
`_uchar/_char8/_char16/_char32/_wchar`.
125+
126+
Example:
127+
128+
```cpp
129+
using namespace mcpplibs::primitives;
130+
using namespace mcpplibs::primitives::literals;
131+
132+
auto value = with<policy::value::checked, policy::error::expected>(42_i32);
133+
```
134+
111135
## `operations` API
112136
113137
The `operations` namespace provides function-style APIs with unified `std::expected` results.
@@ -172,6 +196,58 @@ using namespace mcpplibs::primitives::operators;
172196

173197
Operator results are still `std::expected<...>`, not raw values.
174198

199+
## `conversion` API
200+
201+
The `conversion` namespace provides reusable casts for underlying values,
202+
primitives, and mixed source/destination pairs.
203+
204+
### Common result aliases and risk model
205+
206+
- `cast_result<T> = std::expected<T, conversion::risk::kind>`
207+
- `conversion::risk::kind`:
208+
`none`, `overflow`, `underflow`, `domain_error`, `precision_loss`,
209+
`sign_loss`, `invalid_type_combination`
210+
211+
### Conversion helpers
212+
213+
- `numeric_risk<Dest>(value)`
214+
- `unchecked_cast<Dest>(value)`
215+
- `checked_cast<Dest>(value)`
216+
- `saturating_cast<Dest>(value)`
217+
- `truncating_cast<Dest>(value)`
218+
- `exact_cast<Dest>(value)`
219+
220+
Notes:
221+
222+
- `checked_cast` and `exact_cast` return `cast_result<T>`.
223+
- `saturating_cast` clamps overflow/underflow for numeric destinations.
224+
- `truncating_cast` is useful for floating-to-integral conversions.
225+
- Overloads exist for underlying -> underlying, primitive -> primitive,
226+
primitive -> underlying, and underlying -> primitive paths.
227+
228+
## `algorithms` API
229+
230+
The `algorithms` namespace provides reusable limits metadata and hashing helpers
231+
for built-in and supported custom underlyings.
232+
233+
### Limits
234+
235+
- `limits<T>`
236+
- `limited_type<T>`
237+
- `min_value<T>()`
238+
- `lowest_value<T>()`
239+
- `max_value<T>()`
240+
- `epsilon_value<T>()`
241+
- `infinity_value<T>()`
242+
- `quiet_nan_value<T>()`
243+
244+
### Hashing
245+
246+
- `hash<T>`
247+
- `hash_result_t<T>`
248+
- `hashable<T>`
249+
- `hash_value(value)`
250+
175251
## policy API
176252

177253
### Built-in policy tags

docs/api/zh/README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ import mcpplibs.primitives;
1616
- `mcpplibs.primitives.policy`
1717
- `mcpplibs.primitives.primitive`
1818
- `mcpplibs.primitives.operations`
19+
- `mcpplibs.primitives.algorithms`
20+
- `mcpplibs.primitives.conversion`
1921

2022
## 命名空间总览
2123

2224
- `mcpplibs::primitives`: 核心类型、概念与 `primitive`
2325
- `mcpplibs::primitives::underlying`: underlying traits 与类别。
2426
- `mcpplibs::primitives::policy`: policy 标签、默认值与协议。
2527
- `mcpplibs::primitives::operations`: 分发函数 API。
28+
- `mcpplibs::primitives::conversion`: 数值风险检查与转换辅助接口。
29+
- `mcpplibs::primitives::algorithms`: limits 与 hash 辅助接口。
2630
- `mcpplibs::primitives::operators`: 运算符重载入口。
31+
- `mcpplibs::primitives::literals`: 带范围校验的 underlying 字面量后缀。
2732
- `mcpplibs::primitives::meta`: primitive 元信息 traits。
2833
- `mcpplibs::primitives::types`: 常用 underlying 的 primitive 别名。
2934

@@ -96,7 +101,7 @@ class primitive;
96101

97102
### 预定义别名(`types`
98103

99-
- 整型:`U8/U16/U32/U64``I8/I16/I32/I64`
104+
- 整型:`U8/U16/U32/U64``Size``Diff``I8/I16/I32/I64`
100105
- 浮点:`F32/F64/F80`
101106
- 布尔/字符:`Bool/UChar/Char8/Char16/Char32/WChar`
102107

@@ -108,6 +113,25 @@ using value_t = mcpplibs::primitives::types::I32<
108113
mcpplibs::primitives::policy::error::expected>;
109114
```
110115

116+
### 工厂函数与 literals
117+
118+
- `with(value)`:从 underlying 值按默认策略构造 `primitive`
119+
- `with<Policies...>(value)`:构造时显式指定策略集合
120+
- `with(policies_tuple, value)`:复用已有策略元组
121+
- `mcpplibs::primitives::literals` 提供带校验的后缀,例如
122+
`_u8/_u16/_u32/_u64/_size/_diff``_i8/_i16/_i32/_i64`
123+
`_f32/_f32e/_f64/_f64e/_f80/_f80e`,以及
124+
`_uchar/_char8/_char16/_char32/_wchar`
125+
126+
示例:
127+
128+
```cpp
129+
using namespace mcpplibs::primitives;
130+
using namespace mcpplibs::primitives::literals;
131+
132+
auto value = with<policy::value::checked, policy::error::expected>(42_i32);
133+
```
134+
111135
## `operations` API
112136
113137
`operations` 命名空间提供函数式调用接口,统一返回 `std::expected`。
@@ -172,6 +196,56 @@ using namespace mcpplibs::primitives::operators;
172196

173197
重载结果同样是 `std::expected<...>`,不是裸值。
174198

199+
## `conversion` API
200+
201+
`conversion` 命名空间提供 underlying、primitive 以及混合路径之间的统一转换接口。
202+
203+
### 常用结果类型与风险模型
204+
205+
- `cast_result<T> = std::expected<T, conversion::risk::kind>`
206+
- `conversion::risk::kind`
207+
`none``overflow``underflow``domain_error``precision_loss`
208+
`sign_loss``invalid_type_combination`
209+
210+
### 转换函数
211+
212+
- `numeric_risk<Dest>(value)`
213+
- `unchecked_cast<Dest>(value)`
214+
- `checked_cast<Dest>(value)`
215+
- `saturating_cast<Dest>(value)`
216+
- `truncating_cast<Dest>(value)`
217+
- `exact_cast<Dest>(value)`
218+
219+
说明:
220+
221+
- `checked_cast``exact_cast` 返回 `cast_result<T>`
222+
- `saturating_cast` 会对数值溢出/下溢执行钳制
223+
- `truncating_cast` 适合浮点到整数的截断场景
224+
- 提供 underlying -> underlying、primitive -> primitive、
225+
primitive -> underlying、underlying -> primitive 四类重载
226+
227+
## `algorithms` API
228+
229+
`algorithms` 命名空间提供 limits 元信息与 hash 辅助接口,适用于内置类型和受支持的自定义 underlying。
230+
231+
### Limits
232+
233+
- `limits<T>`
234+
- `limited_type<T>`
235+
- `min_value<T>()`
236+
- `lowest_value<T>()`
237+
- `max_value<T>()`
238+
- `epsilon_value<T>()`
239+
- `infinity_value<T>()`
240+
- `quiet_nan_value<T>()`
241+
242+
### Hash
243+
244+
- `hash<T>`
245+
- `hash_result_t<T>`
246+
- `hashable<T>`
247+
- `hash_value(value)`
248+
175249
## policy API
176250

177251
### 内置策略标签

0 commit comments

Comments
 (0)