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