@chengzhao-dev/dsh-cpp-workflow 0.7.3 → 0.7.5
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 +3 -3
- package/package.json +9 -3
- package/skills/building-cpp/SKILL.md +15 -15
- package/skills/building-cpp/examples/environment.md +4 -3
- package/skills/building-cpp/references/{build-script.md → 01-build-script.md} +4 -3
- package/skills/building-cpp/references/{environment.md → 02-environment.md} +3 -2
- package/skills/building-cpp/steps/01-build.md +2 -2
- package/skills/building-cpp/steps/02-tooling.md +12 -12
- package/skills/building-cpp/steps/03-check.md +5 -5
- package/skills/building-cpp/{project/vscode/02-settings.md → steps/04-vscode.md} +24 -6
- package/skills/developing-cpp/SKILL.md +13 -13
- package/skills/developing-cpp/references/comments/{comment-rules.md → 01-comment-rules.md} +52 -47
- package/skills/developing-cpp/references/comments/{comment-style.md → 02-style.md} +11 -7
- package/skills/developing-cpp/references/comments/examples/{declaration-class.md → 01-declaration-class.md} +2 -1
- package/skills/developing-cpp/references/comments/examples/{declaration-constructor.md → 02-declaration-constructor.md} +2 -1
- package/skills/developing-cpp/references/comments/examples/{declaration-enum.md → 03-declaration-enum.md} +3 -3
- package/skills/developing-cpp/references/comments/examples/{declaration-file.md → 04-declaration-file.md} +4 -3
- package/skills/developing-cpp/references/comments/examples/{declaration-function.md → 05-declaration-function.md} +10 -29
- package/skills/developing-cpp/references/comments/examples/{declaration-member.md → 06-declaration-member.md} +1 -0
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +15 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-struct.md → 08-declaration-struct.md} +1 -0
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +114 -0
- package/skills/developing-cpp/references/comments/examples/10-implementation-todo.md +17 -0
- package/skills/developing-cpp/references/comments/glossary/{comment-terms.md → 01-comment-terms.md} +8 -7
- package/skills/developing-cpp/references/design/{project-design.md → 01-project-design.md} +8 -7
- package/skills/developing-cpp/references/design/examples/{behavior-sentence.md → 01-behavior-sentence.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{coordinate-table.md → 02-coordinate-table.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{header-place.md → 03-header-place.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{library-boundary.md → 04-library-boundary.md} +3 -2
- package/skills/developing-cpp/references/design/examples/{library-split.md → 05-library-split.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{move-result.md → 06-move-result.md} +4 -10
- package/skills/developing-cpp/references/design/examples/{preview-read.md → 07-preview-read.md} +1 -0
- package/skills/developing-cpp/references/design/examples/08-screen-enter-exit.md +40 -0
- package/skills/developing-cpp/references/design/session-layer.md +91 -0
- package/skills/developing-cpp/references/exceptions/{throw-rules.md → 01-exception-rules.md} +10 -18
- package/skills/developing-cpp/references/exceptions/examples/{throw-boundary.md → 01-throw-boundary.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-build-mode.md → 02-throw-build-mode.md} +3 -2
- package/skills/developing-cpp/references/exceptions/examples/{throw-catch.md → 03-throw-catch.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-destructor.md → 04-throw-destructor.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-format.md → 05-throw-format.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-message.md → 06-throw-message.md} +2 -1
- package/skills/developing-cpp/references/exceptions/examples/{throw-when.md → 07-throw-when.md} +1 -0
- package/skills/developing-cpp/references/index.md +134 -0
- package/skills/developing-cpp/references/language/glossary/{language-terms.md → 01-language-terms.md} +3 -2
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +15 -7
- package/skills/developing-cpp/references/language/language-basics/03-deduction.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +7 -3
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +6 -3
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/{10-checklist.md → 10-language-checklist.md} +2 -0
- package/skills/developing-cpp/references/language/performance/01-measure.md +1 -0
- package/skills/developing-cpp/references/language/performance/02-complexity.md +1 -0
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -0
- package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +1 -0
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +1 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +1 -0
- package/skills/developing-cpp/references/language/performance/07-checklist.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +5 -3
- package/skills/developing-cpp/references/language/standard-library/02-access.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/05-string.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +5 -2
- package/skills/developing-cpp/references/language/standard-library/07-time.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +2 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/{10-checklist.md → 10-library-checklist.md} +2 -1
- package/skills/developing-cpp/references/language/value-types/01-width.md +1 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +1 -0
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +1 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +13 -1
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +1 -0
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +1 -0
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +1 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +1 -0
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +2 -1
- package/skills/developing-cpp/references/language/value-types/10-float-bool.md +1 -0
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +2 -0
- package/skills/developing-cpp/references/layout/01-source-layout.md +3 -2
- package/skills/developing-cpp/references/layout/02-file-template.md +4 -3
- package/skills/developing-cpp/references/layout/03-declaration-order.md +15 -4
- package/skills/developing-cpp/references/naming/{identifier-names.md → 01-identifier-names.md} +31 -9
- package/skills/developing-cpp/references/naming/examples/{naming-accessor.md → 01-naming-accessor.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/02-naming-basic.md +4 -0
- package/skills/developing-cpp/references/naming/examples/{naming-current-prefix.md → 03-naming-current-prefix.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-local.md → 04-naming-local.md} +2 -1
- package/skills/developing-cpp/references/naming/examples/{naming-lookup.md → 05-naming-lookup.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-lower-layer.md → 06-naming-lower-layer.md} +2 -2
- package/skills/developing-cpp/references/naming/examples/{naming-multiple-return.md → 07-naming-multiple-return.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-player-value.md → 08-naming-player-value.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-predicate.md → 09-naming-predicate.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-rename.md → 10-naming-rename.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-responsibility.md → 11-naming-responsibility.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-role.md → 12-naming-role.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-state-change.md → 13-naming-state-change.md} +4 -4
- package/skills/developing-cpp/references/naming/glossary/01-naming-terms.md +28 -0
- package/skills/developing-cpp/references/naming/glossary/{position.md → 02-position.md} +2 -1
- package/skills/developing-cpp/references/naming/glossary/03-selection.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/{square.md → 04-square.md} +3 -2
- package/skills/developing-cpp/references/naming/glossary/05-scoring.md +27 -0
- package/skills/developing-cpp/references/naming/glossary/06-difficulty.md +26 -0
- package/skills/developing-cpp/references/naming/glossary/07-stream.md +21 -0
- package/skills/developing-cpp/references/style/{google-cpp-style.md → 01-google-cpp-style.md} +16 -9
- package/skills/developing-cpp/scripts/checking-comments.mjs +107 -8
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +6 -6
- package/skills/developing-cpp/steps/03-declaration.md +8 -8
- package/skills/developing-cpp/steps/04-implementation.md +17 -17
- package/skills/engineering/SKILL.md +14 -14
- package/skills/engineering/references/cmake/linking/01-libraries.md +7 -4
- package/skills/engineering/references/cmake/linking/02-rpath.md +1 -0
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +2 -1
- package/skills/engineering/references/cmake/linking/04-inspect.md +1 -1
- package/skills/engineering/references/cmake/modules/{functions.md → 01-functions.md} +3 -2
- package/skills/engineering/references/cmake/standards/{cxx-standard.md → 01-cxx-standard.md} +3 -2
- package/skills/engineering/references/cmake/standards/examples/{standard.md → 01-standard.md} +2 -1
- package/skills/engineering/references/cmake/standards/{README.md → index.md} +16 -15
- package/skills/engineering/references/cmake/standards/versions.md +44 -0
- package/skills/engineering/references/cmake/targets/{rules.md → 01-target-rules.md} +37 -5
- package/skills/engineering/references/cmake/targets/examples/{rpath.md → 01-rpath.md} +1 -0
- package/skills/engineering/references/cmake/testing/{ctest.md → 01-ctest.md} +3 -2
- package/skills/engineering/references/formatting/01-encoding.md +2 -1
- package/skills/engineering/references/formatting/02-clang-format.md +4 -2
- package/skills/engineering/references/formatting/03-clang-tidy.md +3 -2
- package/skills/engineering/references/formatting/04-clangd.md +2 -0
- package/skills/engineering/references/formatting/examples/{clang-format.md → 01-clang-format.md} +2 -1
- package/skills/engineering/references/formatting/examples/{clang-tidy.md → 02-clang-tidy.md} +1 -0
- package/skills/engineering/references/formatting/examples/{clangd.md → 03-clangd.md} +1 -0
- package/skills/engineering/references/index.md +32 -0
- package/skills/engineering/steps/01-targets.md +3 -3
- package/skills/engineering/steps/02-modules.md +3 -3
- package/skills/engineering/steps/03-check.md +1 -1
- package/skills/shared/budget.md +20 -0
- package/skills/{developing-cpp/references/editing/01-edit-operations.md → shared/operations.md} +7 -3
- package/skills/{developing-cpp/references/editing/02-rule-priority.md → shared/rule-priority.md} +8 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +0 -28
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +0 -14
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +0 -81
- package/skills/developing-cpp/references/comments/examples/implementation-todo.md +0 -16
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +0 -3
- package/skills/developing-cpp/references/naming/glossary/naming-terms.md +0 -23
- package/skills/engineering/references/cmake/standards/c++11/README.md +0 -50
- package/skills/engineering/references/cmake/standards/c++14/README.md +0 -48
- package/skills/engineering/references/cmake/standards/c++17/README.md +0 -50
- package/skills/engineering/references/cmake/standards/c++20/README.md +0 -50
- package/skills/engineering/references/cmake/standards/c++23/README.md +0 -50
- package/skills/engineering/references/cmake/targets/examples/glob.md +0 -22
- package/skills/engineering/references/cmake/targets/examples/target.md +0 -156
- /package/skills/engineering/{references/formatting/scripts → scripts}/checking-encoding.mjs +0 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
<!-- 读取时机:写或改注释的示例时;读完能回答:正反例:实现注释;预算:一次只开这一份(113 行),不自动串联。 -->
|
|
2
|
+
# 正反例:实现注释
|
|
3
|
+
|
|
4
|
+
实现注释只写本层为什么停在这里;声明注释、文件头与配置注释见 `references/comments/examples/` 里的 `04-declaration-file.md`、`01-declaration-class.md` 等案例。
|
|
5
|
+
|
|
6
|
+
**差**:
|
|
7
|
+
|
|
8
|
+
```cpp
|
|
9
|
+
// 遍历所有空格
|
|
10
|
+
for (const auto& pos : empty_squares) {
|
|
11
|
+
// 尝试落子
|
|
12
|
+
const bool wins = game.PreviewWin(pos, state) == WinPreviewResult::kWouldWin;
|
|
13
|
+
// 如果能赢
|
|
14
|
+
if (wins) {
|
|
15
|
+
// 返回这次落子的位置
|
|
16
|
+
return pos;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
每行都在复述代码。
|
|
22
|
+
|
|
23
|
+
**好**:
|
|
24
|
+
|
|
25
|
+
```cpp
|
|
26
|
+
PlaceResult Game::Place(Move move) {
|
|
27
|
+
// 先判终局,再判玩家与方格:失败结果的优先级由此决定。
|
|
28
|
+
if (!IsInProgress()) {
|
|
29
|
+
return PlaceResult::kFinishedGame;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (current_player_ != move.player) {
|
|
33
|
+
return PlaceResult::kInvalidPlayer;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const SquareValue target_square_value = PlayerToSquareValue(move.player);
|
|
37
|
+
const Square target_square = board_.GetSquare(move.position);
|
|
38
|
+
if (!target_square.IsEmpty()) {
|
|
39
|
+
return PlaceResult::kOccupiedSquare;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
43
|
+
board_.SetSquare(move.position, target_square_value);
|
|
44
|
+
history_.push_back(move);
|
|
45
|
+
return PlaceResult::kPlaced;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
每一条拒绝各写一条独立 `if`;分支结果由 `return` 表达,不再写与 `if` 同义的「检查…是否…」。只有失败结果的优先级需要说明时,在第一条 `if` 上方写一句总起;没有真实防御分支时不硬加。局部值由上一行赋值取出、赋值与上一个阶段之间又有空行时,注释写在赋值那一行上方,说明赋值与紧随其后的分支这整块,`if` 上不再重复。`//` 里的枚举值不包反引号。分支的完整规则见 `references/comments/01-comment-rules.md` 的「实现」节。
|
|
50
|
+
|
|
51
|
+
**好(多条件)**:
|
|
52
|
+
|
|
53
|
+
```cpp
|
|
54
|
+
std::array<SquareValue, 3> values{};
|
|
55
|
+
for (std::size_t i = 0; i < line.size(); ++i) {
|
|
56
|
+
values[i] = board_.GetSquare(line[i]).GetValue();
|
|
57
|
+
}
|
|
58
|
+
// 先确认第一个方格有值,再核对三个方格的取值是否相同。
|
|
59
|
+
if (values[0] != SquareValue::kEmpty && values[0] == values[1] && values[1] == values[2]) {
|
|
60
|
+
return values[0] == SquareValue::kX ? GameResult::kXWin : GameResult::kOWin;
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
多条件判断按代码的判定顺序写:先确认的条件在前,后核对的在后;结果由分支里的 `return` 表达,不在注释里重复。三个同类取值存进 `values` 数组并按下标读取,不写 `value0`、`value1`、`value2`。
|
|
65
|
+
|
|
66
|
+
**好(成功路径)**:
|
|
67
|
+
|
|
68
|
+
```cpp
|
|
69
|
+
// 写盘并记录这一步,然后轮到对方。
|
|
70
|
+
current_player_ = PlayerToOpponent(current_player_);
|
|
71
|
+
board_.SetSquare(move.position, target_square_value);
|
|
72
|
+
history_.push_back(move);
|
|
73
|
+
return PlaceResult::kPlaced;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
成功路径有多步状态更新时,用一句阶段注释概括整段;不在每一步上方逐行解释 `push_back` 与赋值。
|
|
77
|
+
|
|
78
|
+
**好(逐条对应)**:
|
|
79
|
+
|
|
80
|
+
```cpp
|
|
81
|
+
void Console::EnterAlternateScreen() {
|
|
82
|
+
output_ << "\033[?1049h"; // 进入备用屏幕。
|
|
83
|
+
output_ << "\033[?25l"; // 隐藏光标。
|
|
84
|
+
output_.flush();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
void Console::ExitAlternateScreen() {
|
|
88
|
+
// 先恢复光标再退出,避免退出后短暂看不到光标。
|
|
89
|
+
output_ << "\033[?25h"; // 恢复光标。
|
|
90
|
+
output_ << "\033[?1049l"; // 退出备用屏幕。
|
|
91
|
+
output_.flush();
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
几条语句各写一件读者要分别知道的事时,事实落在对应语句的行尾;整段共有的次序或防护理由仍只在段首写一句。两段的区别在事实的数量:上一段的赋值与 `push_back` 是同一件事的步骤,共写一句;这里每条序列做的是不同的事,才逐条写在行尾。
|
|
96
|
+
|
|
97
|
+
## 从函数体注释到函数头注释
|
|
98
|
+
|
|
99
|
+
函数体里的「检查…是否…」不再写;如果这句话确实是函数头的契约,把条件写成声明注释的「主体是否状态」和 `@return`,实现里只留 `return`、`throw` 或赋值。
|
|
100
|
+
|
|
101
|
+
## 只在 .cpp 出现的函数
|
|
102
|
+
|
|
103
|
+
```cpp
|
|
104
|
+
/// 从输入流读一行。
|
|
105
|
+
///
|
|
106
|
+
/// @param input 输入流。
|
|
107
|
+
/// @return 读到的行;输入结束为空。
|
|
108
|
+
std::optional<std::string> GetLine(std::istream& input) {
|
|
109
|
+
std::string line;
|
|
110
|
+
return std::getline(input, line) ? std::optional{line} : std::nullopt;
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
头文件里已经声明的函数,定义处不再写 `///`;契约留在声明处。只在 `.cpp` 出现、且有参数或返回值的函数,定义上一行写完整契约。
|
package/skills/developing-cpp/references/comments/glossary/{comment-terms.md → 01-comment-terms.md}
RENAMED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:统一注释术语时;读完能回答:注释术语与用词;预算:一次只开这一份(92 行),不自动串联。 -->
|
|
1
2
|
# 注释术语与用词
|
|
2
3
|
|
|
3
|
-
本文件是 `references/comments/comment-rules.md` 与 `
|
|
4
|
+
本文件是 `references/comments/01-comment-rules.md` 与 `02-style.md` 的附录,只在判不准用词、改写类型注释或核对书稿摘录时打开;它不随这两份一起加载,也不重复句式、位置和长度规则。
|
|
4
5
|
|
|
5
6
|
## 一条概念只认一个词
|
|
6
7
|
|
|
@@ -14,7 +15,7 @@
|
|
|
14
15
|
| `SquareValue` 的取值 | 空、X、O | 状态为空、尚未设置、状态为 X | 行尾写「空」「X」「O」;「空」也用于指没有值的方格本身 |
|
|
15
16
|
| `Place` 的动作 | 落子 | 放置、更新状态、写入 | `Game::Place` 执行一手;`Board::SetSquare` 只写入,不叫落子 |
|
|
16
17
|
| `PlaceResult` 类型 | 一次落子的结果 | 放置结果、落子结果、方格状态变更结果 | 类型注释写「一次落子的结果。」;由 `Game` 产生 |
|
|
17
|
-
| `PlaceResult` 的取值 | `kPlaced`、`kInvalidPlayer`、`kFinishedGame`、`
|
|
18
|
+
| `PlaceResult` 的取值 | `kPlaced`、`kInvalidPlayer`、`kFinishedGame`、`kOccupiedSquare` | 成功、失败、写入成功、落子失败 | 行尾写「成功:…」「失败:…」;成功排第一,其余按固定顺序;玩家不对是 `kInvalidPlayer`,终局是 `kFinishedGame`,占用是 `kOccupiedSquare` |
|
|
18
19
|
| `GameResult` 类型 | 对局结果 | 对局状态、棋局结果、GameState | 类型注释写「对局结果。」 |
|
|
19
20
|
| `GameResult` 的取值 | 对局进行中、X 已获胜、O 已获胜、平局 | 未结束、X 赢、O 赢、和棋 | 每行以「对局」或胜负主体开头,写完整状态 |
|
|
20
21
|
| `Game::Place` 的动作 | 执行一次落子 | 交给棋盘、落下这一手 | 第一句写「执行一次落子。」,交接与记录由 `@retval` 承担 |
|
|
@@ -43,8 +44,8 @@
|
|
|
43
44
|
- `Square` 只说「方格」;「棋盘上的方格」只在需要强调归属时用,类型注释本身不写棋盘。
|
|
44
45
|
- `Board` 只说「棋盘」;`squares_` 说「方格数组」,不写「存储方格的数组」这种复述。
|
|
45
46
|
- 「棋子」只指对局中的实体棋子;取值里的 X 或 O 叫「值」,不叫「棋子」。
|
|
46
|
-
- `PlaceResult`
|
|
47
|
-
- 结果枚举与分支注释的完整写法见 `references/naming/identifier-names.md` 与 `references/comments/comment-rules.md`;本表只定词。
|
|
47
|
+
- `PlaceResult` 的四项先写「成功」或「失败」;冒号后按固定顺序写「对局状态 → 落子玩家 → 目标方格」,某一项不适用时省略。成功说「落子玩家是当前玩家,值已写入方格」,玩家不对说「落子玩家不是当前玩家,没有写入」,终局说「对局已经结束,没有写入」,占用说「目标方格已有值,没有写入」;「没有写入」由类型注释统一交代,不在每一行重复;不说「已有状态」或「占据」。
|
|
48
|
+
- 结果枚举与分支注释的完整写法见 `references/naming/01-identifier-names.md` 与 `references/comments/01-comment-rules.md`;本表只定词。
|
|
48
49
|
|
|
49
50
|
## 玩家
|
|
50
51
|
|
|
@@ -65,9 +66,9 @@
|
|
|
65
66
|
|
|
66
67
|
## 更新与写入
|
|
67
68
|
|
|
68
|
-
- `Square::SetValue` 的动作统一写「设值」:`Square::SetValue` 把传入的值写入方格,覆盖原值;`Board::SetSquare`
|
|
69
|
+
- `Square::SetValue` 的动作统一写「设值」:`Square::SetValue` 把传入的值写入方格,覆盖原值;`Board::SetSquare` 只检查坐标再写入,不判占用。
|
|
69
70
|
- 「放置」不用在任何接口语境:`Board::SetSquare` 说「写入」,`Game::Place` 说「落子」。
|
|
70
|
-
- `kPlaced` 说「成功:落子玩家是当前玩家,值已写入方格」;`kInvalidPlayer` 说「失败:落子玩家不是当前玩家,没有写入」;`kFinishedGame` 说「失败:对局已经结束,没有写入」;`
|
|
71
|
+
- `kPlaced` 说「成功:落子玩家是当前玩家,值已写入方格」;`kInvalidPlayer` 说「失败:落子玩家不是当前玩家,没有写入」;`kFinishedGame` 说「失败:对局已经结束,没有写入」;`kOccupiedSquare` 说「失败:目标方格已有值,没有写入」。枚举项行尾、`@retval` 与设置预览三处用同一句,三者不互换。
|
|
71
72
|
- `Clear` 说「清空」或「清空为 `kEmpty`」,不写「重置为空」「清空值」「删除状态」;`Board::ClearSquare` 说「清空一个方格」,`Board::Clear` 说「清空棋盘」。
|
|
72
73
|
|
|
73
74
|
## 返回值与副本
|
|
@@ -89,4 +90,4 @@
|
|
|
89
90
|
- [LeviLamina C++ 风格指南](https://docs.levimc.org/LeviLamina/zh/maintainer_guides/cpp_style_guide/):类注释至少简介与公开成员,函数注释含参数、返回值与异常。
|
|
90
91
|
- [OpenHarmony C++ 语言编程规范·注释](https://www.bookstack.cn/read/openharmony-1.0-zh-cn/contribute-OpenHarmony-cpp-coding-style-guide.md):按需、简洁、无二义性,不重复代码信息。
|
|
91
92
|
|
|
92
|
-
判不准时先查本表,再查 `references/comments/comment-rules.md` 与 `references/comments/
|
|
93
|
+
判不准时先查本表,再查 `references/comments/01-comment-rules.md` 与 `references/comments/02-style.md` 的句式与位置;改完运行 `node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>`。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:设计类型、接口或目录时;读完能回答:领域建模与项目设计;预算:一次只开这一份(160 行),不自动串联。 -->
|
|
1
2
|
# 领域建模与项目设计
|
|
2
3
|
|
|
3
4
|
在写第一个类之前,先决定领域里有哪些值、哪些规则、哪些边界。类型设计错了,后面的命名、注释和测试都只能补救。
|
|
@@ -25,7 +26,7 @@
|
|
|
25
26
|
- 方格只是值容器:默认是空,写入任意取值,后写覆盖前值;是否允许覆盖、何时清空由容器决定。不要给方格加落子、清空或终局判断。
|
|
26
27
|
- 一次落子能区分的失败放进结果枚举:玩家不对、对局已结束、值非法、方格已有值。棋盘只做坐标检查与写入,不回答这一手成没成;判断留在对局层。
|
|
27
28
|
- 结果枚举跟随产生它的那一层:棋盘没有结果枚举,写入要么完成,要么因越界抛出;落子结果 `PlaceResult` 与整局结果 `GameResult` 都留在对局层。
|
|
28
|
-
- 对局层的落子入口返回这一手的 `PlaceResult`,整局结果由 `GetResult()` 另外回答;不要用 `GameResult`
|
|
29
|
+
- 对局层的落子入口返回这一手的 `PlaceResult`,整局结果由 `GetResult()` 另外回答;不要用 `GameResult` 兼答两件事,也不要再包一层只带成败与终局的类型。结果类型按调用方要区分的东西选:无载荷、要区分每种原因时用 `enum class`(如 `PlaceResult`);缺失不需要第二种原因时用 `std::optional`;成功带载荷或原因时用 `std::expected<T, E>` 或具名 struct;不用 out 参数。选型见 `references/language/value-types/04-enum.md`「什么时候不用枚举」。
|
|
29
30
|
- 调用方已经把完整值对象交进来时,下层直接用它,不再用散参数或查询重新拼出同一对象。可以校验,并在不符时返回原因;不要静默替换成自己重算的值。
|
|
30
31
|
- 构造值对象所需的只读查询留在拥有状态的一层;调用方用它组值对象,下层函数不重复推导。不要为了去掉查询,让每个调用方各自维护轮次副本。
|
|
31
32
|
- 范围检查在接收位置的那一层直接做,越界直接抛出;不要为了统一诊断把 `std::source_location` 穿过每个公共接口。
|
|
@@ -41,13 +42,13 @@
|
|
|
41
42
|
- 状态用连续取值的 `enum class`:`kPending`、`kActive`、`kDone`。
|
|
42
43
|
- 状态转换写成显式函数或规则对象,不用一串 `if` 直接改整数。
|
|
43
44
|
- “动作是否合法”和“动作执行结果”分开:合法性查询返回 `bool` 或原因,执行返回结果对象。
|
|
44
|
-
- 失败是调用方的正常结果(编号不存在、输入格式不对)时用返回值表达;函数无法完成自己的任务、或状态机进入不可能分支时抛异常,规则见 `references/exceptions/
|
|
45
|
+
- 失败是调用方的正常结果(编号不存在、输入格式不对)时用返回值表达;函数无法完成自己的任务、或状态机进入不可能分支时抛异常,规则见 `references/exceptions/01-exception-rules.md`。
|
|
45
46
|
- 结果对象携带足够信息让调用方决定下一步;失败原因不止一种时不要只返回 `bool` 再让调用方猜原因。
|
|
46
47
|
- 动作只有一个失败原因、调用方也不需要区分时返回 `bool`,`@return` 写清 true 与 false 各自的含义,例如「撤回成功返回 true;没有可撤回的落子时返回 false」;不要为单一失败建只有一个失败取值的枚举。
|
|
47
|
-
- 结果枚举先列成功(编号 0),再按固定顺序列失败原因:`kPlaced = 0`、`kInvalidPlayer = 1`、`kFinishedGame = 2`、`
|
|
48
|
+
- 结果枚举先列成功(编号 0),再按固定顺序列失败原因:`kPlaced = 0`、`kInvalidPlayer = 1`、`kFinishedGame = 2`、`kOccupiedSquare = 3`。编号只用来区分结果,不表示检查优先级。
|
|
48
49
|
- 每类能区分的失败单独一个取值,不合并成 `kInvalid`;调用方与测试才知道究竟哪条规则拒绝。
|
|
49
|
-
-
|
|
50
|
-
- 调用方穷举枚举时不写 `default`,让 `-Wswitch` 在新增取值时报警告,工程开了 `-Werror`
|
|
50
|
+
- 终局是正常结果项,不抛异常。占用这类「这一手写不进」也返回结果项,由对局层的落子入口一次答清;只有坐标越界仍由棋盘抛出 `std::out_of_range`,因为那是调用方的程序错误,不是对局里的正常分支。
|
|
51
|
+
- 调用方穷举枚举时不写 `default`,让 `-Wswitch` 在新增取值时报警告,工程开了 `-Werror` 就是错误。出口不写进 `default`。一个值对多个离散取值分派时用 `switch`;范围比较、布尔组合或只有一两个分支时用 `if`。一个函数里有多条拒绝分支时,每条各写一条独立 `if`,检查不通过就返回;不同拒绝原因互不依赖,不串成 `else if`。成功路径写在函数末尾,不再用 `else` 包住;前面已经提前返回,能走到末尾就说明所有检查都通过。只有一条拒绝分支时同样提前返回,末尾直接写成功路径,不补 `throw` 兜底。
|
|
51
52
|
|
|
52
53
|
## 规则核心与外围
|
|
53
54
|
|
|
@@ -109,7 +110,7 @@
|
|
|
109
110
|
- 参数顺序:被操作的对象在前,控制量与选项在后;输出参数放最后并标 `[out]`。
|
|
110
111
|
- 只赋值、不做校验的 setter 返回 `void`,可以标 `noexcept`;不要加 `[[nodiscard]]`,也不要用返回值暗示写入成功。写入是否允许由拥有容器的类型决定,setter 只改值。
|
|
111
112
|
- 落子类函数的检查顺序固定为:整局状态 → 本层前置条件(如玩家) → 值合法性 → 坐标(由下层的读取完成) → 占用 → 写入。整局状态不依赖参数,先判断能在对局结束后立刻短路,也不会把「终局后落子」误报成玩家错误;玩家与终局不依赖容器,先判断可省掉无谓的读取;值同样来自参数,先判断可省一次读取。多条拒绝分支按这个顺序各写一条独立 `if`,不串 `else if`;成功路径落在函数末尾。
|
|
112
|
-
-
|
|
113
|
+
- 公共接口只保留调用方需要的形态;测试专用的入口不放公共头文件。唯一例外是固定随机源的构造函数:`Computer(Difficulty, std::uint32_t seed)` 与默认构造同义,只在测试里固定随机序列;注释写明用途,不额外暴露内部状态。
|
|
113
114
|
- 默认参数只写在声明处;调用点省略参数时,注释要写清省略后的行为,避免「不传 = 全部」成为隐藏约定。
|
|
114
115
|
|
|
115
116
|
## 撤销与回退
|
|
@@ -142,7 +143,7 @@ history_.pop_back();
|
|
|
142
143
|
|
|
143
144
|
- [ ] 领域值有明确类型,封闭集合用 `enum class`。
|
|
144
145
|
- [ ] 规则核心不依赖终端、GUI、Web、Python 或当前环境。
|
|
145
|
-
- [ ] 失败是正常结果时用返回值,函数无法完成任务时抛异常;异常类型和捕获方式符合 `references/exceptions/
|
|
146
|
+
- [ ] 失败是正常结果时用返回值,函数无法完成任务时抛异常;异常类型和捕获方式符合 `references/exceptions/01-exception-rules.md`。
|
|
146
147
|
- [ ] 边界按变化原因划分,依赖方向从外向内。
|
|
147
148
|
- [ ] 共享抽象有第二个真实使用方和明确理由。
|
|
148
149
|
- [ ] 规则歧义已问清或记录了保守假设。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:看设计案例时;读完能回答:正反例:边界;预算:一次只开这一份(30 行),不自动串联。 -->
|
|
1
2
|
# 正反例:边界
|
|
2
3
|
|
|
3
4
|
**差**:库函数直接打印,程序只调用它。
|
|
@@ -20,9 +21,9 @@ void TaskList::Print() const {
|
|
|
20
21
|
[[nodiscard]] std::vector<Task> SortedByPriority() const;
|
|
21
22
|
|
|
22
23
|
// app/cli.cpp(程序)
|
|
23
|
-
void PrintTasks(const std::vector<Task>& tasks, std::ostream&
|
|
24
|
+
void PrintTasks(const std::vector<Task>& tasks, std::ostream& output) {
|
|
24
25
|
for (const auto& task : tasks) {
|
|
25
|
-
|
|
26
|
+
output << std::format("{} {}\n", task.priority, task.name);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
```
|
package/skills/developing-cpp/references/design/examples/{move-result.md → 06-move-result.md}
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:看设计案例时;读完能回答:正反例:落子结果用返回值回答;预算:一次只开这一份(41 行),不自动串联。 -->
|
|
1
2
|
# 正反例:落子结果用返回值回答
|
|
2
3
|
|
|
3
4
|
**差**:`Game::Place` 返回整局结果,一次落子的失败只能挤进异常。调用方要写 `try/catch` 才能提示「方格已被占用」,终局与非法玩家只能从消息文本里猜;想区分失败就得解析字符串。这一手成没成与整局如何是两件事,混在一个返回值里,调用方每步还要再问一次 `GetResult()`。
|
|
@@ -14,27 +15,20 @@ GameResult Game::Place(Move move) {
|
|
|
14
15
|
}
|
|
15
16
|
```
|
|
16
17
|
|
|
17
|
-
**好**:`Game::Place` 返回 `PlaceResult
|
|
18
|
+
**好**:`Game::Place` 返回 `PlaceResult`,四个取值各答一条规则:`kPlaced` 说这一手写成,`kInvalidPlayer`、`kFinishedGame` 与 `kOccupiedSquare` 各说一条拒绝的规则。调用方按值分支,不解析消息;整局结果仍由 `GetResult()` 单独回答。棋盘写入由 `Board::SetSquare` 完成,它只检查坐标,不报告占用,因此占用必须在写入前判断。多个拒绝分支各自写成一条 `if`,检查不通过就返回;拒绝分支不逐条注释,只有失败结果的优先级需要说明时,在第一条 `if` 上方写一句总起;成功路径落在函数末尾,不串 `else if`。
|
|
18
19
|
|
|
19
20
|
```cpp
|
|
20
21
|
PlaceResult Game::Place(Move move) {
|
|
21
|
-
//
|
|
22
|
-
if (
|
|
22
|
+
// 先判终局,再判玩家与方格:失败结果的优先级由此决定。
|
|
23
|
+
if (!IsInProgress()) {
|
|
23
24
|
return PlaceResult::kFinishedGame;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
// 检查落子玩家是否为当前玩家。
|
|
27
27
|
if (current_player_ != move.player) {
|
|
28
28
|
return PlaceResult::kInvalidPlayer;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
// 检查要写入的值是否为空。
|
|
32
31
|
const SquareValue target_square_value = PlayerToSquareValue(move.player);
|
|
33
|
-
if (target_square_value == SquareValue::kEmpty) {
|
|
34
|
-
return PlaceResult::kEmptySquare;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 检查目标方格是否为空。
|
|
38
32
|
const Square target_square = board_.GetSquare(move.position);
|
|
39
33
|
if (!target_square.IsEmpty()) {
|
|
40
34
|
return PlaceResult::kOccupiedSquare;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<!-- 读取时机:设计会话入口与备用屏幕时;读完能回答:正反例:进入与退出显式配对;预算:一次只开这一份(39 行),不自动串联。 -->
|
|
2
|
+
# 正反例:进入与退出显式配对
|
|
3
|
+
|
|
4
|
+
**差**:在 `RunGame` 里声明局部类,把进入与退出藏进构造和析构。
|
|
5
|
+
|
|
6
|
+
```cpp
|
|
7
|
+
// run_game.cpp
|
|
8
|
+
void GameSession::RunGame() {
|
|
9
|
+
class ConsoleSession {
|
|
10
|
+
public:
|
|
11
|
+
explicit ConsoleSession(Console& console) : console_{console} {
|
|
12
|
+
console_.EnterAlternateScreen();
|
|
13
|
+
}
|
|
14
|
+
~ConsoleSession() { console_.ExitAlternateScreen(); }
|
|
15
|
+
private:
|
|
16
|
+
Console& console_;
|
|
17
|
+
} session{console_};
|
|
18
|
+
// ...
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
问题:多了一个只为转调的局部类型,入口不再直接说明屏幕何时切换;`Console` 是会话成员,寿命比一次 `RunGame` 长,备用屏幕也不满足“对象寿命等于资源占用”。
|
|
23
|
+
|
|
24
|
+
**好**:入口直接进入,每个返回路径先退出。
|
|
25
|
+
|
|
26
|
+
```cpp
|
|
27
|
+
// run_game.cpp
|
|
28
|
+
void GameSession::RunGame() {
|
|
29
|
+
console_.EnterAlternateScreen();
|
|
30
|
+
const std::optional<MatchMode> mode = console_.PromptMatchMode();
|
|
31
|
+
if (!mode) {
|
|
32
|
+
console_.ExitAlternateScreen();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// ...
|
|
36
|
+
console_.ExitAlternateScreen();
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
异常交给 `main`,不额外恢复屏幕;`Console` 不放进构造与析构。
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
<!-- 读取时机:设计会话、选着、规则与构建边界时;读完能回答:会话层和程序边界;预算:一次只开这一份(90 行),不自动串联。 -->
|
|
2
|
+
# 会话层与程序边界
|
|
3
|
+
|
|
4
|
+
先定接口,再写实现;实现碰到下层缺口时,回头改接口,小步迭代。本文按会话、选着、规则、构建四层说明这条原则;领域值、结果类型与函数契约仍以 [01-project-design.md](01-project-design.md) 为准,本文只补充「谁持有流、谁做决定、谁不该知道终端」以及三者的构建边界。
|
|
5
|
+
|
|
6
|
+
## 会话
|
|
7
|
+
|
|
8
|
+
会话层把一次进程的生命周期包成一个对象。它持有一个控制台、一个对局与一个电脑对手;输入、输出与诊断三个流由控制台持有。构造只绑定引用,不读、不写、不打印。
|
|
9
|
+
|
|
10
|
+
- 入口只做装配:`main` 构造会话并调用一个 `RunGame`,捕获异常、打印一行诊断、返回退出码;不解析棋盘,不选难度,不写提示语。
|
|
11
|
+
- 会话按顺序问完开局问题(模式、难度、先手),清掉上一局,再进入一局循环;换局时只撤回上一局的落子,不重新构造会话或电脑。
|
|
12
|
+
- 所有提示和诊断走注入的流;会话不直接使用 `std::cout`、`std::cerr` 或全局状态,测试才能用 `std::istringstream` / `std::ostringstream` 驱动完整交互。
|
|
13
|
+
- 依赖只朝内:会话知道规则和电脑,规则与电脑不知道会话;把终端代码编进静态库,等于让规则依赖终端。
|
|
14
|
+
- 先跑通「读一步、走一步、打印一步」的骨架,再加回放、撤销、难度等分支;每加一段,回看会话接口是否仍只有一个入口、是否泄漏了本层不该知道的状态。
|
|
15
|
+
- 备用屏幕的进入、退出与清屏交给控制台类型(如 `Console`);入口显式调用进入,每条返回路径先调用退出,不另加只转调的 `ConsoleSession`,也不在 `RunGame` 内声明局部类。
|
|
16
|
+
- RAII 只覆盖对象寿命等于资源占用的情形;备用屏幕跨越多局,进入与退出不放进构造析构。异常路径交给 `main`,不额外恢复屏幕。
|
|
17
|
+
|
|
18
|
+
tictactoe 的会话是 `GameSession`(`games/tictactoe/tictactoe/include/game_session.h`、`src/game_session.cpp`):构造函数写 `: console_{input, output, diagnostics} {}`,`RunGame` 先调用 `console_.EnterAlternateScreen()`,四个返回路径先 `console_.ExitAlternateScreen()`;依次调用 `PromptMatchMode`、人机时调用 `PromptDifficulty`、`PromptHumanSide`,`while (game_.Undo()) {}` 清局,再调用 `PlayMatch`。`main` 只有构造、`RunGame`、捕获异常与返回退出码四件事。
|
|
19
|
+
|
|
20
|
+
## 选择
|
|
21
|
+
|
|
22
|
+
选着层负责「从合法候选里挑一个」,不拥有对局,也不改对局。它持有自己的策略状态(难度、随机数引擎),向规则层要一个合法着法。
|
|
23
|
+
|
|
24
|
+
- 对外只暴露一个选择入口,例如 `ChoosePosition(Game&)`;随机选与搜索选留在私有函数里,调用方不需要知道难度分支怎样实现。
|
|
25
|
+
- 返回 `std::optional<Position>`:没有合法候选是正常结果,不返回哨兵坐标,不抛异常;终局由调用方先判断,也可以由选择入口防御性返回空。
|
|
26
|
+
- 选择函数在返回时保持对局与调用前一致。搜索用同一局「试走一步、递归、撤回」,不复制整局;复制把 O(1) 的撤回变成 O(n) 的拷贝,搜索树一深就被放大。
|
|
27
|
+
- 难度是封闭枚举(如 `Difficulty::kNormal` / `kHard`),不是 `bool` 或整数;新增一档必须同时补 `switch` 分支,让 `-Wswitch` 与 `std::unreachable()` 兜住漏档。
|
|
28
|
+
- 没有第二个真实实现之前不加抽象基类或策略工厂;先把一个具体类写对,调用方只依赖它的公开接口。
|
|
29
|
+
- 选着层不读终端、不打印、不读环境;随机源由构造函数注入或播种,测试用固定种子复现同一序列。
|
|
30
|
+
|
|
31
|
+
tictactoe 的选着是 `Computer`(`games/tictactoe/tictactoe/include/computer.h`、`src/computer.cpp`):`Difficulty{kNormal, kHard}`、`std::mt19937 engine_`、`ChoosePosition`,私有 `ChooseRandomPosition` / `ChooseBestPosition` / `Minimax` / `ScoreFinishedGame`;写搜索时发现 `Game` 需要公开 `Undo()` 与 `IsInProgress()`,于是回到规则层补接口,而不是在电脑里复制一份棋盘。
|
|
32
|
+
|
|
33
|
+
## 规则
|
|
34
|
+
|
|
35
|
+
规则层只依赖标准库和领域类型,回答「这一手是否合法、这一局是否结束、终局分数是多少」。
|
|
36
|
+
|
|
37
|
+
- 不读终端、不碰 GUI/Web/Python、不读当前时间、环境变量或工作目录;这些由会话或调用方注入。
|
|
38
|
+
- 组合优先:`Game` 拥有 `Board`,`Board` 拥有方格;封闭取值用 `enum class`;结果跟随产生它的层(`PlaceResult`、`GameResult`)。
|
|
39
|
+
- 查询与变更分开;一个函数只做一件事;`Undo()` 只撤回受影响的一处,既服务「换局清盘」,也服务搜索回溯。
|
|
40
|
+
- 信息隐藏按「哪些决定会变」分解,不按流程图分解;接口只暴露稳定契约,私有实现留在 `.cpp` 或私有头。
|
|
41
|
+
- 终局评分是规则查询:`ScoreFinishedGame(const Game&)` 读 `GameResult` 与 `move_count`,X 胜为正、O 胜为负、进行中或平局为 0;不把分数塞进 `GameResult`,也不在电脑里重复判断胜负。
|
|
42
|
+
|
|
43
|
+
## 构建
|
|
44
|
+
|
|
45
|
+
构建边界让「规则库」「会话程序」「测试」各自独立编译,依赖方向与源码一致。
|
|
46
|
+
|
|
47
|
+
- 规则库只编规则与选着:`add_library(tictactoe STATIC src/square.cpp src/board.cpp src/game.cpp src/computer.cpp)`,不含 `game_session.cpp`。
|
|
48
|
+
- 程序目标编会话与入口:`games/tictactoe/apps/CMakeLists.txt` 的 `app` 编 `src/main.cpp`、`../tictactoe/src/game_session.cpp` 与 `../tictactoe/src/console.cpp`,再 `target_link_libraries(app PRIVATE tictactoe)`;会话层不进库,库也不包含 `<iostream>` 或打印。
|
|
49
|
+
- 测试目标按被测层取源文件:规则测试链规则库;终端测试直接编 `../src/game_session.cpp` 与 `../src/console.cpp` 并链规则库,避免为了测试把会话塞进库。
|
|
50
|
+
- `target_link_libraries` 的硬性要求只有第一个参数已由 `add_executable` 或 `add_library` 创建;被链接的库目标可以在本次 configure 稍后出现,CMake 会自动补 ordering dependency。项目仍约定先定义库、再定义程序,这是可读性与顺序一致,不是 CMake 规范。
|
|
51
|
+
- 构建树的 RPATH 与安装树分开:`BUILD_RPATH` 可用 `$ORIGIN/../lib` 指向构建产物;`INSTALL_RPATH` 为空时安装后的可执行文件不会沿用构建树路径。
|
|
52
|
+
- 程序边界由 `main` 定义;库不定义 `main`。头文件自包含、每个符号由直接包含的头导出(SF.11 与 IWYU)是编译单元边界,不是链接边界,放在本节作为「库能独立编译」的旁证,不替代目标依赖规则。
|
|
53
|
+
|
|
54
|
+
tictactoe 的四份 CMake 是这条边界的实物:`games/tictactoe/tictactoe/CMakeLists.txt` 的库不含终端源,`games/tictactoe/apps/CMakeLists.txt` 才有 `app`,`games/tictactoe/tictactoe/tests/CMakeLists.txt` 的 `game_session_test` 直接编 `../src/game_session.cpp` 与 `../src/console.cpp`,根 `games/tictactoe/CMakeLists.txt` 只按顺序加子目录。
|
|
55
|
+
|
|
56
|
+
## 迭代记录
|
|
57
|
+
|
|
58
|
+
这条原则不是一次设计完的,而是每轮实现暴露缺口后回改接口:
|
|
59
|
+
|
|
60
|
+
- 先写 `game_session.h`,把会话入口、输入、输出与诊断三个流、开局问题定成接口;`main.cpp` 只装配。
|
|
61
|
+
- 再写 `computer.h`,把 `Difficulty`、两种构造、`ChoosePosition` 定成选着接口;实现搜索时补 `Game::Undo` / `IsInProgress` 的公开契约。
|
|
62
|
+
- 最后写 `game_session.cpp` 与 CMake,把会话、规则库、测试拆成三个构建目标;测试迁移时把入口与 `app` 目标移入 `apps/`,会话逻辑没有进入静态库。
|
|
63
|
+
|
|
64
|
+
## 依据
|
|
65
|
+
|
|
66
|
+
会话与设计方法:
|
|
67
|
+
|
|
68
|
+
- Wirth, N. *Program Development by Stepwise Refinement*. CACM 14(4), 1971, doi:10.1145/362575.362577。原文页本次不可达;分解与逐步细化的转引见 Cornell 页。
|
|
69
|
+
- Gries, D. *Stepwise refinement*. Cornell JavaHyperText. https://www.cs.cornell.edu/courses/JavaAndDS/stepwise/stepwise.html
|
|
70
|
+
- Freeman, S. & Pryce, N. *Growing Object-Oriented Software Guided by Tests* 目录:Ch4 “First, Test a Walking Skeleton”、Ch5 “Develop from the Inputs to the Outputs”、Ch17 “Teasing Apart Main”。https://growing-object-oriented-software.com/toc.html (书中没有字面 “outside-in” 章节,正文不把它当章节名。)
|
|
71
|
+
- Cockburn, A. *Hexagonal architecture (Ports & Adapters)*. https://alistair.cockburn.us/hexagonal-architecture/
|
|
72
|
+
- Martin, R. C. *The Clean Architecture*. https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
|
|
73
|
+
- Dijkstra, E. W. *Notes on Structured Programming*, EWD249, 1970. https://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD249/EWD249.html
|
|
74
|
+
- Parnas, D. L. *On the criteria to be used in decomposing systems into modules*. CACM 15(12), 1972, doi:10.1145/361598.361623。原文页本次不可达;按「会变的决定分解」的转引使用,正文不冒充读过原文。
|
|
75
|
+
|
|
76
|
+
选着实例:
|
|
77
|
+
|
|
78
|
+
- Stockfish `src/main.cpp`、`src/uci.h`:main 装配命令行与 UCI 引擎,UCI 层持有引擎并接收 position/go 命令。https://github.com/official-stockfish/Stockfish
|
|
79
|
+
- python-chess engine 文档:`engine.play(board, limit)` 返回着法,调用方推进棋盘。https://python-chess.readthedocs.io/en/latest/engine.html
|
|
80
|
+
- OpenSpiel `spiel_bots.h`:`virtual Action Step(const State& state) = 0;`。https://github.com/google-deepmind/open_spiel
|
|
81
|
+
- boardgame.io `src/ai/bot.ts`:`play(state, playerID)` 返回动作,不返回新状态。https://github.com/boardgameio/boardgame.io
|
|
82
|
+
- cutechess-cli `projects/cli/src/main.cpp`:CLI 只装配 Engine 与赛制。https://github.com/cutechess/cutechess
|
|
83
|
+
|
|
84
|
+
规则与构建:
|
|
85
|
+
|
|
86
|
+
- C++ Core Guidelines Enum.3(优先 `enum class`)、C.4(只在需要直接访问表示时做成员)。https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
|
|
87
|
+
- CMake `target_link_libraries`:第一个参数必须已创建;被链接目标可由项目稍后创建并自动补 ordering dependency。https://cmake.org/cmake/help/latest/command/target_link_libraries.html
|
|
88
|
+
- CMake `BUILD_RPATH` 与 `INSTALL_RPATH`。https://cmake.org/cmake/help/latest/prop_tgt/BUILD_RPATH.html、https://cmake.org/cmake/help/latest/prop_tgt/INSTALL_RPATH.html
|
|
89
|
+
- Craig Scott, “add_subdirectory() and target_link_libraries() ordering”, CMake Discourse, 2026-03-05:只有第一个参数必须已定义;先定义后引用是推荐写法。https://discourse.cmake.org/t/add-subdirectory-and-target-link-libraries-ordering/15539
|
|
90
|
+
- C++ Core Guidelines SF.11(头文件自包含)与 include-what-you-use 的 “Why include what you use?”。https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-contained、https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/WhyIWYU.md
|
|
91
|
+
- ISO C++ [basic.start.main]:程序只能有一个全局 `main`。https://eel.is/c++draft/basic.start.main
|
package/skills/developing-cpp/references/exceptions/{throw-rules.md → 01-exception-rules.md}
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:抛异常或写异常契约时;读完能回答:异常与错误;预算:一次只开这一份(207 行),不自动串联。 -->
|
|
1
2
|
# 异常与错误
|
|
2
3
|
|
|
3
4
|
本文件是**开发阶段**的源码写作指导,只在 `developing-cpp` 被打开时阅读。插件运行时不编译 C++、不执行示例,也不把这里的规定送进任何运行时流程;它只是一份随技能分发的 Markdown 参考。
|
|
@@ -17,7 +18,7 @@ E.1 要求尽早决定错误处理策略:先问调用方还能不能继续、
|
|
|
17
18
|
|
|
18
19
|
策略一旦定下,声明、实现、测试和入口都用同一套;不要在同一层里一半返回错误码、一半抛异常。
|
|
19
20
|
|
|
20
|
-
判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare` 坐标越界),就抛;写入操作把「这一手写不进」当作正常结果、调用方本来就要分支(如 `Game::Place` 返回 `PlaceResult
|
|
21
|
+
判断是否算「混用」,看函数合同而不是看返回类型:查询必须产出一个值、失败时产不出(如 `GetSquare` 坐标越界),就抛;写入操作把「这一手写不进」当作正常结果、调用方本来就要分支(如 `Game::Place` 返回 `PlaceResult`,玩家、终局与占用各有一个取值),就用返回值。动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class` 而不是 `bool` 加输出参数。两者并存时,公共头必须把「拒绝」与「抛」的边界各写一行;没有这行注释才算混用。
|
|
21
22
|
|
|
22
23
|
## 什么时候抛
|
|
23
24
|
|
|
@@ -31,9 +32,8 @@ E.2:函数不能完成它被分配的任务时,就抛异常;构造函数
|
|
|
31
32
|
|
|
32
33
|
1. 整局状态:对局是否结束由 `Game::GetResult` 回答,已结束返回 `kFinishedGame`,玩家、坐标、棋盘与历史都不再检查。对局已经无法继续时,再谈「轮到谁」没有意义。
|
|
33
34
|
2. 本层状态的前置条件:这一步是否轮到该玩家。对局自己记着轮次,这一条用返回值 `kInvalidPlayer` 答,调用方按结果分支,不抛。
|
|
34
|
-
3.
|
|
35
|
-
4.
|
|
36
|
-
5. 执行操作并返回 `kPlaced`:换方、写入棋盘、记录历史,三步按成员声明顺序做;构造函数预留了整盘手数的容量,写入阶段不分配内存。
|
|
35
|
+
3. 下层的检查与剩余的写入条件:读取目标格时由 `Board::GetSquare` 抛出 `std::out_of_range`;方格已有值返回 `kOccupiedSquare`。不要为了提前抛出而在 `Game` 里重复 `Board::IsInRange`。
|
|
36
|
+
4. 执行操作并返回 `kPlaced`:换方、写入棋盘、记录历史,三步按成员声明顺序做;构造函数预留了整盘手数的容量,写入阶段不分配内存。
|
|
37
37
|
|
|
38
38
|
整局状态排在最前,因为对局结束后这次调用根本不会落子,玩家与坐标都只是无关的输入;把玩家检查排在前面,会让「终局后落子」被解释成玩家错误。本层负责的返回值检查仍必须排在「交给下层」之前:`Game::Place` 若先把坐标交给 `Board::GetSquare`,越界异常会把「不是当前玩家」一起盖掉,调用方拿不到真正的拒绝原因。属于下层的检查不在此列:对局已结束时 `Game` 不再进入棋盘,越界坐标随 `kFinishedGame` 一起返回,这是正常的短路,不是遗漏。
|
|
39
39
|
|
|
@@ -43,23 +43,16 @@ E.2:函数不能完成它被分配的任务时,就抛异常;构造函数
|
|
|
43
43
|
|
|
44
44
|
```cpp
|
|
45
45
|
PlaceResult Game::Place(Move move) {
|
|
46
|
-
//
|
|
47
|
-
if (
|
|
46
|
+
// 先判终局,再判玩家与方格:失败结果的优先级由此决定。
|
|
47
|
+
if (!IsInProgress()) {
|
|
48
48
|
return PlaceResult::kFinishedGame;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
// 检查落子玩家是否为当前玩家。
|
|
52
51
|
if (current_player_ != move.player) {
|
|
53
52
|
return PlaceResult::kInvalidPlayer;
|
|
54
53
|
}
|
|
55
54
|
|
|
56
|
-
// 检查要写入的值是否为空。
|
|
57
55
|
const SquareValue target_square_value = PlayerToSquareValue(move.player);
|
|
58
|
-
if (target_square_value == SquareValue::kEmpty) {
|
|
59
|
-
return PlaceResult::kEmptySquare;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// 检查目标方格是否为空。
|
|
63
56
|
const Square target_square = board_.GetSquare(move.position);
|
|
64
57
|
if (!target_square.IsEmpty()) {
|
|
65
58
|
return PlaceResult::kOccupiedSquare;
|
|
@@ -78,7 +71,6 @@ PlaceResult Game::Place(Move move) {
|
|
|
78
71
|
|
|
79
72
|
```cpp
|
|
80
73
|
void Board::CheckInRange(Position pos, std::source_location location) {
|
|
81
|
-
// 检查坐标是否越界。
|
|
82
74
|
if (!IsInRange(pos)) {
|
|
83
75
|
const std::string message = std::format(
|
|
84
76
|
"position {} out of range for {}x{} board, at {} ({}:{}:{})", pos, kRowCount,
|
|
@@ -89,7 +81,7 @@ void Board::CheckInRange(Position pos, std::source_location location) {
|
|
|
89
81
|
}
|
|
90
82
|
```
|
|
91
83
|
|
|
92
|
-
声明写成 `static void CheckInRange(Position pos, std::source_location location = std::source_location::current());`,紧挨 `IsInRange`,放在 public 区:对局层要先确认坐标再决定怎么写时可以直接调用它,不必复制一份判断。`source_location::current()` 作为默认参数在调用点求值,所以 `function_name()`、`file_name()`、`line()` 与 `column()` 描述的都是调用方;调用点不写这个参数,上一行写一句 `//
|
|
84
|
+
声明写成 `static void CheckInRange(Position pos, std::source_location location = std::source_location::current());`,紧挨 `IsInRange`,放在 public 区:对局层要先确认坐标再决定怎么写时可以直接调用它,不必复制一份判断。`source_location::current()` 作为默认参数在调用点求值,所以 `function_name()`、`file_name()`、`line()` 与 `column()` 描述的都是调用方;调用点不写这个参数,上一行写一句 `// 坐标越界时由 CheckInRange 抛出。`,本行只写 `CheckInRange(pos)`。
|
|
93
85
|
|
|
94
86
|
`source_location` 是 C++20 设施;`line()` 与 `column()` 从 1 起算。测试只断言值、范围、文件名与消息里出现的函数名,源码移动后测试不会失效。
|
|
95
87
|
|
|
@@ -149,7 +141,7 @@ int main() {
|
|
|
149
141
|
}
|
|
150
142
|
```
|
|
151
143
|
|
|
152
|
-
消息要能让调用者定位操作和关键值,句式见下节;`std::format` 只是构造 `what()`
|
|
144
|
+
消息要能让调用者定位操作和关键值,句式见下节;`std::format` 只是构造 `what()` 或边界消息的方式。写到已有的 `std::ostream` 用 `<<`;没有流对象时,默认用 `std::print` 与 `std::println`,它们只在 C++23 且标准库支持时可用;见 `references/language/standard-library/06-format.md`。
|
|
153
145
|
|
|
154
146
|
## 消息
|
|
155
147
|
|
|
@@ -170,7 +162,7 @@ const std::string message = std::format("ParseConfig: line {} has no key", line_
|
|
|
170
162
|
throw ParseError{message};
|
|
171
163
|
```
|
|
172
164
|
|
|
173
|
-
跨行时先把消息存进 `const std::string message`,再写一行 `throw`:`throw` 行只表达抛什么,格式串与参数留在上一句,读者不必先跨过消息才能看到异常类型的结束。消息没有插值时仍写一行 `throw std::logic_error{"
|
|
165
|
+
跨行时先把消息存进 `const std::string message`,再写一行 `throw`:`throw` 行只表达抛什么,格式串与参数留在上一句,读者不必先跨过消息才能看到异常类型的结束。消息没有插值时仍写一行 `throw std::logic_error{"ReadConfig: the first line is empty"};`。私有检查函数由 `source_location` 取调用点,不带标签,句式见上文「重复的入口检查」。
|
|
174
166
|
|
|
175
167
|
常用库把消息的构造放进异常类型或辅助函数:spdlog 的 `spdlog_ex` 在构造函数里写 `msg_`,CLI11 的 `Error` 族在静态工厂里拼好句子,POCO 的 `Exception` 在构造函数里存消息;调用处因此只有一行 `throw ...(...)`。本工作流在整条 `throw` 跨行时引入 `const std::string message`(需要 `#include <string>`),变量名固定,不写 `std::move`;异常类型构造时复制消息。
|
|
176
168
|
|
|
@@ -183,7 +175,7 @@ const std::string message =
|
|
|
183
175
|
std::format("ReadConfig: {} has no header because the first line is empty", path);
|
|
184
176
|
throw ParseError{message};
|
|
185
177
|
|
|
186
|
-
throw std::logic_error{"
|
|
178
|
+
throw std::logic_error{"ReadConfig: the first line is empty"};
|
|
187
179
|
throw std::format_error{"Position: format specifier is not supported, use {} instead"};
|
|
188
180
|
```
|
|
189
181
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:抛异常或写异常契约时;读完能回答:正反例:Debug 与 Release 的三层合同;预算:一次只开这一份(38 行),不自动串联。 -->
|
|
1
2
|
# 正反例:Debug 与 Release 的三层合同
|
|
2
3
|
|
|
3
4
|
**差**:同一个函数在 Debug 抛异常、在 Release 返回 false;调用方被迫按构建模式写两套分支。
|
|
@@ -25,13 +26,13 @@ void Place(Position pos, Player player);
|
|
|
25
26
|
**差**:用 `assert` 承担 Release 也必须执行的 invariant 检查;`NDEBUG` 后检查消失。
|
|
26
27
|
|
|
27
28
|
```cpp
|
|
28
|
-
assert(game.
|
|
29
|
+
assert(game.IsInProgress());
|
|
29
30
|
```
|
|
30
31
|
|
|
31
32
|
**好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck`。没有这两个宏时,两种模式都必须执行的检查用 `std::terminate`(失败前先打印诊断),只在开发期做的自检用 `assert`。
|
|
32
33
|
|
|
33
34
|
```cpp
|
|
34
|
-
Check(game.
|
|
35
|
+
Check(game.IsInProgress(), "Computer::ChoosePosition: game is already over");
|
|
35
36
|
DebugCheck(index < history_.size(), "history index out of range");
|
|
36
37
|
```
|
|
37
38
|
|