Skip to content

Commit c245ba2

Browse files
Merge pull request #2 from Awesome-Embedded-Learning-Studio/optimize/volume1_rewrite
2 parents c70d3f3 + 5c05d3e commit c245ba2

318 files changed

Lines changed: 23624 additions & 5420 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Claude API 配置(用于 cppref_card_generator.py 和 translate.py)
2+
# 不要将此文件提交到 Git — 复制为 .env 并填入真实值
3+
4+
ANTHROPIC_AUTH_TOKEN=your-anthropic-api-key-here
5+
ANTHROPIC_BASE_URL=https://api.anthropic.com
6+
ANTHROPIC_DEFAULT_SONNET_MODEL=claude-sonnet-4-20250514
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Build Examples Workflow
2+
# Compiles all CMake projects under code/ to ensure code examples remain buildable
3+
4+
name: Build Examples
5+
6+
on:
7+
pull_request:
8+
paths:
9+
- 'code/**'
10+
- 'scripts/build_examples.py'
11+
- '.github/workflows/build-examples.yml'
12+
push:
13+
branches: [main]
14+
paths:
15+
- 'code/**'
16+
- 'scripts/build_examples.py'
17+
18+
jobs:
19+
build-host:
20+
name: Host Examples
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
27+
- name: Install build tools
28+
run: |
29+
sudo apt-get update
30+
sudo apt-get install -y cmake g++ ninja-build
31+
32+
- name: Build host examples
33+
run: python3 scripts/build_examples.py --host
34+
35+
build-stm32:
36+
name: STM32 Examples
37+
runs-on: ubuntu-latest
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v4
42+
with:
43+
submodules: recursive
44+
45+
- name: Install build tools
46+
run: |
47+
sudo apt-get update
48+
sudo apt-get install -y cmake gcc-arm-none-eabi libnewlib-arm-none-eabi ninja-build
49+
50+
- name: Build STM32 examples
51+
run: python3 scripts/build_examples.py --stm32
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Content Quality Workflow
2+
# Runs comprehensive quality checks on documentation Markdown files
3+
4+
name: Content Quality
5+
6+
on:
7+
pull_request:
8+
paths:
9+
- 'documents/**/*.md'
10+
- 'scripts/check_quality.py'
11+
push:
12+
branches: [main]
13+
paths:
14+
- 'documents/**/*.md'
15+
16+
jobs:
17+
quality-check:
18+
name: Quality Check
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v4
24+
25+
- name: Set up Python
26+
uses: actions/setup-python@v5
27+
with:
28+
python-version: '3.11'
29+
30+
- name: Install dependencies
31+
run: pip install pyyaml
32+
33+
- name: Run quality checks
34+
run: python3 scripts/check_quality.py documents/

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ jobs:
3535
# 3. 安装依赖
3636
- name: 安装依赖
3737
run: pip install -e ./scripts
38-
38+
3939
# 4. 构建网站
4040
- name: 构建网站
4141
run: mkdocs build --clean
42-
42+
4343
# 5. 自动部署到 gh-pages 分支(这一步会自动触发 GitHub Pages)
4444
- name: 部署到 GitHub Pages
4545
uses: peaceiris/actions-gh-pages@v4

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# AI
22
.claude
3+
CLAUDE.md
34

45
# Build directories
56
build/
@@ -36,3 +37,9 @@ __pycache__/
3637

