Skip to content

Commit a484387

Browse files
Merge pull request #15 from mcpplibs/refactor-meta
Simplify the interface and hide internal implementations that do not need to be exported
2 parents 4166133 + 33d9eb9 commit a484387

File tree

8 files changed

+452
-266
lines changed

8 files changed

+452
-266
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Primitives 下一步实现计划清单(2026-03-23)
2+
3+
## 总体原则
4+
5+
- [ ] 先定义稳定 API 边界,再落地实现
6+
- [ ] 每个能力都配套 tests + examples + docs
7+
- [ ] 高风险转换默认禁止隐式触发,只允许显式 API
8+
9+
## M1. C API 互操作层(双向)
10+
11+
### 工作清单
12+
13+
- [ ] 定义稳定 C ABI(命名规范、导出宏、版本策略)
14+
- [ ] 设计双向能力:C 调用 primitives;primitives 适配并调用 C API
15+
- [ ] 统一错误模型(error code + 可选 message buffer)
16+
- [ ] 提供最小可用头文件:`include/mcpplibs/primitives/c_api.h`
17+
- [ ] 增加 C/C++ 混合构建测试(C 编译器 + C++ 链接)
18+
- [ ] 增加示例:`examples/c/basic.c``examples/cpp/use_c_api.cpp`
19+
20+
### 验收标准
21+
22+
- [ ] 纯 C 工程可链接并完成基础能力调用
23+
- [ ] C++ 对外部 C API 的适配调用行为可控、无未定义行为
24+
25+
## M2. Concept/Traits 体系增强与元信息 API
26+
27+
### 工作清单
28+
29+
- [ ] 扩展 concept 分层(category/representation/policy-capability)
30+
- [ ] 完善 `traits<T>` 元信息(`kind``rep_type`、limits、policy tags)
31+
- [ ] 提供检测类 API(可转换性/是否有损/错误模型能力)
32+
- [ ] 统一 `constexpr` 查询入口,减少分散 traits 访问
33+
- [ ] 增加编译期测试矩阵(`static_assert` 覆盖)
34+
35+
### 验收标准
36+
37+
- [ ] 上层模块仅依赖公开 concept/traits,不依赖 `details::*`
38+
- [ ] 元信息可支撑转换层与算法层的约束判定
39+
40+
## M3. 显式转换层(任意策略组适用)
41+
42+
### 工作清单
43+
44+
- [ ] 设计统一接口族(建议):`explicit_cast``try_cast``checked_cast`
45+
- [ ] 支持任意策略组组合,不绑定特定策略实现
46+
- [ ] 风险可见化(截断/溢出/精度损失)并可程序化读取
47+
- [ ] 定义失败语义(错误码或 expected 风格,按策略可配置)
48+
- [ ] 建立转换矩阵测试(同类/跨类/跨策略组)
49+
50+
### 验收标准
51+
52+
- [ ] 所有跨类高风险转换必须走显式 API
53+
- [ ] 风险信息可在编译期或运行期被确定性获取
54+
55+
## M4. 算法层(以 max/min 为起点)
56+
57+
### 工作清单
58+
59+
- [ ] 实现 `max`/`min`,并预留 `clamp`/`compare` 扩展位
60+
- [ ] 算法统一依赖 M2+M3 的公开接口,不绕过转换层
61+
- [ ] 支持同类与受约束的异类输入
62+
- [ ] 在可行范围保持 `constexpr`/`noexcept` 特性
63+
- [ ] 增加边界测试(极值、NaN、有符号/无符号混合)
64+
65+
### 验收标准
66+
67+
- [ ] 算法行为与策略约束一致
68+
- [ ] 风险路径始终显式、可审计
69+
70+
## 建议推进顺序
71+
72+
1. M2(先夯实约束与元信息基础)
73+
2. M1(建立跨语言边界)
74+
3. M3(收敛转换风险)
75+
4. M4(复用基础能力实现算法)
76+
77+
## 总体完成跟踪
78+
79+
- [ ] M1 完成
80+
- [ ] M2 完成
81+
- [ ] M3 完成
82+
- [ ] M4 完成

.github/copilot-instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## 正在进行中的计划
1616

17-
- 当前计划目录:`../.github/prompts/`
17+
- 当前计划目录:`../.agents/docs/plans/`
1818

1919
## 说明
2020

src/operations/dispatcher.cppm

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,31 @@ import mcpplibs.primitives.underlying;
1515

