[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 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 | |
---|
[7401] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @ingroup Command CommandExecEval |
---|
| 32 | @brief Declaration of the orxonox::CommandExecutor class which is used to execute and evaluate @ref orxonox::ConsoleCommand "console commands". |
---|
| 33 | |
---|
| 34 | @anchor CommandExecutorExample |
---|
| 35 | |
---|
| 36 | orxonox::CommandExecutor can be used to execute console commands (see orxonox::ConsoleCommand). |
---|
| 37 | Commands are strings that can be entered by the user in the shell or they can be part of a |
---|
| 38 | script. |
---|
| 39 | |
---|
| 40 | A command can be passed to orxonox::CommandExecutor::execute() which will execute them - eiter |
---|
| 41 | directly, or - if requested - passes it to Tcl. See orxonox::TclBind for more information. |
---|
| 42 | Appart from execute() the command can also be passed to orxonox::CommandExecutor::query() which |
---|
| 43 | behaves the same except for that it returns the return value of the command. |
---|
| 44 | |
---|
| 45 | If you don't want to execute the command, but rather gather more information about it, pass it |
---|
| 46 | to orxonox::CommandExecutor::evaluate(). This function returns an instance of |
---|
| 47 | orxonox::CommandEvaluation. This class provides more information about the command, for example |
---|
| 48 | the evaluated instance of orxonox::ConsoleCommand. Its also possible to gather hints about the |
---|
| 49 | command or to complete the command-string by using argument completion functions. More than that |
---|
| 50 | the command evaluation can also evaluate the arguments, which allows faster execution of the |
---|
| 51 | command. |
---|
| 52 | |
---|
| 53 | Example: |
---|
| 54 | @code |
---|
| 55 | CommandExecutor::execute("log test"); // writes "test" to the console |
---|
| 56 | CommandExecutor::execute("log [expr 1+1]"); // writes "2" to the console - expr is a Tcl command |
---|
| 57 | |
---|
| 58 | CommandExecutor::query("expr 1+1"); // returns "2" |
---|
| 59 | CommandExecutor::queryMT("expr 1+1"); // returns 2 |
---|
| 60 | @endcode |
---|
| 61 | |
---|
| 62 | And another example about how to use evaluate(): |
---|
| 63 | @code |
---|
| 64 | CommandEvaluation evaluation; |
---|
| 65 | evaluation = CommandExecutor::evaluate("log test"); // returns an evaluation of "log test" |
---|
| 66 | |
---|
| 67 | evaluation.execute(); // writes "test" to the console |
---|
| 68 | evaluation.hint(); // returns "log: {string}" |
---|
| 69 | @endcode |
---|
| 70 | |
---|
| 71 | @anchor CommandExecutorErrorCodes |
---|
| 72 | |
---|
| 73 | @b Error @b codes: |
---|
| 74 | |
---|
| 75 | orxonox::CommandExecutor defines a number of error codes that are returned |
---|
| 76 | by different functions that operate with commands: |
---|
| 77 | |
---|
| 78 | - CommandExecutor::Success: No error |
---|
| 79 | - CommandExecutor::Error: The command doesn't exist |
---|
| 80 | - CommandExecutor::Incomplete: The command needs more arguments |
---|
| 81 | - CommandExecutor::Deactivated: The command is not active |
---|
| 82 | - CommandExecutor::Denied: The command needs a different @ref orxonox::AccessLevel "access level" |
---|
| 83 | */ |
---|
| 84 | |
---|
[1505] | 85 | #ifndef _CommandExecutor_H__ |
---|
| 86 | #define _CommandExecutor_H__ |
---|
| 87 | |
---|
[7203] | 88 | #include "core/CorePrereqs.h" |
---|
[1505] | 89 | |
---|
[7229] | 90 | #include <map> |
---|
| 91 | #include <list> |
---|
[3196] | 92 | #include <string> |
---|
[1505] | 93 | |
---|
[3196] | 94 | #include "util/MultiType.h" |
---|
[1505] | 95 | #include "CommandEvaluation.h" |
---|
| 96 | |
---|
[2710] | 97 | // tolua_begin |
---|
| 98 | namespace orxonox |
---|
| 99 | { |
---|
[7401] | 100 | /** |
---|
| 101 | @brief This class is used to execute and evaluate command-strings. |
---|
| 102 | |
---|
| 103 | CommandExecutor executes command-strings and returns evaluated commands. It's |
---|
| 104 | also possible to execute Tcl commands if the corresponding argument of execute() |
---|
| 105 | is true. |
---|
| 106 | |
---|
| 107 | @see See @ref CommandExecutorExample "this description" for more information and some examples. |
---|
| 108 | */ |
---|
[1505] | 109 | class _CoreExport CommandExecutor |
---|
| 110 | { |
---|
[2710] | 111 | // tolua_end |
---|
[1505] | 112 | public: |
---|
[8858] | 113 | static int execute(const std::string& command, bool useTcl = true, bool printErrors = true); // tolua_export |
---|
[3035] | 114 | |
---|
[7228] | 115 | static MultiType queryMT(const std::string& command, int* error = 0, bool useTcl = true); |
---|
| 116 | static std::string query(const std::string& command, int* error = 0, bool useTcl = true); // tolua_export |
---|
[7189] | 117 | |
---|
[1505] | 118 | static CommandEvaluation evaluate(const std::string& command); |
---|
| 119 | |
---|
[7401] | 120 | static const int Success = 0; ///< Error code for "success" (or no error) |
---|
[8858] | 121 | static const int Inexistent = 1; ///< Error code if the command doesn't exist |
---|
[7401] | 122 | static const int Incomplete = 2; ///< Error code if the command needs more arguments |
---|
| 123 | static const int Deactivated = 3; ///< Error code if the command is not active |
---|
| 124 | static const int Denied = 4; ///< Error code if the command needs a different access level |
---|
[8858] | 125 | static const int Error = 5; ///< Error code if the command returned an error |
---|
[7228] | 126 | |
---|
[8858] | 127 | static std::string getErrorDescription(int error); |
---|
| 128 | |
---|
[7234] | 129 | static MultiType unhide(const std::string& command); |
---|
[7277] | 130 | static void alias(const std::string& alias, const std::string& command); |
---|
[7401] | 131 | static void _autocomplete(const std::string& group, const std::string& name) {} ///< Pseudo console command used whenever no real command is available. In these cases this command provides auto-completion for console commands and groups. |
---|
[7228] | 132 | |
---|
[1505] | 133 | private: |
---|
[7401] | 134 | CommandExecutor() {} ///< Empty constructor |
---|
| 135 | CommandExecutor(const CommandExecutor& other); ///< Not implemented copy-constructor |
---|
| 136 | ~CommandExecutor() {} ///< Empty destructor |
---|
[1505] | 137 | |
---|
| 138 | static CommandExecutor& getInstance(); |
---|
[7229] | 139 | |
---|
| 140 | bool getCached(const std::string& command, CommandEvaluation& evaluation); |
---|
| 141 | void cache(const std::string& command, const CommandEvaluation& evaluation); |
---|
| 142 | |
---|
[7401] | 143 | /// Helper struct, used to store cached entries |
---|
[7229] | 144 | struct CacheEntry |
---|
| 145 | { |
---|
[7401] | 146 | CommandEvaluation evaluation_; ///< The command evaluation which is stored in the cache |
---|
| 147 | std::list<std::string>::iterator iterator_; ///< The iterator of the corresponding element in cachelist_, used for faster access |
---|
[7229] | 148 | }; |
---|
| 149 | |
---|
[7401] | 150 | std::map<std::string, CacheEntry> cache_; ///< A map that connects command strings and evaluated commands in the cache |
---|
| 151 | std::list<std::string> cachelist_; ///< A list used to sort the elements in the cache by their age |
---|
[1755] | 152 | }; // tolua_export |
---|
| 153 | } // tolua_export |
---|
[1505] | 154 | |
---|
| 155 | #endif /* _CommandExecutor_H__ */ |
---|