aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/ObjectRegistry.hh
diff options
context:
space:
mode:
authorMark Tiefenbruck <mark@fluxbox.org>2008-01-11 07:41:22 (GMT)
committerMark Tiefenbruck <mark@fluxbox.org>2008-01-11 07:41:22 (GMT)
commit9f2f65a698c4cc71373a7fe9d73a0889e0d3487b (patch)
tree4ad67db771d73ea3c48f80a1244037fc9754edd2 /src/FbTk/ObjectRegistry.hh
parent1f01d84c080d607a91eb417efcaf5e500b5f1d7e (diff)
downloadfluxbox_pavel-9f2f65a698c4cc71373a7fe9d73a0889e0d3487b.zip
fluxbox_pavel-9f2f65a698c4cc71373a7fe9d73a0889e0d3487b.tar.bz2
make FbTk::Command a template class, split parsing information out of ObjectRegistry
Diffstat (limited to 'src/FbTk/ObjectRegistry.hh')
-rw-r--r--src/FbTk/ObjectRegistry.hh114
1 files changed, 10 insertions, 104 deletions
diff --git a/src/FbTk/ObjectRegistry.hh b/src/FbTk/ObjectRegistry.hh
index d6a97d9..e68d630 100644
--- a/src/FbTk/ObjectRegistry.hh
+++ b/src/FbTk/ObjectRegistry.hh
@@ -22,124 +22,30 @@
22#ifndef OBJECTREGISTRY_HH 22#ifndef OBJECTREGISTRY_HH
23#define OBJECTREGISTRY_HH 23#define OBJECTREGISTRY_HH
24 24
25#include "StringUtil.hh"
26
27#include <map> 25#include <map>
28 26#include <string>
29using std::string;
30 27
31namespace FbTk { 28namespace FbTk {
32 29
33#define REGISTER_OBJECT_PARSER(name, parser, type) \ 30template <typename Creator>
34 namespace { \
35 static const bool p_register_##type_##name = FbTk::ObjectRegistry<type>::instance().registerObject(#name, parser); \
36 }
37
38#define REGISTER_OBJECT_PARSER_NSBASE(name, parser, space, base) \
39 namespace { \
40 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, parser); \
41 }
42
43/* Include some basic Object creators */
44template <typename Derived, typename Base>
45Base *ObjectCreator(const string &name, const string &args, bool trusted) {
46 return new Derived();
47}
48
49#define REGISTER_OBJECT(name, derived, base) \
50 namespace { \
51 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::ObjectCreator<derived, base >); \
52 }
53
54#define REGISTER_OBJECT_NSBASE(name, derived, space, base) \
55 namespace { \
56 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::ObjectCreator<derived, space::base >); \
57 }
58
59template <typename Derived, typename Base>
60Base *ObjectCreatorWithArgs(const string &name, const string &args,
61 bool trusted) {
62 return new Derived(args);
63}
64
65#define REGISTER_OBJECT_WITH_ARGS(name, derived, base) \
66 namespace { \
67 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::ObjectCreatorWithArgs<derived, base >); \
68 }
69
70#define REGISTER_OBJECT_WITH_ARGS_NSBASE(name, derived, space, base) \
71 namespace { \
72 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::ObjectCreatorWithArgs<derived, space::base >); \
73 }
74
75template <typename Derived, typename Base>
76Base *UntrustedObjectCreator(const string &name, const string &args,
77 bool trusted) {
78 if (!trusted) return 0;
79 return new Derived();
80}
81
82#define REGISTER_UNTRUSTED_OBJECT(name, derived, base) \
83 namespace { \
84 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::UntrustedObjectCreator<derived, base >); \
85 }
86
87#define REGISTER_UNTRUSTED_OBJECT_NSBASE(name, derived, space, base) \
88 namespace { \
89 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::UntrustedObjectCreator<derived, space::base >); \
90 }
91
92template <typename Derived, typename Base>
93Base * UntrustedObjectCreatorWithArgs(const string &name, const string &args,
94 bool trusted) {
95 if (!trusted) return 0;
96 return new Derived(args);
97}
98
99#define REGISTER_UNTRUSTED_OBJECT_WITH_ARGS(name, derived, base) \
100 namespace { \
101 static const bool p_register_##type_##name = FbTk::ObjectRegistry<base >::instance().registerObject(#name, FbTk::UntrustedObjectCreatorWithArgs<derived, base >); \
102 }
103
104#define REGISTER_UNTRUSTED_OBJECT_WITH_ARGS_NSBASE(name, derived, space, base) \
105 namespace { \
106 static const bool p_register_##type_##name = FbTk::ObjectRegistry<space::base >::instance().registerObject(#name, FbTk::UntrustedObjectCreatorWithArgs<derived, space::base >); \
107 }
108
109template <typename Base>
110class ObjectRegistry { 31class ObjectRegistry {
111public: 32public:
112 typedef Base * CreateFunction(const string &name, const string &args, bool trusted); 33 typedef std::map<std::string, Creator> CreatorMap;
113 typedef std::map<string, CreateFunction *> CreatorMap;
114 34
115 static ObjectRegistry<Base > &instance() { 35 static ObjectRegistry<Creator> &instance() {
116 static ObjectRegistry<Base > s_instance; 36 static ObjectRegistry<Creator> s_instance;
117 return s_instance; 37 return s_instance;
118 } 38 }
119 39
120 Base *parse(const string &name, const string &args, bool trusted = true) const { 40 Creator lookup(const std::string &name) {
121 string lc = StringUtil::toLower(name); 41 typename CreatorMap::const_iterator it = m_creators.find(name);
122 typename CreatorMap::const_iterator it = m_creators.find(lc.c_str());
123 if (it == m_creators.end()) 42 if (it == m_creators.end())
124 return 0; 43 return 0;
125 else 44 return it->second;
126 return it->second(lc, args, trusted);
127 }
128
129 Base *parse(const string &line, bool trusted = true) const {
130 // parse args and command
131 string command, args;
132 StringUtil::getFirstWord(line, command, args);
133 StringUtil::removeFirstWhitespace(args);
134 StringUtil::removeTrailingWhitespace(args);
135
136 // now we have parsed command and args
137 return parse(command, args, trusted);
138 } 45 }
139 46
140 bool registerObject(string name, CreateFunction createFunction) { 47 bool registerObject(const std::string &name, Creator creator) {
141 name = StringUtil::toLower(name); 48 m_creators[name] = creator;
142 m_creators[name] = createFunction;
143 return true; 49 return true;
144 } 50 }
145 51