Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Boxhead_FS19/src/libraries/core/command/CommandExecutor.h

Last change on this file was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
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
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
85#ifndef _CommandExecutor_H__
86#define _CommandExecutor_H__
87
88#include "core/CorePrereqs.h"
89
90#include <map>
91#include <list>
92#include <string>
93
94#include "util/MultiType.h"
95#include "CommandEvaluation.h"
96
97// tolua_begin
98namespace orxonox
99{
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    */
109    class _CoreExport CommandExecutor
110    {
111// tolua_end
112        public:
113            static int execute(const std::string& command, bool useTcl = true, bool printErrors = true); // tolua_export
114
115            static MultiType queryMT(const std::string& command, int* error = nullptr, bool useTcl = true);
116            static std::string query(const std::string& command, int* error = NULL, bool useTcl = true); // tolua_export
117
118            static CommandEvaluation evaluate(const std::string& command);
119
120            static constexpr int Success = 0;       ///< Error code for "success" (or no error)
121            static constexpr int Inexistent = 1;    ///< Error code if the command doesn't exist
122            static constexpr int Incomplete = 2;    ///< Error code if the command needs more arguments
123            static constexpr int Deactivated = 3;   ///< Error code if the command is not active
124            static constexpr int Denied = 4;        ///< Error code if the command needs a different access level
125            static constexpr int Error = 5;         ///< Error code if the command returned an error
126
127            static std::string getErrorDescription(int error);
128
129            static MultiType unhide(const std::string& command);
130            static void alias(const std::string& alias, const std::string& command);
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.
132
133        private:
134            CommandExecutor() = default;                      ///< Empty constructor
135            ~CommandExecutor() = default;                     ///< Empty destructor
136
137            // non-copyable:
138            CommandExecutor(const CommandExecutor&) = delete;
139            CommandExecutor& operator=(const CommandExecutor&) = delete;
140
141            static CommandExecutor& getInstance();
142
143            bool getCached(const std::string& command, CommandEvaluation& evaluation);
144            void cache(const std::string& command, const CommandEvaluation& evaluation);
145
146            /// Helper struct, used to store cached entries
147            struct CacheEntry
148            {
149                CommandEvaluation evaluation_;                  ///< The command evaluation which is stored in the cache
150                std::list<std::string>::iterator iterator_;     ///< The iterator of the corresponding element in cachelist_, used for faster access
151            };
152
153            std::map<std::string, CacheEntry> cache_;   ///< A map that connects command strings and evaluated commands in the cache
154            std::list<std::string> cachelist_;          ///< A list used to sort the elements in the cache by their age
155    }; // tolua_export
156} // tolua_export
157
158#endif /* _CommandExecutor_H__ */
Note: See TracBrowser for help on using the repository browser.