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