aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/ObjectRegistry.hh
diff options
context:
space:
mode:
authorMark Tiefenbruck <mark@fluxbox.org>2007-12-16 03:36:01 (GMT)
committerMark Tiefenbruck <mark@fluxbox.org>2007-12-16 03:36:01 (GMT)
commit8516f1e2a21a374127ccfa3f7b8de2443e67fe33 (patch)
treea66f212f856981584b2b44bfc772096c5ee7bebe /src/FbTk/ObjectRegistry.hh
parent90f4f1ecc1635fd5d144f86d64b1674958d0a59f (diff)
downloadfluxbox_pavel-8516f1e2a21a374127ccfa3f7b8de2443e67fe33.zip
fluxbox_pavel-8516f1e2a21a374127ccfa3f7b8de2443e67fe33.tar.bz2
changed CommandRegistry to a template class, renamed to ObjectRegistry<Type>
Diffstat (limited to 'src/FbTk/ObjectRegistry.hh')
-rw-r--r--src/FbTk/ObjectRegistry.hh160
1 files changed, 160 insertions, 0 deletions
diff --git a/src/FbTk/ObjectRegistry.hh b/src/FbTk/ObjectRegistry.hh
new file mode 100644
index 0000000..bc1964c
--- /dev/null
+++ b/src/FbTk/ObjectRegistry.hh
@@ -0,0 +1,160 @@
1// ObjectRegistry.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 OBJECTREGISTRY_HH
25#define OBJECTREGISTRY_HH
26
27#include "StringUtil.hh"
28
29#include <string>
30#include <map>
31
32using std::string;
33
34namespace FbTk {
35
36#define REGISTER_OBJECT_PARSER(name, parser, type) \
37 namespace { \
38 static const bool p_register_##type_##name = FbTk::ObjectRegistry<type>::instance().registerObject(#name, parser); \
39 }
40
41#define REGISTER_OBJECT_PARSER_NSBASE(name, parser, space, base) \
42 namespace { \
43 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, parser); \
44 }
45
46/* Include some basic Object creators */
47template <typename Derived, typename Base>
48Base *ObjectCreator(const string &name, const string &args, bool trusted) {
49 return new Derived();
50}
51
52#define REGISTER_OBJECT(name, derived, base) \
53 namespace { \
54 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::ObjectCreator<derived, base >); \
55 }
56
57#define REGISTER_OBJECT_NSBASE(name, derived, space, base) \
58 namespace { \
59 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::ObjectCreator<derived, space::base >); \
60 }
61
62template <typename Derived, typename Base>
63Base *ObjectCreatorWithArgs(const string &name, const string &args,
64 bool trusted) {
65 return new Derived(args);
66}
67
68#define REGISTER_OBJECT_WITH_ARGS(name, derived, base) \
69 namespace { \
70 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::ObjectCreatorWithArgs<derived, base >); \
71 }
72
73#define REGISTER_OBJECT_WITH_ARGS_NSBASE(name, derived, space, base) \
74 namespace { \
75 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::ObjectCreatorWithArgs<derived, space::base >); \
76 }
77
78template <typename Derived, typename Base>
79Base *UntrustedObjectCreator(const string &name, const string &args,
80 bool trusted) {
81 if (!trusted) return 0;
82 return new Derived();
83}
84
85#define REGISTER_UNTRUSTED_OBJECT(name, derived, base) \
86 namespace { \
87 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::UntrustedObjectCreator<derived, base >); \
88 }
89
90#define REGISTER_UNTRUSTED_OBJECT_NSBASE(name, derived, space, base) \
91 namespace { \
92 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::UntrustedObjectCreator<derived, space::base >); \
93 }
94
95template <typename Derived, typename Base>
96Base * UntrustedObjectCreatorWithArgs(const string &name, const string &args,
97 bool trusted) {
98 if (!trusted) return 0;
99 return new Derived(args);
100}
101
102#define REGISTER_UNTRUSTED_OBJECT_WITH_ARGS(name, derived, base) \
103 namespace { \
104 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::UntrustedObjectCreatorWithArgs<derived, base >); \
105 }
106
107#define REGISTER_UNTRUSTED_OBJECT_WITH_ARGS_NSBASE(name, derived, space, base) \
108 namespace { \
109 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::UntrustedObjectCreatorWithArgs<derived, space::base >); \
110 }
111
112template <typename Base>
113class ObjectRegistry {
114public:
115 typedef Base * CreateFunction(const string &name, const string &args, bool trusted);
116 typedef std::map<string, CreateFunction *> CreatorMap;
117
118 static ObjectRegistry<Base > &instance() {
119 static ObjectRegistry<Base > s_instance;
120 return s_instance;
121 }
122
123 Base *parse(const string &name, const string &args, bool trusted = true) const {
124 string lc = StringUtil::toLower(name);
125 typename CreatorMap::const_iterator it = m_creators.find(lc.c_str());
126 if (it == m_creators.end())
127 return 0;
128 else
129 return it->second(lc, args, trusted);
130 }
131
132 Base *parse(const string &line, bool trusted = true) const {
133 // parse args and command
134 string command, args;
135 StringUtil::getFirstWord(line, command, args);
136 StringUtil::removeFirstWhitespace(args);
137 StringUtil::removeTrailingWhitespace(args);
138
139 // now we have parsed command and args
140 return parse(command, args, trusted);
141 }
142
143 bool registerObject(string name, CreateFunction createFunction) {
144 name = StringUtil::toLower(name);
145 m_creators[name] = createFunction;
146 return true;
147 }
148
149 const CreatorMap creatorMap() const { return m_creators; }
150
151private:
152 ObjectRegistry() {}
153 ~ObjectRegistry() {}
154
155 CreatorMap m_creators;
156};
157
158}; // end namespace FbTk
159
160#endif // OBJECTREGISTRY_HH