Skip to content

Commit c0358ad

Browse files
Merge pull request #3 from mcpplibs/0.1.0
0.1.0
2 parents b8cd620 + b2bcf19 commit c0358ad

File tree

11 files changed

+1240
-17
lines changed

11 files changed

+1240
-17
lines changed

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 1.0.0 LANGUAGES CXX)
9+
project(mcpplibs-primitives VERSION 0.1.0 LANGUAGES CXX)
1010

1111
find_package(Threads REQUIRED)
1212

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,39 @@
44
55
本仓库实现了底层强类型 primitive 基础设施(traits、policy、underlying 类型分类),供上层 `Integer`/`Floating`/`Boolean` 等封装使用。
66

7+
> [!WARNING]
8+
> 目前项目还在开发中,API会随着后续演进而改变
9+
710
## 特性
811

9-
- **C++23 模块**`import mcpplibs.templates;`
12+
- **C++23 模块**`import mcpplibs.primitives;`
1013
- **双构建系统** — 同时支持 xmake 和 CMake
1114
- **CI/CD** — GitHub Actions 多平台构建(Linux / macOS / Windows)
1215
- **标准化结构** — 遵循 [mcpp-style-ref](https://github.com/mcpp-community/mcpp-style-ref) 编码规范
1316
- **开箱即用** — 包含示例、测试和架构文档
1417

18+
## Operators
19+
20+
该库在 `primitive` 类型上重载了常见的 C++ 算术、位运算和一元运算符。算术行为受策略(policy)控制:
21+
22+
- 值策略(`checked_value` / `saturating_value` / `unchecked_value`)决定溢出行为;
23+
- 错误策略(`throw_error` / `expected_error` / `terminate_error`)决定在 `checked_value` 且发生错误时的处理方式。
24+
25+
示例:
26+
27+
```cpp
28+
import mcpplibs.primitives;
29+
using namespace mcpplibs::primitives;
30+
using namespace mcpplibs::primitives::policy;
31+
32+
primitive<int> a{1}, b{2};
33+
auto c = a + b; // primitive<int>
34+
35+
primitive<int, expected_error> x{std::numeric_limits<int>::max()};
36+
primitive<int, expected_error> y{1};
37+
auto maybe = x + y; // std::expected<primitive<int, expected_error>, std::overflow_error>
38+
```
39+
1540
## 项目结构
1641
1742
```

examples/basic.cpp

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11
#include <iostream>
2+
#include <limits>
23

34
import mcpplibs.primitives;
45

56
int main() {
67
using namespace mcpplibs::primitives;
78
using namespace mcpplibs::primitives::policy;
9+
using namespace mcpplibs::primitives::operators;
10+
using namespace mcpplibs::primitives::types;
811

9-
std::cout << "=== mcpplibs.primitives traits & policy example ===\n";
10-
std::cout << std::boolalpha;
11-
std::cout << "int is std_integer: " << std_integer<int> << "\n";
12-
std::cout << "double is std_floating: " << std_floating<double> << "\n";
13-
std::cout << "int is underlying_type: " << underlying_type<int> << "\n";
14-
15-
std::cout
16-
<< "default value policy is unchecked_value: "
17-
<< std::is_same_v<default_value, checked_value> << "\n";
18-
19-
std::cout << "checked_value is a policy_type: "
20-
<< policy_type<checked_value> << "\n";
21-
std::cout << "checked_value category == value: "
22-
<< (policy::traits<checked_value>::kind == category::value)
12+
// Operators
13+
I32<> a{3};
14+
I32<> b{4};
15+
auto c = a + b; // primitive<int>
16+
std::cout << "3 + 4 = " << static_cast<int>(c) << "\n";
17+
18+
// Error handling
19+
I32<checked_value> lhs{1};
20+
I32<checked_value> rhs{std::numeric_limits<std::int32_t>::max()};
21+
22+
try {
23+
auto res = lhs + rhs;
24+
} catch (const std::exception &e) {
25+
std::cout << "An exception occurred: " << e.what() << "\n";
26+
}
27+
28+
I64<checked_value, expected_error> lhs2{1};
29+
I64<checked_value, expected_error> rhs2{std::numeric_limits<std::int64_t>::max()};
30+
31+
auto res2 = lhs2 + rhs2;
32+
33+
// Saturating
34+
I32<saturating_value> s1{std::numeric_limits<std::int32_t>::max()};
35+
I32<saturating_value> s2{1};
36+
auto sat = s1 + s2; // saturating -> stays max
37+
std::cout << "saturating max + 1 = " << static_cast<std::int32_t>(sat)
2338
<< "\n";
2439

40+
// Mixed-type addition
41+
using expected_type = I64<category_compatible_type>;
42+
expected_type L1{5};
43+
I32<category_compatible_type> L2{6};
44+
auto mix = L1 + L2; // common_type -> I64
45+
std::cout << "5 + 6 = " << mix.value() << "\n";
46+
std::cout << std::boolalpha;
47+
std::cout << "Does type of mix is I64<category_compatible_type>: " << std::same_as<decltype(mix), expected_type> << std::endl;
48+
2549
return 0;
2650
}

0 commit comments

Comments
 (0)