Skip to content

Commit 5eddd08

Browse files
committed
Final preparations for the release of the video
1 parent 5c16b11 commit 5eddd08

3 files changed

Lines changed: 33 additions & 34 deletions

File tree

animation/projects/src/std_function/project.meta

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
"resolutionScale": 1
1818
},
1919
"rendering": {
20-
"fps": 60,
21-
"resolutionScale": 1,
20+
"fps": 30,
21+
"resolutionScale": 2,
2222
"colorSpace": "srgb",
2323
"exporter": {
24-
"name": "@motion-canvas/core/image-sequence",
24+
"name": "@motion-canvas/ffmpeg",
2525
"options": {
26-
"fileType": "image/png",
27-
"quality": 100,
28-
"groupByScene": false
26+
"fastStart": true,
27+
"includeAudio": true
2928
}
3029
}
3130
}

lectures/std_function.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
--
33

44
<p align="center">
5-
<a href="https://youtu.be/blah"><img src="https://img.youtube.com/vi/blah/maxresdefault.jpg" alt="Video Thumbnail" align="right" width=50% style="margin: 0.5rem"></a>
5+
<a href="https://youtu.be/_qteFBrAKSM"><img src="https://img.youtube.com/vi/_qteFBrAKSM/maxresdefault.jpg" alt="Video Thumbnail" align="right" width=50% style="margin: 0.5rem"></a>
66
</p>
77

88
- [`std::function`](#stdfunction)
@@ -39,7 +39,7 @@ One thing we could do is to use **templates**. If we template the entire `Button
3939
template <typename Callback>
4040
class Button {
4141
public:
42-
explicit Button(std::string name, Callback callback)
42+
Button(std::string name, Callback callback)
4343
: name_{std::move(name)}, on_click_{std::move(callback)} {}
4444

4545
void Click() const {
@@ -48,17 +48,17 @@ class Button {
4848
}
4949

5050
private:
51-
std::string name_;
52-
Callback on_click_;
51+
std::string name_{};
52+
Callback on_click_{};
5353
};
5454

5555
void QuitGame() { std::cout << "Quitting game...\n"; }
5656

5757
int main() {
58-
auto lambda_play = [] { std::cout << "Playing game!\n"; };
58+
const auto lambda_play = [] { std::cout << "Playing game!\n"; };
5959

60-
Button play_button{"Play", lambda_play};
61-
Button quit_button{"Quit", QuitGame};
60+
const Button play_button{"Play", lambda_play};
61+
const Button quit_button{"Quit", QuitGame};
6262

6363
play_button.Click();
6464
quit_button.Click();
@@ -127,7 +127,7 @@ Let's rewrite our `Button` class using `std::function` then. We will remove the
127127

128128
class Button {
129129
public:
130-
explicit Button(std::string name, std::function<void()> callback)
130+
Button(std::string name, std::function<void()> callback)
131131
: name_{std::move(name)}, on_click_{std::move(callback)} {}
132132

133133
void Click() const {
@@ -136,20 +136,20 @@ class Button {
136136
}
137137

138138
private:
139-
std::string name_;
140-
std::function<void()> on_click_;
139+
std::string name_{};
140+
std::function<void()> on_click_{};
141141
};
142142

143143
void QuitGame() { std::cout << "Quitting game...\n"; }
144144

145145
int main() {
146-
auto lambda_play = [] { std::cout << "Playing game!\n"; };
146+
const auto lambda_play = [] { std::cout << "Playing game!\n"; };
147147

148-
Button play_button{"Play", lambda_play};
149-
Button quit_button{"Quit", QuitGame};
148+
const Button play_button{"Play", lambda_play};
149+
const Button quit_button{"Quit", QuitGame};
150150

151151
// ✅ This works now! Both buttons are the EXACT SAME type.
152-
std::vector<Button> buttons{play_button, quit_button};
152+
const std::vector<Button> buttons{play_button, quit_button};
153153

154154
for (const auto& button : buttons) { button.Click(); }
155155
}
@@ -182,7 +182,7 @@ And what about our second problem? Now that our callbacks are wrapped in a unifo
182182

183183
class Button {
184184
public:
185-
explicit Button(std::string name, std::vector<std::function<void()>> callbacks)
185+
Button(std::string name, std::vector<std::function<void()>> callbacks)
186186
: name_{std::move(name)}, on_click_callbacks_{std::move(callbacks)} {}
187187

188188
void Click() const {
@@ -193,21 +193,21 @@ class Button {
193193
}
194194

195195
private:
196-
std::string name_;
197-
std::vector<std::function<void()>> on_click_callbacks_;
196+
std::string name_{};
197+
std::vector<std::function<void()>> on_click_callbacks_{};
198198
};
199199

200200
void QuitGame() { std::cout << "Quitting game...\n"; }
201201

202202
int main() {
203-
Button play_button{"Play", {
203+
const Button play_button{"Play", {
204204
[] { std::cout << "Playing game!\n"; },
205205
[] { std::cout << "Logging: Play was clicked.\n"; },
206206
}};
207207

208-
Button quit_button{"Quit", {QuitGame}};
208+
const Button quit_button{"Quit", {QuitGame}};
209209

210-
std::vector<Button> buttons{play_button, quit_button};
210+
const std::vector<Button> buttons{play_button, quit_button};
211211

212212
for (const auto& button : buttons) { button.Click(); }
213213
}
@@ -293,10 +293,10 @@ $PLACEHOLDER
293293
294294
class MyFunction {
295295
public:
296+
// Other constructors missing for brevity
296297
template <typename T>
297-
MyFunction(T callable) {
298-
callable_ = std::make_unique<CallableImpl<T>>(std::move(callable));
299-
}
298+
MyFunction(T&& callable)
299+
: callable_{std::make_unique<CallableImpl<T>>(std::forward<T>(callable))} {}
300300
301301
void operator()() const { if (callable_) { callable_->Invoke(); } }
302302
@@ -309,21 +309,21 @@ class MyFunction {
309309
310310
template <typename T>
311311
struct CallableImpl : public CallableBase {
312-
explicit CallableImpl(T callable) : stored_callable(std::move(callable)) {}
312+
explicit CallableImpl(T callable) : stored_callable{std::move(callable)} {}
313313
314314
void Invoke() const override { stored_callable(); }
315315
316316
T stored_callable;
317317
};
318318
319-
std::unique_ptr<CallableBase> callable_;
319+
std::unique_ptr<CallableBase> callable_{};
320320
};
321321
322322
void FreeFunction() { std::cout << "Free function!\n"; }
323323
324324
int main() {
325-
MyFunction func1(FreeFunction);
326-
MyFunction func2([] { std::cout << "Lambda!\n"; });
325+
const MyFunction func1{FreeFunction};
326+
const MyFunction func2{[] { std::cout << "Lambda!\n"; }};
327327
328328
func1();
329329
func2();

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ Headers with classes
789789
<summary>Storing callables with <code>std::function</code></summary>
790790

791791
----------------------------------------------------------
792-
[![Video thumbnail](https://img.youtube.com/vi/blah/maxresdefault.jpg)](https://youtu.be/blah)
792+
[![Video thumbnail](https://img.youtube.com/vi/_qteFBrAKSM/maxresdefault.jpg)](https://youtu.be/_qteFBrAKSM)
793793

794794
- [`std::function`](lectures/std_function.md#stdfunction)
795795
- [Overview](lectures/std_function.md#overview)

0 commit comments

Comments
 (0)