Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added new function to MultiType to check whether or not it contains a value. using this in executor to improve handling of default values.

  • Property svn:eol-style set to native
File size: 10.6 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 void operator()() const
48                { (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
49            inline void operator()(const MultiType& param1) const
50                { (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
51            inline void operator()(const MultiType& param1, const MultiType& param2) const
52                { (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
53            inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3) const
54                { (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
55            inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
56                { (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
57            inline void operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
58                { (*this->functor_)(param1, param2, param3, param4, param5); }
59
60            bool parse(const std::string& params, 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 FunctionType::Value getType() const
71                { return this->functor_->getType(); }
72            inline const MultiType& getReturnvalue() const
73                { return this->functor_->getReturnvalue(); }
74            inline const std::string& getTypenameParam(unsigned int param) const
75                { return this->functor_->getTypenameParam(param); }
76            inline const std::string& getTypenameReturnvalue() const
77                { return this->functor_->getTypenameReturnvalue(); }
78
79            inline void setName(const std::string& name)
80                { this->name_ = name; }
81            inline const std::string& getName() const
82                { return this->name_; }
83
84            Executor& setDefaultValues(const MultiType& param1);
85            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2);
86            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3);
87            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4);
88            Executor& setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5);
89            Executor& setDefaultValue(unsigned int index, const MultiType& param);
90
91            inline MultiType getDefaultValue(unsigned int index) const
92            {
93                if (index < MAX_FUNCTOR_ARGUMENTS)
94                    return this->defaultValue_[index];
95
96                return MT_Type::Null;
97            }
98
99            bool allDefaultValuesSet() const;
100            inline bool defaultValueSet(unsigned int index) const
101            {
102                if (index < MAX_FUNCTOR_ARGUMENTS)
103                    return !this->defaultValue_[index].null();
104
105                return false;
106            }
107
108        protected:
109            Functor* functor_;
110            std::string name_;
111            MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS];
112    };
113
114    class _CoreExport ExecutorStatic : public Executor
115    {
116        public:
117            ExecutorStatic(FunctorStatic* functor, const std::string& name = "") : Executor(functor, name) {}
118            virtual ~ExecutorStatic() {}
119    };
120
121    template <class T>
122    class ExecutorMember : public Executor
123    {
124        public:
125            ExecutorMember(FunctorMember<T>* functor, const std::string& name = "") : Executor(functor, name) {}
126            virtual ~ExecutorMember() {}
127
128            using Executor::operator();
129
130            inline void operator()(T* object) const
131                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
132            inline void operator()(T* object, const MultiType& param1) const
133                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
134            inline void operator()(T* object, const MultiType& param1, const MultiType& param2) const
135                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
136            inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
137                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
138            inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
139                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
140            inline void operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
141                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
142
143
144            inline void operator()(const T* object) const
145                { (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
146            inline void operator()(const T* object, const MultiType& param1) const
147                { (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
148            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2) const
149                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
150            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
151                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
152            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
153                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
154            inline void operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
155                { (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
156
157            inline void setObject(T* object) const
158                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
159            inline void setObject(const T* object) const
160                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
161
162            using Executor::parse;
163
164            bool parse(T* object, const std::string& params, const std::string& delimiter = " ") const
165            {
166                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
167
168                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
169
170                functorMember->setObject(object);
171                bool result = Executor::parse(params, delimiter);
172                functorMember->setObjects(objects);
173
174                return result;
175            }
176
177            bool parse(const T* object, const std::string& params, const std::string& delimiter = " ") const
178            {
179                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
180
181                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
182
183                functorMember->setObject(object);
184                bool result = Executor::parse(params, delimiter);
185                functorMember->setObjects(objects);
186
187                return result;
188            }
189    };
190
191    inline Executor* createExecutor(Functor* functor, const std::string& name = "")
192    {
193        return new Executor(functor, name);
194    }
195
196    template <class T>
197    inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "")
198    {
199        return new ExecutorMember<T>(functor, name);
200    }
201
202    inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "")
203    {
204        return new ExecutorStatic(functor, name);
205    }
206}
207
208#endif /* _Executor_H__ */
Note: See TracBrowser for help on using the repository browser.