Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

pushFunction() (which explicitly adds a new level to the console command stack) was not forwarded to ConsoleCommandManipulator

also fixed a bug which was caused by pushFunction() with a subsequent call to setFunction(…) because it may have unexpectedly changed the functor of the lower stack levels. now pushFunction() not only copyconstructs the executor, but also the Functor (with a clone() function)

  • Property svn:eol-style set to native
File size: 6.4 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#include "CommandExecutor.h"
39
40namespace orxonox
41{
42    Executor::Executor(const FunctorPtr& functor, const std::string& name)
43    {
44        this->functor_ = functor;
45        this->name_ = name;
46    }
47
48    Executor::Executor(const Executor& other) : name_(other.name_), defaultValue_(other.defaultValue_)
49    {
50        this->functor_ = other.functor_->clone();
51    }
52
53    Executor::~Executor()
54    {
55    }
56
57    MultiType Executor::parse(const std::string& arguments, int* error, const std::string& delimiter, bool bPrintError) const
58    {
59        return this->parse(SubString(arguments, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'), error, delimiter, bPrintError);
60    }
61
62    MultiType Executor::parse(const SubString& arguments, int* error, const std::string& delimiter, bool bPrintError) const
63    {
64        MultiType param[MAX_FUNCTOR_ARGUMENTS];
65        unsigned int paramCount = this->evaluateParams(arguments, param, error, delimiter);
66
67        if (error && *error)
68        {
69            if (bPrintError)
70                COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << arguments.join() << ")." << std::endl;
71            return MT_Type::Null;
72        }
73
74        COUT(5) << "Executor::parse: \"" << arguments.join(delimiter) << "\" -> " << paramCount << " params: " << param[0] << " / " << param[1] << " / " << param[2] << " / " << param[3] << " / " << param[4] << std::endl;
75
76        switch (paramCount)
77        {
78            case 0:  return (*this->functor_)();
79            case 1:  return (*this->functor_)(param[0]);
80            case 2:  return (*this->functor_)(param[0], param[1]);
81            case 3:  return (*this->functor_)(param[0], param[1], param[2]);
82            case 4:  return (*this->functor_)(param[0], param[1], param[2], param[3]);
83            case 5:
84            default: return (*this->functor_)(param[0], param[1], param[2], param[3], param[4]);
85        }
86    }
87
88    int Executor::evaluateParams(const SubString& arguments, MultiType param[MAX_FUNCTOR_ARGUMENTS], int* error, const std::string& delimiter) const
89    {
90        unsigned int paramCount = this->functor_->getParamCount();
91        unsigned int argumentCount = arguments.size();
92
93        // if there are not enough params given, check if there are default values
94        for (unsigned int i = argumentCount; i < paramCount; i++)
95        {
96            if (this->defaultValue_[i].null())
97            {
98                if (error)
99                    *error = CommandExecutor::Incomplete;
100                return 0;
101            }
102        }
103
104        // assign all given arguments to the multitypes
105        for (unsigned int i = 0; i < std::min(std::min(argumentCount, paramCount), MAX_FUNCTOR_ARGUMENTS); i++)
106            param[i] = arguments[i];
107
108        // fill the remaining multitypes with default values
109        for (unsigned int i = argumentCount; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++)
110            param[i] = this->defaultValue_[i];
111
112        // assign the remaining arguments all to the last parameter if it is a string
113        if ((paramCount <= MAX_FUNCTOR_ARGUMENTS) &&(argumentCount > paramCount) && (paramCount == 1 || this->functor_->getTypenameParam(paramCount - 1) == "string"))
114            param[paramCount - 1] = arguments.subSet(paramCount - 1).join(delimiter);
115
116        // evaluate the param types through the functor
117        for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++)
118            this->functor_->evaluateParam(i, param[i]);
119
120        if (error)
121            *error = CommandExecutor::Success;
122        return paramCount;
123    }
124
125    void Executor::setDefaultValues(const MultiType& param1)
126    {
127        this->defaultValue_[0] = param1;
128    }
129
130    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2)
131    {
132        this->defaultValue_[0] = param1;
133        this->defaultValue_[1] = param2;
134    }
135
136    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
137    {
138        this->defaultValue_[0] = param1;
139        this->defaultValue_[1] = param2;
140        this->defaultValue_[2] = param3;
141    }
142
143    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
144    {
145        this->defaultValue_[0] = param1;
146        this->defaultValue_[1] = param2;
147        this->defaultValue_[2] = param3;
148        this->defaultValue_[3] = param4;
149    }
150
151    void Executor::setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
152    {
153        this->defaultValue_[0] = param1;
154        this->defaultValue_[1] = param2;
155        this->defaultValue_[2] = param3;
156        this->defaultValue_[3] = param4;
157        this->defaultValue_[4] = param5;
158    }
159
160    void Executor::setDefaultValue(unsigned int index, const MultiType& param)
161    {
162        if (index < MAX_FUNCTOR_ARGUMENTS)
163            this->defaultValue_[index] = param;
164    }
165
166    bool Executor::allDefaultValuesSet() const
167    {
168        for (unsigned int i = 0; i < this->functor_->getParamCount(); i++)
169            if (this->defaultValue_[i].null())
170                return false;
171
172        return true;
173    }
174}
Note: See TracBrowser for help on using the repository browser.