|
1 | 1 | # mcpplibs primitives |
2 | 2 |
|
3 | | -> C++23 模块化原语库 - `import mcpplibs.primitives;` |
| 3 | +> C++23 modular primitives library - `import mcpplibs.primitives;` |
4 | 4 |
|
5 | | -本仓库实现了底层强类型 primitive 基础设施(traits、policy、underlying 类型分类),供上层 `Integer`/`Floating`/`Boolean` 等封装使用。 |
| 5 | +[](https://github.com/d2learn/d2x) |
| 6 | +[](https://github.com/d2learn/d2x) |
| 7 | +[](LICENSE-CODE) |
| 8 | + |
| 9 | +| [中文](README.zh.md) - [English](README.md) - [Forum](https://mcpp.d2learn.org/forum) | |
| 10 | +|---------------------------------------------------------------------------------| |
| 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) | |
| 13 | + |
| 14 | +This repository provides configurable `primitive` infrastructure (`underlying traits`, `policy`, and `operations/dispatcher`) to unify numeric behavior, error handling, and concurrency access semantics. |
6 | 15 |
|
7 | 16 | > [!WARNING] |
8 | | -> 目前项目还在开发中,API会随着后续演进而改变 |
| 17 | +> The project is still evolving quickly, and APIs may change. |
9 | 18 |
|
10 | | -## 特性 |
| 19 | +## Features |
11 | 20 |
|
12 | | -- **C++23 模块** — `import mcpplibs.primitives;` |
13 | | -- **双构建系统** — 同时支持 xmake 和 CMake |
14 | | -- **CI/CD** — GitHub Actions 多平台构建(Linux / macOS / Windows) |
15 | | -- **标准化结构** — 遵循 [mcpp-style-ref](https://github.com/mcpp-community/mcpp-style-ref) 编码规范 |
16 | | -- **开箱即用** — 包含示例、测试和架构文档 |
| 21 | +- **C++23 modules** — `import mcpplibs.primitives;` |
| 22 | +- **Dual build systems** — both xmake and CMake are supported |
| 23 | +- **Policy-driven behavior** — value/type/error/concurrency policies are composable |
| 24 | +- **Mixed operations support** — binary operations between `primitive` and `underlying` are supported |
| 25 | +- **Concurrency access APIs** — `primitive::load/store/compare_exchange` |
17 | 26 |
|
18 | 27 | ## Operators |
19 | 28 |
|
20 | | -该库在 `primitive` 类型上重载了常见的 C++ 算术、位运算和一元运算符。算术行为受策略(policy)控制: |
| 29 | +The library provides unary, arithmetic, bitwise, and comparison operations for `primitive`. |
| 30 | +Arithmetic paths are dispatched through a unified pipeline and return `std::expected<..., policy::error::kind>`. |
21 | 31 |
|
22 | | -- 值策略(`policy::value::checked` / `policy::value::saturating` / `policy::value::unchecked`)决定溢出行为; |
23 | | -- 错误策略(`policy::error::throwing` / `policy::error::expected` / `policy::error::terminate`)决定在 `policy::value::checked` 且发生错误时的处理方式。 |
| 32 | +- Value policies (`policy::value::checked` / `policy::value::saturating` / `policy::value::unchecked`) define overflow behavior. |
| 33 | +- Error policies (`policy::error::throwing` / `policy::error::expected` / `policy::error::terminate`) define how errors are propagated. |
24 | 34 |
|
25 | | -示例: |
| 35 | +Example: |
26 | 36 |
|
27 | 37 | ```cpp |
| 38 | +import std; |
28 | 39 | import mcpplibs.primitives; |
| 40 | + |
29 | 41 | using namespace mcpplibs::primitives; |
30 | | -using namespace mcpplibs::primitives::policy; |
| 42 | +using namespace mcpplibs::primitives::operators; |
31 | 43 |
|
32 | | -primitive<int> a{1}, b{2}; |
33 | | -auto c = a + b; // primitive<int> |
| 44 | +primitive<int> a{1}; |
| 45 | +primitive<int> b{2}; |
| 46 | +auto sum = a + b; // std::expected<primitive<int>, policy::error::kind> |
34 | 47 |
|
35 | | -primitive<int, policy::error::expected> x{std::numeric_limits<int>::max()}; |
36 | | -primitive<int, policy::error::expected> y{1}; |
37 | | -auto maybe = x + y; // std::expected<primitive<int, policy::error::expected>, policy::error::kind> |
| 48 | +using checked_t = |
| 49 | + primitive<int, policy::value::checked, policy::error::expected>; |
| 50 | +auto maybe_overflow = |
| 51 | + checked_t{std::numeric_limits<int>::max()} + checked_t{1}; |
38 | 52 | ``` |
39 | 53 |
|
40 | | -## Policy 协议命名空间 |
| 54 | +## Policy Protocol Namespaces |
41 | 55 |
|
42 | | -自定义 policy 时,协议入口已按职责拆分到子命名空间: |
| 56 | +When implementing custom policies, protocol entry points are split by responsibility: |
43 | 57 |
|
44 | 58 | - `policy::type::handler` / `policy::type::handler_available` |
45 | 59 | - `policy::concurrency::handler` / `policy::concurrency::injection` |
46 | 60 | - `policy::value::handler` / `policy::value::decision` |
47 | 61 | - `policy::error::handler` / `policy::error::request` / `policy::error::kind` |
48 | 62 |
|
49 | | -预设 policy 标签也按类别归档: |
| 63 | +Built-in policy tags: |
50 | 64 |
|
51 | 65 | - `policy::value::{checked, unchecked, saturating}` |
52 | 66 | - `policy::type::{strict, compatible, transparent}` |
53 | 67 | - `policy::error::{throwing, expected, terminate}` |
54 | 68 | - `policy::concurrency::{none, fenced, fenced_relaxed, fenced_acq_rel, fenced_seq_cst}` |
55 | 69 |
|
56 | | -并发策略说明: |
| 70 | +Concurrency notes: |
57 | 71 |
|
58 | | -- `fenced*` 系列是操作级并发语义,通过策略注入内存序 fence; |
59 | | -- `primitive` 存储仍保持统一、零开销布局,不引入额外存储层抽象; |
60 | | -- `primitive::load/store/compare_exchange` 由并发策略的协议实现提供,若策略未实现该协议会在编译期报错。 |
| 72 | +- `fenced*` policies provide operation-level concurrency semantics with injected memory-order fences. |
| 73 | +- `primitive` storage keeps a uniform, zero-extra-storage abstraction. |
| 74 | +- `primitive::load/store/compare_exchange` are provided by concurrency policy protocols and fail at compile time if unsupported. |
61 | 75 |
|
62 | | -示例(并发访问 API): |
| 76 | +Example (concurrent access APIs): |
63 | 77 |
|
64 | 78 | ```cpp |
65 | 79 | using shared_t = primitive<int, policy::value::checked, |
66 | | - policy::concurrency::fenced_acq_rel, |
67 | | - policy::error::expected>; |
| 80 | + policy::concurrency::fenced_acq_rel, |
| 81 | + policy::error::expected>; |
68 | 82 |
|
69 | 83 | shared_t v{1}; |
70 | 84 | v.store(2); |
71 | 85 | auto expected = 2; |
72 | 86 | if (v.compare_exchange(expected, 3)) { |
73 | | - auto now = v.load(); |
74 | | - (void)now; |
| 87 | + auto now = v.load(); |
| 88 | + (void)now; |
75 | 89 | } |
76 | 90 | ``` |
77 | 91 |
|
78 | | -默认策略位于 `policy::defaults`: |
| 92 | +Default policies are available under `policy::defaults`: |
79 | 93 |
|
80 | 94 | - `policy::defaults::value` |
81 | 95 | - `policy::defaults::type` |
82 | 96 | - `policy::defaults::error` |
83 | 97 | - `policy::defaults::concurrency` |
84 | 98 |
|
| 99 | +## Examples |
| 100 | + |
| 101 | +- `ex01_default_arithmetic`: Basic arithmetic under default policies. |
| 102 | +- `ex02_type_policy`: Type negotiation with `strict/compatible`, including how type policy affects construction from `underlying`. |
| 103 | +- `ex03_value_policy`: `checked/unchecked/saturating` behavior, including mixed binary operations with `underlying`. |
| 104 | +- `ex04_error_policy`: Error-handling behavior across different error policies. |
| 105 | +- `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. |
85 | 109 |
|
86 | | -## 项目结构 |
| 110 | +## Project Layout |
87 | 111 |
|
88 | 112 | ``` |
89 | 113 | mcpplibs-primitives/ |
90 | | -├── src/ # 模块源码 |
91 | | -│ └── primitive.cppm # 主模块接口(导出 traits 与 primitive 聚合) |
92 | | -├── tests/ # 测试 |
93 | | -│ ├── main.cpp |
94 | | -│ └── xmake.lua |
95 | | -├── examples/ # 示例 |
96 | | -│ ├── basic.cpp |
97 | | -│ └── xmake.lua |
98 | | -├── docs/ # 文档 |
99 | | -│ └── architecture.md |
100 | | -├── .github/workflows/ # CI/CD |
101 | | -│ └── ci.yml |
102 | | -├── xmake.lua # xmake 构建配置 |
103 | | -├── CMakeLists.txt # CMake 构建配置 |
104 | | -└── config.xlings # xlings 工具链配置 |
| 114 | +├── src/ # module sources |
| 115 | +│ ├── primitives.cppm # top-level aggregate module |
| 116 | +│ ├── primitive/ # primitive definitions and traits |
| 117 | +│ ├── policy/ # policy tags and protocol implementations |
| 118 | +│ ├── operations/ # operation tags / dispatcher / operators |
| 119 | +│ └── underlying/ # underlying traits and common_rep |
| 120 | +├── examples/ # ex01 ~ ex08 examples |
| 121 | +├── tests/ # test entry and basic test suite |
| 122 | +├── xmake.lua # xmake build script |
| 123 | +├── CMakeLists.txt # CMake build script |
| 124 | +└── .xlings.json # xlings package descriptor |
105 | 125 | ``` |
106 | 126 |
|
107 | | -## 快速开始 |
| 127 | +## Quick Start |
108 | 128 |
|
109 | 129 | ```cpp |
110 | 130 | import std; |
111 | 131 | import mcpplibs.primitives; |
112 | 132 |
|
113 | 133 | int main() { |
114 | | - static_assert(mcpplibs::primitives::std_integer<int>); |
115 | | - return 0; |
| 134 | + using namespace mcpplibs::primitives; |
| 135 | + |
| 136 | + using value_t = primitive<int, policy::error::expected>; |
| 137 | + auto const result = operations::add(value_t{40}, value_t{2}); |
| 138 | + return (result.has_value() && result->value() == 42) ? 0 : 1; |
116 | 139 | } |
117 | 140 | ``` |
118 | 141 |
|
119 | | -## 安装与配置 |
| 142 | +## Installation and Setup |
120 | 143 |
|
121 | 144 | ```bash |
122 | 145 | xlings install |
123 | 146 | ``` |
124 | 147 |
|
125 | | -## 构建与运行 |
| 148 | +## Build and Run |
126 | 149 |
|
127 | | -**使用 xmake** |
| 150 | +**Using xmake** |
128 | 151 |
|
129 | 152 | ```bash |
130 | | -xmake build # 构建库 |
131 | | -xmake run basic # 运行基础示例 |
132 | | -xmake run primitives_test # 运行测试 |
| 153 | +xmake build mcpplibs-primitives |
| 154 | +xmake run basic # equivalent to ex01_default_arithmetic |
| 155 | +xmake run ex05_concurrency_policy |
| 156 | +xmake run primitives_test |
133 | 157 | ``` |
134 | 158 |
|
135 | | -**使用 CMake** |
| 159 | +**Using CMake** |
136 | 160 |
|
137 | 161 | ```bash |
138 | 162 | cmake -B build -G Ninja |
139 | | -cmake --build build |
140 | | -ctest --test-dir build |
| 163 | +cmake --build build --target mcpplibs-primitives |
| 164 | +cmake --build build --target ex01_default_arithmetic |
| 165 | +cmake --build build --target basic_tests |
| 166 | +ctest --test-dir build --output-on-failure |
141 | 167 | ``` |
142 | 168 |
|
143 | | -## 集成到构建工具 |
| 169 | +## Build System Integration |
144 | 170 |
|
145 | 171 | ### xmake |
146 | 172 |
|
147 | 173 | ```lua |
148 | 174 | add_repositories("mcpplibs-index https://github.com/mcpplibs/mcpplibs-index.git") |
149 | 175 |
|
150 | | -add_requires("templates") |
| 176 | +add_requires("primitives") |
151 | 177 |
|
152 | 178 | target("myapp") |
153 | 179 | set_kind("binary") |
154 | 180 | set_languages("c++23") |
155 | 181 | add_files("main.cpp") |
156 | | - add_packages("templates") |
| 182 | + add_packages("primitives") |
157 | 183 | set_policy("build.c++.modules", true) |
158 | 184 | ``` |
159 | 185 |
|
160 | | -## 相关链接 |
| 186 | +## Related Links |
161 | 187 |
|
162 | | -- [mcpp-style-ref | 现代C++编码/项目风格参考](https://github.com/mcpp-community/mcpp-style-ref) |
163 | | -- [mcpplibs/cmdline | 命令行解析库](https://github.com/mcpplibs/cmdline) |
164 | | -- [mcpp社区官网](https://mcpp.d2learn.org) |
165 | | -- [mcpp | 现代C++爱好者论坛](https://mcpp.d2learn.org/forum) |
166 | | -- [入门教程: 动手学现代C++](https://github.com/Sunrisepeak/mcpp-standard) |
| 188 | +- [mcpp-style-ref | Modern C++ coding and project style reference](https://github.com/mcpp-community/mcpp-style-ref) |
| 189 | +- [d2mystl | Build a mini STL from scratch](https://github.com/mcpp-community/d2mystl) |
| 190 | +- [mcpp community website](https://mcpp.d2learn.org) |
| 191 | +- [mcpp forum](https://mcpp.d2learn.org/forum) |
| 192 | +- [Getting Started: Learn Modern C++ by Building](https://github.com/Sunrisepeak/mcpp-standard) |
0 commit comments