-
Notifications
You must be signed in to change notification settings - Fork 12
/
RemoteControl.hpp
68 lines (61 loc) · 2.02 KB
/
RemoteControl.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
#ifndef REMOTE_CONTROL_H
#define REMOTE_CONTROL_H
#include "Command.hpp"
#include "NoCommand.hpp"
#include <cstddef>
#include <ostream>
#include <memory>
#include <string>
class RemoteControl {
friend std::ostream& operator<<(std::ostream &, const RemoteControl &remote);
public:
RemoteControl () = default; // see private section for in-class initialization
void setCommand(const size_t &slot, Command *onCommand, Command *offCommand);
void onButtonWasPushed(const Command::cvec_size &slot);
void offButtonWasPushed(const 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; it's fine here
Command::command_vec onCommands = Command::command_vec(SIZE, noCommand.get());
Command::command_vec offCommands = Command::command_vec(SIZE, noCommand.get());
};
inline
void
RemoteControl::onButtonWasPushed(const Command::cvec_size &slot)
{
if (onCommands[slot] != nullptr) { // null object check
onCommands[slot]->execute();
undoCommand = onCommands[slot];
}
}
inline
void
RemoteControl::offButtonWasPushed(const Command::cvec_size &slot)
{
if (offCommands[slot] != nullptr) {
offCommands[slot]->execute();
undoCommand = offCommands[slot];
}
}
inline
void
RemoteControl::setCommand(const Command::cvec_size &slot, Command *onCommand, Command *offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
inline
std::ostream& operator<<(std::ostream &os, const RemoteControl &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_H */