| 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" | 
|---|
| 31 | #include "util/Math.h" | 
|---|
| 32 | #include "util/Convert.h" | 
|---|
| 33 | #include "Language.h" | 
|---|
| 34 |  | 
|---|
| 35 | namespace orxonox | 
|---|
| 36 | { | 
|---|
| 37 | Executor::Executor(Functor* functor, const std::string& name) | 
|---|
| 38 | { | 
|---|
| 39 | this->functor_ = functor; | 
|---|
| 40 | this->name_ = name; | 
|---|
| 41 |  | 
|---|
| 42 | this->bAddedDescription_ = false; | 
|---|
| 43 | this->bAddedDescriptionReturnvalue_ = false; | 
|---|
| 44 |  | 
|---|
| 45 | this->bAddedDescriptionParam_[0] = false; | 
|---|
| 46 | this->bAddedDescriptionParam_[1] = false; | 
|---|
| 47 | this->bAddedDescriptionParam_[2] = false; | 
|---|
| 48 | this->bAddedDescriptionParam_[3] = false; | 
|---|
| 49 | this->bAddedDescriptionParam_[4] = false; | 
|---|
| 50 |  | 
|---|
| 51 | this->bAddedDefaultValue_[0] = false; | 
|---|
| 52 | this->bAddedDefaultValue_[1] = false; | 
|---|
| 53 | this->bAddedDefaultValue_[2] = false; | 
|---|
| 54 | this->bAddedDefaultValue_[3] = false; | 
|---|
| 55 | this->bAddedDefaultValue_[4] = false; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | Executor::~Executor() | 
|---|
| 59 | { | 
|---|
| 60 | delete this->functor_; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | bool Executor::parse(const std::string& params, const std::string& delimiter) const | 
|---|
| 64 | { | 
|---|
| 65 | EXECUTOR_PARSE(normal); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | bool Executor::evaluate(const std::string& params, MultiType param[5], const std::string& delimiter) const | 
|---|
| 69 | { | 
|---|
| 70 | unsigned int paramCount = this->functor_->getParamCount(); | 
|---|
| 71 |  | 
|---|
| 72 | if (paramCount == 1) | 
|---|
| 73 | { | 
|---|
| 74 | // only one param: check if there are params given, otherwise try to use default values | 
|---|
| 75 | std::string temp = getStripped(params); | 
|---|
| 76 | if ((temp != "") && (temp.size() != 0)) | 
|---|
| 77 | { | 
|---|
| 78 | param[0] = params; | 
|---|
| 79 | this->functor_->evaluateParam(0, param[0]); | 
|---|
| 80 | return true; | 
|---|
| 81 | } | 
|---|
| 82 | else if (this->bAddedDefaultValue_[0]) | 
|---|
| 83 | { | 
|---|
| 84 | param[0] = this->defaultValue_[0]; | 
|---|
| 85 | this->functor_->evaluateParam(0, param[0]); | 
|---|
| 86 | return true; | 
|---|
| 87 | } | 
|---|
| 88 | return false; | 
|---|
| 89 | } | 
|---|
| 90 | else | 
|---|
| 91 | { | 
|---|
| 92 | // more than one param | 
|---|
| 93 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); | 
|---|
| 94 |  | 
|---|
| 95 | // if there are not enough params given, check if there are default values | 
|---|
| 96 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) | 
|---|
| 97 | if (!this->bAddedDefaultValue_[i]) | 
|---|
| 98 | return false; | 
|---|
| 99 |  | 
|---|
| 100 | // assign all given arguments to the multitypes | 
|---|
| 101 | for (unsigned int i = 0; i < min(tokens.size(), (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++) | 
|---|
| 102 | param[i] = tokens[i]; | 
|---|
| 103 |  | 
|---|
| 104 | // fill the remaining multitypes with default values | 
|---|
| 105 | for (unsigned int i = tokens.size(); i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++) | 
|---|
| 106 | param[i] = this->defaultValue_[i]; | 
|---|
| 107 |  | 
|---|
| 108 | // evaluate the param types through the functor | 
|---|
| 109 | for (unsigned int i = 0; i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++) | 
|---|
| 110 | this->functor_->evaluateParam(i, param[i]); | 
|---|
| 111 |  | 
|---|
| 112 | return true; | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | Executor& Executor::setDescription(const std::string& description) | 
|---|
| 117 | { | 
|---|
| 118 | if (!this->bAddedDescription_) | 
|---|
| 119 | { | 
|---|
| 120 | this->description_ = std::string("ExecutorDescription::" + this->name_ + "::function"); | 
|---|
| 121 | AddLanguageEntry(this->description_, description); | 
|---|
| 122 | this->bAddedDescription_ = true; | 
|---|
| 123 | } | 
|---|
| 124 | return (*this); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | const std::string& Executor::getDescription() const | 
|---|
| 128 | { | 
|---|
| 129 | return GetLocalisation(this->description_); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | Executor& Executor::setDescriptionParam(unsigned int param, const std::string& description) | 
|---|
| 133 | { | 
|---|
| 134 | if (param < MAX_FUNCTOR_ARGUMENTS) | 
|---|
| 135 | { | 
|---|
| 136 | if (!this->bAddedDescriptionParam_[param]) | 
|---|
| 137 | { | 
|---|
| 138 | std::string paramnumber; | 
|---|
| 139 | if (!convertValue(¶mnumber, param)) | 
|---|
| 140 | return (*this); | 
|---|
| 141 |  | 
|---|
| 142 | this->descriptionParam_[param] = std::string("ExecutorDescription::" + this->name_ + "::param" + paramnumber); | 
|---|
| 143 | AddLanguageEntry(this->descriptionParam_[param], description); | 
|---|
| 144 | this->bAddedDescriptionParam_[param] = true; | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 | return (*this); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | const std::string& Executor::getDescriptionParam(unsigned int param) const | 
|---|
| 151 | { | 
|---|
| 152 | if (param < MAX_FUNCTOR_ARGUMENTS) | 
|---|
| 153 | return GetLocalisation(this->descriptionParam_[param]); | 
|---|
| 154 |  | 
|---|
| 155 | return this->descriptionParam_[0]; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | Executor& Executor::setDescriptionReturnvalue(const std::string& description) | 
|---|
| 159 | { | 
|---|
| 160 | if (!this->bAddedDescriptionReturnvalue_) | 
|---|
| 161 | { | 
|---|
| 162 | this->descriptionReturnvalue_ = std::string("ExecutorDescription::" + this->name_ + "::returnvalue"); | 
|---|
| 163 | AddLanguageEntry(this->descriptionReturnvalue_, description); | 
|---|
| 164 | this->bAddedDescriptionReturnvalue_ = true; | 
|---|
| 165 | } | 
|---|
| 166 | return (*this); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | const std::string& Executor::getDescriptionReturnvalue(int param) const | 
|---|
| 170 | { | 
|---|
| 171 | return GetLocalisation(this->descriptionReturnvalue_); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | Executor& Executor::setDefaultValues(const MultiType& param1) | 
|---|
| 175 | { | 
|---|
| 176 | this->defaultValue_[0] = param1; | 
|---|
| 177 | this->bAddedDefaultValue_[0] = true; | 
|---|
| 178 |  | 
|---|
| 179 | return (*this); | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2) | 
|---|
| 183 | { | 
|---|
| 184 | this->defaultValue_[0] = param1; | 
|---|
| 185 | this->bAddedDefaultValue_[0] = true; | 
|---|
| 186 | this->defaultValue_[1] = param2; | 
|---|
| 187 | this->bAddedDefaultValue_[1] = true; | 
|---|
| 188 |  | 
|---|
| 189 | return (*this); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3) | 
|---|
| 193 | { | 
|---|
| 194 | this->defaultValue_[0] = param1; | 
|---|
| 195 | this->bAddedDefaultValue_[0] = true; | 
|---|
| 196 | this->defaultValue_[1] = param2; | 
|---|
| 197 | this->bAddedDefaultValue_[1] = true; | 
|---|
| 198 | this->defaultValue_[2] = param3; | 
|---|
| 199 | this->bAddedDefaultValue_[2] = true; | 
|---|
| 200 |  | 
|---|
| 201 | return (*this); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) | 
|---|
| 205 | { | 
|---|
| 206 | this->defaultValue_[0] = param1; | 
|---|
| 207 | this->bAddedDefaultValue_[0] = true; | 
|---|
| 208 | this->defaultValue_[1] = param2; | 
|---|
| 209 | this->bAddedDefaultValue_[1] = true; | 
|---|
| 210 | this->defaultValue_[2] = param3; | 
|---|
| 211 | this->bAddedDefaultValue_[2] = true; | 
|---|
| 212 | this->defaultValue_[3] = param4; | 
|---|
| 213 | this->bAddedDefaultValue_[3] = true; | 
|---|
| 214 |  | 
|---|
| 215 | return (*this); | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | Executor& Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) | 
|---|
| 219 | { | 
|---|
| 220 | this->defaultValue_[0] = param1; | 
|---|
| 221 | this->bAddedDefaultValue_[0] = true; | 
|---|
| 222 | this->defaultValue_[1] = param2; | 
|---|
| 223 | this->bAddedDefaultValue_[1] = true; | 
|---|
| 224 | this->defaultValue_[2] = param3; | 
|---|
| 225 | this->bAddedDefaultValue_[2] = true; | 
|---|
| 226 | this->defaultValue_[3] = param4; | 
|---|
| 227 | this->bAddedDefaultValue_[3] = true; | 
|---|
| 228 | this->defaultValue_[4] = param5; | 
|---|
| 229 | this->bAddedDefaultValue_[4] = true; | 
|---|
| 230 |  | 
|---|
| 231 | return (*this); | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | Executor& Executor::setDefaultValue(unsigned int index, const MultiType& param) | 
|---|
| 235 | { | 
|---|
| 236 | if (index < MAX_FUNCTOR_ARGUMENTS) | 
|---|
| 237 | { | 
|---|
| 238 | this->defaultValue_[index] = param; | 
|---|
| 239 | this->bAddedDefaultValue_[index] = true; | 
|---|
| 240 | } | 
|---|
| 241 | return (*this); | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | bool Executor::allDefaultValuesSet() const | 
|---|
| 245 | { | 
|---|
| 246 | for (unsigned int i = 0; i < this->functor_->getParamCount(); i++) | 
|---|
| 247 | if (!this->bAddedDefaultValue_[i]) | 
|---|
| 248 | return false; | 
|---|
| 249 |  | 
|---|
| 250 | return true; | 
|---|
| 251 | } | 
|---|
| 252 | } | 
|---|