Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
55dccaf
chore: Update copyright year and owner in LICENSE file
FrozenLemonTee Mar 22, 2026
d901dc4
refactor: Improve type negotiation and construction behavior descript…
FrozenLemonTee Mar 22, 2026
53dac13
refactor: Update value policy demonstration to include mixed primitiv…
FrozenLemonTee Mar 22, 2026
0d0aab9
refactor: Enhance concurrency policy demonstration with mixed read/wr…
FrozenLemonTee Mar 22, 2026
db80e87
docs: Revise README to enhance clarity on primitive infrastructure an…
FrozenLemonTee Mar 22, 2026
f2f2b7f
docs: Update README to include badges and links for enhanced visibili…
FrozenLemonTee Mar 22, 2026
8b8928b
docs: Update README for improved clarity and add Chinese translation
FrozenLemonTee Mar 22, 2026
6addd3e
bump: Update project version to 0.3.0 in CMakeLists.txt and xmake.lua
FrozenLemonTee Mar 22, 2026
aac10d9
docs: Add initial README file for project documentation
FrozenLemonTee Mar 22, 2026
968cbbf
docs: Update README files to enhance links and navigation for user do…
FrozenLemonTee Mar 22, 2026
2ce626a
docs: Update README files to improve navigation and add user and API …
FrozenLemonTee Mar 22, 2026
2ee202a
docs: Consolidate README files from language-specific directories int…
FrozenLemonTee Mar 22, 2026
6020dbb
docs: Add API documentation for primitives module in English and Chinese
FrozenLemonTee Mar 22, 2026
32b5300
docs: Add user guide for primitives module in English and Chinese
FrozenLemonTee Mar 22, 2026
e3222e6
docs: Add user and API documentation guides in English and Chinese
FrozenLemonTee Mar 22, 2026
bcfe31e
docs: Remove complete plan Markdown file
FrozenLemonTee Mar 22, 2026
65be873
fix: Improve concurrency control by adding wait and notify mechanisms
FrozenLemonTee Mar 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 0 additions & 66 deletions .github/prompts/plan-policyBehaviorProtocol.prompt.md

This file was deleted.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

project(mcpplibs-primitives VERSION 0.2.0 LANGUAGES CXX)
project(mcpplibs-primitives VERSION 0.3.0 LANGUAGES CXX)

find_package(Threads REQUIRED)

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

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

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
164 changes: 95 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,166 +1,192 @@
# mcpplibs primitives

> C++23 模块化原语库 - `import mcpplibs.primitives;`
> C++23 modular primitives library - `import mcpplibs.primitives;`

