annotate scn2k/command_handler.cc @ 54:d7cde171a1de

* scn2k_grp.cc now handles commands in a cleanier way \o/ * some cleaning
author thib
date Mon, 20 Apr 2009 16:18:55 +0000
parents
children f1a27ee7e03c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
1 /*
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
2 * Copyright (c) 2009 Thibaut GIRKA <thib@sitedehib.com>
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
3 * All rights reserved.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
4 *
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
7 * are met:
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
15 *
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
26 */
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
27
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
28 #include <map>
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
29 #include <stdlib.h>
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
30
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
31 #include "scn2k.h"
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
32
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
33 #define CMDMAP_DEBUG 1
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
34
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
35 void CommandHandler::RegisterCommand(int cmd1, int cmd2, int cmd3, const char* descr, CmdImpl func)
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
36 {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
37 if (descr == NULL && func == NULL)
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
38 return;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
39 SimpleCmd cmd(cmd1, cmd2, cmd3);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
40 CommandInfo info;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
41 info.function = func;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
42 info.descr = descr;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
43
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
44 #ifdef CMDMAP_DEBUG
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
45 CommandMap::iterator it;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
46 it = command_map.find(cmd);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
47 if (it != command_map.end()) {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
48 printf("ERROR: Command '%s' already registered as '%s'!\n", descr, it->second.descr);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
49 abort();
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
50 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
51 #endif
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
52
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
53 command_map[cmd] = info;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
54 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
55
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
56 bool CommandHandler::Exec(Cmd &cmd) {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
57 CommandMap::iterator it;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
58
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
59 it = command_map.find(cmd);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
60 if (it == command_map.end()) {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
61 //printf("Unrecognized opcode %d %d %d\n", cmd.cmd1, cmd.cmd2, cmd.cmd3); //Debug
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
62 return false;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
63 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
64 else {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
65 CmdImpl func = it->second.function;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
66 if (func == NULL) printf("Function %s not implemented yet\n", it->second.descr);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
67 else (this->*func)(cmd);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
68 return true;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
69 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
70 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
71