Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/CommandExecutor.h @ 1056

Last change on this file since 1056 was 1056, checked in by landauf, 17 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

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