Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/Executor.cc @ 7186

Last change on this file since 7186 was 7186, checked in by landauf, 14 years ago

Moved ability to possess descriptions from Executor to ConsoleCommand, since no other executors use this feature. Also simplified this code a little by introducing a new shortcut in Language.h. XMLPort has to use a temporary solution for descriptions without Language support atm.

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