1616
export namespace mcpplibs::primitives::operations {
1717

18-
template <typename T>
19-
concept primitive_instance = requires {
20-
typename primitives::meta::traits<
21-
std::remove_cvref_t<T>>::value_type;
22-
};
23-
24-
template <operation OpTag, primitive_instance Lhs, primitive_instance Rhs,
18+
template <operation OpTag, meta::primitive_type Lhs,
19+
meta::primitive_type Rhs,
2520
typename ErrorPayload = policy::error::kind>
2621
struct dispatcher_meta {
2722
using lhs_primitive = std::remove_cvref_t<Lhs>;
2823
using rhs_primitive = std::remove_cvref_t<Rhs>;
2924

30-
using lhs_traits = primitives::meta::traits<lhs_primitive>;
31-
using rhs_traits = primitives::meta::traits<rhs_primitive>;
25+
using lhs_traits = meta::traits<lhs_primitive>;
26+
using rhs_traits = meta::traits<rhs_primitive>;
3227

3328
using lhs_value_type = lhs_traits::value_type;
3429
using rhs_value_type = rhs_traits::value_type;
3530

3631
using lhs_rep = underlying::traits<lhs_value_type>::rep_type;
3732
using rhs_rep = underlying::traits<rhs_value_type>::rep_type;
3833

39-
using lhs_type_policy = typename lhs_traits::type_policy;
40-
using lhs_value_policy = typename lhs_traits::value_policy;
41-
using lhs_error_policy = typename lhs_traits::error_policy;
42-
using lhs_concurrency_policy = typename lhs_traits::concurrency_policy;
34+
using lhs_type_policy = lhs_traits::type_policy;
35+
using lhs_value_policy = lhs_traits::value_policy;
36+
using lhs_error_policy = lhs_traits::error_policy;
37+
using lhs_concurrency_policy = lhs_traits::concurrency_policy;
4338

44-
using rhs_type_policy = typename rhs_traits::type_policy;
45-
using rhs_value_policy = typename rhs_traits::value_policy;
46-
using rhs_error_policy = typename rhs_traits::error_policy;
47-
using rhs_concurrency_policy = typename rhs_traits::concurrency_policy;
39+
using rhs_type_policy = rhs_traits::type_policy;
40+
using rhs_value_policy = rhs_traits::value_policy;
41+
using rhs_error_policy = rhs_traits::error_policy;
42+
using rhs_concurrency_policy = rhs_traits::concurrency_policy;
4843

4944
static constexpr bool policy_group_consistent =
5045
std::is_same_v<lhs_type_policy, rhs_type_policy> &&
@@ -85,15 +80,17 @@ struct dispatcher_meta {
8580
ErrorPayload>;
8681
};
8782

88-
template <operation OpTag, primitive_instance Lhs, primitive_instance Rhs,
83+
template <operation OpTag, primitives::meta::primitive_type Lhs,
84+
primitives::meta::primitive_type Rhs,
8985
typename ErrorPayload = policy::error::kind>
9086
using dispatch_result_t = std::expected<
9187
typename dispatcher_meta<OpTag, Lhs, Rhs, ErrorPayload>::common_rep,
9288
ErrorPayload>;
9389

9490
// Dispatcher pipeline: compile-time negotiation plus runtime chain
9591
// (concurrency -> value -> error) through selected policy handlers.
96-
template <operation OpTag, primitive_instance Lhs, primitive_instance Rhs,
92+
template <operation OpTag, primitives::meta::primitive_type Lhs,
93+
primitives::meta::primitive_type Rhs,
9794
typename ErrorPayload = policy::error::kind>
9895
constexpr auto dispatch(Lhs const &lhs, Rhs const &rhs)
9996
-> dispatch_result_t<OpTag, Lhs, Rhs, ErrorPayload> {

src/operations/invoker.cppm

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import mcpplibs.primitives.policy.handler;
1717
import mcpplibs.primitives.policy.impl;
1818
import mcpplibs.primitives.policy.traits;
1919

20-
export namespace mcpplibs::primitives::operations::runtime {
2120

22-
namespace details {
21+
namespace mcpplibs::primitives::operations::runtime::details {
2322

2423
template <typename CommonRep>
2524
constexpr auto make_error(policy::error::kind kind, char const *reason,
@@ -781,18 +780,22 @@ constexpr auto make_div_zero(char const *reason)
781780
return make_error<CommonRep>(policy::error::kind::divide_by_zero, reason);
782781
}
783782

784-
constexpr auto apply_runtime_fence(bool enabled,
785-
std::memory_order order) noexcept -> void {
783+
constexpr auto apply_runtime_fence(const bool enabled,
784+
const std::memory_order order) noexcept -> void {
786785
if (!enabled) {
787786
return;
788787
}
789788

790-
if (!std::is_constant_evaluated()) {
789+
if !consteval {
791790
std::atomic_thread_fence(order);
792791
}
793792
}
794793

795-
} // namespace details
794+
} // namespace mcpplibs::primitives::operations::runtime::details
795+
796+
797+
798+
export namespace mcpplibs::primitives::operations::runtime {
796799

797800
template <operation OpTag, policy::value_policy ValuePolicy, typename CommonRep>
798801
struct op_binding {

0 commit comments

Comments
 (0)