Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 955 was 955, checked in by landauf, 16 years ago
  • added input buffer: this class captures key-input (at the moment it's using OIS directly, later it will use the InputHandler) and writes it into a string - other classes can listen to changes and can read and modify the string.
  • fixed some bugs in CommandExecutor
  • fixed a small bug (or changed a questionable feature) in Functor
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 "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        public:
71            CommandEvaluation();
72
73            KeybindMode getKeybindMode();
74            void setAdditionalParameter(const std::string& param);
75            bool isValid() const;
76
77            std::string processedCommand_;
78            SubString tokens_;
79            std::string additionalParameter_;
80
81            std::list<const std::string*> listOfPossibleFunctionClasses_;
82            std::list<const std::string*> listOfPossibleShortcuts_;
83            std::list<const std::string*> listOfPossibleFunctions_;
84            std::list<const std::string*> listOfPossibleConfigValueClasses_;
85            std::list<const std::string*> listOfPossibleConfigValues_;
86            std::list<const std::string*> listOfPossibleKeys_;
87
88            Identifier* functionclass_;
89            Identifier* configvalueclass_;
90            ExecutorStatic* shortcut_;
91            ExecutorStatic* function_;
92            ConfigValueContainer* configvalue_;
93            ConfigValueContainer* key_;
94
95            std::string errorMessage_;
96            CommandState state_;
97    };
98
99    /////////////////////
100    // CommandExecutor //
101    /////////////////////
102    class _CoreExport CommandExecutor
103    {
104        public:
105            static bool execute(const std::string& command);
106            static bool execute(const CommandEvaluation& evaluation);
107
108            static std::string complete(const std::string& command);
109            static std::string complete(const CommandEvaluation& evaluation);
110
111            static std::string hint(const std::string& command);
112            static std::string hint(const CommandEvaluation& evaluation);
113
114            static CommandEvaluation evaluate(const std::string& command);
115
116            static bool addConsoleCommandShortcut(ExecutorStatic* executor);
117            static ExecutorStatic* getConsoleCommandShortcut(const std::string& name);
118            static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name);
119
120            /** @brief Returns the map that stores all console commands. @return The const_iterator */
121            static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_; }
122            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */
123            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_.begin(); }
124            /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */
125            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_.end(); }
126
127            /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */
128            static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_; }
129            /** @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 */
130            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.begin(); }
131            /** @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 */
132            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); }
133
134        private:
135            CommandExecutor() {}
136            CommandExecutor(const CommandExecutor& other) {}
137            ~CommandExecutor() {}
138
139            static CommandExecutor& getInstance();
140            static CommandEvaluation& getEvaluation();
141
142            static void parse(const std::string& command, bool bInitialize = true);
143            static void initialize(const std::string& command);
144
145            static bool argumentsGiven(unsigned int num);
146            static unsigned int argumentsGiven();
147
148            static std::string getToken(unsigned int index);
149
150            static bool enoughParametersGiven(unsigned int head, Executor* executor);
151
152            static void createListOfPossibleShortcuts(const std::string& fragment);
153            static void createListOfPossibleFunctionClasses(const std::string& fragment);
154            static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier);
155            static void createListOfPossibleConfigValueClasses(const std::string& fragment);
156            static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier);
157            static void createListOfPossibleKeys(const std::string& fragment);
158
159            static bool compareStringsInList(const std::string* first, const std::string* second);
160
161            static std::string dump(const std::list<const std::string*>& list);
162            static std::string dump(const ExecutorStatic* executor);
163            static std::string dump(const ConfigValueContainer* container);
164
165            static std::string getCommonBegin(const std::list<const std::string*>& list);
166
167            static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name);
168            static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name);
169            static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier);
170            static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name);
171            static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier);
172            static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name);
173
174            CommandEvaluation evaluation_;
175
176            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_;
177            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_;
178    };
179}
180
181#endif /* _CommandExecutor_H__ */
Note: See TracBrowser for help on using the repository browser.