-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample_02.cpp
More file actions
73 lines (56 loc) · 2.05 KB
/
Copy pathConceptualExample_02.cpp
File metadata and controls
73 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ===========================================================================
// ConceptualExample_02.cpp // Command Pattern
// ===========================================================================
#include <functional> // std::move_only_function (C++23) or std::function
#include <print> // std::println
#include <string>
#include <string_view>
namespace ConceptualExample_Command_Pattern_Modern {
class Receiver final {
public:
void action(std::string_view message) const {
std::println("Action called with message {}", message);
}
};
class FunctionalInvoker final {
private:
// C++23:
// std::move_only_function is perfect for exclusive/movable callables (like lambdas with unique_ptr)
// In case of you are using C++20: use std::function<void()> instead.
std::move_only_function<void() const> m_command;
public:
void setCommand(std::move_only_function<void() const> command) {
m_command = std::move(command);
}
void executeCommand() const {
if (m_command) {
m_command();
}
}
};
}
void test_conceptual_example_02() {
/**
* Variant 2:
* => idiomatic Modern C++
* => command represented by a callable
* => usually preferred unless polymorphic commands are required
*/
using namespace ConceptualExample_Command_Pattern_Modern;
auto receiver = std::make_shared<Receiver>();
FunctionalInvoker invoker;
std::string data{ "Hello from Lambda!" };
/**
* The "command" is a lambda expression that captures both the receiver and the data.
* Note the use of the "Generalized Lambda Capture"
*/
invoker.setCommand(
[receiver, data = std::move(data)]() {
receiver->action(data);
}
);
invoker.executeCommand();
}
// ===========================================================================
// End-of-File
// ===========================================================================