Skip to content

Commit 6ff3602

Browse files
Merge pull request #21 from mcpplibs/pump-0.4.0
Pump 0.4.0
2 parents 4d89252 + fb13425 commit 6ff3602

35 files changed

+723
-369
lines changed
Lines changed: 125 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,125 @@
1-
# Primitives 下一步实现计划清单(2026-03-26)
2-
3-
## 总体原则
4-
5-
- [ ] 先定义稳定 API 边界,再落地实现
6-
- [ ] 每个能力都配套 tests + examples + docs
7-
- [ ] 高风险转换默认禁止隐式触发,只允许显式 API
8-
- [ ] 明确分层职责:`algorithms` 负责值域/排序/边界元信息与值算法;`conversion` 负责显式类型变换
9-
- [ ] 明确依赖方向:`conversion` 可依赖 `algorithms``algorithms` 不反向依赖 `conversion`
10-
11-
## M1. C API 互操作层(双向)
12-
13-
### 工作清单
14-
15-
- [ ] 定义稳定 C ABI(命名规范、导出宏、版本策略)
16-
- [ ] 设计双向能力:C 调用 primitives;primitives 适配并调用 C API
17-
- [ ] 统一错误模型(error code + 可选 message buffer)
18-
- [ ] 提供最小可用头文件:`include/mcpplibs/primitives/c_api.h`
19-
- [ ] 增加 C/C++ 混合构建测试(C 编译器 + C++ 链接)
20-
- [ ] 增加示例:`examples/c/basic.c``examples/cpp/use_c_api.cpp`
21-
22-
### 验收标准
23-
24-
- [ ] 纯 C 工程可链接并完成基础能力调用
25-
- [ ] C++ 对外部 C API 的适配调用行为可控、无未定义行为
26-
27-
## M2. Concept/Traits 体系增强与元信息 API
28-
29-
### 工作清单
30-
31-
- [ ] 扩展 concept 分层(category/representation/policy-capability)
32-
- [ ] 明确 `underlying::traits<T>``algorithms::traits<T>` 的职责边界:前者描述表示层;后者描述值算法层
33-
- [ ] 完善 `traits<T>` 元信息(`kind``rep_type`、policy tags)
34-
- [ ] 设计 `algorithms::traits<T>`,内容参考 `std::numeric_limits`,但只保留算法/比较所需子集:
35-
- [ ] `min()` / `lowest()` / `max()` / `epsilon()` / `infinity()` / `quiet_nan()`
36-
- [ ] `is_bounded` / `is_exact` / `is_signed` / `is_integer` / `is_iec559`
37-
- [ ] `has_infinity` / `has_quiet_nan` / `digits` / `digits10` / `radix`
38-
- [ ] 增加排序能力元信息:`comparison_category``totally_ordered``partially_ordered``unordered_possible`
39-
- [ ] 提供双参聚合元信息:`algorithms::common_traits<Lhs, Rhs>`,统一暴露 `common_rep`、可比较性、边界查询与策略兼容性
40-
- [ ] 提供检测类 API(可比较性/可裁剪性/可转换性/是否有损/错误模型能力)
41-
- [ ] 统一 `constexpr` 查询入口,减少分散 traits 访问
42-
- [ ] 增加编译期测试矩阵(`static_assert` 覆盖)
43-
44-
### 验收标准
45-
46-
- [ ] 上层模块仅依赖公开 concept/traits,不依赖 `details::*`
47-
- [ ] `algorithms::traits` 可直接支撑算法层判定,并作为 conversion 的元信息依赖
48-
- [ ] `std::numeric_limits` 相关逻辑不再散落在 `conversion`/`operations` 内部
49-
50-
## M3. 显式转换层(任意策略组适用)
51-
52-
### 工作清单
53-
54-
- [ ] 设计统一接口族(建议):`explicit_cast``try_cast``checked_cast`
55-
- [ ] 将边界、NaN、无穷大、精度与排序相关判定统一改为依赖 `algorithms::traits` / `algorithms::common_traits`
56-
- [ ] 支持任意策略组组合,不绑定特定策略实现
57-
- [ ] 风险可见化(截断/溢出/精度损失)并可程序化读取
58-
- [ ] 定义失败语义(错误码或 expected 风格,按策略可配置)
59-
- [ ] 建立转换矩阵测试(同类/跨类/跨策略组)
60-
61-
### 验收标准
62-
63-
- [ ] 所有跨类高风险转换必须走显式 API
64-
- [ ] 风险信息可在编译期或运行期被确定性获取
65-
- [ ] `conversion` 模块仅消费 `algorithms` 提供的元信息,不反向定义算法层协议
66-
67-
## M4. 算法层(traits 先行,以 max/min 为起点)
68-
69-
### 工作清单
70-
71-
- [ ] 确定模块结构:`mcpplibs.primitives.algorithms``mcpplibs.primitives.algorithms.traits``mcpplibs.primitives.algorithms.compare``mcpplibs.primitives.algorithms.minmax`
72-
- [ ] 先实现 `algorithms::traits``algorithms::common_traits`,作为整个算法层与 conversion 的基础依赖
73-
- [ ] 约束算法职责只涉及值的比较、选择、裁剪与边界处理,不提供目标类型导向的 cast API
74-
- [ ] `max`/`min` 内部仅允许做 `common_rep` 归一化或排序语义协商,不绕回 conversion 层
75-
- [ ] 实现 `max`/`min`,并预留 `clamp`/`compare` 扩展位
76-
- [ ] 支持同类输入与受约束的异类输入;异类场景必须满足 `common_rep`、排序能力与策略组约束
77-
- [ ] 明确 `partial_ordering` / `unordered` 语义,尤其是浮点 `NaN` 路径
78-
- [ ]`clamp`/`compare` 抽出复用内核:公共比较、边界判定、值选择
79-
- [ ] 在可行范围保持 `constexpr`/`noexcept` 特性
80-
- [ ] 增加边界测试(极值、NaN、有符号/无符号混合、不同 `comparison_category`
81-
82-
### 验收标准
83-
84-
- [ ] 算法行为与策略约束一致
85-
- [ ] 风险路径始终显式、可审计
86-
- [ ] `algorithms` 可独立编译并被 `conversion` 依赖,不形成反向模块耦合
87-
- [ ] 算法层实现不通过“显式转换 API”间接完成值比较
88-
89-
## 建议推进顺序
90-
91-
1. M2(先夯实约束与元信息基础)
92-
2. M4 前半:`algorithms::traits` / `common_traits` / 排序能力查询
93-
3. M3(让 conversion 改为依赖 algorithms 元信息)
94-
4. M4 后半:`max` / `min` / `clamp` / `compare`
95-
5. M1(建立跨语言边界,可按需求并行推进)
96-
97-
## 总体完成跟踪
98-
99-
- [ ] M1 完成
100-
- [ ] M2 完成
101-
- [ ] M3 完成
102-
- [ ] M4 完成
1+
# Primitives 下一步实现计划清单(2026-03-27)
2+
3+
## 当前状态
4+
5+
- [ ] M1 完成:C API 互操作层(双向)
6+
- [x] M2 完成:Concept/Traits 体系增强与元信息 API
7+
- [x] M3 完成:显式转换层(任意策略组适用)
8+
- [x] M4 完成:算法层(traits 先行,以 max/min 为起点)
9+
10+
## 持续约束
11+
12+
- 先定义稳定 API 边界,再落地实现。
13+
- 每个能力都配套 `tests + examples + docs`
14+
- 高风险转换默认禁止隐式触发,只允许显式 API。
15+
- 明确分层职责:`algorithms` 负责值域/排序/边界元信息与值算法;`conversion` 负责显式类型变换。
16+
- 明确依赖方向:`conversion` 可依赖 `algorithms``algorithms` 不反向依赖 `conversion`
17+
- C ABI 只暴露稳定、可版本化的边界;不直接泄露模板、模块实现细节或未稳定的策略矩阵。
18+
19+
## M1. C API 互操作层(最后剩余部分)
20+
21+
### M1-0. 范围冻结与接口裁剪
22+
23+
- [ ] 冻结 `M1 v1` 范围:仅支持 `int32_t` / `uint32_t` / `double` 三类基础值。
24+
- [ ] 冻结 `M1 v1` 能力面:仅提供 `add` / `sub` / `mul` / `div`、基础比较、显式转换、版本查询。
25+
- [ ] 明确第一版不纳入 ABI 的内容:`char` 系列、`long double`、自定义 underlying、完整策略组合、完整 operator 集。
26+
- [ ] 统一第一版语义映射为“显式调用 + checked 路径 + 错误码返回”,不允许异常跨越 C ABI。
27+
- [ ] 输出一份最小公共命名规范:`mcpplibs_primitives_{type}_{op}`
28+
29+
### M1-1. 稳定 C ABI 与头文件
30+
31+
- [ ] 新增最小公共头文件:`include/mcpplibs/primitives/c_api.h`
32+
- [ ] 定义导出宏:`MCPPLIBS_PRIMITIVES_C_API``MCPPLIBS_PRIMITIVES_EXTERN_C`
33+
- [ ] 定义 C API 版本宏:`MCPPLIBS_PRIMITIVES_C_API_VERSION_MAJOR` / `MINOR` / `PATCH`
34+
- [ ] 定义统一错误码枚举,至少覆盖:
35+
- [ ] `ok`
36+
- [ ] `invalid_argument`
37+
- [ ] `overflow`
38+
- [ ] `underflow`
39+
- [ ] `divide_by_zero`
40+
- [ ] `domain_error`
41+
- [ ] `unsupported`
42+
- [ ] `internal_error`
43+
- [ ] 定义统一消息缓冲区协议:`char* message` + `size_t message_size`,支持空指针与零长度缓冲区。
44+
- [ ] 定义统一返回约定:函数返回错误码,业务结果通过 `out` 参数返回。
45+
- [ ] 提供版本查询函数与能力探测函数,确保后续 ABI 演进可协商。
46+
47+
### M1-2. C -> primitives 调用桥
48+
49+
- [ ]`src/c_api/` 下建立实现目录。
50+
- [ ] 拆分实现文件,建议至少包含:
51+
- [ ] `src/c_api/abi.cpp`
52+
- [ ] `src/c_api/arithmetic.cpp`
53+
- [ ] `src/c_api/compare.cpp`
54+
- [ ] `src/c_api/conversion.cpp`
55+
- [ ] 以公开模块能力为唯一实现入口:只调用 `mcpplibs.primitives` 已导出的 `types``operations``conversion`
56+
- [ ] 为每个 ABI 入口补齐参数校验:`out == nullptr`、非法 buffer、除零、溢出、域错误。
57+
- [ ] 建立统一错误翻译层:将 `policy::error::kind` 与转换风险映射到 C 错误码。
58+
- [ ] 在 ABI 边界统一拦截异常并映射为 `internal_error`,禁止任何 C++ 异常穿透到 C 侧。
59+
- [ ] 保证所有导出函数不暴露 `std::expected`、模板实例、模块内部类型或 STL 容器布局。
60+
61+
### M1-3. primitives -> 外部 C API 适配桥
62+
63+
- [ ] 新增 C++ 适配层,建议文件:
64+
- [ ] `src/c_api/adapter.cppm`
65+
- [ ] `src/c_api/adapter.cpp`
66+
- [ ] 用“函数表 + `void* context`”建模外部 C API,而不是散落裸函数指针。
67+
- [ ] 第一版只适配与 `M1 v1` 对称的能力:四则、比较、显式 cast。
68+
- [ ] 明确适配协议:
69+
- [ ] 适配层不拥有 `context`
70+
- [ ] 回调必须是 `noexcept` 语义约束
71+
- [ ] 返回码必须可映射到 `policy::error::kind`
72+
- [ ] 外部 C API 回传值必须再经过 primitives 的范围与错误检查,不能绕开现有风险路径。
73+
- [ ] 为适配层建立最小公共抽象,避免未来直接把外部 C API 绑定死到具体函数签名。
74+
75+
### M1-4. 构建系统与安装集成
76+
77+
- [ ] 更新 `CMakeLists.txt`:将 `project(... LANGUAGES CXX)` 扩展为 `project(... LANGUAGES C CXX)`
78+
- [ ] 为 C API 头文件添加安装与导出规则,确保 `include/mcpplibs/primitives/c_api.h` 进入安装产物。
79+
- [ ] 决定 C ABI 的产物形态:
80+
- [ ] 方案 A:并入现有 `mcpplibs-primitives` 静态库
81+
- [ ] 方案 B:新增单独目标 `mcpplibs-primitives-capi`
82+
- [ ] 更新 `xmake.lua`,确保 C 示例与 C 测试能参与构建。
83+
- [ ] 更新 `examples/CMakeLists.txt``examples/xmake.lua`,纳入 C 示例和 C++ 适配示例。
84+
- [ ] 更新 `tests/CMakeLists.txt``tests/basic/CMakeLists.txt``tests/xmake.lua`,纳入 C/C++ 混合验证目标。
85+
86+
### M1-5. 测试、示例与文档
87+
88+
- [ ] 新增纯 C 示例:`examples/c/basic.c`
89+
- [ ] 新增 C++ 调用 C API 示例:`examples/cpp/use_c_api.cpp`
90+
- [ ] 新增纯 C smoke test,验证:
91+
- [ ] 头文件可被 C 编译器独立包含
92+
- [ ] C 目标可链接库产物
93+
- [ ] `int32 add``double div` 成功路径可运行
94+
- [ ] 除零或溢出错误路径可观测
95+
- [ ] 新增 C++ 适配层测试,验证:
96+
- [ ] 外部 C API 成功路径映射正确
97+
- [ ] 错误码映射正确
98+
- [ ] 空指针/非法参数会被拒绝
99+
- [ ] 回调异常或非法返回不会引入未定义行为
100+
- [ ] 新增 ABI 边界测试,覆盖消息 buffer 截断、空 buffer、空 `out` 参数、版本查询。
101+
- [ ] 更新 `README.md` / `README.zh.md`,补充 C API 简介、边界约束与示例入口。
102+
- [ ] 更新 `docs/api/en` / `docs/api/zh`,补充错误模型、版本策略和双向互操作说明。
103+
104+
### M1-6. 收口与验收
105+
106+
- [ ] 纯 C 工程可独立包含 `c_api.h`、成功链接并调用基础能力。
107+
- [ ] C API 成功路径、错误路径、异常隔离路径全部可回归测试。
108+
- [ ] C++ 对外部 C API 的适配调用行为可控、无未定义行为。
109+
- [ ] ABI 命名、导出宏、版本查询、错误码模型在文档中有明确承诺。
110+
- [ ] `M1 v1` 中未纳入 ABI 的能力在文档中明确标注为“未稳定/不承诺”。
111+
112+
## 建议推进顺序(按 PR / 工作包)
113+
114+
1. `M1-0 + M1-1`:先冻结第一版范围,并提交 `c_api.h` 与 ABI 契约。
115+
2. `M1-2`:实现 C 调用 primitives 的最小闭环,先跑通 `int32_t``double`
116+
3. `M1-4`:补齐 CMake/xmake/安装规则,让 C 目标进入正式构建链路。
117+
4. `M1-5`:补齐 `examples/c/basic.c``examples/cpp/use_c_api.cpp` 和混合测试。
118+
5. `M1-3`:实现 primitives 对外部 C API 的适配桥,并补齐适配测试。
119+
6. `M1-6`:统一收口文档、版本策略与验收项,准备关闭 M1。
120+
121+
## 已完成里程碑归档
122+
123+
- [x] M2 已完成,后续不再作为阻塞项跟踪。
124+
- [x] M3 已完成,后续仅在 M1 适配中复用已有显式转换能力。
125+
- [x] M4 已完成,后续仅在 M1 实现中复用已有算法层与 traits 元信息。

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 23)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77
set(CMAKE_CXX_EXTENSIONS OFF)
88

