Orxonox  0.0.5 Codename: Arcturus
Executor.h
Go to the documentation of this file.
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 
80 #ifndef _Executor_H__
81 #define _Executor_H__
82 
83 #include "core/CorePrereqs.h"
84 
85 #include <string>
86 #include "util/MultiType.h"
87 #include "Functor.h"
88 #include "ExecutorPtr.h"
89 
90 namespace orxonox
91 {
98  {
99  public:
100  Executor(const FunctorPtr& functor, const std::string& name = "");
101  Executor(const Executor& other);
102  virtual ~Executor() = default;
103 
105  inline MultiType operator()() const
106  { return (*this->functor_)(this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
108  inline MultiType operator()(const MultiType& arg1) const
109  { return (*this->functor_)(arg1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
111  inline MultiType operator()(const MultiType& arg1, const MultiType& arg2) const
112  { return (*this->functor_)(arg1, arg2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
114  inline MultiType operator()(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3) const
115  { return (*this->functor_)(arg1, arg2, arg3, this->defaultValue_[3], this->defaultValue_[4]); }
117  inline MultiType operator()(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4) const
118  { return (*this->functor_)(arg1, arg2, arg3, arg4, this->defaultValue_[4]); }
120  inline MultiType operator()(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5) const
121  { return (*this->functor_)(arg1, arg2, arg3, arg4, arg5); }
122 
123  MultiType parse(const std::string& arguments, int* error = nullptr, const std::string& delimiter = " ", bool bPrintError = false) const;
124  MultiType parse(const SubString& arguments, int* error = nullptr, const std::string& delimiter = " ", bool bPrintError = false) const;
125 
126  int evaluateArguments(const SubString& arguments, MultiType arg[MAX_FUNCTOR_ARGUMENTS], int* error = nullptr, const std::string& delimiter = " ") const;
127 
129  inline void setFunctor(const FunctorPtr& functor)
130  { this->functor_ = functor; }
132  inline const FunctorPtr& getFunctor() const
133  { return this->functor_; }
134 
136  inline void setName(const std::string& name)
137  { this->name_ = name; }
139  inline const std::string& getName() const
140  { return this->name_; }
141 
143  inline unsigned int getParamCount() const
144  { return this->functor_->getParamCount(); }
146  inline bool hasReturnvalue() const
147  { return this->functor_->hasReturnvalue(); }
149  inline Functor::Type getType() const
150  { return this->functor_->getType(); }
152  inline std::string getTypenameParam(unsigned int param) const
153  { return this->functor_->getTypenameParam(param); }
156  { return this->functor_->getTypenameReturnvalue(); }
157 
158  void setDefaultValues(const MultiType& arg1);
159  void setDefaultValues(const MultiType& arg1, const MultiType& arg2);
160  void setDefaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3);
161  void setDefaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4);
162  void setDefaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5);
163  void setDefaultValue(unsigned int index, const MultiType& arg);
164 
166  inline MultiType getDefaultValue(unsigned int index) const
167  {
168  if (index < MAX_FUNCTOR_ARGUMENTS)
169  return this->defaultValue_[index];
170 
171  return MultiType::Null;
172  }
173 
174  bool allDefaultValuesSet() const;
175 
177  inline bool defaultValueSet(unsigned int index) const
178  {
179  if (index < MAX_FUNCTOR_ARGUMENTS)
180  return !this->defaultValue_[index].null();
181 
182  return false;
183  }
184 
185  protected:
189  };
190 
197  {
198  public:
200  ExecutorStatic(const FunctorStaticPtr& functor, const std::string& name = "") : Executor(functor, name) {}
202  virtual ~ExecutorStatic() {}
203  };
204 
211  template <class T>
212  class ExecutorMember : public Executor
213  {
214  public:
216  ExecutorMember(const FunctorMemberPtr<T>& functor, const std::string& name = "") : Executor(functor, name), functorMember_(functor) {}
218  virtual ~ExecutorMember() {}
219 
220  using Executor::operator();
221 
223  inline MultiType operator()(T* object) const
224  { return (*this->functorMember_)(object, this->defaultValue_[0], this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
226  inline MultiType operator()(T* object, const MultiType& arg1) const
227  { return (*this->functorMember_)(object, arg1, this->defaultValue_[1], this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
229  inline MultiType operator()(T* object, const MultiType& arg1, const MultiType& arg2) const
230  { return (*this->functorMember_)(object, arg1, arg2, this->defaultValue_[2], this->defaultValue_[3], this->defaultValue_[4]); }
232  inline MultiType operator()(T* object, const MultiType& arg1, const MultiType& arg2, const MultiType& arg3) const
233  { return (*this->functorMember_)(object, arg1, arg2, arg3, this->defaultValue_[3], this->defaultValue_[4]); }
235  inline MultiType operator()(T* object, const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4) const
236  { return (*this->functorMember_)(object, arg1, arg2, arg3, arg4, this->defaultValue_[4]); }
238  inline MultiType operator()(T* object, const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5) const
239  { return (*this->functorMember_)(object, arg1, arg2, arg3, arg4, arg5); }
240 
242  inline void setObject(T* object) const
243  { this->functorMember_->setObject(object); }
245  inline T* getObject() const
246  { return this->functorMember_->getObject(); }
247 
248  using Executor::parse;
249 
251  MultiType parse(T* object, const std::string& arguments, int* error = nullptr, const std::string& delimiter = " ", bool bPrintError = false) const
252  {
253  T* oldobject = this->functorMember_->getObject();
254 
255  this->functorMember_->setObject(object);
256  const MultiType& result = this->Executor::parse(arguments, error, delimiter, bPrintError);
257  this->functorMember_->setObject(oldobject);
258 
259  return result;
260  }
261 
262  protected:
264  };
265 
267  inline ExecutorPtr createExecutor(const FunctorPtr& functor, const std::string& name = "")
268  {
269  return std::make_shared<Executor>(functor, name);
270  }
271 
273  template <class T>
274  inline ExecutorMemberPtr<T> createExecutor(const FunctorMemberPtr<T>& functor, const std::string& name = "")
275  {
276  return std::make_shared<ExecutorMember<T>>(functor, name);
277  }
278 
280  inline ExecutorStaticPtr createExecutor(const FunctorStaticPtr& functor, const std::string& name = "")
281  {
282  return std::make_shared<ExecutorStatic>(functor, name);
283  }
284 }
285 
286 #endif /* _Executor_H__ */
std::shared_ptr< ExecutorStatic > ExecutorStaticPtr
Definition: ExecutorPtr.h:56
FunctorPtr functor_
The underlying Functor that wraps a function.
Definition: Executor.h:186
std::shared_ptr< Functor > FunctorPtr
Definition: FunctorPtr.h:57
ExecutorMember(const FunctorMemberPtr< T > &functor, const std::string &name="")
Constructor: Initializes the parent class and the pointer to the member-functor.
Definition: Executor.h:216
MultiType operator()(T *object, const MultiType &arg1, const MultiType &arg2, const MultiType &arg3, const MultiType &arg4) const
Calls the wrapped function with 4 arguments and an object-pointer. If the function needs more argumen...
Definition: Executor.h:235
void error(const std::string &text)
Prints output with error level.
Definition: ConsoleCommandCompilation.cc:145
FunctorMemberPtr< T > functorMember_
A pointer to the FunctorMember is stored separately to avoid casting when executing the function...
Definition: Executor.h:263
Definition of orxonox::Functor and its specialized subclasses, as well as the createFunctor() functio...
MultiType operator()(const MultiType &arg1, const MultiType &arg2, const MultiType &arg3) const
Calls the wrapped function with 3 arguments. If the function needs more arguments, the executor&#39;s default values are used.
Definition: Executor.h:114
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
bool hasReturnvalue() const
Returns true if the wrapped function returns a value.
Definition: Executor.h:146
T * getObject() const
Returns the object-pointer of the underlying FunctorMember.
Definition: Executor.h:245
ExecutorPtr createExecutor(const FunctorPtr &functor, const std::string &name="")
Creates a new Executor that wraps a given Functor.
Definition: Executor.h:267
MultiType operator()(T *object, const MultiType &arg1) const
Calls the wrapped function with 1 argument and an object-pointer. If the function needs more argument...
Definition: Executor.h:226
MultiType operator()(const MultiType &arg1, const MultiType &arg2, const MultiType &arg3, const MultiType &arg4, const MultiType &arg5) const
Calls the wrapped function with 5 arguments. If the function needs more arguments, the executor&#39;s default values are used.
Definition: Executor.h:120
Type
Defines the type of a function (static or member)
Definition: Functor.h:179
Functor::Type getType() const
Returns the type of the wrapped function (static or member).
Definition: Executor.h:149
MultiType getDefaultValue(unsigned int index) const
Returns the default value for a parameter with given index (the first parameter has index 0)...
Definition: Executor.h:166
This class is used to wrap a Functor and to store default values for any of its parameters.
Definition: Executor.h:97
std::string getTypenameParam(unsigned int param) const
Returns the name of the type of a parameter with given index (the first parameter has index 0)...
Definition: Executor.h:152
A child class of Executor, used for easier handling of non-static member-functions.
Definition: Executor.h:212
A child class of Executor, used to distinguish executors that wrap static functions from executors th...
Definition: Executor.h:196
virtual ~ExecutorMember()
Destructor.
Definition: Executor.h:218
void setFunctor(const FunctorPtr &functor)
Changes the functor.
Definition: Executor.h:129
A class that splits a string into multiple tokens using different options.
Definition: SubString.h:101
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Declaration of the MultiType and some helper constructs.
MultiType parse(const std::string &arguments, int *error=nullptr, const std::string &delimiter=" ", bool bPrintError=false) const
Calls the wrapped function with arguments that are passed in a string.
Definition: Executor.cc:76
#define _CoreExport
Definition: CorePrereqs.h:61
ExecutorStatic(const FunctorStaticPtr &functor, const std::string &name="")
Constructor: Initializes the parent class.
Definition: Executor.h:200
The MultiType can hold a value of many possible types and convert them to other types.
Definition: MultiType.h:130
Typedefs and definitions of ExecutorPtr, ExecutorStaticPtr, and ExecutorMemberPtr.
void setObject(T *object) const
Changes the object-pointer of the underlying FunctorMember.
Definition: Executor.h:242
static const MultiType Null
Definition: MultiType.h:304
Definition: InputPrereqs.h:78
MultiType operator()(T *object) const
Calls the wrapped function with 0 arguments and an object-pointer. If the function needs more argumen...
Definition: Executor.h:223
MultiType operator()(T *object, const MultiType &arg1, const MultiType &arg2) const
Calls the wrapped function with 2 arguments and an object-pointer. If the function needs more argumen...
Definition: Executor.h:229
MultiType operator()(const MultiType &arg1, const MultiType &arg2) const
Calls the wrapped function with 2 arguments. If the function needs more arguments, the executor&#39;s default values are used.
Definition: Executor.h:111
std::shared_ptr< ExecutorMember< T >> ExecutorMemberPtr
Definition: ExecutorPtr.h:58
std::string name_
The name of the executor.
Definition: Executor.h:187
const unsigned int MAX_FUNCTOR_ARGUMENTS
The maximum number of parameters of a function that is supported by Functor.
Definition: Functor.h:127
std::shared_ptr< FunctorMember< void >> FunctorStaticPtr
Definition: FunctorPtr.h:60
std::shared_ptr< FunctorMember< T >> FunctorMemberPtr
Definition: FunctorPtr.h:59
void setName(const std::string &name)
Changes the name of the executor.
Definition: Executor.h:136
unsigned int getParamCount() const
Returns the number of parameters of the wrapped function.
Definition: Executor.h:143
std::string getTypenameReturnvalue() const
Returns the name of the type of the return value.
Definition: Executor.h:155
MultiType operator()(T *object, const MultiType &arg1, const MultiType &arg2, const MultiType &arg3) const
Calls the wrapped function with 3 arguments and an object-pointer. If the function needs more argumen...
Definition: Executor.h:232
MultiType parse(T *object, const std::string &arguments, int *error=nullptr, const std::string &delimiter=" ", bool bPrintError=false) const
Overloads Executor::parse() with an additional object-pointer.
Definition: Executor.h:251
bool defaultValueSet(unsigned int index) const
Returns true if the executor contains a default value for the parameter with given index (the first p...
Definition: Executor.h:177
MultiType operator()(const MultiType &arg1) const
Calls the wrapped function with 1 argument. If the function needs more arguments, the executor&#39;s defa...
Definition: Executor.h:108
const std::string & getName() const
Returns the name of the executor.
Definition: Executor.h:139
internal::String name_
Definition: gtest.cc:2289
const FunctorPtr & getFunctor() const
Returns the functor.
Definition: Executor.h:132
virtual ~ExecutorStatic()
Destructor.
Definition: Executor.h:202
std::shared_ptr< Executor > ExecutorPtr
Definition: ExecutorPtr.h:55
MultiType operator()() const
Calls the wrapped function with 0 arguments. If the function needs more arguments, the executor&#39;s default values are used.
Definition: Executor.h:105
MultiType operator()(const MultiType &arg1, const MultiType &arg2, const MultiType &arg3, const MultiType &arg4) const
Calls the wrapped function with 4 arguments. If the function needs more arguments, the executor&#39;s default values are used.
Definition: Executor.h:117
MultiType operator()(T *object, const MultiType &arg1, const MultiType &arg2, const MultiType &arg3, const MultiType &arg4, const MultiType &arg5) const
Calls the wrapped function with 5 arguments and an object-pointer. If the function needs more argumen...
Definition: Executor.h:238