Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/Executor.cc @ 932

Last change on this file since 932 was 932, checked in by landauf, 16 years ago

added debug output to executor

File size: 9.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 *   Inspiration: Executor by Benjamin Grauer
27 */
28
29#include "Executor.h"
30#include "Language.h"
31
32namespace orxonox
33{
34    Executor::Executor(Functor* functor, const std::string& name)
35    {
36        this->functor_ = functor;
37        this->name_ = name;
38        this->bAddedDescription_ = false;
39        this->bAddedDescriptionReturnvalue_ = false;
40
41        this->bAddedDescriptionParam_[0] = false;
42        this->bAddedDescriptionParam_[1] = false;
43        this->bAddedDescriptionParam_[2] = false;
44        this->bAddedDescriptionParam_[3] = false;
45        this->bAddedDescriptionParam_[4] = false;
46
47        this->bAddedDefaultValue_[0] = false;
48        this->bAddedDefaultValue_[1] = false;
49        this->bAddedDefaultValue_[2] = false;
50        this->bAddedDefaultValue_[3] = false;
51        this->bAddedDefaultValue_[4] = false;
52    }
53
54    Executor::~Executor()
55    {
56        delete this->functor_;
57    }
58
59    bool Executor::parse(const std::string& params, const std::string& delimiter) const
60    {
61        unsigned int paramCount = this->functor_->getParamCount();
62
63        if (paramCount == 0)
64        {
65            COUT(5) << "Calling Executor " << this->name_ << " through parser without parameters." << std::endl;
66            (*this->functor_)();
67        }
68        else if (paramCount == 1)
69        {
70            std::string temp = getStripped(params);
71            if ((temp != "") && (temp.size() != 0))
72            {
73                COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using whole string: " << params << std::endl;
74                (*this->functor_)(MultiTypeMath(params));
75            }
76            else if (this->bAddedDefaultValue_[0])
77            {
78                COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using default value: " << this->defaultValue_[0] << std::endl;
79                (*this->functor_)(this->defaultValue_[0]);
80            }
81            else
82            {
83                COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given." << std::endl;
84                return false;
85            }
86        }
87        else
88        {
89            SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0');
90
91            for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++)
92            {
93                if (!this->bAddedDefaultValue_[i])
94                {
95                    COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given." << std::endl;
96                    return false;
97                }
98            }
99
100            MultiTypeMath param[paramCount];
101            COUT(5) << "Calling Executor " << this->name_ << " through parser with " << paramCount << " parameters, using " << tokens.size() << " tokens (";
102            for (unsigned int i = 0; i < tokens.size(); i++)
103            {
104                param[i] = tokens[i];
105                if (i != 0)
106                {
107                    COUT(5) << ", ";
108                }
109                COUT(5) << tokens[i];
110            }
111            COUT(5) << ") and " << (paramCount - tokens.size()) << " default values (";
112            for (unsigned int i = tokens.size(); i < paramCount; i++)
113            {
114                param[i] = this->defaultValue_[i];
115                if (i != 0)
116                {
117                    COUT(5) << ", ";
118                }
119                COUT(5) << this->defaultValue_[i];
120            }
121            COUT(5) << ")." << std::endl;
122
123            switch(paramCount)
124            {
125                case 2:
126                    (*this->functor_)(param[0], param[1]);
127                    break;
128                case 3:
129                    (*this->functor_)(param[0], param[1], param[2]);
130                    break;
131                case 4:
132                    (*this->functor_)(param[0], param[1], param[2], param[3]);
133                    break;
134                case 5:
135                    (*this->functor_)(param[0], param[1], param[2], param[3], param[4]);
136                    break;
137            }
138        }
139
140        return true;
141    }
142
143    void Executor::setName(const std::string name)
144    {
145        this->name_ = name;
146    }
147
148    const std::string& Executor::getName() const
149    {
150        return this->name_;
151    }
152
153    void Executor::description(const std::string& description)
154    {
155        if (!this->bAddedDescription_)
156        {
157            this->description_ = std::string("ExecutorDescription::" + this->name_ + "::function");
158            AddLanguageEntry(this->description_, description);
159            this->bAddedDescription_ = true;
160        }
161    }
162
163    const std::string& Executor::getDescription() const
164    {
165        return GetLocalisation(this->description_);
166    }
167
168    void Executor::descriptionParam(int param, const std::string& description)
169    {
170        if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS)
171        {
172            if (!this->bAddedDescriptionParam_[param])
173            {
174                std::string paramnumber;
175                if (!Convert::ToString(&paramnumber, param))
176                    return;
177
178                this->descriptionParam_[param] = std::string("ExecutorDescription::" + this->name_ + "::param" + paramnumber);
179                AddLanguageEntry(this->descriptionParam_[param], description);
180                this->bAddedDescriptionParam_[param] = true;
181            }
182        }
183    }
184
185    const std::string& Executor::getDescriptionParam(int param) const
186    {
187        if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS)
188            return GetLocalisation(this->descriptionParam_[param]);
189
190        return this->descriptionParam_[0];
191    }
192
193    void Executor::descriptionReturnvalue(const std::string& description)
194    {
195        if (!this->bAddedDescriptionReturnvalue_)
196        {
197            this->descriptionReturnvalue_ = std::string("ExecutorDescription::" + this->name_ + "::returnvalue");
198            AddLanguageEntry(this->descriptionReturnvalue_, description);
199            this->bAddedDescriptionReturnvalue_ = true;
200        }
201    }
202
203    const std::string& Executor::getDescriptionReturnvalue(int param) const
204    {
205        return GetLocalisation(this->descriptionReturnvalue_);
206    }
207
208    void Executor::setDefaultValues(const MultiTypeMath& param1)
209    {
210        this->defaultValue_[0] = param1;
211        this->bAddedDefaultValue_[0] = true;
212    }
213
214    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2)
215    {
216        this->defaultValue_[0] = param1;
217        this->bAddedDefaultValue_[0] = true;
218        this->defaultValue_[1] = param2;
219        this->bAddedDefaultValue_[1] = true;
220    }
221
222    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3)
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    }
231
232    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4)
233    {
234        this->defaultValue_[0] = param1;
235        this->bAddedDefaultValue_[0] = true;
236        this->defaultValue_[1] = param2;
237        this->bAddedDefaultValue_[1] = true;
238        this->defaultValue_[2] = param3;
239        this->bAddedDefaultValue_[2] = true;
240        this->defaultValue_[3] = param4;
241        this->bAddedDefaultValue_[3] = true;
242    }
243
244    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5)
245    {
246        this->defaultValue_[0] = param1;
247        this->bAddedDefaultValue_[0] = true;
248        this->defaultValue_[1] = param2;
249        this->bAddedDefaultValue_[1] = true;
250        this->defaultValue_[2] = param3;
251        this->bAddedDefaultValue_[2] = true;
252        this->defaultValue_[3] = param4;
253        this->bAddedDefaultValue_[3] = true;
254        this->defaultValue_[4] = param5;
255        this->bAddedDefaultValue_[4] = true;
256    }
257
258    void Executor::setDefaultValue(int index, const MultiTypeMath& param)
259    {
260        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
261        {
262            this->defaultValue_[index] = param;
263            this->bAddedDefaultValue_[index] = true;
264        }
265    }
266}
Note: See TracBrowser for help on using the repository browser.