9-
project(mcpplibs-primitives VERSION 0.3.0 LANGUAGES CXX)
9+
project(mcpplibs-primitives VERSION 0.4.0 LANGUAGES CXX)
1010

1111
find_package(Threads REQUIRED)
1212

README.md

Lines changed: 16 additions & 9 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

@@ -98,14 +98,16 @@ Default policies are available under `policy::defaults`:
9898

9999
## Examples
100100

101-
- `ex01_default_arithmetic`: Basic arithmetic under default policies.
101+
- `ex01_basic_usage`: Literals + primitive factory helpers with the built-in operator set.
102102
- `ex02_type_policy`: Type negotiation with `strict/compatible`, including how type policy affects construction from `underlying`.
103103
- `ex03_value_policy`: `checked/unchecked/saturating` behavior, including mixed binary operations with `underlying`.
104104
- `ex04_error_policy`: Error-handling behavior across different error policies.
105105
- `ex05_concurrency_policy`: Representative mixed read/write concurrency workload (writer `store` + reader `add/sub` + `CAS`).
106-
- `ex06_custom_underlying`: Custom underlying traits, rep validation, and common-rep extension.
107-
- `ex07_custom_policy`: Custom policy protocol implementation.
108-
- `ex08_custom_operation`: Custom operation extension.
106+
- `ex06_conversion`: Checked/saturating/truncating/exact conversion helpers across underlying values and primitives.
107+
- `ex07_algorithms`: Limits metadata, special numeric values, and hashing helpers.
108+
- `ex08_custom_underlying`: Custom underlying traits, rep validation, and common-rep extension.
109+
- `ex09_custom_policy`: Custom policy protocol implementation.
110+
- `ex10_custom_operation`: Custom operation extension.
109111

