-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRemoteControl.hpp
More file actions
59 lines (52 loc) · 1.73 KB
/
Copy pathRemoteControl.hpp
File metadata and controls
59 lines (52 loc) · 1.73 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
#ifndef REMOTE_CONTROL_H
#define REMOTE_CONTROL_H
#include "Command.hpp"
#include "NoCommand.hpp"
#include <ostream>
#include <memory>
#include <string>
class RemoteControl {
friend std::ostream& operator<<(std::ostream &, const RemoteControl &remote);
public:
RemoteControl () = default; // see private section for inclass initialization
void setCommand(Command::cvec_size slot, Command *onCommand, Command *offCommand);
void onButtonWasPushed(Command::cvec_size slot) const;
void offButtonWasPushed(Command::cvec_size slot) const;
protected:
constexpr static Command::cvec_size SIZE = 7; // to avoid magic numbers
std::shared_ptr<NoCommand> noCommand = std::make_shared<NoCommand>(); // shared ownership
Command::command_vec onCommands = Command::command_vec(SIZE, noCommand.get()); // construct vector
Command::command_vec offCommands = Command::command_vec(SIZE, noCommand.get());
};
inline
void
RemoteControl::onButtonWasPushed(Command::cvec_size slot) const
{
if (onCommands[slot] != nullptr) // null object check
onCommands[slot]->execute();
}
inline
void
RemoteControl::offButtonWasPushed(Command::cvec_size slot) const
{
if (offCommands[slot] != nullptr)
offCommands[slot]->execute();
}
inline
void
RemoteControl::setCommand(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';
}
return os;
}
#endif /* REMOTE_CONTROL_H */