Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

adjusted includes in core/command/*

  • 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 "core/CorePrereqs.h"
34
35#include <string>
36#include "util/MultiType.h"
37#include "Functor.h"
38#include "ExecutorPtr.h"
39
40namespace orxonox
41{
42    class _CoreExport Executor
43    {
44        public:
45            Executor(const FunctorPtr& functor, const std::string& name = "");
46            virtual ~Executor();
47
48            inline MultiType operator()() const
49                { return (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
50            inline MultiType operator()(const MultiType& param1) const
51                { return (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
52            inline MultiType operator()(const MultiType& param1, const MultiType& param2) const
53                { return (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
54            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3) const
55                { return (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
56            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
57                { return (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
58            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
59                { return (*this->functor_)(param1, param2, param3, param4, param5); }
60
61            MultiType parse(const std::string& params, bool* success = 0, const std::string& delimiter = " ") const;
62
63            bool evaluate(const std::string& params, MultiType param[5], const std::string& delimiter = " ") const;
64
65            inline const FunctorPtr& getFunctor() const
66                { return this->functor_; }
67            inline unsigned int getParamCount() const
68                { return this->functor_->getParamCount(); }
69            inline bool hasReturnvalue() const
70                { return this->functor_->hasReturnvalue(); }
71            inline Functor::Type::Enum getType() const
72                { return this->functor_->getType(); }
73            inline std::string getTypenameParam(unsigned int param) const
74                { return this->functor_->getTypenameParam(param); }
75            inline std::string getTypenameReturnvalue() const
76                { return this->functor_->getTypenameReturnvalue(); }
77
78            inline void setName(const std::string& name)
79                { this->name_ = name; }
80            inline const std::string& getName() const
81                { return this->name_; }
82
83            void setDefaultValues(const MultiType& param1);
84            void setDefaultValues(const MultiType& param1, const MultiType& param2);
85            void setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3);
86            void setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4);
87            void setDefaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5);
88            void setDefaultValue(unsigned int index, const MultiType& param);
89
90            inline MultiType getDefaultValue(unsigned int index) const
91            {
92                if (index < MAX_FUNCTOR_ARGUMENTS)
93                    return this->defaultValue_[index];
94
95                return MT_Type::Null;
96            }
97
98            bool allDefaultValuesSet() const;
99            inline bool defaultValueSet(unsigned int index) const
100            {
101                if (index < MAX_FUNCTOR_ARGUMENTS)
102                    return !this->defaultValue_[index].null();
103
104                return false;
105            }
106
107        protected:
108            FunctorPtr functor_;
109            std::string name_;
110            MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS];
111    };
112
113    class _CoreExport ExecutorStatic : public Executor
114    {
115        public:
116            ExecutorStatic(const FunctorStaticPtr& functor, const std::string& name = "") : Executor(functor, name) {}
117            virtual ~ExecutorStatic() {}
118    };
119
120    template <class T>
121    class ExecutorMember : public Executor
122    {
123        public:
124            ExecutorMember(const FunctorMemberPtr<T>& functor, const std::string& name = "") : Executor(functor, name), functorMember_(functor) {}
125            virtual ~ExecutorMember() {}
126
127            using Executor::operator();
128
129            inline MultiType operator()(T* object) const
130                { return (*this->functorMember_)(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
131            inline MultiType operator()(T* object, const MultiType& param1) const
132                { return (*this->functorMember_)(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
133            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2) const
134                { return (*this->functorMember_)(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
135            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
136                { return (*this->functorMember_)(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
137            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
138                { return (*this->functorMember_)(object, param1, param2, param3, param4, this->defaultValue_[4]); }
139            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
140                { return (*this->functorMember_)(object, param1, param2, param3, param4, param5); }
141
142
143            inline MultiType operator()(const T* object) const
144                { return (*this->functorMember_)(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
145            inline MultiType operator()(const T* object, const MultiType& param1) const
146                { return (*this->functorMember_)(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
147            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2) const
148                { return (*this->functorMember_)(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
149            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
150                { return (*this->functorMember_)(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
151            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
152                { return (*this->functorMember_)(object, param1, param2, param3, param4, this->defaultValue_[4]); }
153            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
154                { return (*this->functorMember_)(object, param1, param2, param3, param4, param5); }
155
156            inline void setObject(T* object) const
157                { this->functorMember_->setObject(object); }
158            inline void setObject(const T* object) const
159                { this->functorMember_->setObject(object); }
160
161            using Executor::parse;
162
163            MultiType parse(T* object, const std::string& params, bool* success = 0, const std::string& delimiter = " ") const
164            {
165                const typename FunctorMember<T>::Objects& objects = this->functorMember_->getObjects();
166
167                this->functorMember_->setObject(object);
168                const MultiType& result = this->Executor::parse(params, success, delimiter);
169                this->functorMember_->setObjects(objects);
170
171                return result;
172            }
173
174            MultiType parse(const T* object, const std::string& params, bool* success = 0, const std::string& delimiter = " ") const
175            {
176                const typename FunctorMember<T>::Objects& objects = this->functorMember_->getObjects();
177
178                this->functorMember_->setObject(object);
179                const MultiType& result = this->Executor::parse(params, success, delimiter);
180                this->functorMember_->setObjects(objects);
181
182                return result;
183            }
184
185        protected:
186            FunctorMemberPtr<T> functorMember_;
187    };
188
189    inline ExecutorPtr createExecutor(const FunctorPtr& functor, const std::string& name = "")
190    {
191        return new Executor(functor, name);
192    }
193
194    template <class T>
195    inline ExecutorMemberPtr<T> createExecutor(const FunctorMemberPtr<T>& functor, const std::string& name = "")
196    {
197        return new ExecutorMember<T>(functor, name);
198    }
199
200    inline ExecutorStaticPtr createExecutor(const FunctorStaticPtr& 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.