Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/Executor.h @ 7189

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

changed passing of the returnvalue in the command execution pipeline:

  • Functor: operator() directly returns the returnvalue of the executed function (if any, otherwise a MultiType whose null() function evaluates to true). The returnvalue is no longer stored in the Functor.
  • Executor: The same behavior of operator() like in Functor. Additionally the parse() function returns the returnvalue of the executed function instead of a boolean status. The status can be retrieved by passing a pointer to a bool to the function.
  • CommandExecutor: execute() works like before (returns only a boolean status), but added a new function query() which returns the returnvalue of the executed command. The status of query() can be retrieved by optionally passing an pointer to a bool.
  • CommandEvaluation: same as for CommandExecutor: execute() like before, added query()
  • TclBind::eval() returns the returnvalue of the evaluated tcl command. The status can also be retrieved by passing a pointer to a bool.
  • The Shell prints the returnvalue (if available) of an executed command
  • added a constructor to MultiType to directly create it from an mbool. The mbool will be converted to a bool, so it loses it's internal state.
  • Property svn:eol-style set to native
File size: 10.8 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#ifndef _Executor_H__
31#define _Executor_H__
32
33#include "CorePrereqs.h"
34
35#include <string>
36#include "util/MultiType.h"
37#include "Functor.h"
38
39namespace orxonox
40{
41    class _CoreExport Executor
42    {
43        public:
44            Executor(Functor* functor, const std::string& name = "");
45            virtual ~Executor();
46
47            inline MultiType operator()() const
48                { return (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
49            inline MultiType operator()(const MultiType& param1) const
50                { return (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
51            inline MultiType operator()(const MultiType& param1, const MultiType& param2) const
52                { return (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
53            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3) const
54                { return (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
55            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
56                { return (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
57            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
58                { return (*this->functor_)(param1, param2, param3, param4, param5); }
59
60            MultiType parse(const std::string& params, bool* success = 0, const std::string& delimiter = " ") const;
61
62            bool evaluate(const std::string& params, MultiType param[5], const std::string& delimiter = " ") const;
63
64            inline Functor* getFunctor() const
65                { return this->functor_; }
66            inline unsigned int getParamCount() const
67                { return this->functor_->getParamCount(); }
68            inline bool hasReturnvalue() const
69                { return this->functor_->hasReturnvalue(); }
70            inline Functor::Type::Enum getType() const
71                { return this->functor_->getType(); }
72            inline std::string getTypenameParam(unsigned int param) const
73                { return this->functor_->getTypenameParam(param); }
74            inline std::string getTypenameReturnvalue() const
75                { return this->functor_->getTypenameReturnvalue(); }
76
77            inline void setName(const std::string& name)
78                { this->name_ = name; }
79            inline const std::string& getName() const
80                { return this->name_; }
81
82            Executor& setDefaultValues(const MultiType& param1);
83            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2);
84            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3);
85            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4);
86            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5);
87            Executor& setDefaultValue(unsigned int index, const MultiType& param);
88
89            inline MultiType getDefaultValue(unsigned int index) const
90            {
91                if (index < MAX_FUNCTOR_ARGUMENTS)
92                    return this->defaultValue_[index];
93
94                return MT_Type::Null;
95            }
96
97            bool allDefaultValuesSet() const;
98            inline bool defaultValueSet(unsigned int index) const
99            {
100                if (index < MAX_FUNCTOR_ARGUMENTS)
101                    return !this->defaultValue_[index].null();
102
103                return false;
104            }
105
106        protected:
107            Functor* functor_;
108            std::string name_;
109            MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS];
110    };
111
112    class _CoreExport ExecutorStatic : public Executor
113    {
114        public:
115            ExecutorStatic(FunctorStatic* functor, const std::string& name = "") : Executor(functor, name) {}
116            virtual ~ExecutorStatic() {}
117    };
118
119    template <class T>
120    class ExecutorMember : public Executor
121    {
122        public:
123            ExecutorMember(FunctorMember<T>* functor, const std::string& name = "") : Executor(functor, name) {}
124            virtual ~ExecutorMember() {}
125
126            using Executor::operator();
127
128            inline MultiType operator()(T* object) const
129                { return (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
130            inline MultiType operator()(T* object, const MultiType& param1) const
131                { return (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
132            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2) const
133                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
134            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
135                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
136            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
137                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
138            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
139                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
140
141
142            inline MultiType operator()(const T* object) const
143                { return (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
144            inline MultiType operator()(const T* object, const MultiType& param1) const
145                { return (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
146            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2) const
147                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
148            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
149                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
150            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
151                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
152            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
153                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
154
155            inline void setObject(T* object) const
156                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
157            inline void setObject(const T* object) const
158                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
159
160            using Executor::parse;
161
162            MultiType parse(T* object, const std::string& params, bool* success = 0, const std::string& delimiter = " ") const
163            {
164                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
165
166                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
167
168                functorMember->setObject(object);
169                const MultiType& result = Executor::parse(params, success, delimiter);
170                functorMember->setObjects(objects);
171
172                return result;
173            }
174
175            MultiType parse(const T* object, const std::string& params, bool* success = 0, const std::string& delimiter = " ") const
176            {
177                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
178
179                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
180
181                functorMember->setObject(object);
182                const MultiType& result = Executor::parse(params, success, delimiter);
183                functorMember->setObjects(objects);
184
185                return result;
186            }
187    };
188
189    inline Executor* createExecutor(Functor* functor, const std::string& name = "")
190    {
191        return new Executor(functor, name);
192    }
193
194    template <class T>
195    inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "")
196    {
197        return new ExecutorMember<T>(functor, name);
198    }
199
200    inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "")
201    {
202        return new ExecutorStatic(functor, name);
203    }
204}
205
206#endif /* _Executor_H__ */
Note: See TracBrowser for help on using the repository browser.