110112
## Project Layout
111113

@@ -117,7 +119,7 @@ mcpplibs-primitives/
117119
│ ├── policy/ # policy tags and protocol implementations
118120
│ ├── operations/ # operation tags / dispatcher / operators
119121
│ └── underlying/ # underlying traits and common_rep
120-
├── examples/ # ex01 ~ ex08 examples
122+
├── examples/ # example programs
121123
├── tests/ # test entry and basic test suite
122124
├── xmake.lua # xmake build script
123125
├── CMakeLists.txt # CMake build script
@@ -151,17 +153,22 @@ xlings install
151153

152154
```bash
153155
xmake build mcpplibs-primitives
154-
xmake run basic # equivalent to ex01_default_arithmetic
156+
xmake run basic # compatibility alias for ex01_basic_usage
157+
xmake run ex01_basic_usage
158+
xmake run ex06_conversion
159+
xmake run ex07_algorithms
155160
xmake run ex05_concurrency_policy
156161
xmake run primitives_test
157162
```
158163

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

161166
```bash
162167
cmake -B build -G Ninja
163168
cmake --build build --target mcpplibs-primitives
164-
cmake --build build --target ex01_default_arithmetic
169+
cmake --build build --target ex01_basic_usage
170+
cmake --build build --target ex06_conversion
171+
cmake --build build --target ex07_algorithms
165172
cmake --build build --target basic_tests
166173
ctest --test-dir build --output-on-failure
167174
```

0 commit comments

Comments
 (0)