Skip to content

Commit 3a6e779

Browse files
Merge pull request #5 from DoctorLai/views-zip-enumerate
Add clang-format and views example and change to C++23
2 parents ed4871d + 898d88c commit 3a6e779

File tree

11 files changed

+197
-30
lines changed

11 files changed

+197
-30
lines changed

.clang-format

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4
3+
ColumnLimit: 120
4+
AlignEscapedNewlines: Left
5+
AlignAfterOpenBracket: AlwaysBreak
6+
#
7+
# Bind * to the type rather than the name.
8+
PointerAlignment: Left
9+
#
10+
# Put function name on separate line from return type.
11+
AlwaysBreakAfterReturnType: All
12+
#
13+
# Put arguments either all on same line or on separate lines.
14+
BinPackArguments: false
15+
#
16+
# Put function parameters on separate lines.
17+
BinPackParameters: false
18+
#
19+
# Open brace goes on new line only when starting a new struct, enum, or func.
20+
BreakBeforeBraces: Mozilla
21+
#
22+
# Don't sort includes in alphabetical order because Windows headers are odd.
23+
SortIncludes: false

.github/workflows/ci.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ jobs:
2020
- name: Install build tools
2121
run: |
2222
sudo apt-get update
23-
sudo apt-get install -y build-essential g++ clang
23+
sudo apt-get install -y build-essential g++ clang clang-format
2424
25-
# 3. Build and run all examples dynamically
25+
# 3. Clang-format check
26+
- name: Clang-format Check
27+
run: |
28+
./clang-check.sh *.cpp *.hpp *.c *.h
29+
30+
# 4. Build and run all examples dynamically
2631
- name: Build and Run
2732
run: |
2833
for san in address thread undefined; do

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Examples include (and will expand to):
1616
* Smart pointers
1717
* [unique-ptr-basics](./unique-ptr-basics/)
1818
* Lock‑free / wait‑free data structures
19+
* Views
20+
* [views-zip-enumerate](./views-zip-enumerate/)
1921
* Atomics and memory ordering
2022
* RAII and ownership patterns
2123
* Performance‑oriented C++ idioms
@@ -101,7 +103,7 @@ Common compiler settings live in [common.mk](./common.mk):
101103

102104
```make
103105
CXX := g++
104-
CXXFLAGS := -std=c++20 -Wall -Wextra
106+
CXXFLAGS := -std=c++23 -Wall -Wextra
105107
```
106108

107109
Individual examples may extend this, e.g.:
@@ -131,6 +133,15 @@ make SANITIZE=
131133

132134
---
133135

136+
## Clang Format
137+
The `clang-format` is used to ensure the code format.
138+
139+
```bash
140+
./clang-check.sh *.cpp *.hpp
141+
```
142+
143+
---
144+
134145
## Continuous Integration
135146

136147
GitHub Actions automatically builds all examples on:
@@ -140,11 +151,16 @@ GitHub Actions automatically builds all examples on:
140151

141152
The CI setup requires **no updates** when new example folders are added.
142153

154+
The CI will perform:
155+
1. `./clang-check.sh *.cpp *.hpp`
156+
2. `make SANITIZE=[address, thread, undefined]`
157+
3. `make run`
158+
143159
---
144160

145161
## Toolchain
146162

147-
* C++20
163+
* C++23
148164
* GNU Make
149165
* GCC / Clang (CI currently uses GCC)
150166
* Linux (Ubuntu)

clang-check.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Supported extensions
5+
supported_ext=("c" "h" "cpp" "hpp")
6+
7+
# Check if a file is supported
8+
is_supported_file() {
9+
local file="$1"
10+
local ext="${file##*.}"
11+
for e in "${supported_ext[@]}"; do
12+
[[ "$ext" == "$e" ]] && return 0
13+
done
14+
return 1
15+
}
16+
17+
# Run clang-format in dry-run mode on a single file
18+
format_file() {
19+
local file="$1"
20+
if is_supported_file "$file"; then
21+
echo "Check Formatting $file"
22+
clang-format --dry-run --Werror "$file"
23+
else
24+
echo "Skipping unsupported file: $file"
25+
fi
26+
}
27+
28+
# Run clang-format on a glob pattern recursively
29+
format_pattern() {
30+
local pattern="$1"
31+
# Find files matching the pattern
32+
find . -type f -iname "$pattern" ! -path "./.git/*" -print0 | while IFS= read -r -d '' file; do
33+
format_file "$file"
34+
done
35+
}
36+
37+
# Main
38+
if [ "$#" -eq 0 ]; then
39+
# Default: format all supported files
40+
for ext in "${supported_ext[@]}"; do
41+
format_pattern "*.$ext"
42+
done
43+
else
44+
for arg in "$@"; do
45+
if [ -f "$arg" ]; then
46+
format_file "$arg"
47+
else
48+
# Treat as a pattern
49+
format_pattern "$arg"
50+
fi
51+
done
52+
fi

common.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ endif
2626
# --------------------------
2727
# Compiler / Linker flags
2828
# --------------------------
29-
CXXFLAGS := -std=c++20 -Wall -Wextra $(OPTFLAGS) $(SAN_FLAGS)
29+
CXXFLAGS := -std=c++23 -Wall -Wextra $(OPTFLAGS) $(SAN_FLAGS)
3030
LDFLAGS := $(SAN_FLAGS)
3131

