Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/CommandExecutor.h @ 965

Last change on this file since 965 was 965, checked in by landauf, 16 years ago

even smarter autocompletion

File size: 8.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#ifndef _CommandExecutor_H__
29#define _CommandExecutor_H__
30
31#include <string>
32#include <map>
33#include <list>
34
35#include "util/SubString.h"
36#include "util/MultiTypeMath.h"
37#include "CorePrereqs.h"
38
39#define COMMAND_EXECUTOR_CURSOR '$'
40
41namespace orxonox
42{
43    enum CommandState
44    {
45        CS_Uninitialized,
46        CS_Empty,
47        CS_FunctionClass_Or_Shortcut_Or_Keyword,
48        CS_Shortcut_Params,
49        CS_Shortcut_Finished,
50        CS_Function,
51        CS_Function_Params,
52        CS_Function_Finished,
53        CS_ConfigValueClass,
54        CS_ConfigValue,
55        CS_ConfigValueType,
56        CS_ConfigValueFinished,
57        CS_KeybindKey,
58        CS_KeybindCommand,
59        CS_KeybindFinished,
60        CS_Error
61    };
62
63    enum KeybindMode {}; // temporary
64
65    ///////////////////////
66    // CommandEvaluation //
67    ///////////////////////
68    class _CoreExport CommandEvaluation
69    {
70        friend class CommandExecutor;
71
72        public:
73            CommandEvaluation();
74
75            KeybindMode getKeybindMode();
76            bool isValid() const;
77
78            inline void setAdditionalParameter(const std::string& param)
79                { this->additionalParameter_ = param; }
80            inline std::string getAdditionalParameter() const
81                { return (this->additionalParameter_ != "") ? (" " + this->additionalParameter_) : ""; }
82
83        private:
84            std::string processedCommand_;
85            SubString tokens_;
86            std::string additionalParameter_;
87
88            std::list<const std::string*> listOfPossibleFunctionClasses_;
89            std::list<const std::string*> listOfPossibleShortcuts_;
90            std::list<const std::string*> listOfPossibleFunctions_;
91            std::list<const std::string*> listOfPossibleConfigValueClasses_;
92            std::list<const std::string*> listOfPossibleConfigValues_;
93            std::list<const std::string*> listOfPossibleKeys_;
94
95            Identifier* functionclass_;
96            Identifier* configvalueclass_;
97            ExecutorStatic* shortcut_;
98            ExecutorStatic* function_;
99            ConfigValueContainer* configvalue_;
100            ConfigValueContainer* key_;
101
102            std::string errorMessage_;
103            CommandState state_;
104    };
105
106    /////////////////////
107    // CommandExecutor //
108    /////////////////////
109    class _CoreExport CommandExecutor
110    {
111        public:
112            static bool execute(const std::string& command);
113            static bool execute(const CommandEvaluation& evaluation);
114
115            static std::string complete(const std::string& command);
116            static std::string complete(const CommandEvaluation& evaluation);
117
118            static std::string hint(const std::string& command);
119            static std::string hint(const CommandEvaluation& evaluation);
120
121            static CommandEvaluation evaluate(const std::string& command);
122
123            static bool addConsoleCommandShortcut(ExecutorStatic* executor);
124            static ExecutorStatic* getConsoleCommandShortcut(const std::string& name);
125            static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name);
126
127            /** @brief Returns the map that stores all console commands. @return The const_iterator */
128            static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_; }
129            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */
130            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_.begin(); }
131            /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */
132            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_.end(); }
133
134            /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */
135            static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_; }
136            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands with their names in lowercase. @return The const_iterator */
137            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.begin(); }
138            /** @brief Returns a const_iterator to the end of the map that stores all console commands with their names in lowercase. @return The const_iterator */
139            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); }
140
141        private:
142            CommandExecutor() {}
143            CommandExecutor(const CommandExecutor& other) {}
144            ~CommandExecutor() {}
145
146            static CommandExecutor& getInstance();
147            static CommandEvaluation& getEvaluation();
148
149            static void parse(const std::string& command, bool bInitialize = true);
150            static void initialize(const std::string& command);
151
152            static bool argumentsGiven(unsigned int num);
153            static unsigned int argumentsGiven();
154
155            static std::string getToken(unsigned int index);
156
157            static bool enoughParametersGiven(unsigned int head, Executor* executor);
158
159            static void createListOfPossibleShortcuts(const std::string& fragment);
160            static void createListOfPossibleFunctionClasses(const std::string& fragment);
161            static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier);
162            static void createListOfPossibleConfigValueClasses(const std::string& fragment);
163            static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier);
164            static void createListOfPossibleKeys(const std::string& fragment);
165
166            static bool compareStringsInList(const std::string* first, const std::string* second);
167
168            static std::string dump(const std::list<const std::string*>& list);
169            static std::string dump(const ExecutorStatic* executor);
170            static std::string dump(const ConfigValueContainer* container);
171
172            static std::string getCommonBegin(const std::list<const std::string*>& list);
173
174            static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name);
175            static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name);
176            static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier);
177            static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name);
178            static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier);
179            static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name);
180
181            CommandEvaluation evaluation_;
182
183            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_;
184            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_;
185    };
186}
187
188#endif /* _CommandExecutor_H__ */
Note: See TracBrowser for help on using the repository browser.