Mercurial > hg > svcore
comparison base/Command.h @ 1831:498ed1e86f92
Add GenericCommand
author | Chris Cannam |
---|---|
date | Fri, 27 Mar 2020 10:06:03 +0000 |
parents | c01cbe41aeb5 |
children |
comparison
equal
deleted
inserted
replaced
1826:a4dce53b3353 | 1831:498ed1e86f92 |
---|---|
17 #define SV_COMMAND_H | 17 #define SV_COMMAND_H |
18 | 18 |
19 #include <QObject> | 19 #include <QObject> |
20 #include <QString> | 20 #include <QString> |
21 #include <vector> | 21 #include <vector> |
22 #include <functional> | |
22 | 23 |
23 #include "Debug.h" | 24 #include "Debug.h" |
24 | 25 |
25 class Command | 26 class Command |
26 { | 27 { |
29 | 30 |
30 virtual void execute() = 0; | 31 virtual void execute() = 0; |
31 virtual void unexecute() = 0; | 32 virtual void unexecute() = 0; |
32 virtual QString getName() const = 0; | 33 virtual QString getName() const = 0; |
33 }; | 34 }; |
35 | |
36 /** | |
37 * GenericCommand is a Command that can be constructed directly using | |
38 * lambdas, without having to create a subclass. Best for commands | |
39 * invoked only in a single place, and where little state is involved. | |
40 */ | |
41 class GenericCommand : public Command | |
42 { | |
43 public: | |
44 GenericCommand(QString name, | |
45 std::function<void()> execute, | |
46 std::function<void()> unexecute) : | |
47 m_name(name), | |
48 m_execute(execute), | |
49 m_unexecute(unexecute), | |
50 m_onDelete([] {}) { | |
51 } | |
52 | |
53 GenericCommand(QString name, | |
54 std::function<void()> execute, | |
55 std::function<void()> unexecute, | |
56 std::function<void()> onDelete) : | |
57 m_name(name), | |
58 m_execute(execute), | |
59 m_unexecute(unexecute), | |
60 m_onDelete(onDelete) { | |
61 } | |
62 | |
63 virtual ~GenericCommand() { | |
64 m_onDelete(); | |
65 } | |
66 | |
67 QString getName() const override { | |
68 return m_name; | |
69 } | |
70 | |
71 void execute() override { | |
72 m_execute(); | |
73 } | |
74 | |
75 void unexecute() override { | |
76 m_unexecute(); | |
77 } | |
78 | |
79 private: | |
80 QString m_name; | |
81 std::function<void()> m_execute; | |
82 std::function<void()> m_unexecute; | |
83 std::function<void()> m_onDelete; | |
84 }; | |
34 | 85 |
35 class MacroCommand : public Command | 86 class MacroCommand : public Command |
36 { | 87 { |
37 public: | 88 public: |
38 MacroCommand(QString name); | 89 MacroCommand(QString name); |