3232
# --------------------------

thread-safe-queue/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $(TARGET): $(OBJS)
1717
$(CXX) $(CXXFLAGS) -c $< -o $@
1818

1919
run: $(TARGET)
20-
./$(TARGET)
20+
./$(TARGET) $(ARGS)
2121

2222
clean:
2323
rm -f $(OBJS) $(TARGET)

thread-safe-queue/main.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,53 @@
55
#include <memory>
66
#include <thread>
77

8-
template <typename T>
9-
class ThreadSafeQueue {
10-
public:
8+
template <typename T> class ThreadSafeQueue
9+
{
10+
public:
1111
ThreadSafeQueue() = default;
1212
~ThreadSafeQueue() = default;
1313

14-
void enqueue(T item) {
14+
void
15+
enqueue(T item)
16+
{
1517
std::lock_guard<std::mutex> lock(mutex_);
1618
queue_.push(std::move(item));
1719
cond_var_.notify_one();
1820
}
1921

20-
T dequeue() {
22+
T
23+
dequeue()
24+
{
2125
std::unique_lock<std::mutex> lock(mutex_);
2226
cond_var_.wait(lock, [this]() { return !queue_.empty(); });
2327
T item = queue_.front();
2428
queue_.pop();
2529
return item;
2630
}
2731

28-
bool isEmpty() const {
32+
bool
33+
isEmpty() const
34+
{
2935
std::lock_guard<std::mutex> lock(mutex_);
3036
return queue_.empty();
3137
}
3238

33-
size_t size() const {
39+
size_t
40+
size() const
41+
{
3442
std::lock_guard<std::mutex> lock(mutex_);
3543
return queue_.size();
3644
}
3745

38-
private:
46+
private:
3947
mutable std::mutex mutex_;
4048
std::condition_variable cond_var_;
4149
std::queue<T> queue_;
4250
};
4351

44-
int main() {
52+
int
53+
main()
54+
{
4555
ThreadSafeQueue<int> tsQueue;
4656

4757
// Enqueue some items

unique-ptr-basics/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $(TARGET): $(OBJS)
1414
$(CXX) $(CXXFLAGS) -c $< -o $@
1515

1616
run: $(TARGET)
17-
./$(TARGET)
17+
./$(TARGET) $(ARGS)
1818

1919
clean:
2020
rm -f $(OBJS) $(TARGET)

unique-ptr-basics/main.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,50 @@
22
#include <memory>
33
#include <utility>
44

5-
class Resource {
6-
public:
7-
explicit Resource(int id) : id_(id) {
8-
std::cout << "Resource " << id_ << " acquired\n";
9-
}
5+
class Resource
6+
{
7+
public:
8+
explicit Resource(int id) : id_(id) { std::cout << "Resource " << id_ << " acquired\n"; }
109

11-
~Resource() {
12-
std::cout << "Resource " << id_ << " released\n";
13-
}
10+
~Resource() { std::cout << "Resource " << id_ << " released\n"; }
1411

15-
void use() const {
12+
void
13+
use() const
14+
{
1615
std::cout << "Using resource " << id_ << "\n";
1716
}
1817

19-
private:
18+
private:
2019
int id_;
2120
};
2221

2322
// Factory function: returns ownership
24-
std::unique_ptr<Resource> make_resource(int id) {
23+
std::unique_ptr<Resource>
24+
make_resource(int id)
25+
{
2526
return std::make_unique<Resource>(id);
2627
}
2728

2829
// Takes ownership explicitly
29-
void consume_resource(std::unique_ptr<Resource> res) {
30+
void
31+
consume_resource(std::unique_ptr<Resource> res)
32+
{
3033
std::cout << "Consuming resource\n";
3134
res->use();
3235
// res is destroyed here
3336
}
3437

3538
// Borrows resource (no ownership transfer)
36-
void inspect_resource(const Resource& res) {
39+
void
40+
inspect_resource(const Resource& res)
41+
{
3742
std::cout << "Inspecting resource\n";
3843
res.use();
3944
}
4045

41-
int main() {
46+
int
47+
main()
48+
{
4249
std::cout << "=== create resource ===\n";
4350
auto r1 = make_resource(1);
4451

views-zip-enumerate/Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# pull in shared compiler settings
2+
include ../common.mk
3+
4+
# per-example flags
5+
CXXFLAGS += -pthread
6+
7+
TARGET := views-zip-enumerate
8+
SRCS := main.cpp
9+
OBJS := $(SRCS:.cpp=.o)
10+
11+
all: $(TARGET)
12+
13+
$(TARGET): $(OBJS)
14+
$(CXX) $(CXXFLAGS) -o $@ $^
15+
16+
%.o: %.cpp
17+
$(CXX) $(CXXFLAGS) -c $< -o $@
18+
19+
run: $(TARGET)
20+
./$(TARGET) $(ARGS)
21+
22+
clean:
23+
rm -f $(OBJS) $(TARGET)
24+
25+
.PHONY: all clean run

0 commit comments

Comments
 (0)