[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 | * Inspiration: Executor by Benjamin Grauer |
---|
| 28 | */ |
---|
| 29 | |
---|
[7401] | 30 | /** |
---|
| 31 | @file |
---|
| 32 | @brief Implementation of orxonox::Executor |
---|
| 33 | */ |
---|
| 34 | |
---|
[1505] | 35 | #include "Executor.h" |
---|
[3196] | 36 | |
---|
[7163] | 37 | #include <algorithm> |
---|
| 38 | |
---|
[1625] | 39 | #include "util/Convert.h" |
---|
[8858] | 40 | #include "util/Output.h" |
---|
[7163] | 41 | #include "util/StringUtils.h" |
---|
| 42 | #include "util/SubString.h" |
---|
[7228] | 43 | #include "CommandExecutor.h" |
---|
[1505] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
[7401] | 47 | /** |
---|
| 48 | @brief Constructor: Creates an executor. |
---|
| 49 | @param functor The wrapped functor |
---|
| 50 | @param name The name of the executor (optional, used mostly for debug output) |
---|
| 51 | */ |
---|
[7198] | 52 | Executor::Executor(const FunctorPtr& functor, const std::string& name) |
---|
[1505] | 53 | { |
---|
| 54 | this->functor_ = functor; |
---|
| 55 | this->name_ = name; |
---|
| 56 | } |
---|
| 57 | |
---|
[7401] | 58 | /** |
---|
| 59 | @brief Copy-constructor: Creates a new executor with the same values and a clone of the wrapped Functor. |
---|
| 60 | */ |
---|
[7274] | 61 | Executor::Executor(const Executor& other) : name_(other.name_) |
---|
[7270] | 62 | { |
---|
[7283] | 63 | for (size_t i = 0; i < MAX_FUNCTOR_ARGUMENTS; ++i) |
---|
[7274] | 64 | defaultValue_[i] = other.defaultValue_[i]; |
---|
[7270] | 65 | this->functor_ = other.functor_->clone(); |
---|
| 66 | } |
---|
| 67 | |
---|
[7401] | 68 | /** |
---|
| 69 | @brief Calls the wrapped function with arguments that are passed in a string. |
---|
| 70 | @param arguments The arguments that should be passed to the function, separated by @a delimiter |
---|
[11071] | 71 | @param error A pointer to a variable (or nullptr) that is used to store the error code (see @ref CommandExecutorErrorCodes "CommandExecutor error codes") |
---|
[7401] | 72 | @param delimiter The delimiter that is used to separate the arguments in the string @a arguments |
---|
| 73 | @param bPrintError If true, errors are printed to the console if the function couldn't be executed with the given arguments |
---|
[9550] | 74 | @return Returns the return value of the function (or MultiType::Null if there is no return value) |
---|
[7401] | 75 | */ |
---|
[7230] | 76 | MultiType Executor::parse(const std::string& arguments, int* error, const std::string& delimiter, bool bPrintError) const |
---|
[1505] | 77 | { |
---|
[7276] | 78 | return this->parse(SubString(arguments, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '{', '}', true, '\0'), error, delimiter, bPrintError); |
---|
[7230] | 79 | } |
---|
[7189] | 80 | |
---|
[7401] | 81 | /** |
---|
| 82 | @brief Calls the wrapped function with arguments that are passed as tokens in a SubString |
---|
| 83 | @param arguments The arguments that should be passed to the function |
---|
[11071] | 84 | @param error A pointer to a variable (or nullptr) that is used to store the error code (see @ref CommandExecutorErrorCodes "CommandExecutor error codes") |
---|
[7401] | 85 | @param delimiter The delimiter that was used to separate the arguments in the SubString @a arguments (used to join the surplus arguments) |
---|
| 86 | @param bPrintError If true, errors are printed to the console if the function couldn't be executed with the given arguments |
---|
[9550] | 87 | @return Returns the return value of the function (or MultiType::Null if there is no return value) |
---|
[7401] | 88 | */ |
---|
[7230] | 89 | MultiType Executor::parse(const SubString& arguments, int* error, const std::string& delimiter, bool bPrintError) const |
---|
| 90 | { |
---|
[7401] | 91 | // evaluate the arguments |
---|
| 92 | MultiType arg[MAX_FUNCTOR_ARGUMENTS]; |
---|
| 93 | unsigned int argCount = this->evaluateArguments(arguments, arg, error, delimiter); |
---|
[7186] | 94 | |
---|
[7401] | 95 | // check if an error occurred |
---|
[7230] | 96 | if (error && *error) |
---|
[7163] | 97 | { |
---|
[7230] | 98 | if (bPrintError) |
---|
[8858] | 99 | orxout(internal_warning) << "Can't call executor " << this->name_ << " through parser: Not enough arguments or default values given (input: " << arguments.join() << ")." << endl; |
---|
[9550] | 100 | return MultiType::Null; |
---|
[7163] | 101 | } |
---|
[7230] | 102 | |
---|
[8858] | 103 | orxout(verbose, context::misc::executor) << "Executor::parse: \"" << arguments.join(delimiter) << "\" -> " << argCount << " arguments: " << arg[0] << " / " << arg[1] << " / " << arg[2] << " / " << arg[3] << " / " << arg[4] << endl; |
---|
[7265] | 104 | |
---|
[7401] | 105 | // execute the function with the evaluated arguments (the default values of the executor are also included in these arguments) |
---|
| 106 | switch (argCount) |
---|
[7163] | 107 | { |
---|
[7230] | 108 | case 0: return (*this->functor_)(); |
---|
[7401] | 109 | case 1: return (*this->functor_)(arg[0]); |
---|
| 110 | case 2: return (*this->functor_)(arg[0], arg[1]); |
---|
| 111 | case 3: return (*this->functor_)(arg[0], arg[1], arg[2]); |
---|
| 112 | case 4: return (*this->functor_)(arg[0], arg[1], arg[2], arg[3]); |
---|
[7230] | 113 | case 5: |
---|
[7401] | 114 | default: return (*this->functor_)(arg[0], arg[1], arg[2], arg[3], arg[4]); |
---|
[7163] | 115 | } |
---|
[1505] | 116 | } |
---|
| 117 | |
---|
[7401] | 118 | /** |
---|
| 119 | @brief Converts the arguments in a SubString to the right type, so they can be used to execute the function without further conversions. |
---|
| 120 | @param arguments The arguments that should be converted |
---|
| 121 | @param arg An array of MultiType where the converted arguments will be stored |
---|
[11071] | 122 | @param error A pointer to a variable (or nullptr) that is used to store the error code (see @ref CommandExecutorErrorCodes "CommandExecutor error codes") |
---|
[7401] | 123 | @param delimiter The delimiter that was used to separate the arguments in the SubString @a arguments (used to join the surplus arguments) |
---|
| 124 | @return Returns the number of evaluated arguments |
---|
| 125 | */ |
---|
| 126 | int Executor::evaluateArguments(const SubString& arguments, MultiType arg[MAX_FUNCTOR_ARGUMENTS], int* error, const std::string& delimiter) const |
---|
[1505] | 127 | { |
---|
| 128 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
[7230] | 129 | unsigned int argumentCount = arguments.size(); |
---|
[1505] | 130 | |
---|
[7230] | 131 | // if there are not enough params given, check if there are default values |
---|
| 132 | for (unsigned int i = argumentCount; i < paramCount; i++) |
---|
[1505] | 133 | { |
---|
[7230] | 134 | if (this->defaultValue_[i].null()) |
---|
[1505] | 135 | { |
---|
[7230] | 136 | if (error) |
---|
| 137 | *error = CommandExecutor::Incomplete; |
---|
| 138 | return 0; |
---|
[1505] | 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
[7230] | 142 | // assign all given arguments to the multitypes |
---|
[7265] | 143 | for (unsigned int i = 0; i < std::min(std::min(argumentCount, paramCount), MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[7401] | 144 | arg[i] = arguments[i]; |
---|
[1505] | 145 | |
---|
[7230] | 146 | // fill the remaining multitypes with default values |
---|
| 147 | for (unsigned int i = argumentCount; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[7401] | 148 | arg[i] = this->defaultValue_[i]; |
---|
[1505] | 149 | |
---|
[7230] | 150 | // assign the remaining arguments all to the last parameter if it is a string |
---|
| 151 | if ((paramCount <= MAX_FUNCTOR_ARGUMENTS) &&(argumentCount > paramCount) && (paramCount == 1 || this->functor_->getTypenameParam(paramCount - 1) == "string")) |
---|
[7401] | 152 | arg[paramCount - 1] = arguments.subSet(paramCount - 1).join(delimiter); |
---|
[1505] | 153 | |
---|
[7401] | 154 | // evaluate the parameter types through the functor |
---|
[7230] | 155 | for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[7401] | 156 | this->functor_->evaluateArgument(i, arg[i]); |
---|
[1505] | 157 | |
---|
[7230] | 158 | if (error) |
---|
| 159 | *error = CommandExecutor::Success; |
---|
| 160 | return paramCount; |
---|
[1505] | 161 | } |
---|
| 162 | |
---|
[7401] | 163 | /// Defines the default value for the first parameter. |
---|
| 164 | void Executor::setDefaultValues(const MultiType& arg1) |
---|
[1505] | 165 | { |
---|
[7401] | 166 | this->defaultValue_[0] = arg1; |
---|
[1505] | 167 | } |
---|
| 168 | |
---|
[7401] | 169 | /// Defines the default value for the first two parameters. |
---|
| 170 | void Executor::setDefaultValues(const MultiType& arg1, const MultiType& arg2) |
---|
[1505] | 171 | { |
---|
[7401] | 172 | this->defaultValue_[0] = arg1; |
---|
| 173 | this->defaultValue_[1] = arg2; |
---|
[1505] | 174 | } |
---|
| 175 | |
---|
[7401] | 176 | /// Defines the default value for the first three parameters. |
---|
| 177 | void Executor::setDefaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3) |
---|
[1505] | 178 | { |
---|
[7401] | 179 | this->defaultValue_[0] = arg1; |
---|
| 180 | this->defaultValue_[1] = arg2; |
---|
| 181 | this->defaultValue_[2] = arg3; |
---|
[1505] | 182 | } |
---|
| 183 | |
---|
[7401] | 184 | /// Defines the default value for the first four parameters. |
---|
| 185 | void Executor::setDefaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4) |
---|
[1505] | 186 | { |
---|
[7401] | 187 | this->defaultValue_[0] = arg1; |
---|
| 188 | this->defaultValue_[1] = arg2; |
---|
| 189 | this->defaultValue_[2] = arg3; |
---|
| 190 | this->defaultValue_[3] = arg4; |
---|
[1505] | 191 | } |
---|
| 192 | |
---|
[7401] | 193 | /// Defines the default value for the first five parameters. |
---|
| 194 | void Executor::setDefaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5) |
---|
[1505] | 195 | { |
---|
[7401] | 196 | this->defaultValue_[0] = arg1; |
---|
| 197 | this->defaultValue_[1] = arg2; |
---|
| 198 | this->defaultValue_[2] = arg3; |
---|
| 199 | this->defaultValue_[3] = arg4; |
---|
| 200 | this->defaultValue_[4] = arg5; |
---|
[1505] | 201 | } |
---|
| 202 | |
---|
[7401] | 203 | /// Defines the default value for a parameter with given index (the first parameter has index 0). |
---|
| 204 | void Executor::setDefaultValue(unsigned int index, const MultiType& arg) |
---|
[1505] | 205 | { |
---|
[1879] | 206 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
[7401] | 207 | this->defaultValue_[index] = arg; |
---|
[1505] | 208 | } |
---|
| 209 | |
---|
[7401] | 210 | /// Returns true if the executor has a default value for each parameter of the wrapped function, so it can be called without passing additional arguments. |
---|
[1505] | 211 | bool Executor::allDefaultValuesSet() const |
---|
| 212 | { |
---|
| 213 | for (unsigned int i = 0; i < this->functor_->getParamCount(); i++) |
---|
[7187] | 214 | if (this->defaultValue_[i].null()) |
---|
[1505] | 215 | return false; |
---|
| 216 | |
---|
| 217 | return true; |
---|
| 218 | } |
---|
| 219 | } |
---|