Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

re-implemented Functor - without macros!

  • Property svn:eol-style set to native
File size: 8.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(const FunctorPtr& functor, const std::string& name)
42    {
43        this->functor_ = functor;
44        this->name_ = name;
45    }
46
47    Executor::~Executor()
48    {
49    }
50
51    MultiType Executor::parse(const std::string& params, bool* success, const std::string& delimiter) const
52    {
53        if (success)
54            *success = true;
55
56        unsigned int paramCount = this->functor_->getParamCount();
57
58        if (paramCount == 0)
59        {
60            COUT(5) << "Calling Executor " << this->name_ << " through parser without parameters." << std::endl;
61            return (*this->functor_)();
62        }
63        else if (paramCount == 1)
64        {
65            const std::string& temp = getStripped(params);
66            if (!temp.empty())
67            {
68                COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using whole string: " << params << std::endl;
69                return (*this->functor_)(params);
70            }
71            else if (!this->defaultValue_[0].null())
72            {
73                COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using default value: " << this->defaultValue_[0] << std::endl;
74                return (*this->functor_)(this->defaultValue_[0]);
75            }
76            else
77            {
78                COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << temp << ")." << std::endl;
79                if (success)
80                    *success = false;
81                return MT_Type::Null;
82            }
83        }
84        else
85        {
86            SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0');
87
88            for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++)
89            {
90                if (this->defaultValue_[i].null())
91                {
92                    COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input:" << params << ")." << std::endl;
93                    if (success)
94                        *success = false;
95                    return MT_Type::Null;
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                    return (*this->functor_)(param[0], param[1]);
129                case 3:
130                    return (*this->functor_)(param[0], param[1], param[2]);
131                case 4:
132                    return (*this->functor_)(param[0], param[1], param[2], param[3]);
133                case 5:
134                    return (*this->functor_)(param[0], param[1], param[2], param[3], param[4]);
135            }
136        }
137
138        return MT_Type::Null;
139    }
140
141    bool Executor::evaluate(const std::string& params, MultiType param[5], const std::string& delimiter) const
142    {
143        unsigned int paramCount = this->functor_->getParamCount();
144
145        if (paramCount == 1)
146        {
147            // only one param: check if there are params given, otherwise try to use default values
148            if (!getStripped(params).empty())
149            {
150                param[0] = params;
151                this->functor_->evaluateParam(0, param[0]);
152                return true;
153            }
154            else if (!this->defaultValue_[0].null())
155            {
156                param[0] = this->defaultValue_[0];
157                this->functor_->evaluateParam(0, param[0]);
158                return true;
159            }
160            return false;
161        }
162        else
163        {
164            // more than one param
165            SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0');
166
167            // if there are not enough params given, check if there are default values
168            for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++)
169                if (this->defaultValue_[i].null())
170                    return false;
171
172            // assign all given arguments to the multitypes
173            for (unsigned int i = 0; i < std::min(tokens.size(), MAX_FUNCTOR_ARGUMENTS); i++)
174                param[i] = tokens[i];
175
176            // fill the remaining multitypes with default values
177            for (unsigned int i = tokens.size(); i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++)
178                param[i] = this->defaultValue_[i];
179
180            // evaluate the param types through the functor
181            for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++)
182                this->functor_->evaluateParam(i, param[i]);
183
184            return true;
185        }
186    }
187
188    void Executor::setDefaultValues(const MultiType& param1)
189    {
190        this->defaultValue_[0] = param1;
191    }
192
193    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2)
194    {
195        this->defaultValue_[0] = param1;
196        this->defaultValue_[1] = param2;
197    }
198
199    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
200    {
201        this->defaultValue_[0] = param1;
202        this->defaultValue_[1] = param2;
203        this->defaultValue_[2] = param3;
204    }
205
206    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
207    {
208        this->defaultValue_[0] = param1;
209        this->defaultValue_[1] = param2;
210        this->defaultValue_[2] = param3;
211        this->defaultValue_[3] = param4;
212    }
213
214    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
215    {
216        this->defaultValue_[0] = param1;
217        this->defaultValue_[1] = param2;
218        this->defaultValue_[2] = param3;
219        this->defaultValue_[3] = param4;
220        this->defaultValue_[4] = param5;
221    }
222
223    void Executor::setDefaultValue(unsigned int index, const MultiType& param)
224    {
225        if (index < MAX_FUNCTOR_ARGUMENTS)
226            this->defaultValue_[index] = param;
227    }
228
229    bool Executor::allDefaultValuesSet() const
230    {
231        for (unsigned int i = 0; i < this->functor_->getParamCount(); i++)
232            if (this->defaultValue_[i].null())
233                return false;
234
235        return true;
236    }
237}
Note: See TracBrowser for help on using the repository browser.