@chengzhao-dev/dsh-cpp-workflow 0.6.16 → 0.6.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +7 -7
- package/README.md +7 -7
- package/lib/client.js +1 -1
- package/lib/comment-policy.js +1 -1
- package/lib/index.js +1 -1
- package/package.json +3 -3
- package/skills/building-cpp/SKILL.md +29 -16
- package/skills/building-cpp/project/vscode/01-extensions.md +22 -0
- package/skills/building-cpp/project/vscode/02-settings.md +33 -0
- package/skills/building-cpp/references/build-script.md +6 -6
- package/skills/building-cpp/references/environment.md +1 -1
- package/skills/building-cpp/steps/01-build.md +6 -6
- package/skills/building-cpp/steps/02-tooling.md +16 -8
- package/skills/building-cpp/steps/03-check.md +6 -6
- package/skills/developing-cpp/SKILL.md +37 -13
- package/skills/developing-cpp/scripts/checking-comments.mjs +33 -6
- package/skills/developing-cpp/{references/change-model.md → source/01-editing/01-edit-operations.md} +27 -9
- package/skills/developing-cpp/{references/priority.md → source/01-editing/02-rule-priority.md} +1 -1
- package/skills/developing-cpp/{references/google-cpp-style.md → source/02-style/01-google-cpp-style.md} +10 -9
- package/skills/developing-cpp/{references/project-design.md → source/03-design/01-project-design.md} +12 -5
- package/skills/developing-cpp/source/03-design/examples/behavior-sentence.md +14 -0
- package/skills/developing-cpp/source/03-design/examples/coordinate-table.md +31 -0
- package/skills/developing-cpp/source/03-design/examples/header-place.md +26 -0
- package/skills/developing-cpp/source/03-design/examples/library-boundary.md +30 -0
- package/skills/developing-cpp/source/03-design/examples/library-split.md +7 -0
- package/skills/developing-cpp/source/03-design/examples/move-result.md +39 -0
- package/skills/developing-cpp/source/03-design/examples/preview-read.md +40 -0
- package/skills/developing-cpp/{references/layout.md → source/04-layout/01-source-layout.md} +4 -4
- package/skills/developing-cpp/{references/file-template.md → source/04-layout/02-file-template.md} +2 -2
- package/skills/developing-cpp/{references/declaration-order.md → source/04-layout/03-declaration-order.md} +4 -4
- package/skills/developing-cpp/source/05-language/01-language-basics/01-objects.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/02-initialization.md +19 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/03-deduction.md +6 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/04-const.md +15 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/05-expressions.md +8 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/06-functions.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/07-classes.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/08-raii.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/09-templates.md +20 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/10-checklist.md +9 -0
- package/skills/developing-cpp/source/05-language/02-value-types/01-width.md +11 -0
- package/skills/developing-cpp/source/05-language/02-value-types/02-signed.md +20 -0
- package/skills/developing-cpp/source/05-language/02-value-types/03-integer-roles.md +23 -0
- package/skills/developing-cpp/source/05-language/02-value-types/04-enum.md +17 -0
- package/skills/developing-cpp/source/05-language/02-value-types/05-strong-type.md +17 -0
- package/skills/developing-cpp/source/05-language/02-value-types/06-const-method.md +17 -0
- package/skills/developing-cpp/source/05-language/02-value-types/07-nodiscard.md +9 -0
- package/skills/developing-cpp/source/05-language/02-value-types/08-comparison.md +4 -0
- package/skills/developing-cpp/source/05-language/02-value-types/09-subscript.md +14 -0
- package/skills/developing-cpp/source/05-language/02-value-types/10-float-bool.md +7 -0
- package/skills/developing-cpp/source/05-language/02-value-types/11-checklist.md +8 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/01-containers.md +23 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/02-access.md +8 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/03-iteration.md +12 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/04-algorithms.md +20 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/05-string.md +7 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/06-format.md +44 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/07-time.md +3 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/08-random.md +3 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/09-files.md +4 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/10-checklist.md +9 -0
- package/skills/developing-cpp/source/05-language/04-performance/01-measure.md +15 -0
- package/skills/developing-cpp/source/05-language/04-performance/02-complexity.md +7 -0
- package/skills/developing-cpp/source/05-language/04-performance/03-memory.md +8 -0
- package/skills/developing-cpp/source/05-language/04-performance/04-undefined-behavior.md +20 -0
- package/skills/developing-cpp/source/05-language/04-performance/05-pitfalls.md +11 -0
- package/skills/developing-cpp/source/05-language/04-performance/06-false-optimizations.md +6 -0
- package/skills/developing-cpp/source/05-language/04-performance/07-checklist.md +8 -0
- package/skills/developing-cpp/{references/glossary.md → source/05-language/glossary/language-terms.md} +2 -2
- package/skills/developing-cpp/{references/naming.md → source/06-naming/01-identifier-names.md} +8 -3
- package/skills/developing-cpp/source/06-naming/examples/naming-accessor.md +17 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-basic.md +3 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-current-prefix.md +19 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-lookup.md +18 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-lower-layer.md +39 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-multiple-return.md +20 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-player-mark.md +17 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-predicate.md +18 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-rename.md +5 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-responsibility.md +19 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-role.md +21 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-state-change.md +57 -0
- package/skills/developing-cpp/{references/comments.md → source/07-comments/01-comment-rules.md} +55 -32
- package/skills/developing-cpp/source/07-comments/examples/declaration-class.md +44 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-constructor.md +22 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-enum.md +24 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-file.md +66 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-function.md +100 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-member.md +25 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-predicate.md +13 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-struct.md +18 -0
- package/skills/developing-cpp/source/07-comments/examples/implementation-body.md +42 -0
- package/skills/developing-cpp/source/07-comments/examples/implementation-todo.md +16 -0
- package/skills/developing-cpp/source/07-comments/glossary/comment-terms.md +75 -0
- package/skills/developing-cpp/{references/exceptions.md → source/08-exceptions/01-throw-rules.md} +46 -8
- package/skills/developing-cpp/source/08-exceptions/examples/throw-boundary.md +28 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-build-mode.md +38 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-catch.md +21 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-destructor.md +21 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-format.md +13 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-message.md +17 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-when.md +27 -0
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +9 -9
- package/skills/developing-cpp/steps/03-declaration.md +11 -10
- package/skills/developing-cpp/steps/04-implementation.md +15 -13
- package/skills/engineering/SKILL.md +84 -0
- package/skills/{configuring-cmake/references/cmake.md → engineering/cmake/01-targets/01-rules.md} +3 -3
- package/skills/engineering/cmake/01-targets/examples/glob.md +22 -0
- package/skills/engineering/cmake/01-targets/examples/rpath.md +24 -0
- package/skills/{configuring-cmake/examples/cmake.md → engineering/cmake/01-targets/examples/target.md} +1 -69
- package/skills/{configuring-cmake/references/modules.md → engineering/cmake/02-modules/01-functions.md} +2 -2
- package/skills/{configuring-cmake/references/cxx-standard.md → engineering/cmake/03-standard/01-cxx-standard.md} +1 -1
- package/skills/engineering/cmake/03-standard/examples/standard.md +19 -0
- package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/README.md +6 -6
- package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++20/README.md +1 -1
- package/skills/{building-cpp/references/linking.md → engineering/cmake/linking/01-libraries.md} +1 -48
- package/skills/engineering/cmake/linking/02-rpath.md +17 -0
- package/skills/engineering/cmake/linking/03-undefined-reference.md +14 -0
- package/skills/engineering/cmake/linking/04-inspect.md +15 -0
- package/skills/{configuring-cmake → engineering/cmake}/steps/01-targets.md +3 -3
- package/skills/{configuring-cmake → engineering/cmake}/steps/02-modules.md +2 -2
- package/skills/{configuring-cmake → engineering/cmake}/steps/03-check.md +2 -2
- package/skills/{building-cpp/references/testing.md → engineering/cmake/testing/ctest.md} +3 -3
- package/skills/engineering/formatting/02-clang-format.md +14 -0
- package/skills/engineering/formatting/03-clang-tidy.md +51 -0
- package/skills/engineering/formatting/04-clangd.md +83 -0
- package/skills/engineering/formatting/examples/clang-format.md +17 -0
- package/skills/engineering/formatting/examples/clang-tidy.md +64 -0
- package/skills/{building-cpp/examples/tooling.md → engineering/formatting/examples/clangd.md} +1 -80
- package/templates/cpp-workflow-project/SKILL.md +34 -4
- package/skills/building-cpp/references/static-analysis.md +0 -148
- package/skills/configuring-cmake/SKILL.md +0 -68
- package/skills/developing-cpp/examples/comments-declaration.md +0 -313
- package/skills/developing-cpp/examples/comments-implementation.md +0 -66
- package/skills/developing-cpp/examples/exceptions.md +0 -173
- package/skills/developing-cpp/examples/naming.md +0 -150
- package/skills/developing-cpp/examples/structure.md +0 -155
- package/skills/developing-cpp/references/comment-terms.md +0 -65
- package/skills/developing-cpp/references/language.md +0 -121
- package/skills/developing-cpp/references/performance.md +0 -83
- package/skills/developing-cpp/references/standard-library.md +0 -101
- package/skills/developing-cpp/references/types.md +0 -154
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/CompileSettings.cmake +0 -0
- /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/LinkSettings.cmake +0 -0
- /package/skills/{building-cpp/references/encoding.md → engineering/formatting/01-encoding.md} +0 -0
- /package/skills/{building-cpp → engineering/formatting}/scripts/checking-encoding.mjs +0 -0
|
@@ -7,17 +7,25 @@ metadata:
|
|
|
7
7
|
|
|
8
8
|
# developing-cpp
|
|
9
9
|
|
|
10
|
-
本技能处理换一台机器也不会改的源码内容:可观察行为、库与程序的边界、声明、命名、异常和源码注释。文件槽位与守卫以 `
|
|
10
|
+
本技能处理换一台机器也不会改的源码内容:可观察行为、库与程序的边界、声明、命名、异常和源码注释。文件槽位与守卫以 `source/04-layout/02-file-template.md` 为准;排版、命名、头文件与包含顺序以 `source/02-style/01-google-cpp-style.md` 的 Google 基线为准。异常条文取自 C++ Core Guidelines 的 Error handling,只约束开发期的源码,不进入插件运行时。
|
|
11
|
+
|
|
12
|
+
下面这些文件不归本技能:
|
|
13
|
+
|
|
14
|
+
- `CMakeLists.txt` 与 `cmake/`:开 `engineering`。
|
|
15
|
+
- `running-build.sh`、`.clang-*`、`.vscode/` 和 `environment.json`:开 `building-cpp`,本技能只读其中的 `standard`。
|
|
16
|
+
- `.qmd`、仓库 README、Python 和非 C++ 文档:不打开本技能。
|
|
17
|
+
|
|
18
|
+
只改函数体、命名、异常或注释且入口命令可运行时,定实现后只开 `building-cpp` 的「通过检查」;新增或移动源文件、改库与程序的边界、补测试 target 时,声明和实现可以先写,但完成前必须开 `engineering` 把文件写进 CMake,再开 `building-cpp` 定工具配置并通过检查。
|
|
11
19
|
|
|
12
20
|
## 适用场景
|
|
13
21
|
|
|
14
22
|
存在项目技能 `cpp-workflow-project` 时先读它的 `SKILL.md`:工程名、库与程序的边界、阶段表、术语增补、入口命令与语言档位。项目技能没有写档位时,才读 `environment.json` 的 `standard`;两者都没有时才用本技能的新工程默认 C++23。声明 C++20 的工程里不要写 C++23 才有的设施;没有项目技能时门禁不会放出本技能,不要猜项目约定。
|
|
15
23
|
|
|
16
|
-
源码和构建文件的编码以 `
|
|
24
|
+
源码和构建文件的编码以 `engineering` 的 `formatting/01-encoding.md` 为准;行尾与 `.clang-format` 的 `LineEnding` 跟仓库根 `.gitattributes` 和 `.editorconfig`。
|
|
17
25
|
|
|
18
26
|
## 三种操作
|
|
19
27
|
|
|
20
|
-
一次只选一种,范围(单点、整段)是任务属性。操作与域的判据、边界和交接见 `
|
|
28
|
+
一次只选一种,范围(单点、整段)是任务属性。操作与域的判据、边界和交接见 `source/01-editing/01-edit-operations.md`;四类改动是域,不是操作。
|
|
21
29
|
|
|
22
30
|
| 操作 | 什么时候用 | 从哪步进 |
|
|
23
31
|
| --- | --- | --- |
|
|
@@ -36,11 +44,27 @@ metadata:
|
|
|
36
44
|
| 3 | 定声明 | `steps/03-declaration.md` | 写命名、声明顺序、头文件契约和声明注释 |
|
|
37
45
|
| 4 | 定实现 | `steps/04-implementation.md` | 写 `.cpp` 函数体的操作顺序、分组和实现注释 |
|
|
38
46
|
|
|
39
|
-
每步只打开同号的一个步骤文件。判不准才打开 `
|
|
47
|
+
每步只打开同号的一个步骤文件。判不准才打开 `source/` 里与该步对应的案例;完整条文只在排错、整目录重构,或步骤里明确指向某一条时按需打开。性能问题读 `source/05-language/04-performance/01-measure.md`,标准库设施选型读 `source/05-language/03-standard-library/01-containers.md`,注释术语判不准读 `source/07-comments/glossary/comment-terms.md`,链接与产物的细节读 `engineering` 的 `cmake/linking/01-libraries.md`。
|
|
48
|
+
|
|
49
|
+
### 入口表
|
|
50
|
+
|
|
51
|
+
先按「改哪一类东西」选一行,再打开那一行点名的一份;同一步骤里的细节文件留到它真正需要时再开。
|
|
52
|
+
|
|
53
|
+
| 改什么 | 从哪一份进 |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| 行为、库与程序的边界 | `steps/01-behavior.md`、`steps/02-boundary.md`;案例在 `source/03-design/examples/` |
|
|
56
|
+
| 文件槽位、包含守卫、声明顺序 | `source/04-layout/02-file-template.md`、`source/04-layout/03-declaration-order.md` |
|
|
57
|
+
| 命名与改名 | `source/06-naming/01-identifier-names.md`;案例在 `source/06-naming/examples/` |
|
|
58
|
+
| 声明注释与实现注释 | `source/07-comments/01-comment-rules.md`;案例在 `source/07-comments/examples/` |
|
|
59
|
+
| 异常规则与异常案例 | `source/08-exceptions/01-throw-rules.md`;案例在 `source/08-exceptions/examples/` |
|
|
60
|
+
| 语言细节、类型、标准库、性能 | `source/05-language/` 下按主题只开一份:`01-language-basics/01-objects.md`、`02-value-types/01-width.md`、`03-standard-library/01-containers.md`、`04-performance/01-measure.md` |
|
|
61
|
+
| 操作判不准、规则冲突 | `source/01-editing/01-edit-operations.md`、`source/01-editing/02-rule-priority.md` |
|
|
62
|
+
|
|
63
|
+
案例目录里没有数字前缀的文件按语义选:数字前缀只表示同一主题内的阅读顺序,不是案例编号。
|
|
40
64
|
|
|
41
65
|
本轮激活按三条上限走:
|
|
42
66
|
|
|
43
|
-
- 只开当前步对应的一个步骤文件;同一改动域内不同时展开两个技能的步骤。跨域按 `源码 →
|
|
67
|
+
- 只开当前步对应的一个步骤文件;同一改动域内不同时展开两个技能的步骤。跨域按 `源码 → engineering → building-cpp` 的顺序逐段进入,前一段结论落下再开下一段。
|
|
44
68
|
- 条文只开该步点名的一份;同号案例留到下一次激活。
|
|
45
69
|
- 标准档只打开项目技能声明的那一档,没有声明时用本技能已有的新工程默认,不把其余标准档读进来。
|
|
46
70
|
|
|
@@ -48,24 +72,24 @@ metadata:
|
|
|
48
72
|
|
|
49
73
|
### 四类改动
|
|
50
74
|
|
|
51
|
-
四类改动是操作内部的域:操作定入口,域定层次。四域的定义、边界与次序见 `
|
|
75
|
+
四类改动是操作内部的域:操作定入口,域定层次。四域的定义、边界与次序见 `source/01-editing/01-edit-operations.md`;一次改动混了两类时,按「内容 → 结构 → 版式 → 措辞」拆开做,前一类落定并经检查再进下一类。
|
|
52
76
|
|
|
53
77
|
| 类型 | 典型请求 | 入口步骤 |
|
|
54
78
|
| --- | --- | --- |
|
|
55
|
-
|
|
|
56
|
-
|
|
|
57
|
-
|
|
|
58
|
-
|
|
|
79
|
+
| 内容 | 新增类型、函数或文件;改可观察行为;改库与程序的边界;补构造后置条件与成员事实 | `steps/01-behavior.md` |
|
|
80
|
+
| 结构 | 换文件位置、拆头文件、调声明顺序、补测试 target;改注释位置与行尾形态 | `steps/03-declaration.md` |
|
|
81
|
+
| 版式 | 只改缩进、行宽、包含顺序或注释符号 | `steps/03-declaration.md` |
|
|
82
|
+
| 措辞 | 只改命名、只改注释措辞 | `steps/03-declaration.md` |
|
|
59
83
|
|
|
60
84
|
只改格式时不动行为,只改注释时不动签名;条文由入口步骤按需打开。
|
|
61
85
|
|
|
62
|
-
同一处命中多条规则时,先按 `
|
|
86
|
+
同一处命中多条规则时,先按 `source/01-editing/02-rule-priority.md` 的五级次序判:行为与契约 > 结构 > 清晰 > 一致 > 形式。高层未解决,不先做低层润色;低层改动碰到高层事实就停手退回。
|
|
63
87
|
|
|
64
88
|
## 完成判据
|
|
65
89
|
|
|
66
90
|
- [ ] 第 1 步的行为句能被用户或测试验收;有阶段表时只覆盖对应行,没有时只覆盖用户点名的行为。
|
|
67
91
|
- [ ] 第 2 步的库 target 不依赖终端、GUI 或脚本;程序 target 只做装配和输入输出;共享抽象有第二个真实使用方。
|
|
68
|
-
- [ ] 第 3 步的公共头文件自包含,命名、声明顺序和异常声明符合 `
|
|
92
|
+
- [ ] 第 3 步的公共头文件自包含,命名、声明顺序和异常声明符合 `source/` 的条文。
|
|
69
93
|
- [ ] 第 4 步的实现只兑现声明;资源由 RAII 管理;失败是正常结果时用返回值,函数无法完成任务时抛异常,不拿异常做正常分支。
|
|
70
|
-
- [ ] 源码注释符合 `
|
|
94
|
+
- [ ] 源码注释符合 `source/07-comments/01-comment-rules.md` 的四节规范,`node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>` 无输出。
|
|
71
95
|
- [ ] 没有改 `CMakeLists.txt`、`.clang-*`、构建脚本,也没有把外部资料路径或项目专属类名写进通用条文。
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// 只读注释审计:按
|
|
2
|
+
// 只读注释审计:按 source/07-comments/01-comment-rules.md 的硬规则检查 C/C++ 源文件。
|
|
3
3
|
//
|
|
4
|
-
// 检查项:Doxygen
|
|
4
|
+
// 检查项:Doxygen 文件头、注释行宽度、列表缩进、标签、标签间空行、注释与声明的贴合、
|
|
5
5
|
// 类型注释存在性、成员功能清单、设计论证(方案对比)、否定式边界、领域尺寸、谓词首句、@param 完整性、枚举成员注释一致性、枚举行尾句式、数据成员主注释位置。
|
|
6
6
|
// 不重写任何文件。
|
|
7
7
|
//
|
|
@@ -89,7 +89,7 @@ function parseArgs(argv) {
|
|
|
89
89
|
return options
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
/**
|
|
92
|
+
/** 从指定目录的 .clang-format 读取 ColumnLimit;没有配置时返回 undefined。 */
|
|
93
93
|
function readColumnLimit(root) {
|
|
94
94
|
const dir = statSync(root).isDirectory() ? root : path.dirname(root)
|
|
95
95
|
try {
|
|
@@ -101,6 +101,20 @@ function readColumnLimit(root) {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/** 从文件所在目录向上查找最近的 .clang-format,直到工程根。 */
|
|
105
|
+
function readColumnLimitNear(file, root) {
|
|
106
|
+
const stop = path.resolve(root)
|
|
107
|
+
let dir = path.dirname(path.resolve(file))
|
|
108
|
+
while (true) {
|
|
109
|
+
const limit = readColumnLimit(dir)
|
|
110
|
+
if (limit !== undefined) return limit
|
|
111
|
+
if (dir === stop) return undefined
|
|
112
|
+
const parent = path.dirname(dir)
|
|
113
|
+
if (parent === dir) return undefined
|
|
114
|
+
dir = parent
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
104
118
|
/** 中文按两列计宽,用于判断渲染后的行宽。 */
|
|
105
119
|
export function displayWidth(text) {
|
|
106
120
|
let width = 0
|
|
@@ -311,9 +325,12 @@ export function checkText(text, options = {}) {
|
|
|
311
325
|
const trimmed = line.trim()
|
|
312
326
|
if (trimmed.startsWith('///') || (trimmed.startsWith('//') && !trimmed.startsWith('//!'))) {
|
|
313
327
|
if (displayWidth(trimmed) > width) add(index + 1, 'comment-width', `注释行 ${displayWidth(trimmed)} 列,超过 ${width} 列`)
|
|
328
|
+
if (/^\/\/\/ {0,2}- /.test(trimmed)) {
|
|
329
|
+
add(index + 1, 'list-indent', '注释列表项要在 /// 后空三格再写 -:`/// - `,与总起句拉开层级')
|
|
330
|
+
}
|
|
314
331
|
for (const tag of FORBIDDEN_TAGS) {
|
|
315
332
|
if (options.allowTags?.has(tag.slice(1))) continue
|
|
316
|
-
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按
|
|
333
|
+
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按 source/07-comments/01-comment-rules.md 的标签表改写`)
|
|
317
334
|
}
|
|
318
335
|
}
|
|
319
336
|
}
|
|
@@ -327,7 +344,12 @@ export function checkText(text, options = {}) {
|
|
|
327
344
|
if (displayWidth(rendered) > width) add(doc.start + 1, 'comment-width', `注释行 ${displayWidth(rendered)} 列,超过 ${width} 列`)
|
|
328
345
|
for (const tag of FORBIDDEN_TAGS) {
|
|
329
346
|
if (options.allowTags?.has(tag.slice(1))) continue
|
|
330
|
-
if (content.includes(tag)) add(doc.start + 1, 'forbidden-tag', `禁止使用 ${tag},按
|
|
347
|
+
if (content.includes(tag)) add(doc.start + 1, 'forbidden-tag', `禁止使用 ${tag},按 source/07-comments/01-comment-rules.md 的标签表改写`)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
for (let raw = doc.start; raw <= doc.end; raw += 1) {
|
|
351
|
+
if (/^\s*\* {0,2}- /.test(lines[raw])) {
|
|
352
|
+
add(raw + 1, 'list-indent', '块注释列表项要在 * 后空三格再写 -:` * - `,与总起句拉开层级')
|
|
331
353
|
}
|
|
332
354
|
}
|
|
333
355
|
if (!doc.contents.some((content) => content.includes('@file')) && hasTagGap(doc.contents)) {
|
|
@@ -597,7 +619,12 @@ function main(argv) {
|
|
|
597
619
|
process.stderr.write(`错误:读取 ${file} 失败:${error.message}\n`)
|
|
598
620
|
return EXIT.usage
|
|
599
621
|
}
|
|
600
|
-
|
|
622
|
+
const width = options.widthExplicit
|
|
623
|
+
? options.width
|
|
624
|
+
: (readColumnLimitNear(file, root) ?? options.width)
|
|
625
|
+
for (const issue of checkText(text, { ...options, width, fileName: path.relative(root, file) || file })) {
|
|
626
|
+
results.push({ file: path.relative(root, file) || file, ...issue })
|
|
627
|
+
}
|
|
601
628
|
}
|
|
602
629
|
if (options.json) {
|
|
603
630
|
process.stdout.write(`${JSON.stringify({ root, files: files.length, issues: results }, null, 2)}\n`)
|
package/skills/developing-cpp/{references/change-model.md → source/01-editing/01-edit-operations.md}
RENAMED
|
@@ -10,22 +10,40 @@
|
|
|
10
10
|
| 改写 | 可观察行为与契约不变,组织、顺序或位置不对 | 移动、删除、换序、重写块段;保留事实与论证线 | 新增事实、数字、命令、结论或行为;改变否定、条件、范围、时间、归属 |
|
|
11
11
|
| 润色 | 只改措辞、命名或注释 | 删复述、换词、拆句、统一术语 | 改结构、改事实、加句子或加信息;把可能或通常改成确定 |
|
|
12
12
|
|
|
13
|
+
## 判不准时
|
|
14
|
+
|
|
15
|
+
按顺序问,第一问为是就是写作:
|
|
16
|
+
|
|
17
|
+
1. 缺一个可观察行为、契约或事实吗?
|
|
18
|
+
2. 现有组织、顺序或位置,读者能走通吗?不能走通是改写。
|
|
19
|
+
3. 只改措辞、命名或注释,行为与结构会变吗?不会变是润色。
|
|
20
|
+
|
|
21
|
+
三问都否,是只读审查:只报告,不改文件,不调构建。
|
|
22
|
+
|
|
23
|
+
## 越界信号
|
|
24
|
+
|
|
25
|
+
- 润色时想补声明里没有的事实:停;改判写作,或把事实退回源码与用户。
|
|
26
|
+
- 改写时发现原句的事实、条件或数字有误:停;改写不修事实,报告后按写作处理。
|
|
27
|
+
- 写作时发现行为与边界都对、只是措辞差:从第 3 步按润色进,不重走前两步。
|
|
28
|
+
- 润色时发现两处注释讲同一件事:删重复属于润色,换位置属于结构。
|
|
29
|
+
- 一轮里三种操作都出现:按写作 → 改写 → 润色拆开,前一段落定并经检查再进下一段。
|
|
30
|
+
|
|
13
31
|
范围随任务定:单点修正只改判为不合格的一处;整段重构先定删留,再改块段,最后逐句。只读审查不改文件,按操作判缺什么,只报告;不调构建、渲染或回执。
|
|
14
32
|
|
|
15
33
|
## 四个域
|
|
16
34
|
|
|
17
35
|
| 域 | 回答的问题 | 可以改 | 不能改 |
|
|
18
36
|
| --- | --- | --- | --- |
|
|
19
|
-
|
|
|
20
|
-
|
|
|
21
|
-
|
|
|
22
|
-
|
|
|
37
|
+
| 内容 | 这次要保证什么新事实或新行为? | 行为、契约、后置条件、成员事实 | 已有事实、否定、条件、数字、引文 |
|
|
38
|
+
| 结构 | 它放在哪一层、哪个文件、什么顺序? | 文件位置、模块边界、声明顺序、注释位置 | 行为、措辞、命名 |
|
|
39
|
+
| 版式 | 它长什么样? | 缩进、行宽、空行、注释符号、标签格式 | 标识符、字符串、命令、语义 |
|
|
40
|
+
| 措辞 | 这句话是否准确、必要、可读? | 命名、用词、句式、复述、术语统一 | 结构、事实、段落顺序、信息量 |
|
|
23
41
|
|
|
24
|
-
|
|
42
|
+
四域按「内容 → 结构 → 版式 → 措辞」推进。前一层没落定,不进入下一层;后一层发现前一层没做完,退回那一层,不在当前层补。`source/01-editing/02-rule-priority.md` 的五级次序是冲突时的判据:行为与契约 > 结构 > 清晰 > 一致 > 形式。
|
|
25
43
|
|
|
26
44
|
## 跨技能交接
|
|
27
45
|
|
|
28
|
-
源码 → `
|
|
46
|
+
源码 → `engineering` → `building-cpp`。一次改动同时碰到三层时,先定源码行为与边界,再写 target,最后配工具并跑检查;前一段结论落下再开下一段。只改注释时,声明注释 → 实现注释 → CMake 契约注释 → 构建诊断,逐层按四域走。
|
|
29
47
|
|
|
30
48
|
## 写作边界
|
|
31
49
|
|
|
@@ -36,7 +54,7 @@
|
|
|
36
54
|
|
|
37
55
|
## 依据
|
|
38
56
|
|
|
39
|
-
- IPEd《Types of editing》把编辑分为 substantive editing、copyediting 与 proofreading
|
|
57
|
+
- IPEd《Types of editing》把编辑分为 substantive editing、copyediting 与 proofreading;本模型把「写作、改写、润色」对应到「新增、重组、措辞」三档,版式独立成形式层。
|
|
40
58
|
- Google C++ Style Guide、LLVM Coding Standards 与 ISO C++ Core Guidelines 的注释条目都要求写意图、用法与陷阱,不把代码翻译成自然语言。
|
|
41
59
|
- 这些资料只解释模型,不替代项目技能、`environment.json` 或用户明确要求。
|
|
42
60
|
|
|
@@ -47,9 +65,9 @@
|
|
|
47
65
|
## 通用自检
|
|
48
66
|
|
|
49
67
|
1. 操作判对了吗:这次是新增行为、重组结构,还是只改措辞?范围是单点还是整段?
|
|
50
|
-
2.
|
|
68
|
+
2. 域判对了吗:这次只动一层,还是混了两层?混了时是否按内容 → 结构 → 版式 → 措辞拆开?
|
|
51
69
|
3. 事实有没有变:有没有新增数字、命令、结论、引文,或把「可能」「通常」改成确定?
|
|
52
70
|
4. 边界写正了吗:有没有「不检查…」清单?前置条件是否写成「调用方保证…」「仅接受…」或 `@pre`?
|
|
53
71
|
5. 原样保留了吗:术语、命令、标识符、路径、URL 与围栏内容是否未被改写?
|
|
54
|
-
6. 次序守住了吗:前一层是否已落定并经检查,后一层才开始?冲突是否按 `priority.md` 判?
|
|
72
|
+
6. 次序守住了吗:前一层是否已落定并经检查,后一层才开始?冲突是否按 `source/01-editing/02-rule-priority.md` 判?
|
|
55
73
|
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
## 头文件
|
|
14
14
|
|
|
15
|
-
- 每个头文件用 `#define` 守卫,符号由项目源码树全路径生成:`<PROJECT>_<PATH>_<FILE>_H_`,例如 `FOO_BAR_BAZ_H_`。不要用 `#pragma once`,也不要两者并存;模板见 `
|
|
15
|
+
- 每个头文件用 `#define` 守卫,符号由项目源码树全路径生成:`<PROJECT>_<PATH>_<FILE>_H_`,例如 `FOO_BAR_BAZ_H_`。不要用 `#pragma once`,也不要两者并存;模板见 `source/04-layout/02-file-template.md`。
|
|
16
16
|
- 头文件必须自包含:自己用到的类型、宏、标准库头都直接包含,不依赖调用方的包含顺序。
|
|
17
17
|
- `.cpp` 第一个包含自己的配对头,然后按下面的顺序包含。
|
|
18
18
|
- 包含顺序:配对头 → C 系统头 → C++ 标准库头 → 其他库头 → 本项目头。每组之间空一行,组内按字母序;不要依赖 `clang-format` 的 `IncludeCategories` 自定义分组,本项目不再覆盖 Google 预设。
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
|
|
25
25
|
## 命名
|
|
26
26
|
|
|
27
|
-
大小写、前缀、成员尾随 `_`、常量与枚举子的 `k` 前缀、命名空间与文件名的完整规则表,以及访问器、谓词、`Try`、映射函数、`current_` 前缀的语义角色,见 `
|
|
27
|
+
大小写、前缀、成员尾随 `_`、常量与枚举子的 `k` 前缀、命名空间与文件名的完整规则表,以及访问器、谓词、`Try`、映射函数、`current_` 前缀的语义角色,见 `source/06-naming/01-identifier-names.md`。本文只保留两处 Google 原文允许选择、而本项目需要统一的点:
|
|
28
28
|
|
|
29
29
|
- 访问器、修改器 Google 允许 PascalCase 或 snake_case;本项目统一 PascalCase,同一 API 不混用两套。
|
|
30
30
|
- 测试名用 PascalCase 描述行为,例如 `RejectsOutOfRangeTask`;测试文件用 snake_case。
|
|
@@ -34,13 +34,14 @@
|
|
|
34
34
|
- 纯数据聚合用 `struct`;有不变量的类型用 `class`,不变量在构造函数和公共成员函数里维持。
|
|
35
35
|
- 单参数构造函数写 `explicit`;零参数构造函数不写。
|
|
36
36
|
- 默认成员初始化写在声明处(NSDMI),不要在构造函数初始化列表里重复。
|
|
37
|
-
- 默认用花括号初始化:`int count{};`、`std::vector<int> values{1, 2, 3}
|
|
37
|
+
- 默认用花括号初始化:`int count{};`、`std::vector<int> values{1, 2, 3};`。成员声明处一律写 `{}`;只有需要拷贝初始化或 `auto` 推导时用 `=`;`name = {{` 只用于多行聚合常量表,单个对象和空初始化仍写 `count{}`;成员写 `=` 的例外见 `source/05-language/01-language-basics/02-initialization.md`;不要用 `()` 初始化普通对象。
|
|
38
38
|
- 类声明顺序:public → protected → private;类型与别名 → 静态常量 → 工厂 → 构造/赋值/析构 → 其他函数 → 数据成员。
|
|
39
39
|
- 用户声明的拷贝/移动/析构函数在头文件声明、在 `.cpp` 定义;只有零参数且默认实现足够时才写 `= default`。
|
|
40
|
-
- 查询成员函数标尾部 `const`;返回结果对象的 API 加 `[[nodiscard]]`,不要给所有非 `void` 函数机械添加。标注位置与 `friend` 的例外见 `
|
|
40
|
+
- 查询成员函数标尾部 `const`;返回结果对象的 API 加 `[[nodiscard]]`,不要给所有非 `void` 函数机械添加。标注位置与 `friend` 的例外见 `source/05-language/02-value-types/07-nodiscard.md`。
|
|
41
41
|
- 所有权优先 `std::unique_ptr`;没有共享所有权的理由时不用 `std::shared_ptr`。禁止裸 `new` / `delete`。
|
|
42
42
|
- 禁止 C 风格强制转换;用 `static_cast`、`const_cast`、`reinterpret_cast`、`std::bit_cast`,并让 `clang-tidy` 的 `google-readability-casting` 兜底。
|
|
43
43
|
- `.cpp` 里不需要外部引用的 `constexpr` 直接放具名命名空间;仅一个函数使用时才用函数内 `static constexpr`;函数和类型仍放匿名命名空间。
|
|
44
|
+
- 一个 `.cpp` 只写一个匿名命名空间,公开函数的定义放在它后面。
|
|
44
45
|
- 常量要出 `.cpp` 时按四步升:1) 其他翻译单元要的是算出来的答案,给类加查询、表留 `.cpp`;2) 要同一份坐标但仍不是公开契约,放组件 `src/` 的私有头;3) 对方必须写 `Board::kLines` 且这是类型的稳定事实,才用类内 `public static constexpr`;不依赖某个对象的公开常量则用命名空间 `inline constexpr`;4) 第二使用方复用同一套坐标时再抽共享组件。
|
|
45
46
|
|
|
46
47
|
## 函数
|
|
@@ -60,15 +61,15 @@
|
|
|
60
61
|
- 混合优先级算术加显式括号,消除 `readability-math-missing-parentheses`。
|
|
61
62
|
- 内建整数默认用 `int`;需要确定宽度时用 `<cstdint>` 的 `std::int32_t`、`std::int64_t` 等(可省略 `std::` 前缀),可能到 2^31 的值用 64 位;`std::size_t` / `std::ptrdiff_t` 只用于大小和指针差。不要用无符号类型表达「这个值不会为负」。
|
|
62
63
|
- 封闭状态码用 `enum class`;底层类型默认 `int`,只有确实需要缩小存储时才写固定底层类型并说明理由。枚举值总是带作用域:`OrderResult::kSuccess`。
|
|
63
|
-
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `
|
|
64
|
+
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `source/07-comments/01-comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
64
65
|
- 浮点不用 `==` 比较计算结果;按量纲给容差,金额用最小货币单位的整数。
|
|
65
|
-
- 数值宽度、符号、枚举、强类型、`const` 成员和下标访问的完整规则见 `
|
|
66
|
+
- 数值宽度、符号、枚举、强类型、`const` 成员和下标访问的完整规则见 `source/05-language/02-value-types/07-nodiscard.md`;初始化、RAII、移动和模板见 `source/05-language/01-language-basics/`。
|
|
66
67
|
|
|
67
68
|
## 异常
|
|
68
69
|
|
|
69
|
-
Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指导中按 `
|
|
70
|
+
Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指导中按 `source/08-exceptions/01-throw-rules.md` 采用 C++ Core Guidelines 的 Error handling,插件运行时不编译、不执行这套策略。其余条目仍以 Google 原文为准。
|
|
70
71
|
|
|
71
|
-
异常消息的组织方式见 `
|
|
72
|
+
异常消息的组织方式见 `source/08-exceptions/01-throw-rules.md` 的「消息」节。
|
|
72
73
|
|
|
73
74
|
## 输出与格式化
|
|
74
75
|
|
|
@@ -81,5 +82,5 @@ Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指
|
|
|
81
82
|
## 注释
|
|
82
83
|
|
|
83
84
|
- 注释写意图、契约、取舍,不复述代码。
|
|
84
|
-
- 文件头、声明注释、实现注释与配置注释的格式见 `
|
|
85
|
+
- 文件头、声明注释、实现注释与配置注释的格式见 `source/07-comments/01-comment-rules.md`。
|
|
85
86
|
- TODO 统一写 `// TODO: <阶段或 issue> - <要做什么>`;不要写 `@todo`、`FIXME` 或只有「参考」没有动作的占位。
|
package/skills/developing-cpp/{references/project-design.md → source/03-design/01-project-design.md}
RENAMED
|
@@ -24,16 +24,23 @@
|
|
|
24
24
|
- 位置是行和列,不是格子的另一种取值。胜负、轮到谁、已下过的序列属于棋盘或对局,不要塞进位置类型。坐标类型单独一个头文件;持有尺寸的类型包含它,不把坐标塞回尺寸类型里。
|
|
25
25
|
- 有不变量的格子用公开构造的类,默认是空,X 或 O 至多写入一次。私有构造函数只留给必须经过工厂才能建立的对象,不用来表达三种取值,也不用来藏起行和列。
|
|
26
26
|
- 一次写入能区分的失败都放进结果枚举。传入空取值是结果项,不抛异常。
|
|
27
|
-
-
|
|
27
|
+
- 只由某一个类型产生的结果枚举嵌在该类型里:格子的放置结果和对局的落子结果取值不同,不合成命名空间里的一个 `PlaceResult`。
|
|
28
|
+
- 调用方已经把完整值对象交进来时,下层直接用它,不再用散参数或查询重新拼出同一对象。可以校验,并在不符时返回原因;不要静默替换成自己重算的值。
|
|
29
|
+
- 构造值对象所需的只读查询留在拥有状态的一层;调用方用它组值对象,下层函数不重复推导。不要为了去掉查询,让每个调用方各自维护轮次副本。
|
|
30
|
+
- 范围检查在接收位置的那一层直接做,越界直接抛出;不要为了统一诊断把 `std::source_location` 穿过每个公共接口。
|
|
28
31
|
- 尺寸是某个类型的事实时,写成该类型的 `static constexpr`,例如 `Board::kRowCount`;不要再造一个只做 `static_cast<std::size_t>` 的别名常量。
|
|
29
32
|
- 一个局面的结果只用一个枚举和一个查询。不要把满盘、终局和赢家拆成三个入口,再由调用方拼回去。
|
|
30
33
|
|
|
34
|
+
## 读取边界
|
|
35
|
+
|
|
36
|
+
- 任务路由表登记到具体文件:声明、实现与测试各写清楚;不登记整个工程目录。目录路由会把几十个无关文件带进上下文,真正要改的格式化器、查询或异常消息反而被淹没。
|
|
37
|
+
|
|
31
38
|
## 封闭状态与结果
|
|
32
39
|
|
|
33
40
|
- 状态用连续取值的 `enum class`:`kPending`、`kActive`、`kDone`。
|
|
34
41
|
- 状态转换写成显式函数或规则对象,不用一串 `if` 直接改整数。
|
|
35
42
|
- “动作是否合法”和“动作执行结果”分开:合法性查询返回 `bool` 或原因,执行返回结果对象。
|
|
36
|
-
- 失败是调用方的正常结果(编号不存在、输入格式不对)时用返回值表达;函数无法完成自己的任务、或状态机进入不可能分支时抛异常,规则见 `
|
|
43
|
+
- 失败是调用方的正常结果(编号不存在、输入格式不对)时用返回值表达;函数无法完成自己的任务、或状态机进入不可能分支时抛异常,规则见 `source/08-exceptions/01-throw-rules.md`。
|
|
37
44
|
- 结果对象携带足够信息让调用方决定下一步,不要只返回 `bool` 再让调用方猜原因。
|
|
38
45
|
|
|
39
46
|
## 规则核心与外围
|
|
@@ -70,9 +77,9 @@
|
|
|
70
77
|
- 规则变化(优先级规则、合法动作)和界面变化(提示文案、按钮布局)是两个原因,放两个组件。
|
|
71
78
|
- 协议变化(JSON 字段)和规则变化是两个原因,协议转换放应用/适配层。
|
|
72
79
|
- 平台变化(Windows/WSL/macOS 路径、动态库查找)放构建脚本或适配层,不进核心。
|
|
73
|
-
-
|
|
80
|
+
- 只有调用方应当替换的外部依赖(时钟、随机源、输入输出)才通过接口注入;组件自己规定的策略留在实现里,测试断言可观察结果。
|
|
74
81
|
|
|
75
|
-
公共头文件只暴露稳定契约;实现细节放 `.cpp` 或私有头。目录形态、扩展名与头文件槽位以 `
|
|
82
|
+
公共头文件只暴露稳定契约;实现细节放 `.cpp` 或私有头。目录形态、扩展名与头文件槽位以 `source/04-layout/01-source-layout.md` 和 `source/04-layout/02-file-template.md` 为准。
|
|
76
83
|
|
|
77
84
|
## 什么时候抽共享库
|
|
78
85
|
|
|
@@ -92,7 +99,7 @@
|
|
|
92
99
|
|
|
93
100
|
- [ ] 领域值有明确类型,封闭集合用 `enum class`。
|
|
94
101
|
- [ ] 规则核心不依赖终端、GUI、Web、Python 或当前环境。
|
|
95
|
-
- [ ] 失败是正常结果时用返回值,函数无法完成任务时抛异常;异常类型和捕获方式符合 `
|
|
102
|
+
- [ ] 失败是正常结果时用返回值,函数无法完成任务时抛异常;异常类型和捕获方式符合 `source/08-exceptions/01-throw-rules.md`。
|
|
96
103
|
- [ ] 边界按变化原因划分,依赖方向从外向内。
|
|
97
104
|
- [ ] 共享抽象有第二个真实使用方和明确理由。
|
|
98
105
|
- [ ] 规则歧义已问清或记录了保守假设。
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# 正反例:行为句
|
|
2
|
+
|
|
3
|
+
**差**:实现一个 `TaskList` 类,用 `std::vector` 保存任务,支持添加、删除、按优先级排序,并在终端打印表格。
|
|
4
|
+
|
|
5
|
+
问题:把类型、容器、实现步骤混进行为;无法验收「表格」长什么样。
|
|
6
|
+
|
|
7
|
+
**好**:
|
|
8
|
+
|
|
9
|
+
```text
|
|
10
|
+
给定一组任务(每行「优先级 名称」),程序按优先级从高到低输出名称;
|
|
11
|
+
输入为空时输出「没有任务」并返回 0;某行格式非法时输出行号并返回 1。
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
可以写成测试:输入三行,断言输出顺序;输入空,断言提示与退出码;输入坏行,断言行号。
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# 正反例:固定坐标用表
|
|
2
|
+
|
|
3
|
+
**差**:每次判断成线,都用循环把八条线的坐标重新拼一遍;行、列、对角线混在一个循环里,读者要跟着跑两遍才知道哪八条。
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
// 差:用循环现写八条线,坐标藏在控制流里。
|
|
7
|
+
std::vector<std::vector<Position>> BuildLines() {
|
|
8
|
+
std::vector<std::vector<Position>> lines;
|
|
9
|
+
for (int row = 0; row < kRows; ++row) { /* ... */ }
|
|
10
|
+
// 列与两条对角线再各写一段
|
|
11
|
+
return lines;
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**好**:固定坐标写成具名命名空间里的 `constexpr` 常量表,`HasLine` 与 `PreviewWin` 共用,所以不放进任何一个函数体;格子用 `at()`,整条线 `all_of`、八条线 `any_of`,函数不标 `noexcept`。
|
|
16
|
+
|
|
17
|
+
```cpp
|
|
18
|
+
// board.cpp 的 namespace tictactoe:行、列、对角线三组,坐标固定。
|
|
19
|
+
constexpr std::array<std::array<Position, 3>, 8> kLines = {{
|
|
20
|
+
{{{0, 0}, {0, 1}, {0, 2}}}, // 行 0
|
|
21
|
+
{{{1, 0}, {1, 1}, {1, 2}}}, // 行 1
|
|
22
|
+
{{{2, 0}, {2, 1}, {2, 2}}}, // 行 2
|
|
23
|
+
{{{0, 0}, {1, 0}, {2, 0}}}, // 列 0
|
|
24
|
+
{{{0, 1}, {1, 1}, {2, 1}}}, // 列 1
|
|
25
|
+
{{{0, 2}, {1, 2}, {2, 2}}}, // 列 2
|
|
26
|
+
{{{0, 0}, {1, 1}, {2, 2}}}, // 对角线
|
|
27
|
+
{{{0, 2}, {1, 1}, {2, 0}}}, // 反对角线
|
|
28
|
+
}};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
每个格子要分别处理时仍写 `row` / `col` 循环,例如收集空格、终端打印。
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# 正反例:头文件位置
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```text
|
|
6
|
+
src/task_list.h // 公共头藏在 src,测试无法只依赖公共接口
|
|
7
|
+
src/detail.h // 私有头被测试直接包含
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```cpp
|
|
11
|
+
#include "src/task_list.h" // 从仓库根写到了实现目录
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
**好**:
|
|
15
|
+
|
|
16
|
+
```text
|
|
17
|
+
task_list/include/task_list.h // 公共头
|
|
18
|
+
task_list/src/task_list.cpp // 实现
|
|
19
|
+
task_list/src/detail.h // 私有头,只被本组件包含
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```cpp
|
|
23
|
+
#include "task_list.h" // 公共头按裸文件名包含
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
构建时把仓库根加入 include 搜索路径;不要用 `#include "../task_list/include/task_list.h"` 这种随文件位置漂移的写法。
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# 正反例:边界
|
|
2
|
+
|
|
3
|
+
**差**:库函数直接打印,程序只调用它。
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
// task_list.cpp(库)
|
|
7
|
+
void TaskList::Print() const {
|
|
8
|
+
for (const auto& task : tasks_) {
|
|
9
|
+
std::cout << task.priority << " " << task.name << "\n";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
问题:库依赖终端,测试要捕获 `std::cout`,GUI 或文件输出无法复用。
|
|
15
|
+
|
|
16
|
+
**好**:库返回数据,程序决定怎么显示。
|
|
17
|
+
|
|
18
|
+
```cpp
|
|
19
|
+
// task_list.h(库)
|
|
20
|
+
[[nodiscard]] std::vector<Task> SortedByPriority() const;
|
|
21
|
+
|
|
22
|
+
// app/cli.cpp(程序)
|
|
23
|
+
void PrintTasks(const std::vector<Task>& tasks, std::ostream& out) {
|
|
24
|
+
for (const auto& task : tasks) {
|
|
25
|
+
out << std::format("{} {}\n", task.priority, task.name);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
测试直接断言 `SortedByPriority()`;输出格式由程序测试负责。
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 正反例:落子结果不能丢,也不要重建一手
|
|
2
|
+
|
|
3
|
+
**差**:调用方已经拿着一手,`Game` 又用 `GetCurrentPlayer()` 按位置重建一手;`Board::Place` 的 `[[nodiscard]]` 结果还被 `static_cast<void>` 丢掉。重建的玩家与调用方给的不一致时,历史会记下棋盘没有接受的落子。
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
if (!board_.GetCell(move.position).IsEmpty()) {
|
|
7
|
+
return MoveResult::kOccupied;
|
|
8
|
+
}
|
|
9
|
+
const Move rebuilt{move.position, GetCurrentPlayer()};
|
|
10
|
+
static_cast<void>(board_.Place(rebuilt.position, PlayerToCellState(rebuilt.player)));
|
|
11
|
+
history_.push_back(rebuilt);
|
|
12
|
+
return MoveResult::kPlaced;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**好**:`Game::Place` 直接接收一手,先确认是否轮到该玩家,然后判终局,最后交给 `Board::Place`;坐标范围由棋盘检查。按 `Cell::PlaceResult` 分支写历史并换方。`kRejected` 说明棋盘收到了空取值,抛 `std::logic_error`;`kFinished` 仍由 `Game` 判,因为棋盘不知道整局已经结束。
|
|
16
|
+
|
|
17
|
+
```cpp
|
|
18
|
+
Game::MoveResult Game::Place(Move move) {
|
|
19
|
+
// 检查落子玩家是否为当前玩家。玩家不是当前玩家时,抛出 std::logic_error。
|
|
20
|
+
if (move.player != current_player_) {
|
|
21
|
+
throw std::logic_error{"Game::Place: move player is not the current player"};
|
|
22
|
+
}
|
|
23
|
+
// 检查对局是否已经结束。对局已经结束时,返回 kFinished。
|
|
24
|
+
if (board_.GetState() != BoardState::kInProgress) {
|
|
25
|
+
return MoveResult::kFinished;
|
|
26
|
+
}
|
|
27
|
+
switch (board_.Place(move.position, PlayerToCellState(move.player))) {
|
|
28
|
+
case Cell::PlaceResult::kUpdated:
|
|
29
|
+
history_.push_back(move);
|
|
30
|
+
current_player_ = GetOpponent(current_player_);
|
|
31
|
+
return MoveResult::kPlaced;
|
|
32
|
+
case Cell::PlaceResult::kOccupied:
|
|
33
|
+
return MoveResult::kOccupied;
|
|
34
|
+
case Cell::PlaceResult::kRejected:
|
|
35
|
+
throw std::logic_error{"Game::Place: board rejected the move"};
|
|
36
|
+
}
|
|
37
|
+
throw std::logic_error{"Game::Place: unhandled Cell::PlaceResult"};
|
|
38
|
+
}
|
|
39
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 正反例:推演一步读原数据
|
|
2
|
+
|
|
3
|
+
**差**:先复制整个棋盘,再在副本上落子,最后判断成线;调用方看不出副本只是中间量,还要重复处理落子失败。
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
Board hypothetical = *this;
|
|
7
|
+
const Cell::PlaceResult placed = hypothetical.Place(pos, state);
|
|
8
|
+
if (placed != Cell::PlaceResult::kUpdated) {
|
|
9
|
+
return WinPreviewResult::kInvalidPlace;
|
|
10
|
+
}
|
|
11
|
+
return hypothetical.HasLine(state) ? WinPreviewResult::kWouldWin
|
|
12
|
+
: WinPreviewResult::kWouldNotWin;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**好**:判断「这一格换成某种棋子后会不会成线」不需要第二份棋盘。越界、空格、已占用交给 `CheckPlaceable` 统一返回原因;通过之后只在包含该坐标的线上把这一格算作 `state`,其余格子读原棋盘。
|
|
16
|
+
|
|
17
|
+
```cpp
|
|
18
|
+
WinPreviewResult Board::PreviewWin(Position pos, CellState state) const {
|
|
19
|
+
if (CheckPlaceable(pos, state) != Cell::PlaceResult::kUpdated) {
|
|
20
|
+
return WinPreviewResult::kInvalidPlace;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const auto matches = [&](Position cell) {
|
|
24
|
+
if (cell.row == pos.row && cell.col == pos.col) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return cells_.at(cell.row).at(cell.col) == state;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const bool completes = std::ranges::any_of(kLines, [&](const auto& line) {
|
|
31
|
+
const bool contains = std::ranges::find_if(line, [&](Position cell) {
|
|
32
|
+
return cell.row == pos.row && cell.col == pos.col;
|
|
33
|
+
}) != line.end();
|
|
34
|
+
return contains && std::ranges::all_of(line, matches);
|
|
35
|
+
});
|
|
36
|
+
return completes ? WinPreviewResult::kWouldWin : WinPreviewResult::kWouldNotWin;
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
先用 `IsInRange` 拒绝越界,再用 `at()` 读取;失败路径不碰数组,`noexcept` 也就不再成立。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 源码布局
|
|
2
2
|
|
|
3
|
-
布局服务于边界:库是纯的,程序是薄的,测试从公共接口进入。这里只定目录和文件职责;CMake target 由 `
|
|
3
|
+
布局服务于边界:库是纯的,程序是薄的,测试从公共接口进入。这里只定目录和文件职责;CMake target 由 `engineering` 的 `steps/01-targets.md` 定。
|
|
4
4
|
|
|
5
5
|
## 组件
|
|
6
6
|
|
|
@@ -49,11 +49,11 @@ app/
|
|
|
49
49
|
|
|
50
50
|
## 文件扩展名与归属
|
|
51
51
|
|
|
52
|
-
扩展名、槽位与守卫名以 `
|
|
52
|
+
扩展名、槽位与守卫名以 `source/04-layout/02-file-template.md` 为准;本文只定位置:公共头放 `include/`,私有头放 `src/`,实现文件放 `src/`,测试放 `tests/`。
|
|
53
53
|
|
|
54
54
|
## 头文件契约
|
|
55
55
|
|
|
56
|
-
自包含、共享常量、`using namespace`、模板实现与 `<iostream>` 的规则见 `
|
|
56
|
+
自包含、共享常量、`using namespace`、模板实现与 `<iostream>` 的规则见 `source/02-style/01-google-cpp-style.md`「头文件」和 `source/04-layout/02-file-template.md`。本文只强调一条边界:公共头只暴露稳定契约,测试只包含公共头。
|
|
57
57
|
|
|
58
58
|
## 测试
|
|
59
59
|
|
|
@@ -68,6 +68,6 @@ app/
|
|
|
68
68
|
|
|
69
69
|
1. 先定边界(第 2 步),列出公共头。
|
|
70
70
|
2. 移动文件,不改行为;测试保持绿色。
|
|
71
|
-
3. 再改 CMake target(`
|
|
71
|
+
3. 再改 CMake target(`engineering` 的 `steps/01-targets.md`),最后跑 `building-cpp` 的 `steps/03-check.md`。
|
|
72
72
|
|
|
73
73
|
一次只做一种迁移;不要把「拆库 + 改名 + 改行为」混在一起。
|
package/skills/developing-cpp/{references/file-template.md → source/04-layout/02-file-template.md}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 文件模板
|
|
2
2
|
|
|
3
|
-
本文件是头文件与实现文件槽位的唯一权威。`google-cpp-style.md` 只保留「路径式守卫、不用 `#pragma once`」的决定;`layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `declaration-order.md`,本文件不复制。
|
|
3
|
+
本文件是头文件与实现文件槽位的唯一权威。`source/02-style/01-google-cpp-style.md` 只保留「路径式守卫、不用 `#pragma once`」的决定;`source/04-layout/01-source-layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `source/04-layout/03-declaration-order.md`,本文件不复制。
|
|
4
4
|
|
|
5
5
|
## 选择扩展名
|
|
6
6
|
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
|
|
40
40
|
槽位顺序固定:
|
|
41
41
|
|
|
42
|
-
1. 文件头注释(格式见 `comments.md`,留在守卫外)
|
|
42
|
+
1. 文件头注释(格式见 `source/07-comments/01-comment-rules.md`,留在守卫外)
|
|
43
43
|
2. `#ifndef` / `#define`
|
|
44
44
|
3. 分组包含(组间空行,组内字母序)
|
|
45
45
|
4. 命名空间内:常量 → 枚举 → 结构体 → 自由函数 → 类
|