本仓库实现了底层强类型 primitive 基础设施(traits、policy、underlying 类型分类),供上层 `Integer`/`Floating`/`Boolean` 等封装使用。
[![d2x](https://img.shields.io/badge/d2x-ok-green.svg)](https://github.com/d2learn/d2x)
[![Online-ebook](https://img.shields.io/badge/online-ebook-orange.svg)](https://github.com/d2learn/d2x)
[![License](https://img.shields.io/badge/license-Apache_2.0-blue.svg)](LICENSE-CODE)

| [中文](README.zh.md) - [English](README.md) - [Forum](https://mcpp.d2learn.org/forum) |
|---------------------------------------------------------------------------------|
| [用户文档](docs/guide/zh/README.md) - [User Documentation](docs/guide/en/README.md) |
| [API文档](docs/guide/api/README.md) - [API Documentation](docs/api/en/README.md) |

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

> [!WARNING]
> 目前项目还在开发中,API会随着后续演进而改变
> The project is still evolving quickly, and APIs may change.

## 特性
## Features

- **C++23 模块** — `import mcpplibs.primitives;`
- **双构建系统** — 同时支持 xmake CMake
- **CI/CD** — GitHub Actions 多平台构建(Linux / macOS / Windows)
- **标准化结构** — 遵循 [mcpp-style-ref](https://github.com/mcpp-community/mcpp-style-ref) 编码规范
- **开箱即用** — 包含示例、测试和架构文档
- **C++23 modules** — `import mcpplibs.primitives;`
- **Dual build systems** — both xmake and CMake are supported
- **Policy-driven behavior** — value/type/error/concurrency policies are composable
- **Mixed operations support** — binary operations between `primitive` and `underlying` are supported
- **Concurrency access APIs** — `primitive::load/store/compare_exchange`

## Operators

该库在 `primitive` 类型上重载了常见的 C++ 算术、位运算和一元运算符。算术行为受策略(policy)控制:
The library provides unary, arithmetic, bitwise, and comparison operations for `primitive`.
Arithmetic paths are dispatched through a unified pipeline and return `std::expected<..., policy::error::kind>`.

- 值策略(`policy::value::checked` / `policy::value::saturating` / `policy::value::unchecked`)决定溢出行为;
- 错误策略(`policy::error::throwing` / `policy::error::expected` / `policy::error::terminate`)决定在 `policy::value::checked` 且发生错误时的处理方式。
- Value policies (`policy::value::checked` / `policy::value::saturating` / `policy::value::unchecked`) define overflow behavior.
- Error policies (`policy::error::throwing` / `policy::error::expected` / `policy::error::terminate`) define how errors are propagated.

示例:
Example:

```cpp
import std;
import mcpplibs.primitives;

using namespace mcpplibs::primitives;
using namespace mcpplibs::primitives::policy;
using namespace mcpplibs::primitives::operators;

primitive<int> a{1}, b{2};
auto c = a + b; // primitive<int>
primitive<int> a{1};
primitive<int> b{2};
auto sum = a + b; // std::expected<primitive<int>, policy::error::kind>

primitive<int, policy::error::expected> x{std::numeric_limits<int>::max()};
primitive<int, policy::error::expected> y{1};
auto maybe = x + y; // std::expected<primitive<int, policy::error::expected>, policy::error::kind>
using checked_t =
primitive<int, policy::value::checked, policy::error::expected>;
auto maybe_overflow =
checked_t{std::numeric_limits<int>::max()} + checked_t{1};
```

## Policy 协议命名空间
## Policy Protocol Namespaces

自定义 policy 时,协议入口已按职责拆分到子命名空间:
When implementing custom policies, protocol entry points are split by responsibility:

- `policy::type::handler` / `policy::type::handler_available`
- `policy::concurrency::handler` / `policy::concurrency::injection`
- `policy::value::handler` / `policy::value::decision`
- `policy::error::handler` / `policy::error::request` / `policy::error::kind`

预设 policy 标签也按类别归档:
Built-in policy tags:

- `policy::value::{checked, unchecked, saturating}`
- `policy::type::{strict, compatible, transparent}`
- `policy::error::{throwing, expected, terminate}`
- `policy::concurrency::{none, fenced, fenced_relaxed, fenced_acq_rel, fenced_seq_cst}`

并发策略说明:
Concurrency notes:

- `fenced*` 系列是操作级并发语义,通过策略注入内存序 fence;
- `primitive` 存储仍保持统一、零开销布局,不引入额外存储层抽象;
- `primitive::load/store/compare_exchange` 由并发策略的协议实现提供,若策略未实现该协议会在编译期报错。
- `fenced*` policies provide operation-level concurrency semantics with injected memory-order fences.
- `primitive` storage keeps a uniform, zero-extra-storage abstraction.
- `primitive::load/store/compare_exchange` are provided by concurrency policy protocols and fail at compile time if unsupported.

示例(并发访问 API):
Example (concurrent access APIs):

```cpp
using shared_t = primitive<int, policy::value::checked,
policy::concurrency::fenced_acq_rel,
policy::error::expected>;
policy::concurrency::fenced_acq_rel,
policy::error::expected>;

shared_t v{1};
v.store(2);
auto expected = 2;
if (v.compare_exchange(expected, 3)) {
auto now = v.load();
(void)now;
auto now = v.load();
(void)now;
}
```

默认策略位于 `policy::defaults`
Default policies are available under `policy::defaults`:

- `policy::defaults::value`
- `policy::defaults::type`
- `policy::defaults::error`
- `policy::defaults::concurrency`

## Examples

- `ex01_default_arithmetic`: Basic arithmetic under default policies.
- `ex02_type_policy`: Type negotiation with `strict/compatible`, including how type policy affects construction from `underlying`.
- `ex03_value_policy`: `checked/unchecked/saturating` behavior, including mixed binary operations with `underlying`.
- `ex04_error_policy`: Error-handling behavior across different error policies.
- `ex05_concurrency_policy`: Representative mixed read/write concurrency workload (writer `store` + reader `add/sub` + `CAS`).
- `ex06_custom_underlying`: Custom underlying traits, rep validation, and common-rep extension.
- `ex07_custom_policy`: Custom policy protocol implementation.
- `ex08_custom_operation`: Custom operation extension.

## 项目结构
## Project Layout

```
mcpplibs-primitives/
├── src/ # 模块源码
│ └── primitive.cppm # 主模块接口(导出 traits 与 primitive 聚合)
├── tests/ # 测试
│ ├── main.cpp
│ └── xmake.lua
├── examples/ # 示例
│ ├── basic.cpp
│ └── xmake.lua
├── docs/ # 文档
│ └── architecture.md
├── .github/workflows/ # CI/CD
│ └── ci.yml
├── xmake.lua # xmake 构建配置
├── CMakeLists.txt # CMake 构建配置
└── config.xlings # xlings 工具链配置
├── src/ # module sources
│ ├── primitives.cppm # top-level aggregate module
│ ├── primitive/ # primitive definitions and traits
│ ├── policy/ # policy tags and protocol implementations
│ ├── operations/ # operation tags / dispatcher / operators
│ └── underlying/ # underlying traits and common_rep
├── examples/ # ex01 ~ ex08 examples
├── tests/ # test entry and basic test suite
├── xmake.lua # xmake build script
├── CMakeLists.txt # CMake build script
└── .xlings.json # xlings package descriptor
```

## 快速开始
## Quick Start

```cpp
import std;
import mcpplibs.primitives;

int main() {
static_assert(mcpplibs::primitives::std_integer<int>);
return 0;
using namespace mcpplibs::primitives;

using value_t = primitive<int, policy::error::expected>;
auto const result = operations::add(value_t{40}, value_t{2});
return (result.has_value() && result->value() == 42) ? 0 : 1;
}
```

## 安装与配置
## Installation and Setup

```bash
xlings install
```

## 构建与运行
## Build and Run

**使用 xmake**
**Using xmake**

```bash
xmake build # 构建库
xmake run basic # 运行基础示例
xmake run primitives_test # 运行测试
xmake build mcpplibs-primitives
xmake run basic # equivalent to ex01_default_arithmetic
xmake run ex05_concurrency_policy
xmake run primitives_test
```

**使用 CMake**
**Using CMake**

```bash
cmake -B build -G Ninja
cmake --build build
ctest --test-dir build
cmake --build build --target mcpplibs-primitives
cmake --build build --target ex01_default_arithmetic
cmake --build build --target basic_tests
ctest --test-dir build --output-on-failure
```

## 集成到构建工具
## Build System Integration

### xmake

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

add_requires("templates")
add_requires("primitives")

target("myapp")
set_kind("binary")
set_languages("c++23")
add_files("main.cpp")
add_packages("templates")
add_packages("primitives")
set_policy("build.c++.modules", true)
```

## 相关链接
## Related Links

- [mcpp-style-ref | 现代C++编码/项目风格参考](https://github.com/mcpp-community/mcpp-style-ref)
- [mcpplibs/cmdline | 命令行解析库](https://github.com/mcpplibs/cmdline)
- [mcpp社区官网](https://mcpp.d2learn.org)
- [mcpp | 现代C++爱好者论坛](https://mcpp.d2learn.org/forum)
- [入门教程: 动手学现代C++](https://github.com/Sunrisepeak/mcpp-standard)
- [mcpp-style-ref | Modern C++ coding and project style reference](https://github.com/mcpp-community/mcpp-style-ref)
- [d2mystl | Build a mini STL from scratch](https://github.com/mcpp-community/d2mystl)
- [mcpp community website](https://mcpp.d2learn.org)
- [mcpp forum](https://mcpp.d2learn.org/forum)
- [Getting Started: Learn Modern C++ by Building](https://github.com/Sunrisepeak/mcpp-standard)
Loading
Loading