Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

expanded Executor

File size: 7.7 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#include "util/String.h"
32
33namespace orxonox
34{
35    Executor::Executor(Functor* functor, const std::string& name)
36    {
37        this->functor_ = functor;
38        this->name_ = name;
39        this->bAddedDescription_ = false;
40        this->bAddedDescriptionReturnvalue_ = false;
41
42        this->bAddedDescriptionParam_[0] = false;
43        this->bAddedDescriptionParam_[1] = false;
44        this->bAddedDescriptionParam_[2] = false;
45        this->bAddedDescriptionParam_[3] = false;
46        this->bAddedDescriptionParam_[4] = false;
47
48        this->bAddedDefaultValue_[0] = false;
49        this->bAddedDefaultValue_[1] = false;
50        this->bAddedDefaultValue_[2] = false;
51        this->bAddedDefaultValue_[3] = false;
52        this->bAddedDefaultValue_[4] = false;
53    }
54
55    Executor::~Executor()
56    {
57        delete this->functor_;
58    }
59
60    bool Executor::parse(const std::string& params, const std::string& delimiter) const
61    {
62        unsigned int paramCount = this->functor_->getParamCount();
63
64        if (paramCount == 0)
65        {
66            (*this->functor_)();
67        }
68        else if (paramCount == 1)
69        {
70            (*this->functor_)(MultiTypeMath(params));
71        }
72        else
73        {
74            SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0');
75
76            for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++)
77                if (!this->bAddedDefaultValue_[i])
78                    return false;
79
80            MultiTypeMath param[paramCount];
81            for (unsigned int i = 0; i < tokens.size(); i++)
82                param[i] = tokens[i];
83            for (unsigned int i = tokens.size(); i < paramCount; i++)
84                param[i] = this->defaultValue_[i];
85
86            switch(paramCount)
87            {
88                case 2:
89                    (*this->functor_)(param[0], param[1]);
90                    break;
91                case 3:
92                    (*this->functor_)(param[0], param[1], param[2]);
93                    break;
94                case 4:
95                    (*this->functor_)(param[0], param[1], param[2], param[3]);
96                    break;
97                case 5:
98                    (*this->functor_)(param[0], param[1], param[2], param[3], param[4]);
99                    break;
100            }
101        }
102
103        return true;
104    }
105
106    void Executor::setName(const std::string name)
107    {
108        this->name_ = name;
109    }
110
111    const std::string& Executor::getName() const
112    {
113        return this->name_;
114    }
115
116    void Executor::description(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    }
125
126    const std::string& Executor::getDescription() const
127    {
128        return GetLocalisation(this->description_);
129    }
130
131    void Executor::descriptionParam(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(&paramnumber, param))
139                    return;
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    }
147
148    const std::string& Executor::getDescriptionParam(int param) const
149    {
150        if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS)
151            return GetLocalisation(this->descriptionParam_[param]);
152
153        return this->descriptionParam_[0];
154    }
155
156    void Executor::descriptionReturnvalue(const std::string& description)
157    {
158        if (!this->bAddedDescriptionReturnvalue_)
159        {
160            this->descriptionReturnvalue_ = std::string("ExecutorDescription::" + this->name_ + "::returnvalue");
161            AddLanguageEntry(this->descriptionReturnvalue_, description);
162            this->bAddedDescriptionReturnvalue_ = true;
163        }
164    }
165
166    const std::string& Executor::getDescriptionReturnvalue(int param) const
167    {
168        return GetLocalisation(this->descriptionReturnvalue_);
169    }
170
171    void Executor::setDefaultValues(const MultiTypeMath& param1)
172    {
173        this->defaultValue_[0] = param1;
174        this->bAddedDefaultValue_[0] = true;
175    }
176
177    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2)
178    {
179        this->defaultValue_[0] = param1;
180        this->bAddedDefaultValue_[0] = true;
181        this->defaultValue_[1] = param2;
182        this->bAddedDefaultValue_[1] = true;
183    }
184
185    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3)
186    {
187        this->defaultValue_[0] = param1;
188        this->bAddedDefaultValue_[0] = true;
189        this->defaultValue_[1] = param2;
190        this->bAddedDefaultValue_[1] = true;
191        this->defaultValue_[2] = param3;
192        this->bAddedDefaultValue_[2] = true;
193    }
194
195    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4)
196    {
197        this->defaultValue_[0] = param1;
198        this->bAddedDefaultValue_[0] = true;
199        this->defaultValue_[1] = param2;
200        this->bAddedDefaultValue_[1] = true;
201        this->defaultValue_[2] = param3;
202        this->bAddedDefaultValue_[2] = true;
203        this->defaultValue_[3] = param4;
204        this->bAddedDefaultValue_[3] = true;
205    }
206
207    void Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5)
208    {
209        this->defaultValue_[0] = param1;
210        this->bAddedDefaultValue_[0] = true;
211        this->defaultValue_[1] = param2;
212        this->bAddedDefaultValue_[1] = true;
213        this->defaultValue_[2] = param3;
214        this->bAddedDefaultValue_[2] = true;
215        this->defaultValue_[3] = param4;
216        this->bAddedDefaultValue_[3] = true;
217        this->defaultValue_[4] = param5;
218        this->bAddedDefaultValue_[4] = true;
219    }
220
221    void Executor::setDefaultValue(int index, const MultiTypeMath& param)
222    {
223        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
224        {
225            this->defaultValue_[index] = param;
226            this->bAddedDefaultValue_[index] = true;
227        }
228    }
229}
Note: See TracBrowser for help on using the repository browser.