summaryrefslogtreecommitdiff
path: root/src/FbTk/CommandRegistry.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/CommandRegistry.hh')
-rw-r--r--src/FbTk/CommandRegistry.hh130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/FbTk/CommandRegistry.hh b/src/FbTk/CommandRegistry.hh
new file mode 100644
index 0000000..f3b6880
--- /dev/null
+++ b/src/FbTk/CommandRegistry.hh
@@ -0,0 +1,130 @@
1// CommandRegistry.hh for FbTk
2// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: $
23
24#ifndef COMMANDREGISTRY_HH
25#define COMMANDREGISTRY_HH
26
27#include <string>
28#include <map>
29
30using std::string;
31
32namespace FbTk {
33class Command;
34class BoolCommand;
35
36#define REGISTER_COMMAND_PARSER(name, parser) \
37 namespace { \
38 static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, parser); \
39 }
40
41#define REGISTER_BOOLCOMMAND_PARSER(name, parser) \
42 namespace { \
43 static const bool p_register_##name = FbTk::CommandRegistry::instance().registerBoolCommand(#name, parser); \
44 }
45
46/* Include some basic command creators */
47template <typename Cmd, typename Ret>
48Ret *CommandCreator(const string &name, const string &args, bool trusted) {
49 return new Cmd();
50}
51
52#define REGISTER_COMMAND(name, classname) \
53 namespace { \
54 static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::CommandCreator<classname, FbTk::Command>); \
55 }
56
57template <typename Cmd, typename Ret>
58Ret *CommandCreatorWithArgs(const string &name, const string &args,
59 bool trusted) {
60 return new Cmd(args);
61}
62
63#define REGISTER_COMMAND_WITH_ARGS(name, classname) \
64 namespace { \
65 static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::CommandCreatorWithArgs<classname, FbTk::Command>); \
66 }
67
68#define REGISTER_BOOLCOMMAND_WITH_ARGS(name, classname) \
69 namespace { \
70 static const bool p_register_##name = FbTk::CommandRegistry::instance().registerBoolCommand(#name, FbTk::CommandCreatorWithArgs<classname, FbTk::BoolCommand>); \
71 }
72
73template <typename Cmd, typename Ret>
74Ret *UntrustedCommandCreator(const string &name, const string &args,
75 bool trusted) {
76 if (!trusted) return 0;
77 return new Cmd();
78}
79
80#define REGISTER_UNTRUSTED_COMMAND(name, classname) \
81 namespace { \
82 static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::UntrustedCommandCreator<classname, FbTk::Command>); \
83 }
84
85template <typename Cmd, typename Ret>
86Ret * UntrustedCommandCreatorWithArgs(const string &name, const string &args,
87 bool trusted) {
88 if (!trusted) return 0;
89 return new Cmd(args);
90}
91
92#define REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(name, classname) \
93 namespace { \
94 static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::UntrustedCommandCreatorWithArgs<classname, FbTk::Command>); \
95 }
96
97/* Not built to be virtual at the moment */
98class CommandRegistry {
99public:
100 typedef Command * CreateFunction(const string &name, const string &args, bool trusted);
101 typedef BoolCommand * BoolCreateFunction(const string &name,
102 const string &args, bool trusted);
103 typedef std::map<string, CreateFunction *> CreatorMap;
104 typedef std::map<string, BoolCreateFunction *> BoolCreatorMap;
105
106 static CommandRegistry &instance();
107
108 Command *parseLine(const string &line, bool trusted = true) const;
109 BoolCommand *parseBoolLine(const string &line, bool trusted = true) const;
110 Command *getCommand(const string &name, const string &args, bool trusted) const;
111 BoolCommand *getBoolCommand(const string &name, const string &args,
112 bool trusted) const;
113
114 /* Note: the ownership of this object passes to this class */
115 bool registerCommand(string name, CreateFunction createFunction);
116 bool registerBoolCommand(string name, BoolCreateFunction bcf);
117
118 const CreatorMap commandMap() const { return m_commandCreators; }
119
120private:
121 CommandRegistry() {}
122 ~CommandRegistry() {}
123
124 CreatorMap m_commandCreators;
125 BoolCreatorMap m_boolcommandCreators;
126};
127
128}; // end namespace FbTk
129
130#endif // COMMANDREGISTRY_HH