Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/command/CommandExecutor.h @ 10159

Last change on this file since 10159 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 6.9 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 = 0, bool useTcl = true);
116            static std::string query(const std::string& command, int* error = 0, bool useTcl = true); // tolua_export
117
118            static CommandEvaluation evaluate(const std::string& command);
119
120            static const int Success = 0;       ///< Error code for "success" (or no error)
121            static const int Inexistent = 1;    ///< Error code if the command doesn't exist
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
125            static const 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() {}                            ///< Empty constructor
135            CommandExecutor(const CommandExecutor& other);  ///< Not implemented copy-constructor
136            ~CommandExecutor() {}                           ///< Empty destructor
137
138            static CommandExecutor& getInstance();
139
140            bool getCached(const std::string& command, CommandEvaluation& evaluation);
141            void cache(const std::string& command, const CommandEvaluation& evaluation);
142
143            /// Helper struct, used to store cached entries
144            struct CacheEntry
145            {
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
148            };
149
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
152    }; // tolua_export
153} // tolua_export
154
155#endif /* _CommandExecutor_H__ */
Note: See TracBrowser for help on using the repository browser.