Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 957 was 957, checked in by landauf, 16 years ago
  • added set and tset functions to the ConfigValueContainer to (temporary) set a config-value to a new value
  • ConfigValueContainer uses now the functions of MultiTypeMath to convert and assign values
  • added some errorhandling to the CommandExecutor in case there are not enough parameters when executing the command
  • added updateConfigValues function to Identifier
  • added addTime and removeTime functions to the Timer
  • some changes in Executor to allow adding description and default-values when using the ConsoleCommand macro
File size: 8.3 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            void setAdditionalParameter(const std::string& param);
77            bool isValid() const;
78
79        private:
80            std::string processedCommand_;
81            SubString tokens_;
82            std::string additionalParameter_;
83
84            std::list<const std::string*> listOfPossibleFunctionClasses_;
85            std::list<const std::string*> listOfPossibleShortcuts_;
86            std::list<const std::string*> listOfPossibleFunctions_;
87            std::list<const std::string*> listOfPossibleConfigValueClasses_;
88            std::list<const std::string*> listOfPossibleConfigValues_;
89            std::list<const std::string*> listOfPossibleKeys_;
90
91            Identifier* functionclass_;
92            Identifier* configvalueclass_;
93            ExecutorStatic* shortcut_;
94            ExecutorStatic* function_;
95            ConfigValueContainer* configvalue_;
96            ConfigValueContainer* key_;
97
98            std::string errorMessage_;
99            CommandState state_;
100    };
101
102    /////////////////////
103    // CommandExecutor //
104    /////////////////////
105    class _CoreExport CommandExecutor
106    {
107        public:
108            static bool execute(const std::string& command);
109            static bool execute(const CommandEvaluation& evaluation);
110
111            static std::string complete(const std::string& command);
112            static std::string complete(const CommandEvaluation& evaluation);
113
114            static std::string hint(const std::string& command);
115            static std::string hint(const CommandEvaluation& evaluation);
116
117            static CommandEvaluation evaluate(const std::string& command);
118
119            static bool addConsoleCommandShortcut(ExecutorStatic* executor);
120            static ExecutorStatic* getConsoleCommandShortcut(const std::string& name);
121            static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name);
122
123            /** @brief Returns the map that stores all console commands. @return The const_iterator */
124            static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_; }
125            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */
126            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_.begin(); }
127            /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */
128            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_.end(); }
129
130            /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */
131            static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_; }
132            /** @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 */
133            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.begin(); }
134            /** @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 */
135            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); }
136
137        private:
138            CommandExecutor() {}
139            CommandExecutor(const CommandExecutor& other) {}
140            ~CommandExecutor() {}
141
142            static CommandExecutor& getInstance();
143            static CommandEvaluation& getEvaluation();
144
145            static void parse(const std::string& command, bool bInitialize = true);
146            static void initialize(const std::string& command);
147
148            static bool argumentsGiven(unsigned int num);
149            static unsigned int argumentsGiven();
150
151            static std::string getToken(unsigned int index);
152
153            static bool enoughParametersGiven(unsigned int head, Executor* executor);
154
155            static void createListOfPossibleShortcuts(const std::string& fragment);
156            static void createListOfPossibleFunctionClasses(const std::string& fragment);
157            static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier);
158            static void createListOfPossibleConfigValueClasses(const std::string& fragment);
159            static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier);
160            static void createListOfPossibleKeys(const std::string& fragment);
161
162            static bool compareStringsInList(const std::string* first, const std::string* second);
163
164            static std::string dump(const std::list<const std::string*>& list);
165            static std::string dump(const ExecutorStatic* executor);
166            static std::string dump(const ConfigValueContainer* container);
167
168            static std::string getCommonBegin(const std::list<const std::string*>& list);
169
170            static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name);
171            static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name);
172            static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier);
173            static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name);
174            static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier);
175            static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name);
176
177            CommandEvaluation evaluation_;
178
179            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_;
180            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_;
181    };
182}
183
184#endif /* _CommandExecutor_H__ */
Note: See TracBrowser for help on using the repository browser.