This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandMgr.cpp
78 lines (63 loc) · 1.78 KB
/
CommandMgr.cpp
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
/*
* CommandMgr.cpp
*
* Created on: 2011-4-1
* Author: simophin
*/
#include "CommandMgr.h"
#include "Command.h"
#include "CommandHandler.h"
#include "commands/VideoCommand.h"
#include <list>
#include <utility>
#include <map>
typedef std::list<CommandHandler *> CommandHandlerList;
typedef std::map<String, CommandHandlerList> CommandMap;
class CommandMgr::Impl {
public:
CommandMap commandMap;
bool hasRegistered;
Impl()
:hasRegistered(false) {
}
};
CommandMgr::CommandMgr()
:d(new CommandMgr::Impl) {
}
CommandMgr::~CommandMgr() {
for (CommandMap::const_iterator iter = d->commandMap.begin();
iter != d->commandMap.end(); ++ iter) {
for (CommandHandlerList::const_iterator i = iter->second.begin();
i != iter->second.end(); ++ i) {
delete *i;
}
}
delete d;
}
void CommandMgr::
handleCommand(const Command &cmd, const CommandContext *ctx)
{
CommandMap::const_iterator found = d->commandMap.find(cmd.getName());
if (found == d->commandMap.end()) return;
CommandHandlerList hlist = found->second;
for (CommandHandlerList::const_iterator i = hlist.begin();
i != hlist.end();
++ i) {
(*i)->onHandle(cmd,ctx);
}
}
void CommandMgr::
registerCommandHandler(CommandHandler *handler)
{
d->commandMap[handler->getCommandName()].push_back(handler);
}
void CommandMgr::registerAllCommands()
{
if (d->hasRegistered) return;
registerCommandHandler(new VideoCommand::QueryInfoCommandHandler);
registerCommandHandler(new VideoCommand::SetParameterCommandHandler);
registerCommandHandler(new VideoCommand::GetParameterCommandHandler);
registerCommandHandler(new VideoCommand::StartCaptureCommandHandler);
registerCommandHandler(new VideoCommand::StopCaptureCommandHandler);
d->hasRegistered = true;
}