3738
# pip editable install artifacts
3839
*.egg-info/
40+
41+
# cppreference crawler cache (contains API keys in env)
42+
scripts/cppref_cache/
43+
44+
# Environment variables
45+
.env
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: "特性名称"
3+
description: "一句话摘要"
4+
chapter: 99
5+
order: 0
6+
tags:
7+
- host
8+
- cpp-modern
9+
- beginner
10+
difficulty: beginner
11+
cpp_standard: [11, 14, 17]
12+
---
13+
14+
<!--
15+
参考卡模板 (Reference Card Template)
16+
用于 documents/cpp-reference/ 下的特性速查页。
17+
与 article-template.md 不同,参考卡走精炼的结构化格式,不需要叙事风格。
18+
19+
标签使用规则:
20+
1. 必须包含 1 个 platform 标签(参考卡统一用 host)
21+
2. 必须包含 1 个 difficulty 标签
22+
3. 至少包含 1 个 topic 标签
23+
4. 从 scripts/validate_frontmatter.py 的 VALID_TAGS 集合中选取
24+
-->
25+
26+
# 特性名称(C++XX)
27+
28+
## 一句话
29+
30+
用一句人话说清楚这是什么、解决什么问题。
31+
32+
## 头文件
33+
34+
`#include <...>`
35+
36+
## 核心 API 速查
37+
38+
| 操作 | 签名 | 说明 |
39+
|------|------|------|
40+
| ... | `...` | ... |
41+
42+
## 最小示例
43+
44+
```cpp
45+
// 完整可编译的最小示例,不超过 20 行
46+
// Standard: C++XX
47+
```
48+
49+
## 嵌入式适用性:高/中/低
50+
51+
- 要点 1
52+
- 要点 2
53+
54+
## 编译器支持
55+
56+
| GCC | Clang | MSVC |
57+
|-----|-------|------|
58+
| X.Y | X.Y | 19.X |
59+
60+
## 另见
61+
62+
- [教程:对应章节](相对路径)
63+
- [cppreference: 特性名](https://en.cppreference.com/w/cpp/...)
64+
65+
---
66+
67+
*部分内容参考自 [cppreference.com](https://en.cppreference.com/),采用 [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) 许可*

code/examples/chapter05/01_dynamic_memory_problems/fragmentation_demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void fragmentation_scenario_1() {
5353
std::vector<void*> allocations;
5454

5555
// 分配不同大小的块
56-
sizes_t sizes[] = {16, 32, 64, 128, 256, 512, 1024};
56+
size_t sizes[] = {16, 32, 64, 128, 256, 512, 1024};
5757

5858
for (int round = 0; round < 3; ++round) {
5959
for (size_t size : sizes) {

code/examples/chapter05/02_static_vs_stack_allocation/static_allocation_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int global_initialized = 100;
1111
int global_uninitialized;
1212

1313
// 3. 只读常量 (.rodata段 - 通常在Flash中)
14-
static const uint16_t sine_table[16] = {
14+
static const int16_t sine_table[16] = {
1515
0, 6424, 11773, 15836,
1616
18479, 19595, 19151, 17205,
1717
13938, 9605, 4479, 0,
@@ -59,7 +59,7 @@ void constexpr_static_demo() {
5959
}
6060

6161
// 演示静态存储用于查表
62-
uint16_t fast_sin(uint8_t angle) {
62+
int16_t fast_sin(uint8_t angle) {
6363
// 简化版:只演示查表访问
6464
return sine_table[angle % 16];
6565
}

code/examples/chapter05/03_object_pool_pattern/object_pool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <cstddef>
33
#include <cstdint>
4+
#include <cstring>
45
#include <new>
56
#include <type_traits>
67
#include <cassert>
@@ -117,7 +118,7 @@ class ObjectPool {
117118
size_t free_head_ = kInvalidIndex;
118119
size_t used_count_ = 0;
119120

120-
static size_t ptr_to_index(T* ptr) {
121+
size_t ptr_to_index(T* ptr) {
121122
uintptr_t base = reinterpret_cast<uintptr_t>(&storage_[0]);
122123
uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
123124
EP_ASSERT(p >= base);

code/examples/chapter05/04_placement_new/basic_placement_new.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstddef>
44
#include <cstdint>
55
#include <type_traits>
6+
#include <stdexcept>
67

78
// 演示placement new的基础用法
89

@@ -157,40 +158,40 @@ void alignment_check_demo() {
157158
if (aligned) {
158159
int* p = new (ptr) int(42);
159160
std::cout << " Successfully constructed int at " << p << "\n";
160-
p->~int();
161+
// No explicit destructor needed for trivial types
161162
}
162163
}
163164
}
164165

165166
// 在栈上构造"动态"大小的对象
166-
void stack_vector_demo() {
167-
std::cout << "\n=== Stack-Allocated \"Dynamic\" Container ===\n\n";
168-
169-
template<size_t N>
170-
class StackVector {
171-
alignas(double) unsigned char buffer_[N * sizeof(double)];
172-
size_t size_ = 0;
173-
174-
public:
175-
void push(double v) {
176-
if (size_ < N) {
177-
new (buffer_ + size_ * sizeof(double)) double(v);
178-
++size_;
179-
}
167+
template<size_t N>
168+
class StackVector {
169+
alignas(double) unsigned char buffer_[N * sizeof(double)];
170+
size_t size_ = 0;
171+
172+
public:
173+
void push(double v) {
174+
if (size_ < N) {
175+
new (buffer_ + size_ * sizeof(double)) double(v);
176+
++size_;
180177
}
178+
}
181179

182-
double& operator[](size_t i) {
183-
return *reinterpret_cast<double*>(buffer_ + i * sizeof(double));
184-
}
180+
double& operator[](size_t i) {
181+
return *reinterpret_cast<double*>(buffer_ + i * sizeof(double));
182+
}
185183

186-
size_t size() const { return size_; }
184+
size_t size() const { return size_; }
187185

188-
~StackVector() {
189-
for (size_t i = 0; i < size_; ++i) {
190-
reinterpret_cast<double*>(buffer_ + i * sizeof(double))->~double();
191-
}
186+
~StackVector() {
187+
for (size_t i = 0; i < size_; ++i) {
188+
reinterpret_cast<double*>(buffer_ + i * sizeof(double)); // trivial destructor, no-op
192189
}
193-
};
190+
}
191+
};
192+
193+
void stack_vector_demo() {
194+
std::cout << "\n=== Stack-Allocated \"Dynamic\" Container ===\n\n";
194195

195196
StackVector<10> vec;
196197
for (int i = 0; i < 5; ++i) {

0 commit comments

Comments
 (0)