-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.hpp
98 lines (76 loc) · 1.88 KB
/
command.hpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef __LOOPLINE_COMMAND_HPP__
#define __LOOPLINE_COMMAND_HPP__
#include <functional>
namespace loopline
{
class Command
{
public:
virtual ~Command() {}
virtual void execute() = 0;
virtual void undo() = 0;
private:
};
class NullCommand : public Command
{
public:
NullCommand() {}
virtual void execute() {}
virtual void undo() {}
};
class LambdaCommand : public Command
{
public:
LambdaCommand(std::function<void()> exec, std::function<void()> un = [](){}) : exec(exec), un(un) {}
virtual ~LambdaCommand() {}
virtual void execute()
{
exec();
}
virtual void undo()
{
un();
}
private:
std::function<void()> exec, un;
};
class UndoCommand : public Command
{
public:
UndoCommand(std::vector<Command *> *commandList, std::vector<Command *> *undoList) : list(commandList), undoList(undoList) {}
virtual void execute()
{
if (list->empty())
return;
list->back()->undo();
undoList->push_back(list->back());
list->pop_back();
}
virtual void undo()
{
if (undoList->empty())
return;
undoList->back()->execute();
list->push_back(undoList->back());
undoList->pop_back();
}
private:
std::vector<Command *> *list;
std::vector<Command *> *undoList;
};
class RedoCommand : public Command
{
public:
RedoCommand(UndoCommand *undoCommand) : undoCommand(undoCommand) {}
virtual void execute()
{
undoCommand->undo();
}
virtual void undo()
{
}
private:
UndoCommand *undoCommand;
};
}; // namespace loopline
#endif