Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Changed implementation of SharedPtr, it's now non-intrusive and uses an allocated counter instead. Hence it's possible to create an instance of SharedPtr<T> even if T is only known from a forward declaration. Also changed some parts of the code to reflect the inheritance of the underlying object pointers without using inheritance in the SharedPtr itself.

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