Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added a small SharedPtr template for use in Functor and Executor. It's an intrusive approach that requires the object to implement a reference counter. The SharedPtr is extensible to reflect the hierarchy of Functor, FunctorStatic, FunctorMember<T>, and all subclasses (same for Executor).

  • Property svn:eol-style set to native
File size: 11.7 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        friend class SharedPtr<Executor>;
44
45        public:
46            Executor(Functor* functor, const std::string& name = "");
47            virtual ~Executor();
48
49            inline MultiType operator()() const
50                { return (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
51            inline MultiType operator()(const MultiType& param1) const
52                { return (*this->functor_)(param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
53            inline MultiType operator()(const MultiType& param1, const MultiType& param2) const
54                { return (*this->functor_)(param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
55            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3) const
56                { return (*this->functor_)(param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
57            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
58                { return (*this->functor_)(param1, param2, param3, param4, this->defaultValue_[4]); }
59            inline MultiType operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
60                { return (*this->functor_)(param1, param2, param3, param4, param5); }
61
62            MultiType parse(const std::string& params, bool* success = 0, const std::string& delimiter = " ") const;
63
64            bool evaluate(const std::string& params, MultiType param[5], const std::string& delimiter = " ") const;
65
66            inline Functor* getFunctor() const
67                { return this->functor_; }
68            inline unsigned int getParamCount() const
69                { return this->functor_->getParamCount(); }
70            inline bool hasReturnvalue() const
71                { return this->functor_->hasReturnvalue(); }
72            inline Functor::Type::Enum getType() const
73                { return this->functor_->getType(); }
74            inline std::string getTypenameParam(unsigned int param) const
75                { return this->functor_->getTypenameParam(param); }
76            inline 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        private:
114            inline void incrementReferenceCount()
115                { ++this->references_; }
116            inline void decrementReferenceCount()
117                { --this->references_; if (this->references_ == 0) delete this; }
118
119            int references_;
120            static int instances_s;
121    };
122
123    class _CoreExport ExecutorStatic : public Executor
124    {
125        public:
126            ExecutorStatic(FunctorStatic* functor, const std::string& name = "") : Executor(functor, name) {}
127            virtual ~ExecutorStatic() {}
128    };
129
130    template <class T>
131    class ExecutorMember : public Executor
132    {
133        public:
134            ExecutorMember(FunctorMember<T>* functor, const std::string& name = "") : Executor(functor, name) {}
135            virtual ~ExecutorMember() {}
136
137            using Executor::operator();
138
139            inline MultiType operator()(T* object) const
140                { return (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
141            inline MultiType operator()(T* object, const MultiType& param1) const
142                { return (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
143            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2) const
144                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
145            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
146                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
147            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
148                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
149            inline MultiType operator()(T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
150                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
151
152
153            inline MultiType operator()(const T* object) const
154                { return (*((FunctorMember<T>*)this->functor_))(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
155            inline MultiType operator()(const T* object, const MultiType& param1) const
156                { return (*((FunctorMember<T>*)this->functor_))(object, param1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
157            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2) const
158                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
159            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3) const
160                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, this->defaultValue_[3], this->defaultValue_[4]); }
161            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) const
162                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, this->defaultValue_[4]); }
163            inline MultiType operator()(const T* object, const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) const
164                { return (*((FunctorMember<T>*)this->functor_))(object, param1, param2, param3, param4, param5); }
165
166            inline void setObject(T* object) const
167                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
168            inline void setObject(const T* object) const
169                { ((FunctorMember<T>*)this->functor_)->setObject(object); }
170
171            using Executor::parse;
172
173            MultiType parse(T* object, const std::string& params, bool* success = 0, const std::string& delimiter = " ") const
174            {
175                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
176
177                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
178
179                functorMember->setObject(object);
180                const MultiType& result = Executor::parse(params, success, delimiter);
181                functorMember->setObjects(objects);
182
183                return result;
184            }
185
186            MultiType parse(const T* object, const std::string& params, bool* success = 0, const std::string& delimiter = " ") const
187            {
188                FunctorMember<T>* functorMember = static_cast<FunctorMember<T>*>(this->functor_);
189
190                const typename FunctorMember<T>::Objects& objects = functorMember->getObjects();
191
192                functorMember->setObject(object);
193                const MultiType& result = Executor::parse(params, success, delimiter);
194                functorMember->setObjects(objects);
195
196                return result;
197            }
198    };
199
200
201
202    typedef SharedPtr<Executor> ExecutorPtr;
203
204    typedef SharedChildPtr<ExecutorStatic, Executor> ExecutorStaticPtr;
205
206    template <class T>
207    class ExecutorMemberPtr : public SharedChildPtr<ExecutorMember<T>, Executor>
208    {
209        public:
210            inline ExecutorMemberPtr() : SharedChildPtr<ExecutorMember<T>, Executor>() {}
211            inline ExecutorMemberPtr(ExecutorMember<T>* pointer) : SharedChildPtr<ExecutorMember<T>, Executor>(pointer) {}
212//            inline ExecutorMemberPtr(const ExecutorMemberPtr& other) : SharedChildPtr<ExecutorMember<T>, Executor>(other) {}
213    };
214
215
216
217    inline Executor* createExecutor(Functor* functor, const std::string& name = "")
218    {
219        return new Executor(functor, name);
220    }
221
222    template <class T>
223    inline ExecutorMember<T>* createExecutor(FunctorMember<T>* functor, const std::string& name = "")
224    {
225        return new ExecutorMember<T>(functor, name);
226    }
227
228    inline ExecutorStatic* createExecutor(FunctorStatic* functor, const std::string& name = "")
229    {
230        return new ExecutorStatic(functor, name);
231    }
232}
233
234#endif /* _Executor_H__ */
Note: See TracBrowser for help on using the repository browser.