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