Skip to content

Commit 4166133

Browse files
Merge pull request #14 from mcpplibs/pre-0.3.0
Update documentation, enhance README, and version bump to 0.3.0
2 parents 7340b4f + 65be873 commit 4166133

24 files changed

+1659
-195
lines changed

.github/prompts/plan-policyBehaviorProtocol.prompt.md

Lines changed: 0 additions & 66 deletions
This file was deleted.

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.2.0 LANGUAGES CXX)
9+
project(mcpplibs-primitives VERSION 0.3.0 LANGUAGES CXX)
1010

1111
find_package(Threads REQUIRED)
1212

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright [2026] [mcpp-community]
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 95 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,192 @@
11
# mcpplibs primitives
22

3-
> C++23 模块化原语库 - `import mcpplibs.primitives;`
3+
> C++23 modular primitives library - `import mcpplibs.primitives;`
44
5-
本仓库实现了底层强类型 primitive 基础设施(traits、policy、underlying 类型分类),供上层 `Integer`/`Floating`/`Boolean` 等封装使用。
5+
[![d2x](https://img.shields.io/badge/d2x-ok-green.svg)](https://github.com/d2learn/d2x)
6+
[![Online-ebook](https://img.shields.io/badge/online-ebook-orange.svg)](https://github.com/d2learn/d2x)
7+
[![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](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.
615

716
> [!WARNING]
8-
> 目前项目还在开发中,API会随着后续演进而改变
17+
> The project is still evolving quickly, and APIs may change.
918
10-
## 特性
19+
## Features
1120

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`
1726

1827
## Operators
1928

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>`.
2131

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.
2434

25-
示例:
35+
Example:
2636

2737
```cpp
38+
import std;
2839
import mcpplibs.primitives;
40+
2941
using namespace mcpplibs::primitives;
30-
using namespace mcpplibs::primitives::policy;
42+
using namespace mcpplibs::primitives::operators;
3143

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>
3447

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};
3852
```
3953
40-
## Policy 协议命名空间
54+
## Policy Protocol Namespaces
4155
42-
自定义 policy 时,协议入口已按职责拆分到子命名空间:
56+
When implementing custom policies, protocol entry points are split by responsibility:
4357
4458
- `policy::type::handler` / `policy::type::handler_available`
4559
- `policy::concurrency::handler` / `policy::concurrency::injection`
4660
- `policy::value::handler` / `policy::value::decision`
4761
- `policy::error::handler` / `policy::error::request` / `policy::error::kind`
4862
49-
预设 policy 标签也按类别归档:
63+
Built-in policy tags:
5064
5165
- `policy::value::{checked, unchecked, saturating}`
5266
- `policy::type::{strict, compatible, transparent}`
5367
- `policy::error::{throwing, expected, terminate}`
5468
- `policy::concurrency::{none, fenced, fenced_relaxed, fenced_acq_rel, fenced_seq_cst}`
5569
56-
并发策略说明:
70+
Concurrency notes:
5771
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.
6175
62-
示例(并发访问 API):
76+
Example (concurrent access APIs):
6377
6478
```cpp
6579
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>;
6882
6983
shared_t v{1};
7084
v.store(2);
7185
auto expected = 2;
7286
if (v.compare_exchange(expected, 3)) {
73-
auto now = v.load();
74-
(void)now;
87+
auto now = v.load();
88+
(void)now;
7589
}
7690
```
7791

78-
默认策略位于 `policy::defaults`
92+
Default policies are available under `policy::defaults`:
7993

8094
- `policy::defaults::value`
8195
- `policy::defaults::type`
8296
- `policy::defaults::error`
8397
- `policy::defaults::concurrency`
8498

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.
85109

86-
## 项目结构
110+
## Project Layout
87111

88112
```
89113
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
105125
```
106126

107-
## 快速开始
127+
## Quick Start
108128

109129
```cpp
110130
import std;
111131
import mcpplibs.primitives;
112132

113133
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;
116139
}
117140
```
118141

119-
## 安装与配置
142+
## Installation and Setup
120143

121144
```bash
122145
xlings install
123146
```
124147

125-
## 构建与运行
148+
## Build and Run
126149

127-
**使用 xmake**
150+
**Using xmake**
128151

129152
```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
133157
```
134158

135-
**使用 CMake**
159+
**Using CMake**
136160

137161
```bash
138162
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
141167
```
142168

143-
## 集成到构建工具
169+
## Build System Integration
144170

145171
### xmake
146172

147173
```lua
148174
add_repositories("mcpplibs-index https://github.com/mcpplibs/mcpplibs-index.git")
149175

150-
add_requires("templates")
176+
add_requires("primitives")
151177

152178
target("myapp")
153179
set_kind("binary")
154180
set_languages("c++23")
155181
add_files("main.cpp")
156-
add_packages("templates")
182+
add_packages("primitives")
157183
set_policy("build.c++.modules", true)
158184
```
159185

160-
## 相关链接
186+
## Related Links
161187

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

Comments
 (0)