[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 | |
---|
| 30 | #include "Executor.h" |
---|
[3196] | 31 | |
---|
[7163] | 32 | #include <algorithm> |
---|
| 33 | |
---|
[1625] | 34 | #include "util/Convert.h" |
---|
[7163] | 35 | #include "util/Debug.h" |
---|
| 36 | #include "util/StringUtils.h" |
---|
| 37 | #include "util/SubString.h" |
---|
[7228] | 38 | #include "CommandExecutor.h" |
---|
[1505] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[7198] | 42 | Executor::Executor(const FunctorPtr& functor, const std::string& name) |
---|
[1505] | 43 | { |
---|
| 44 | this->functor_ = functor; |
---|
| 45 | this->name_ = name; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | Executor::~Executor() |
---|
| 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
[7228] | 52 | MultiType Executor::parse(const std::string& params, int* error, const std::string& delimiter, bool bPrintError) const |
---|
[1505] | 53 | { |
---|
[7228] | 54 | if (error) |
---|
| 55 | *error = CommandExecutor::Success; |
---|
[7189] | 56 | |
---|
[7163] | 57 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
[7186] | 58 | |
---|
[7163] | 59 | if (paramCount == 0) |
---|
| 60 | { |
---|
| 61 | COUT(5) << "Calling Executor " << this->name_ << " through parser without parameters." << std::endl; |
---|
[7189] | 62 | return (*this->functor_)(); |
---|
[7163] | 63 | } |
---|
| 64 | else if (paramCount == 1) |
---|
| 65 | { |
---|
| 66 | const std::string& temp = getStripped(params); |
---|
| 67 | if (!temp.empty()) |
---|
| 68 | { |
---|
| 69 | COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using whole string: " << params << std::endl; |
---|
[7212] | 70 | return (*this->functor_)(params); |
---|
[7163] | 71 | } |
---|
[7187] | 72 | else if (!this->defaultValue_[0].null()) |
---|
[7163] | 73 | { |
---|
| 74 | COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using default value: " << this->defaultValue_[0] << std::endl; |
---|
[7189] | 75 | return (*this->functor_)(this->defaultValue_[0]); |
---|
[7163] | 76 | } |
---|
| 77 | else |
---|
| 78 | { |
---|
[7228] | 79 | if (bPrintError) |
---|
| 80 | COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << temp << ")." << std::endl; |
---|
| 81 | if (error) |
---|
| 82 | *error = CommandExecutor::Incomplete; |
---|
[7189] | 83 | return MT_Type::Null; |
---|
[7163] | 84 | } |
---|
| 85 | } |
---|
| 86 | else |
---|
| 87 | { |
---|
| 88 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); |
---|
[7186] | 89 | |
---|
[7163] | 90 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) |
---|
| 91 | { |
---|
[7187] | 92 | if (this->defaultValue_[i].null()) |
---|
[7163] | 93 | { |
---|
[7228] | 94 | if (bPrintError) |
---|
| 95 | COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << params << ")." << std::endl; |
---|
| 96 | if (error) |
---|
| 97 | *error = CommandExecutor::Incomplete; |
---|
[7189] | 98 | return MT_Type::Null; |
---|
[7163] | 99 | } |
---|
| 100 | } |
---|
[7186] | 101 | |
---|
[7163] | 102 | MultiType param[MAX_FUNCTOR_ARGUMENTS]; |
---|
| 103 | COUT(5) << "Calling Executor " << this->name_ << " through parser with " << paramCount << " parameters, using " << tokens.size() << " tokens ("; |
---|
| 104 | for (unsigned int i = 0; i < tokens.size() && i < MAX_FUNCTOR_ARGUMENTS; i++) |
---|
| 105 | { |
---|
| 106 | param[i] = tokens[i]; |
---|
| 107 | if (i != 0) |
---|
| 108 | { |
---|
| 109 | COUT(5) << ", "; |
---|
| 110 | } |
---|
| 111 | COUT(5) << tokens[i]; |
---|
| 112 | } |
---|
| 113 | COUT(5) << ") and " << std::max((int)paramCount - (int)tokens.size(), 0) << " default values ("; |
---|
| 114 | for (unsigned int i = tokens.size(); i < paramCount; i++) |
---|
| 115 | { |
---|
| 116 | param[i] = this->defaultValue_[i]; |
---|
| 117 | if (i != 0) |
---|
| 118 | { |
---|
| 119 | COUT(5) << ", "; |
---|
| 120 | } |
---|
| 121 | COUT(5) << this->defaultValue_[i]; |
---|
| 122 | } |
---|
| 123 | COUT(5) << ")." << std::endl; |
---|
[7186] | 124 | |
---|
[7163] | 125 | if ((tokens.size() > paramCount) && (this->functor_->getTypenameParam(paramCount - 1) == "string")) |
---|
| 126 | param[paramCount - 1] = tokens.subSet(paramCount - 1).join(); |
---|
[7186] | 127 | |
---|
[7163] | 128 | switch(paramCount) |
---|
| 129 | { |
---|
| 130 | case 2: |
---|
[7189] | 131 | return (*this->functor_)(param[0], param[1]); |
---|
[7163] | 132 | case 3: |
---|
[7189] | 133 | return (*this->functor_)(param[0], param[1], param[2]); |
---|
[7163] | 134 | case 4: |
---|
[7189] | 135 | return (*this->functor_)(param[0], param[1], param[2], param[3]); |
---|
[7163] | 136 | case 5: |
---|
[7189] | 137 | return (*this->functor_)(param[0], param[1], param[2], param[3], param[4]); |
---|
[7163] | 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
[7189] | 141 | return MT_Type::Null; |
---|
[1505] | 142 | } |
---|
| 143 | |
---|
[1747] | 144 | bool Executor::evaluate(const std::string& params, MultiType param[5], const std::string& delimiter) const |
---|
[1505] | 145 | { |
---|
| 146 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
| 147 | |
---|
| 148 | if (paramCount == 1) |
---|
| 149 | { |
---|
| 150 | // only one param: check if there are params given, otherwise try to use default values |
---|
[6417] | 151 | if (!getStripped(params).empty()) |
---|
[1505] | 152 | { |
---|
| 153 | param[0] = params; |
---|
| 154 | this->functor_->evaluateParam(0, param[0]); |
---|
| 155 | return true; |
---|
| 156 | } |
---|
[7187] | 157 | else if (!this->defaultValue_[0].null()) |
---|
[1505] | 158 | { |
---|
| 159 | param[0] = this->defaultValue_[0]; |
---|
| 160 | this->functor_->evaluateParam(0, param[0]); |
---|
| 161 | return true; |
---|
| 162 | } |
---|
| 163 | return false; |
---|
| 164 | } |
---|
| 165 | else |
---|
| 166 | { |
---|
| 167 | // more than one param |
---|
| 168 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); |
---|
| 169 | |
---|
| 170 | // if there are not enough params given, check if there are default values |
---|
| 171 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) |
---|
[7187] | 172 | if (this->defaultValue_[i].null()) |
---|
[1505] | 173 | return false; |
---|
| 174 | |
---|
| 175 | // assign all given arguments to the multitypes |
---|
[3301] | 176 | for (unsigned int i = 0; i < std::min(tokens.size(), MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[1505] | 177 | param[i] = tokens[i]; |
---|
| 178 | |
---|
| 179 | // fill the remaining multitypes with default values |
---|
[3301] | 180 | for (unsigned int i = tokens.size(); i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[1505] | 181 | param[i] = this->defaultValue_[i]; |
---|
| 182 | |
---|
| 183 | // evaluate the param types through the functor |
---|
[3301] | 184 | for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[1505] | 185 | this->functor_->evaluateParam(i, param[i]); |
---|
| 186 | |
---|
| 187 | return true; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[7200] | 191 | void Executor::setDefaultValues(const MultiType& param1) |
---|
[1505] | 192 | { |
---|
| 193 | this->defaultValue_[0] = param1; |
---|
| 194 | } |
---|
| 195 | |
---|
[7200] | 196 | void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2) |
---|
[1505] | 197 | { |
---|
| 198 | this->defaultValue_[0] = param1; |
---|
| 199 | this->defaultValue_[1] = param2; |
---|
| 200 | } |
---|
| 201 | |
---|
[7200] | 202 | void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3) |
---|
[1505] | 203 | { |
---|
| 204 | this->defaultValue_[0] = param1; |
---|
| 205 | this->defaultValue_[1] = param2; |
---|
| 206 | this->defaultValue_[2] = param3; |
---|
| 207 | } |
---|
| 208 | |
---|
[7200] | 209 | void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) |
---|
[1505] | 210 | { |
---|
| 211 | this->defaultValue_[0] = param1; |
---|
| 212 | this->defaultValue_[1] = param2; |
---|
| 213 | this->defaultValue_[2] = param3; |
---|
| 214 | this->defaultValue_[3] = param4; |
---|
| 215 | } |
---|
| 216 | |
---|
[7200] | 217 | void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) |
---|
[1505] | 218 | { |
---|
| 219 | this->defaultValue_[0] = param1; |
---|
| 220 | this->defaultValue_[1] = param2; |
---|
| 221 | this->defaultValue_[2] = param3; |
---|
| 222 | this->defaultValue_[3] = param4; |
---|
| 223 | this->defaultValue_[4] = param5; |
---|
| 224 | } |
---|
| 225 | |
---|
[7200] | 226 | void Executor::setDefaultValue(unsigned int index, const MultiType& param) |
---|
[1505] | 227 | { |
---|
[1879] | 228 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
[1505] | 229 | this->defaultValue_[index] = param; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | bool Executor::allDefaultValuesSet() const |
---|
| 233 | { |
---|
| 234 | for (unsigned int i = 0; i < this->functor_->getParamCount(); i++) |
---|
[7187] | 235 | if (this->defaultValue_[i].null()) |
---|
[1505] | 236 | return false; |
---|
| 237 | |
---|
| 238 | return true; |
---|
| 239 | } |
---|
| 240 | } |
---|