Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

a singleton might be better implemented with a private constructor… so here we go

File size: 7.4 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    class _CoreExport CommandExecutor
43    {
44        enum CommandState
45        {
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        public:
64            static bool execute(const std::string& command);
65            static std::string complete(const std::string& command);
66            static std::string hint(const std::string& command);
67
68            static bool addConsoleCommandShortcut(ExecutorStatic* executor);
69            static ExecutorStatic* getConsoleCommandShortcut(const std::string& name);
70            static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name);
71
72            /** @brief Returns the map that stores all console commands. @return The const_iterator */
73            static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_s; }
74            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */
75            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_s.begin(); }
76            /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */
77            static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_s.end(); }
78
79            /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */
80            static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_s; }
81            /** @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 */
82            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_s.begin(); }
83            /** @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 */
84            static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_s.end(); }
85
86        private:
87            CommandExecutor() {}
88            CommandExecutor(const CommandExecutor& other) {}
89            ~CommandExecutor() {}
90
91            static CommandExecutor& getInstance();
92
93            static void parse(const std::string& command, bool bInitialize = true);
94            static void initialize();
95
96            static bool argumentsGiven(unsigned int num);
97            static unsigned int argumentsGiven();
98
99            static std::string getToken(unsigned int index);
100
101            static bool enoughParametersGiven(unsigned int head, Executor* executor);
102
103            static void createListOfPossibleShortcuts(const std::string& fragment);
104            static void createListOfPossibleFunctionClasses(const std::string& fragment);
105            static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier);
106            static void createListOfPossibleConfigValueClasses(const std::string& fragment);
107            static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier);
108            static void createListOfPossibleKeys(const std::string& fragment);
109
110            static bool compareStringsInList(const std::string* first, const std::string* second);
111
112            static std::string dump(const std::list<const std::string*>& list);
113            static std::string dump(const ExecutorStatic* executor);
114            static std::string dump(const ConfigValueContainer* container);
115
116            static std::string getCommonBegin(const std::list<const std::string*>& list);
117
118            static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name);
119            static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name);
120            static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier);
121            static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name);
122            static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier);
123            static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name);
124
125            std::string lastProcessedCommand_s;
126            SubString tokens_s;
127            std::list<const std::string*> listOfPossibleFunctionClasses_s;
128            std::list<const std::string*> listOfPossibleShortcuts_s;
129            std::list<const std::string*> listOfPossibleFunctions_s;
130            std::list<const std::string*> listOfPossibleConfigValueClasses_s;
131            std::list<const std::string*> listOfPossibleConfigValues_s;
132            std::list<const std::string*> listOfPossibleKeys_s;
133
134            Identifier* functionclass_s;
135            Identifier* configvalueclass_s;
136            ExecutorStatic* shortcut_s;
137            ExecutorStatic* function_s;
138            ConfigValueContainer* configvalue_s;
139            ConfigValueContainer* key_s;
140
141            std::string errorMessage_s;
142            CommandState state_s;
143
144            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_s;
145            std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_s;
146    };
147}
148
149#endif /* _CommandExecutor_H__ */
Note: See TracBrowser for help on using the repository browser.