[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 | |
---|
[7158] | 32 | #include <algorithm> |
---|
| 33 | |
---|
[1625] | 34 | #include "util/Convert.h" |
---|
[7158] | 35 | #include "util/Debug.h" |
---|
| 36 | #include "util/StringUtils.h" |
---|
| 37 | #include "util/SubString.h" |
---|
[1505] | 38 | #include "Language.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | Executor::Executor(Functor* functor, const std::string& name) |
---|
| 43 | { |
---|
| 44 | this->functor_ = functor; |
---|
| 45 | this->name_ = name; |
---|
| 46 | |
---|
| 47 | this->bAddedDescription_ = false; |
---|
| 48 | this->bAddedDescriptionReturnvalue_ = false; |
---|
| 49 | |
---|
| 50 | this->bAddedDescriptionParam_[0] = false; |
---|
| 51 | this->bAddedDescriptionParam_[1] = false; |
---|
| 52 | this->bAddedDescriptionParam_[2] = false; |
---|
| 53 | this->bAddedDescriptionParam_[3] = false; |
---|
| 54 | this->bAddedDescriptionParam_[4] = false; |
---|
| 55 | |
---|
| 56 | this->bAddedDefaultValue_[0] = false; |
---|
| 57 | this->bAddedDefaultValue_[1] = false; |
---|
| 58 | this->bAddedDefaultValue_[2] = false; |
---|
| 59 | this->bAddedDefaultValue_[3] = false; |
---|
| 60 | this->bAddedDefaultValue_[4] = false; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | Executor::~Executor() |
---|
| 64 | { |
---|
| 65 | delete this->functor_; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | bool Executor::parse(const std::string& params, const std::string& delimiter) const |
---|
| 69 | { |
---|
[7158] | 70 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
| 71 | |
---|
| 72 | if (paramCount == 0) |
---|
| 73 | { |
---|
| 74 | COUT(5) << "Calling Executor " << this->name_ << " through parser without parameters." << std::endl; |
---|
| 75 | (*this->functor_)(); |
---|
| 76 | } |
---|
| 77 | else if (paramCount == 1) |
---|
| 78 | { |
---|
| 79 | const std::string& temp = getStripped(params); |
---|
| 80 | if (!temp.empty()) |
---|
| 81 | { |
---|
| 82 | COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using whole string: " << params << std::endl; |
---|
| 83 | (*this->functor_)(MultiType(params)); |
---|
| 84 | } |
---|
| 85 | else if (this->bAddedDefaultValue_[0]) |
---|
| 86 | { |
---|
| 87 | COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using default value: " << this->defaultValue_[0] << std::endl; |
---|
| 88 | (*this->functor_)(this->defaultValue_[0]); |
---|
| 89 | } |
---|
| 90 | else |
---|
| 91 | { |
---|
| 92 | COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << temp << ")." << std::endl; |
---|
| 93 | return false; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); |
---|
| 99 | |
---|
| 100 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) |
---|
| 101 | { |
---|
| 102 | if (!this->bAddedDefaultValue_[i]) |
---|
| 103 | { |
---|
| 104 | COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input:" << params << ")." << std::endl; |
---|
| 105 | return false; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | MultiType param[MAX_FUNCTOR_ARGUMENTS]; |
---|
| 110 | COUT(5) << "Calling Executor " << this->name_ << " through parser with " << paramCount << " parameters, using " << tokens.size() << " tokens ("; |
---|
| 111 | for (unsigned int i = 0; i < tokens.size() && i < MAX_FUNCTOR_ARGUMENTS; i++) |
---|
| 112 | { |
---|
| 113 | param[i] = tokens[i]; |
---|
| 114 | if (i != 0) |
---|
| 115 | { |
---|
| 116 | COUT(5) << ", "; |
---|
| 117 | } |
---|
| 118 | COUT(5) << tokens[i]; |
---|
| 119 | } |
---|
| 120 | COUT(5) << ") and " << std::max((int)paramCount - (int)tokens.size(), 0) << " default values ("; |
---|
| 121 | for (unsigned int i = tokens.size(); i < paramCount; i++) |
---|
| 122 | { |
---|
| 123 | param[i] = this->defaultValue_[i]; |
---|
| 124 | if (i != 0) |
---|
| 125 | { |
---|
| 126 | COUT(5) << ", "; |
---|
| 127 | } |
---|
| 128 | COUT(5) << this->defaultValue_[i]; |
---|
| 129 | } |
---|
| 130 | COUT(5) << ")." << std::endl; |
---|
| 131 | |
---|
| 132 | if ((tokens.size() > paramCount) && (this->functor_->getTypenameParam(paramCount - 1) == "string")) |
---|
| 133 | param[paramCount - 1] = tokens.subSet(paramCount - 1).join(); |
---|
| 134 | |
---|
| 135 | switch(paramCount) |
---|
| 136 | { |
---|
| 137 | case 2: |
---|
| 138 | (*this->functor_)(param[0], param[1]); |
---|
| 139 | break; |
---|
| 140 | case 3: |
---|
| 141 | (*this->functor_)(param[0], param[1], param[2]); |
---|
| 142 | break; |
---|
| 143 | case 4: |
---|
| 144 | (*this->functor_)(param[0], param[1], param[2], param[3]); |
---|
| 145 | break; |
---|
| 146 | case 5: |
---|
| 147 | (*this->functor_)(param[0], param[1], param[2], param[3], param[4]); |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | return true; |
---|
[1505] | 153 | } |
---|
| 154 | |
---|
[1747] | 155 | bool Executor::evaluate(const std::string& params, MultiType param[5], const std::string& delimiter) const |
---|
[1505] | 156 | { |
---|
| 157 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
| 158 | |
---|
| 159 | if (paramCount == 1) |
---|
| 160 | { |
---|
| 161 | // only one param: check if there are params given, otherwise try to use default values |
---|
[6417] | 162 | if (!getStripped(params).empty()) |
---|
[1505] | 163 | { |
---|
| 164 | param[0] = params; |
---|
| 165 | this->functor_->evaluateParam(0, param[0]); |
---|
| 166 | return true; |
---|
| 167 | } |
---|
| 168 | else if (this->bAddedDefaultValue_[0]) |
---|
| 169 | { |
---|
| 170 | param[0] = this->defaultValue_[0]; |
---|
| 171 | this->functor_->evaluateParam(0, param[0]); |
---|
| 172 | return true; |
---|
| 173 | } |
---|
| 174 | return false; |
---|
| 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | // more than one param |
---|
| 179 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); |
---|
| 180 | |
---|
| 181 | // if there are not enough params given, check if there are default values |
---|
| 182 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) |
---|
| 183 | if (!this->bAddedDefaultValue_[i]) |
---|
| 184 | return false; |
---|
| 185 | |
---|
| 186 | // assign all given arguments to the multitypes |
---|
[3301] | 187 | for (unsigned int i = 0; i < std::min(tokens.size(), MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[1505] | 188 | param[i] = tokens[i]; |
---|
| 189 | |
---|
| 190 | // fill the remaining multitypes with default values |
---|
[3301] | 191 | for (unsigned int i = tokens.size(); i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[1505] | 192 | param[i] = this->defaultValue_[i]; |
---|
| 193 | |
---|
| 194 | // evaluate the param types through the functor |
---|
[3301] | 195 | for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) |
---|
[1505] | 196 | this->functor_->evaluateParam(i, param[i]); |
---|
| 197 | |
---|
| 198 | return true; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | Executor& Executor::setDescription(const std::string& description) |
---|
| 203 | { |
---|
| 204 | if (!this->bAddedDescription_) |
---|
| 205 | { |
---|
| 206 | this->description_ = std::string("ExecutorDescription::" + this->name_ + "::function"); |
---|
| 207 | AddLanguageEntry(this->description_, description); |
---|
| 208 | this->bAddedDescription_ = true; |
---|
| 209 | } |
---|
| 210 | return (*this); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | const std::string& Executor::getDescription() const |
---|
| 214 | { |
---|
| 215 | return GetLocalisation(this->description_); |
---|
| 216 | } |
---|
| 217 | |
---|
[1786] | 218 | Executor& Executor::setDescriptionParam(unsigned int param, const std::string& description) |
---|
[1505] | 219 | { |
---|
[1879] | 220 | if (param < MAX_FUNCTOR_ARGUMENTS) |
---|
[1505] | 221 | { |
---|
| 222 | if (!this->bAddedDescriptionParam_[param]) |
---|
| 223 | { |
---|
| 224 | std::string paramnumber; |
---|
[1625] | 225 | if (!convertValue(¶mnumber, param)) |
---|
[1505] | 226 | return (*this); |
---|
| 227 | |
---|
| 228 | this->descriptionParam_[param] = std::string("ExecutorDescription::" + this->name_ + "::param" + paramnumber); |
---|
| 229 | AddLanguageEntry(this->descriptionParam_[param], description); |
---|
| 230 | this->bAddedDescriptionParam_[param] = true; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | return (*this); |
---|
| 234 | } |
---|
| 235 | |
---|
[1786] | 236 | const std::string& Executor::getDescriptionParam(unsigned int param) const |
---|
[1505] | 237 | { |
---|
[1879] | 238 | if (param < MAX_FUNCTOR_ARGUMENTS) |
---|
[1505] | 239 | return GetLocalisation(this->descriptionParam_[param]); |
---|
| 240 | |
---|
| 241 | return this->descriptionParam_[0]; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | Executor& Executor::setDescriptionReturnvalue(const std::string& description) |
---|
| 245 | { |
---|
| 246 | if (!this->bAddedDescriptionReturnvalue_) |
---|
| 247 | { |
---|
| 248 | this->descriptionReturnvalue_ = std::string("ExecutorDescription::" + this->name_ + "::returnvalue"); |
---|
| 249 | AddLanguageEntry(this->descriptionReturnvalue_, description); |
---|
| 250 | this->bAddedDescriptionReturnvalue_ = true; |
---|
| 251 | } |
---|
| 252 | return (*this); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | const std::string& Executor::getDescriptionReturnvalue(int param) const |
---|
| 256 | { |
---|
| 257 | return GetLocalisation(this->descriptionReturnvalue_); |
---|
| 258 | } |
---|
| 259 | |
---|
[1747] | 260 | Executor& Executor::setDefaultValues(const MultiType& param1) |
---|
[1505] | 261 | { |
---|
| 262 | this->defaultValue_[0] = param1; |
---|
| 263 | this->bAddedDefaultValue_[0] = true; |
---|
| 264 | |
---|
| 265 | return (*this); |
---|
| 266 | } |
---|
| 267 | |
---|
[1747] | 268 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2) |
---|
[1505] | 269 | { |
---|
| 270 | this->defaultValue_[0] = param1; |
---|
| 271 | this->bAddedDefaultValue_[0] = true; |
---|
| 272 | this->defaultValue_[1] = param2; |
---|
| 273 | this->bAddedDefaultValue_[1] = true; |
---|
| 274 | |
---|
| 275 | return (*this); |
---|
| 276 | } |
---|
| 277 | |
---|
[1747] | 278 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3) |
---|
[1505] | 279 | { |
---|
| 280 | this->defaultValue_[0] = param1; |
---|
| 281 | this->bAddedDefaultValue_[0] = true; |
---|
| 282 | this->defaultValue_[1] = param2; |
---|
| 283 | this->bAddedDefaultValue_[1] = true; |
---|
| 284 | this->defaultValue_[2] = param3; |
---|
| 285 | this->bAddedDefaultValue_[2] = true; |
---|
| 286 | |
---|
| 287 | return (*this); |
---|
| 288 | } |
---|
| 289 | |
---|
[1747] | 290 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) |
---|
[1505] | 291 | { |
---|
| 292 | this->defaultValue_[0] = param1; |
---|
| 293 | this->bAddedDefaultValue_[0] = true; |
---|
| 294 | this->defaultValue_[1] = param2; |
---|
| 295 | this->bAddedDefaultValue_[1] = true; |
---|
| 296 | this->defaultValue_[2] = param3; |
---|
| 297 | this->bAddedDefaultValue_[2] = true; |
---|
| 298 | this->defaultValue_[3] = param4; |
---|
| 299 | this->bAddedDefaultValue_[3] = true; |
---|
| 300 | |
---|
| 301 | return (*this); |
---|
| 302 | } |
---|
| 303 | |
---|
[1747] | 304 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) |
---|
[1505] | 305 | { |
---|
| 306 | this->defaultValue_[0] = param1; |
---|
| 307 | this->bAddedDefaultValue_[0] = true; |
---|
| 308 | this->defaultValue_[1] = param2; |
---|
| 309 | this->bAddedDefaultValue_[1] = true; |
---|
| 310 | this->defaultValue_[2] = param3; |
---|
| 311 | this->bAddedDefaultValue_[2] = true; |
---|
| 312 | this->defaultValue_[3] = param4; |
---|
| 313 | this->bAddedDefaultValue_[3] = true; |
---|
| 314 | this->defaultValue_[4] = param5; |
---|
| 315 | this->bAddedDefaultValue_[4] = true; |
---|
| 316 | |
---|
| 317 | return (*this); |
---|
| 318 | } |
---|
| 319 | |
---|
[1747] | 320 | Executor& Executor::setDefaultValue(unsigned int index, const MultiType& param) |
---|
[1505] | 321 | { |
---|
[1879] | 322 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
[1505] | 323 | { |
---|
| 324 | this->defaultValue_[index] = param; |
---|
| 325 | this->bAddedDefaultValue_[index] = true; |
---|
| 326 | } |
---|
| 327 | return (*this); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | bool Executor::allDefaultValuesSet() const |
---|
| 331 | { |
---|
| 332 | for (unsigned int i = 0; i < this->functor_->getParamCount(); i++) |
---|
| 333 | if (!this->bAddedDefaultValue_[i]) |
---|
| 334 | return false; |
---|
| 335 | |
---|
| 336 | return true; |
---|
| 337 | } |
---|
| 338 | } |
---|