-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRemoteControlWithUndo.hpp
More file actions
68 lines (61 loc) · 2.1 KB
/
Copy pathRemoteControlWithUndo.hpp
File metadata and controls
68 lines (61 loc) · 2.1 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
#ifndef REMOTE_CONTROL_WITH_UNDO_H
#define REMOTE_CONTROL_WITH_UNDO_H
#include "Command.hpp"
#include "NoCommand.hpp"
#include <cstddef>
#include <ostream>
#include <memory>
#include <string>
class RemoteControlWithUndo {
friend std::ostream& operator<<(std::ostream &, const RemoteControlWithUndo &remote);
public:
RemoteControlWithUndo () = default; // see private section for in-class initialization
void setCommand(Command::cvec_size slot, Command *onCommand, Command *offCommand);
void onButtonWasPushed(Command::cvec_size slot);
void offButtonWasPushed(Command::cvec_size slot);
void undoButtonWasPushed() { undoCommand->undo(); }
protected:
// Note: in-class initialization
constexpr static Command::cvec_size SIZE = 7;
std::shared_ptr<NoCommand> noCommand = std::make_shared<NoCommand>(); // shared_ptr make sense
Command *undoCommand = noCommand.get(); // use get() with caution; O.K. here as there is no ownership
Command::command_vec onCommands = Command::command_vec(SIZE, noCommand.get());
Command::command_vec offCommands = Command::command_vec(SIZE, noCommand.get());
};
inline
void
RemoteControlWithUndo::onButtonWasPushed(Command::cvec_size slot)
{
if (onCommands[slot] != nullptr) { // null object check
onCommands[slot]->execute();
undoCommand = onCommands[slot];
}
}
inline
void
RemoteControlWithUndo::offButtonWasPushed(Command::cvec_size slot)
{
if (offCommands[slot] != nullptr) {
offCommands[slot]->execute();
undoCommand = offCommands[slot];
}
}
inline
void
RemoteControlWithUndo::setCommand(Command::cvec_size slot, Command *onCommand, Command *offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
inline
std::ostream& operator<<(std::ostream &os, const RemoteControlWithUndo &remote)
{
os << "\n------ Remote Control ------\n";
for (Command::cvec_size i = 0; i < remote.onCommands.size(); ++i) {
os << "[slot " << i << "] " << remote.onCommands[i]->getClassName()
<< "\t" << remote.offCommands[i]->getClassName() << '\n';
}
os << "[undo] " << remote.undoCommand->getClassName() << '\n';
return os;
}
#endif /* REMOTE_CONTROL_WITH_UNDO_H */