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