Orxonox  0.0.5 Codename: Arcturus
ConsoleCommand.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  */
28 
35 #ifndef _ConsoleCommand_H__
36 #define _ConsoleCommand_H__
37 
38 #include "core/CorePrereqs.h"
39 
40 #include <stack>
41 #include <vector>
42 
44 #include "Executor.h"
45 
46 namespace orxonox
47 {
52  namespace prototype
53  {
54  inline void void__void(void) {}
55  inline void void__string(const std::string&) {}
56 
57  inline std::string string__bool(bool) { return ""; }
58  inline std::string string__string(const std::string&) { return ""; }
59  inline std::string string__uint_uint_bool(unsigned int, unsigned int, bool) { return ""; }
60  }
61 
65  enum class AccessLevel
66  {
67  All,
68  Standalone,
69  Master,
70  Server,
71  Client,
72  Online,
73  Offline,
74  None
75  };
76 
89  {
91 
95  struct Command
96  {
99  std::vector<void*> objectStack_;
100  };
101 
102  public:
106  struct CommandName
107  {
108  CommandName(const std::string& group, const std::string& name) : group_(group), name_(name) {}
111  };
112 
125  {
126  public:
129 
131  template <class F>
132  inline ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
133  {
134  if (this->command_)
135  {
136  // check if the headers match. If they do, only change the function-pointer of the current Functor instead of creating a new Functor
137  if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
138  {
139  FunctorPointer<F>* functor = static_cast<FunctorPointer<F>*>(this->command_->getExecutor()->getFunctor().get());
140  functor->setFunction(function);
141  return *this;
142  }
143  this->command_->setFunction(createFunctor(function), bForce);
144  }
145  return *this;
146  }
148  template <class F, class O>
149  inline ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false)
150  {
151  if (this->command_)
152  {
153  // check if the headers match. If they do, only change the function-pointer of the current Functor instead of creating a new Functor
154  if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
155  {
156  FunctorPointer<F, O>* functor = static_cast<FunctorPointer<F, O>*>(this->command_->getExecutor()->getFunctor().get());
157  functor->setFunction(function);
158  functor->setObject(object);
159  return *this;
160  }
161  this->command_->setFunction(createFunctor(function, object), bForce);
162  }
163  return *this;
164  }
166  inline ConsoleCommandManipulator& setFunction(const FunctorPtr& functor, bool bForce = false)
167  { if (this->command_) { this->command_->setFunction(functor, bForce); } return *this; }
169  inline ConsoleCommandManipulator& setFunction(const ExecutorPtr& executor, bool bForce = false)
170  { if (this->command_) { this->command_->setFunction(executor, bForce); } return *this; }
171 
174  { if (this->command_) { this->command_->pushFunction(); } return *this; }
176  template <class F>
177  inline ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
178  { if (this->command_) { this->command_->pushFunction(createFunctor(function), bForce); } return *this; }
180  template <class F, class O>
181  inline ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false)
182  { if (this->command_) { this->command_->pushFunction(createFunctor(function, object), bForce); } return *this; }
184  inline ConsoleCommandManipulator& pushFunction(const FunctorPtr& functor, bool bForce = false)
185  { if (this->command_) { this->command_->pushFunction(functor, bForce); } return *this; }
187  inline ConsoleCommandManipulator& pushFunction(const ExecutorPtr& executor, bool bForce = false)
188  { if (this->command_) { this->command_->pushFunction(executor, bForce); } return *this; }
189 
192  { if (this->command_) { this->command_->popFunction(); } return *this; }
193 
196  { if (this->command_) { this->command_->resetFunction(); } return *this; }
197 
199  inline ConsoleCommandManipulator& setObject(void* object)
200  { if (this->command_) { this->command_->setObject(object); } return *this; }
202  inline ConsoleCommandManipulator& pushObject(void* object)
203  { if (this->command_) { this->command_->pushObject(object); } return *this; }
206  { if (this->command_) { this->command_->popObject(); } return *this; }
207 
209  inline ConsoleCommandManipulator& setActive(bool bActive)
210  { if (this->command_) { this->command_->setActive(bActive); } return *this; }
213  { return this->setActive(true); }
216  { return this->setActive(false); }
217 
219  inline ConsoleCommandManipulator& setHidden(bool bHidden)
220  { if (this->command_) { this->command_->setHidden(bHidden); } return *this; }
223  { return this->setHidden(true); }
226  { return this->setHidden(false); }
227 
230  { if (this->command_) { this->command_->defaultValues(arg1); } return *this; }
232  inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2)
233  { if (this->command_) { this->command_->defaultValues(arg1, arg2); } return *this; }
235  inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3)
236  { if (this->command_) { this->command_->defaultValues(arg1, arg2, arg3); } return *this; }
238  inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4)
239  { if (this->command_) { this->command_->defaultValues(arg1, arg2, arg3, arg4); } return *this; }
241  inline ConsoleCommandManipulator& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5)
242  { if (this->command_) { this->command_->defaultValues(arg1, arg2, arg3, arg4, arg5); } return *this; }
244  inline ConsoleCommandManipulator& defaultValue(unsigned int index, const MultiType& arg)
245  { if (this->command_) { this->command_->defaultValue(index, arg); } return *this; }
246 
249  { if (this->command_) { this->command_->accessLevel(level); } return *this; }
250 
252  inline ConsoleCommandManipulator& argumentCompleter(unsigned int index, ArgumentCompleter* completer)
253  { if (this->command_) { this->command_->argumentCompleter(index, completer); } return *this; }
254 
257  { if (this->command_) { this->command_->setAsInputCommand(); } return *this; }
260  { if (this->command_) { this->command_->changeKeybindMode(mode); } return *this; }
263  { if (this->command_) { this->command_->inputConfiguredParam(index); } return *this; }
264 
265  private:
267  };
268 
269  public:
270  ConsoleCommand(const std::string& name, const ExecutorPtr& executor, bool bInitialized = true);
271  ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true);
272  ~ConsoleCommand();
273 
274  ConsoleCommand& addShortcut();
275  ConsoleCommand& addShortcut(const std::string& name);
276  ConsoleCommand& addGroup(const std::string& group);
277  ConsoleCommand& addGroup(const std::string& group, const std::string& name);
278 
280  inline const std::string& getName() const
281  { return this->baseName_; }
282 
283  const ExecutorPtr& getExecutor() const;
285  inline const FunctorPtr& getBaseFunctor() const
286  { return this->baseFunctor_; }
287 
289  inline ConsoleCommand& setActive(bool bActive)
290  { this->bActive_ = bActive; return *this; }
293  { return this->setActive(true); }
296  { return this->setActive(false); }
297 
299  inline ConsoleCommand& setHidden(bool bHidden)
300  { this->bHidden_ = bHidden; return *this; }
303  { return this->setHidden(true); }
306  { return this->setHidden(false); }
307 
308  bool isActive() const;
309  bool hasAccess() const;
311  inline bool isHidden() const
312  { return this->bHidden_; }
313 
314  ConsoleCommand& description(const std::string& description);
315  const std::string& getDescription() const;
316 
317  ConsoleCommand& descriptionParam(unsigned int index, const std::string& description);
318  const std::string& getDescriptionParam(unsigned int index) const;
319 
320  ConsoleCommand& descriptionReturnvalue(const std::string& description);
321  const std::string& getDescriptionReturnvalue(int index) const;
322 
323  ConsoleCommand& defaultValues(const MultiType& arg1);
324  ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2);
325  ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3);
326  ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4);
327  ConsoleCommand& defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5);
328  ConsoleCommand& defaultValue(unsigned int index, const MultiType& arg);
329 
332  { this->accessLevel_ = level; return *this; }
335  { return this->accessLevel_; }
336 
337  ConsoleCommand& argumentCompleter(unsigned int index, ArgumentCompleter* completer);
338  ArgumentCompleter* getArgumentCompleter(unsigned int index) const;
339 
342  {
343  this->keybindMode(KeybindMode::OnHold);
344  this->defaultValue(0, Vector2(0.0f, 0.0f));
345  this->inputConfiguredParam(0);
346  return *this;
347  }
348 
351  { this->keybindMode_ = mode; return *this; }
354  { return this->keybindMode_; }
355 
356  ConsoleCommand& changeKeybindMode(KeybindMode::Value mode);
357 
360  { this->inputConfiguredParam_ = index; return *this; }
362  inline int getInputConfiguredParam() const
363  { return this->inputConfiguredParam_; }
364 
367  { return this; }
368 
369  inline const std::vector<CommandName>& getNames()
370  { return this->names_; }
371 
372  private:
373  void init(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized);
374 
375  bool headersMatch(const FunctorPtr& functor);
376  bool headersMatch(const ExecutorPtr& executor);
377 
378  bool setFunction(const ExecutorPtr& executor, bool bForce = false);
379  bool setFunction(const FunctorPtr& functor, bool bForce = false);
380  void pushFunction(const ExecutorPtr& executor, bool bForce = false);
381  void pushFunction(const FunctorPtr& functor, bool bForce = false);
382  void pushFunction();
383  void popFunction();
384  void resetFunction();
385 
386  bool setObject(void* object);
387  void pushObject(void* object);
388  void popObject();
389  void* getObject() const;
390 
391  bool bActive_;
392  bool bHidden_;
395  std::vector<CommandName> names_;
397 
399  std::stack<Command> commandStack_;
400  std::vector<void*> objectStack_;
401 
403 
406 
410  };
411 }
412 
413 #endif /* _ConsoleCommand_H__ */
std::shared_ptr< Functor > FunctorPtr
Definition: FunctorPtr.h:57
ConsoleCommandManipulator & argumentCompleter(unsigned int index, ArgumentCompleter *completer)
Changes the argument completer for the given parameter.
Definition: ConsoleCommand.h:252
Declaration of all argument completion functions and macros used to define them.
Definition: InputPrereqs.h:82
FunctorPtr baseFunctor_
The functor that defines the header of the command-function.
Definition: ConsoleCommand.h:396
The ConsoleCommand class stores all information about a console command which can be executed by Comm...
Definition: ConsoleCommand.h:88
ConsoleCommandManipulator & setFunction(F function, bool bForce=false)
Changes the current function of the command.
Definition: ConsoleCommand.h:132
const std::string & getName() const
Returns the name that was first used for this command.
Definition: ConsoleCommand.h:280
ConsoleCommandManipulator & defaultValues(const MultiType &arg1, const MultiType &arg2)
Changes the default values of the current executor (doesn&#39;t modify executors on deeper levels of the ...
Definition: ConsoleCommand.h:232
ConsoleCommand & activate()
Activates the command.
Definition: ConsoleCommand.h:292
ConsoleCommand & inputConfiguredParam(int index)
Changes the input configured param to the given index.
Definition: ConsoleCommand.h:359
ConsoleCommandManipulator & pushFunction()
Pushes a copy of the current Executor on the command-stack, that can be altered without changing the ...
Definition: ConsoleCommand.h:173
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
std::string string__bool(bool)
Definition: ConsoleCommand.h:57
std::vector< CommandName > names_
All names and aliases of this command.
Definition: ConsoleCommand.h:395
KeybindMode::Value getKeybindMode() const
Returns the keybind mode.
Definition: ConsoleCommand.h:353
std::string group_
Definition: ConsoleCommand.h:109
ConsoleCommandManipulator & setHidden(bool bHidden)
Changes the visibility of the command.
Definition: ConsoleCommand.h:219
std::string name_
Definition: ConsoleCommand.h:110
std::stack< Command > commandStack_
A stack of commands, used to push and pop different functions.
Definition: ConsoleCommand.h:399
Helper class that is used to put the current state of the ConsoleCommand on a stack.
Definition: ConsoleCommand.h:95
ConsoleCommandManipulator & defaultValues(const MultiType &arg1)
Changes the default values of the current executor (doesn&#39;t modify executors on deeper levels of the ...
Definition: ConsoleCommand.h:229
FunctorPtr functor_
The function that is used with the executor - has to be stored separatley because the executor is oft...
Definition: ConsoleCommand.h:98
LanguageEntryLabel description_
The description of the command.
Definition: ConsoleCommand.h:407
Client *client; The network/Client class This class implements all necessary function for the network...
Definition: Client.h:68
ConsoleCommandManipulator & resetFunction()
Sets the current function-pointer to nullptr, which also deactivates the command. ...
Definition: ConsoleCommand.h:195
ConsoleCommandManipulator & keybindMode(KeybindMode::Value mode)
Changes the keybind mode of the command.
Definition: ConsoleCommand.h:259
const FunctorPtr & getBaseFunctor() const
Returns the functor that defines the required header for this command (but isn&#39;t necessarily executed...
Definition: ConsoleCommand.h:285
ConsoleCommandManipulator & setFunction(const FunctorPtr &functor, bool bForce=false)
Changes the current Functor of the command.
Definition: ConsoleCommand.h:166
Definition: InputPrereqs.h:91
ExecutorPtr executor_
The executor.
Definition: ConsoleCommand.h:97
ConsoleCommandManipulator & setFunction(F function, O *object, bool bForce=false)
Changes the current function of the command.
Definition: ConsoleCommand.h:149
ConsoleCommandManipulator & defaultValues(const MultiType &arg1, const MultiType &arg2, const MultiType &arg3)
Changes the default values of the current executor (doesn&#39;t modify executors on deeper levels of the ...
Definition: ConsoleCommand.h:235
FunctorMemberPtr< O > createFunctor(R(O::*functionPointer)(Params...), OO *object)
Creates a new FunctorMember with the given function-pointer and an assigned object.
Definition: Functor.h:583
bool bActive_
True if the command should be active (it can still be inactive, for example if the function is missin...
Definition: ConsoleCommand.h:391
ConsoleCommandManipulator & popObject()
Removes the current object from the object-stack and restores the old object (or nullptr if there&#39;s n...
Definition: ConsoleCommand.h:205
const std::vector< CommandName > & getNames()
Definition: ConsoleCommand.h:369
Defines the name of a command, consisting of an optional group ("" means no group) and the name itsel...
Definition: ConsoleCommand.h:106
int inputConfiguredParam_
The input configured param.
Definition: ConsoleCommand.h:405
ConsoleCommand & setActive(bool bActive)
Changes the activity of the command.
Definition: ConsoleCommand.h:289
ConsoleCommandManipulator & deactivate()
Deactivates the command.
Definition: ConsoleCommand.h:215
false defaultValue(2, false).argumentCompleter(0
AccessLevel
Possible access levels: A command can only be executed if the program is in the state which is reques...
Definition: ConsoleCommand.h:65
Definition: CorePrereqs.h:127
ConsoleCommand & setAsInputCommand()
Defines the command to be an input command.
Definition: ConsoleCommand.h:341
std::vector< void * > objectStack_
A stack of objects, used to push and pop different objects for a function.
Definition: ConsoleCommand.h:400
AccessLevel accessLevel_
The access level (the state of the game in which you can access the command)
Definition: ConsoleCommand.h:393
Declaration of the orxonox::Executor class and the createExecutor() functions.
ConsoleCommandManipulator & pushObject(void *object)
Pushes a new object on the object-stack.
Definition: ConsoleCommand.h:202
ConsoleCommandManipulator & activate()
Activates the command.
Definition: ConsoleCommand.h:212
int getInputConfiguredParam() const
Returns the input configured param.
Definition: ConsoleCommand.h:362
ConsoleCommand & show()
Makes the command visible.
Definition: ConsoleCommand.h:305
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
bool bHidden_
True if the command is hidden (it is still executable, but not visible in the list of available comma...
Definition: ConsoleCommand.h:392
ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI() command(const std::string &fragment)
Returns a list of commands and groups and also supports auto-completion of the arguments of these com...
Definition: ArgumentCompletionFunctions.cc:178
Value
Definition: CorePrereqs.h:124
#define _CoreExport
Definition: CorePrereqs.h:61
ConsoleCommandManipulator getManipulator()
Returns a manipulator for this command.
Definition: ConsoleCommand.h:366
The MultiType can hold a value of many possible types and convert them to other types.
Definition: MultiType.h:130
This class is the root class of the network module for a server.
Definition: Server.h:52
CommandName(const std::string &group, const std::string &name)
Definition: ConsoleCommand.h:108
std::vector< void * > objectStack_
The object stack.
Definition: ConsoleCommand.h:99
ConsoleCommandManipulator & defaultValues(const MultiType &arg1, const MultiType &arg2, const MultiType &arg3, const MultiType &arg4, const MultiType &arg5)
Changes the default values of the current executor (doesn&#39;t modify executors on deeper levels of the ...
Definition: ConsoleCommand.h:241
ConsoleCommand & accessLevel(AccessLevel level)
Changes the access level of the command.
Definition: ConsoleCommand.h:331
ConsoleCommandManipulator(ConsoleCommand *command)
Constructor: Creates a manipulator for a given ConsoleCommand.
Definition: ConsoleCommand.h:128
ConsoleCommand * command_
The command which is being manipulated by this object.
Definition: ConsoleCommand.h:266
void void__string(const std::string &)
Definition: ConsoleCommand.h:55
std::string string__uint_uint_bool(unsigned int, unsigned int, bool)
Definition: ConsoleCommand.h:59
bool isHidden() const
Returns true if the command is currently hidden.
Definition: ConsoleCommand.h:311
ConsoleCommandManipulator & pushFunction(const FunctorPtr &functor, bool bForce=false)
Pushes a new Functor on the command-stack.
Definition: ConsoleCommand.h:184
ConsoleCommandManipulator & setFunction(const ExecutorPtr &executor, bool bForce=false)
Changes the current Executor of the command.
Definition: ConsoleCommand.h:169
ConsoleCommandManipulator & inputConfiguredParam(int index)
Sets the input configured param to the given index.
Definition: ConsoleCommand.h:262
ConsoleCommandManipulator & defaultValue(unsigned int index, const MultiType &arg)
Changes the default value of the argument with given index of the current executor (doesn&#39;t modify ex...
Definition: ConsoleCommand.h:244
ConsoleCommandManipulator & show()
Makes the command visible.
Definition: ConsoleCommand.h:225
const unsigned int MAX_FUNCTOR_ARGUMENTS
The maximum number of parameters of a function that is supported by Functor.
Definition: Functor.h:127
ConsoleCommandManipulator & setObject(void *object)
Changes the current object (used for member-functions).
Definition: ConsoleCommand.h:199
ConsoleCommandManipulator & defaultValues(const MultiType &arg1, const MultiType &arg2, const MultiType &arg3, const MultiType &arg4)
Changes the default values of the current executor (doesn&#39;t modify executors on deeper levels of the ...
Definition: ConsoleCommand.h:238
ConsoleCommandManipulator & setActive(bool bActive)
Changes the activity of the command.
Definition: ConsoleCommand.h:209
KeybindMode::Value keybindMode_
The keybind mode.
Definition: ConsoleCommand.h:404
std::string string__string(const std::string &)
Definition: ConsoleCommand.h:58
ConsoleCommand & setHidden(bool bHidden)
Changes the visibility of the command.
Definition: ConsoleCommand.h:299
ConsoleCommand & deactivate()
Deactivates the command.
Definition: ConsoleCommand.h:295
ConsoleCommandManipulator & accessLevel(AccessLevel level)
Changes the access level of the command.
Definition: ConsoleCommand.h:248
ConsoleCommand & keybindMode(KeybindMode::Value mode)
Sets the keybind mode. Note: use changeKeybindMode if you intend to change the mode.
Definition: ConsoleCommand.h:350
ConsoleCommandManipulator & hide()
Hides the command (can still be executed, but is not visible in the list of available commands)...
Definition: ConsoleCommand.h:222
ExecutorPtr executor_
The Executor that is used to execute the command.
Definition: ConsoleCommand.h:398
ConsoleCommandManipulator & popFunction()
Removes the current function from the stack and restores the old state. If there&#39;s no other function ...
Definition: ConsoleCommand.h:191
std::string baseName_
The name that was first assigned to the command.
Definition: ConsoleCommand.h:394
std::string LanguageEntryLabel
Definition: CorePrereqs.h:141
LanguageEntryLabel descriptionReturnvalue_
A description of the return-value.
Definition: ConsoleCommand.h:408
void void__void(void)
Definition: ConsoleCommand.h:54
ConsoleCommandManipulator & pushFunction(const ExecutorPtr &executor, bool bForce=false)
Pushes a new Executor on the command-stack.
Definition: ConsoleCommand.h:187
void setObject(O *object)
Assigns an object-pointer to the functor which is used to execute a member-function.
Definition: Functor.h:265
ConsoleCommand & hide()
Hides the command (can still be executed, but is not visible in the list of available commands)...
Definition: ConsoleCommand.h:302
internal::String name_
Definition: gtest.cc:2289
std::shared_ptr< Executor > ExecutorPtr
Definition: ExecutorPtr.h:55
void setFunction(F functionPointer)
Changes the function-pointer.
Definition: Functor.h:376
AccessLevel getAccessLevel() const
Returns the access level of the command.
Definition: ConsoleCommand.h:334
FunctorPointer is a child class of FunctorMember and extends it with a function-pointer (or a functio...
Definition: Functor.h:369
ConsoleCommandManipulator & setAsInputCommand()
Defines the command to be an input command.
Definition: ConsoleCommand.h:256
This class executes an argument completion function and returns a list of the possible arguments...
Definition: ArgumentCompleter.h:70
ConsoleCommandManipulator & pushFunction(F function, O *object, bool bForce=false)
Pushes a new function on the command-stack.
Definition: ConsoleCommand.h:181
ConsoleCommandManipulator & pushFunction(F function, bool bForce=false)
Pushes a new function on the command-stack.
Definition: ConsoleCommand.h:177
Helper class that is used to manipulate console commands.
Definition: ConsoleCommand.h:124