Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 953 was 953, checked in by landauf, 16 years ago
  • added some functions to CommandEvaluation
  • added possibility of additional parameters to CommandEvaluator and CommandExecutor
File size: 8.2 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 "CorePrereqs.h"
37
38#define COMMAND_EXECUTOR_CURSOR '$'
39
40namespace orxonox
41{
42    enum CommandState
43    {
44        CS_Empty,
45        CS_FunctionClass_Or_Shortcut_Or_Keyword,
46        CS_Shortcut_Params,
47        CS_Shortcut_Finished,
48        CS_Function,
49        CS_Function_Params,
50        CS_Function_Finished,
51        CS_ConfigValueClass,
52        CS_ConfigValue,
53        CS_ConfigValueType,
54        CS_ConfigValueFinished,
55        CS_KeybindKey,
56        CS_KeybindCommand,
57        CS_KeybindFinished,
58        CS_Error
59    };
60
61    enum KeybindMode {}; // temporary
62
63    ///////////////////////
64    // CommandEvaluation //
65    ///////////////////////
66    class _CoreExport CommandEvaluation
67    {
68        public:
69            KeybindMode getKeybindMode();
70            void setAdditionalParameter(const std::string& param);
71            bool isValid() const;
72
73            std::string processedCommand_;
74            SubString tokens_;
75            std::string additionalParameter_;
76
77            std::list<const std::string*> listOfPossibleFunctionClasses_;
78            std::list<const std::string*> listOfPossibleShortcuts_;
79            std::list<const std::string*> listOfPossibleFunctions_;
80            std::list<const std::string*> listOfPossibleConfigValueClasses_;
81            std::list<const std::string*> listOfPossibleConfigValues_;
82            std::list<const std::string*> listOfPossibleKeys_;
83
84            Identifier* functionclass_;
85            Identifier* configvalueclass_;
86            ExecutorStatic* shortcut_;
87            ExecutorStatic* function_;
88            ConfigValueContainer* configvalue_;
89            ConfigValueContainer* key_;
90
91            std::string errorMessage_;
92            CommandState state_;
93    };
94
95    /////////////////////
96    // CommandExecutor //
97    /////////////////////
98    class _CoreExport CommandExecutor
99    {
100        public:
101            static bool execute(const std::string& command);
102            static bool execute(const CommandEvaluation& evaluation);
103
104            static std::string complete(const std::string& command);
105            static std::string complete(const CommandEvaluation& evaluation);
106
107            static std::string hint(const std::string& command);
108            static std::string hint(const CommandEvaluation& evaluation);
109
110            static CommandEvaluation evaluate(const std::string& command);
111
112            static bool addConsoleCommandShortcut(ExecutorStatic* executor);
113            static ExecutorStatic* getConsoleCommandShortcut(const std::string& name);
114            static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name);
115
116            /** @brief Returns the map that stores all console commands. @return The const_iterator */
117            static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_; }
118            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */
119            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_.begin(); }
120            /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */
121            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_.end(); }
122
123            /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */
124            static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_; }
125            /** @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 */
126            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.begin(); }
127            /** @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 */
128            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); }
129
130        private:
131            CommandExecutor() {}
132            CommandExecutor(const CommandExecutor& other) {}
133            ~CommandExecutor() {}
134
135            static CommandExecutor& getInstance();
136            static CommandEvaluation& getEvaluation();
137
138            static void parse(const std::string& command, bool bInitialize = true);
139            static void initialize(const std::string& command);
140
141            static bool argumentsGiven(unsigned int num);
142            static unsigned int argumentsGiven();
143
144            static std::string getToken(unsigned int index);
145
146            static bool enoughParametersGiven(unsigned int head, Executor* executor);
147
148            static void createListOfPossibleShortcuts(const std::string& fragment);
149            static void createListOfPossibleFunctionClasses(const std::string& fragment);
150            static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier);
151            static void createListOfPossibleConfigValueClasses(const std::string& fragment);
152            static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier);
153            static void createListOfPossibleKeys(const std::string& fragment);
154
155            static bool compareStringsInList(const std::string* first, const std::string* second);
156
157            static std::string dump(const std::list<const std::string*>& list);
158            static std::string dump(const ExecutorStatic* executor);
159            static std::string dump(const ConfigValueContainer* container);
160
161            static std::string getCommonBegin(const std::list<const std::string*>& list);
162
163            static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name);
164            static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name);
165            static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier);
166            static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name);
167            static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier);
168            static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name);
169
170            CommandEvaluation evaluation_;
171
172            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_;
173            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_;
174    };
175}
176
177#endif /* _CommandExecutor_H__ */
Note: See TracBrowser for help on using the repository browser.