diff options
Diffstat (limited to 'src/FbTk/CommandParser.hh')
-rw-r--r-- | src/FbTk/CommandParser.hh | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/FbTk/CommandParser.hh b/src/FbTk/CommandParser.hh index 006036d..45ce001 100644 --- a/src/FbTk/CommandParser.hh +++ b/src/FbTk/CommandParser.hh | |||
@@ -22,9 +22,11 @@ | |||
22 | #ifndef CommandParser_HH | 22 | #ifndef CommandParser_HH |
23 | #define CommandParser_HH | 23 | #define CommandParser_HH |
24 | 24 | ||
25 | #include "ObjectRegistry.hh" | ||
26 | #include "StringUtil.hh" | 25 | #include "StringUtil.hh" |
27 | 26 | ||
27 | #include <string> | ||
28 | #include <map> | ||
29 | |||
28 | using std::string; | 30 | using std::string; |
29 | 31 | ||
30 | namespace FbTk { | 32 | namespace FbTk { |
@@ -86,6 +88,7 @@ template <typename Type> | |||
86 | class CommandParser { | 88 | class CommandParser { |
87 | public: | 89 | public: |
88 | typedef Command<Type> *(*Creator)(const string &, const string &, bool); | 90 | typedef Command<Type> *(*Creator)(const string &, const string &, bool); |
91 | typedef std::map<std::string, Creator> CreatorMap; | ||
89 | 92 | ||
90 | static CommandParser<Type> &instance() { | 93 | static CommandParser<Type> &instance() { |
91 | static CommandParser<Type> s_instance; | 94 | static CommandParser<Type> s_instance; |
@@ -93,9 +96,9 @@ public: | |||
93 | } | 96 | } |
94 | 97 | ||
95 | Command<Type> *parse(const string &name, const string &args, | 98 | Command<Type> *parse(const string &name, const string &args, |
96 | bool trusted = true) const { | 99 | bool trusted = true) const { |
97 | string lc = StringUtil::toLower(name); | 100 | string lc = StringUtil::toLower(name); |
98 | Creator creator = ObjectRegistry<Creator>::instance().lookup(lc); | 101 | Creator creator = lookup(lc); |
99 | if (creator) | 102 | if (creator) |
100 | return creator(lc, args, trusted); | 103 | return creator(lc, args, trusted); |
101 | return 0; | 104 | return 0; |
@@ -114,13 +117,24 @@ public: | |||
114 | 117 | ||
115 | bool registerCommand(string name, Creator creator) { | 118 | bool registerCommand(string name, Creator creator) { |
116 | name = StringUtil::toLower(name); | 119 | name = StringUtil::toLower(name); |
117 | return ObjectRegistry<Creator>::instance().registerObject(name, | 120 | m_creators[name] = creator; |
118 | creator); | 121 | return true; |
122 | } | ||
123 | |||
124 | Creator lookup(const std::string &name) const { | ||
125 | typename CreatorMap::const_iterator it = m_creators.find(name); | ||
126 | if (it == m_creators.end()) | ||
127 | return 0; | ||
128 | return it->second; | ||
119 | } | 129 | } |
120 | 130 | ||
131 | const CreatorMap creatorMap() const { return m_creators; } | ||
132 | |||
121 | private: | 133 | private: |
122 | CommandParser() {} | 134 | CommandParser() {} |
123 | ~CommandParser() {} | 135 | ~CommandParser() {} |
136 | |||
137 | CreatorMap m_creators; | ||
124 | }; | 138 | }; |
125 | 139 | ||
126 | } // end namespace FbTk | 140 | } // end namespace FbTk |