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