-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndoRedoClass.h
More file actions
69 lines (54 loc) · 1.48 KB
/
UndoRedoClass.h
File metadata and controls
69 lines (54 loc) · 1.48 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
/**
* @author DarkLich
* @date 2023.03.11
*/
#ifndef UNDOREDOPOOL_UNDOREDOCLASS_H
#define UNDOREDOPOOL_UNDOREDOCLASS_H
#include <vector>
#include <functional>
#include <future>
#include <memory>
// 撤销和重做的对象
class UndoRedoClass {
private:
// 撤销执行的函数
std::vector<std::function<void()>> undoFunction;
// 重做执行的函数
std::vector<std::function<void()>> redoFunction;
public:
void Undo();
void Redo();
template<class F, class... Args>
void MakeUndo(F&& f, Args&&... args);
template<class F, class... Args>
void MakeRedo(F&& f, Args&&... args);
};
void UndoRedoClass::Undo() {
for (int i = 0; i < undoFunction.size(); ++i) {
std::function<void()> temp = undoFunction[i];
temp();
}
}
void UndoRedoClass::Redo() {
for (int i = 0; i < redoFunction.size(); ++i) {
std::function<void()> temp = redoFunction[i];
temp();
}
}
template<class F, class... Args>
void UndoRedoClass::MakeUndo(F&& f, Args&&... args) {
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::function<return_type()>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
undoFunction.push_back(task);
}
template<class F, class... Args>
void UndoRedoClass::MakeRedo(F&& f, Args&&... args) {
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::function<return_type()>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
redoFunction.push_back(task);
}
#endif //UNDOREDOPOOL_UNDOREDOCLASS_H