Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/ConsoleCommand.h @ 7215

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

new console command interface now supports all functions of the old implementation

  • Property svn:eol-style set to native
File size: 24.5 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 */
28
29#ifndef _ConsoleCommand_H__
30#define _ConsoleCommand_H__
31
32#include "core/CorePrereqs.h"
33
34#include <stack>
35#include <boost/preprocessor/cat.hpp>
36#include <boost/preprocessor/facilities/expand.hpp>
37
38#include "util/VA_NARGS.h"
39#include "core/Identifier.h"
40#include "ArgumentCompletionFunctions.h"
41#include "CommandExecutor.h"
42#include "Executor.h"
43
44
45#define SetConsoleCommand(classname, function, bCreateShortcut) \
46    SetConsoleCommandGeneric(classname, function, #function, bCreateShortcut)
47#define SetConsoleCommandAlias(classname, function, name, bCreateShortcut) \
48    SetConsoleCommandGeneric(classname, function, name, bCreateShortcut)
49
50#define SetConsoleCommandGeneric(classname, function, name, bCreateShortcut) \
51    orxonox::ConsoleCommand& BOOST_PP_CAT(classname##function##consolecommand__, __LINE__) = orxonox::ClassIdentifier<classname>::getIdentifier(#classname)->addConsoleCommand(orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name), bCreateShortcut)
52
53
54#define SetConsoleCommandShortcut(classname, function) \
55    SetConsoleCommandShortcutAliasGeneric(classname, function, #function)
56#define SetConsoleCommandShortcutAlias(classname, function, name) \
57    SetConsoleCommandShortcutAliasGeneric(classname, function, name)
58#define SetConsoleCommandShortcutAliasGeneric(classname, function, name) \
59    SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name))
60
61#define SetConsoleCommandShortcutExtern(function) \
62    SetConsoleCommandShortcutExternAliasGeneric(function, #function)
63#define SetConsoleCommandShortcutExternAlias(function, name) \
64    SetConsoleCommandShortcutExternAliasGeneric(function, name)
65#define SetConsoleCommandShortcutExternAliasGeneric(function, name) \
66    SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&function), name))
67
68#define SetConsoleCommandShortcutGeneric(fakevariable, command) \
69    orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command, true)
70
71
72namespace orxonox
73{
74    namespace AccessLevel
75    {
76        enum Value
77        {
78            None,
79            User,
80            Admin,
81            Offline,
82            Debug,
83            Disabled
84        };
85    }
86
87    class _CoreExport ConsoleCommand : public Executor
88    {
89        public:
90            ConsoleCommand(const FunctorPtr& functor, const std::string& name = "");
91
92            ConsoleCommand& description(const std::string& description);
93            const std::string& getDescription() const;
94
95            ConsoleCommand& descriptionParam(unsigned int param, const std::string& description);
96            const std::string& getDescriptionParam(unsigned int param) const;
97
98            ConsoleCommand& descriptionReturnvalue(const std::string& description);
99            const std::string& getDescriptionReturnvalue(int param) const;
100
101            inline ConsoleCommand& defaultValues(const MultiType& param1)
102                { this->Executor::setDefaultValues(param1); return (*this); }
103            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2)
104                { this->Executor::setDefaultValues(param1, param2); return (*this); }
105            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
106                { this->Executor::setDefaultValues(param1, param2, param3); return (*this); }
107            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
108                { this->Executor::setDefaultValues(param1, param2, param3, param4); return (*this); }
109            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
110                { this->Executor::setDefaultValues(param1, param2, param3, param4, param5); return (*this); }
111            inline ConsoleCommand& defaultValue(unsigned int index, const MultiType& param)
112                { this->Executor::setDefaultValue(index, param); return (*this); }
113
114            inline ConsoleCommand& accessLevel(AccessLevel::Value level)
115                { this->accessLevel_ = level; return (*this); }
116            inline AccessLevel::Value getAccessLevel() const
117                { return this->accessLevel_; }
118
119            ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer);
120            ArgumentCompleter* getArgumentCompleter(unsigned int param) const;
121
122            void createArgumentCompletionList(unsigned int param, const std::string& param1 = "", const std::string& param2 = "", const std::string& param3 = "", const std::string& param4 = "", const std::string& param5 = "");
123            const ArgumentCompletionList& getArgumentCompletionList() const
124                { return this->argumentList_; }
125            ArgumentCompletionList::const_iterator getArgumentCompletionListBegin() const
126                { return this->argumentList_.begin(); }
127            ArgumentCompletionList::const_iterator getArgumentCompletionListEnd() const
128                { return this->argumentList_.end(); }
129
130            inline ConsoleCommand& setAsInputCommand()
131            {
132                this->keybindMode(KeybindMode::OnHold);
133                this->defaultValue(0, Vector2(0.0f, 0.0f));
134                this->inputConfiguredParam(0);
135                return *this;
136            }
137
138            inline ConsoleCommand& keybindMode(KeybindMode::Value mode)
139                { this->keybindMode_ = mode; return *this; }
140            inline KeybindMode::Value getKeybindMode() const
141                { return this->keybindMode_; }
142
143            inline ConsoleCommand& inputConfiguredParam(int index)
144                { this->inputConfiguredParam_ = index; return *this; }
145            inline int getInputConfiguredParam_() const
146                { return this->inputConfiguredParam_; }
147
148        private:
149            AccessLevel::Value accessLevel_;
150            ArgumentCompleter* argumentCompleter_[5];
151            ArgumentCompletionList argumentList_;
152
153            KeybindMode::Value keybindMode_;
154            int inputConfiguredParam_;
155
156            LanguageEntryLabel description_;
157            LanguageEntryLabel descriptionReturnvalue_;
158            LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];
159    };
160
161    inline ConsoleCommand* createConsoleCommand(const FunctorPtr& functor, const std::string& name = "")
162    {
163        return new ConsoleCommand(functor, name);
164    }
165}
166
167
168#define _SetConsoleCommand(...) \
169    BOOST_PP_EXPAND(BOOST_PP_CAT(_SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__))
170#define _SetConsoleCommand2(name, functionpointer) \
171    _SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer))
172#define _SetConsoleCommand3(group, name, functionpointer) \
173    _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer))
174#define _SetConsoleCommand4(group, name, functionpointer, object) \
175    _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object))
176
177#define _SetConsoleCommandGeneric(group, name, functor) \
178    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createExecutor(functor)))
179
180
181#define _DeclareConsoleCommand(...) \
182    BOOST_PP_CAT(_DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)
183#define _DeclareConsoleCommand2(name, functionpointer) \
184    _DeclareConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer))
185#define _DeclareConsoleCommand3(group, name, functionpointer) \
186    _DeclareConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer))
187#define _DeclareConsoleCommand4(group, name, functionpointer, object) \
188    _DeclareConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object))
189
190#define _DeclareConsoleCommandGeneric(group, name, functor) \
191    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = orxonox::_createConsoleCommand(group, name, orxonox::createExecutor(functor), false)
192
193
194namespace orxonox
195{
196    class _CoreExport _ConsoleCommand
197    {
198        friend struct _ConsoleCommandManipulator;
199
200        struct Command
201        {
202            ExecutorPtr executor_;
203            FunctorPtr functor_;
204        };
205
206        struct AccessLevel
207        {
208            enum Enum
209            {
210                All,
211                Standalone,
212                Master,
213                Server,
214                Client,
215                Online,
216                Offline,
217                None
218            };
219        };
220
221        public:
222            struct _ConsoleCommandManipulator
223            {
224                public:
225                    _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {}
226
227                    template <class F>
228                    inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
229                        {
230                            if (this->command_)
231                            {
232                                if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
233                                {
234                                    FunctorPointer<F>* functor = static_cast<FunctorPointer<F>*>(this->command_->getExecutor()->getFunctor().get());
235                                    functor->setFunction(function);
236                                    return *this;
237                                }
238                                this->command_->setFunction(createFunctor(function), bForce);
239                            }
240                            return *this;
241                        }
242                    template <class F, class O>
243                    inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false)
244                        {
245                            if (this->command_)
246                            {
247                                if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
248                                {
249                                    FunctorPointer<F, O>* functor = static_cast<FunctorPointer<F, O>*>(this->command_->getExecutor()->getFunctor().get());
250                                    functor->setFunction(function);
251                                    functor->setObject(object);
252                                    return *this;
253                                }
254                                this->command_->setFunction(createFunctor(function, object), bForce);
255                            }
256                            return *this;
257                        }
258                    inline _ConsoleCommandManipulator& setFunction(const FunctorPtr& functor, bool bForce = false)
259                        { if (this->command_) { this->command_->setFunction(functor, bForce); } return *this; }
260                    inline _ConsoleCommandManipulator& setFunction(const ExecutorPtr& executor, bool bForce = false)
261                        { if (this->command_) { this->command_->setFunction(executor, bForce); } return *this; }
262
263                    template <class F>
264                    inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
265                        { if (this->command_) { this->command_->pushFunction(createFunctor(function), bForce); } return *this; }
266                    template <class F, class O>
267                    inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false)
268                        { if (this->command_) { this->command_->pushFunction(createFunctor(function, object), bForce); } return *this; }
269                    inline _ConsoleCommandManipulator& pushFunction(const FunctorPtr& functor, bool bForce = false)
270                        { if (this->command_) { this->command_->pushFunction(functor, bForce); } return *this; }
271                    inline _ConsoleCommandManipulator& pushFunction(const ExecutorPtr& executor, bool bForce = false)
272                        { if (this->command_) { this->command_->pushFunction(executor, bForce); } return *this; }
273
274                    inline _ConsoleCommandManipulator& popFunction()
275                        { if (this->command_) { this->command_->popFunction(); } return *this; }
276
277                    inline _ConsoleCommandManipulator& setObject(void* object)
278                        { if (this->command_) { this->command_->setObject(object); } return *this; }
279                    inline _ConsoleCommandManipulator& pushObject(void* object)
280                        { if (this->command_) { this->command_->pushObject(object); } return *this; }
281                    inline _ConsoleCommandManipulator& popObject()
282                        { if (this->command_) { this->command_->popObject(); } return *this; }
283
284                    inline _ConsoleCommandManipulator& setActive(bool bActive)
285                        { if (this->command_) { this->command_->setActive(bActive); } return *this; }
286                    inline _ConsoleCommandManipulator& activate()
287                        { return this->setActive(true); }
288                    inline _ConsoleCommandManipulator& deactivate()
289                        { return this->setActive(false); }
290
291                    inline _ConsoleCommandManipulator& setHidden(bool bHidden)
292                        { if (this->command_) { this->command_->setHidden(bHidden); } return *this; }
293                    inline _ConsoleCommandManipulator& hide()
294                        { return this->setHidden(true); }
295                    inline _ConsoleCommandManipulator& show()
296                        { return this->setHidden(false); }
297
298                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1)
299                        { if (this->command_) { this->command_->defaultValues(param1); } return *this; }
300                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2)
301                        { if (this->command_) { this->command_->defaultValues(param1, param2); } return *this; }
302                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
303                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3); } return *this; }
304                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
305                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4); } return *this; }
306                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
307                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4, param5); } return *this; }
308                    inline _ConsoleCommandManipulator& defaultValue(unsigned int index, const MultiType& param)
309                        { if (this->command_) { this->command_->defaultValue(index, param); } return *this; }
310
311                    inline _ConsoleCommandManipulator& accessLevel(_ConsoleCommand::AccessLevel::Enum level)
312                        { if (this->command_) { this->command_->accessLevel(level); } return *this; }
313
314                    inline _ConsoleCommandManipulator& argumentCompleter(unsigned int param, ArgumentCompleter* completer)
315                        { if (this->command_) { this->command_->argumentCompleter(param, completer); } return *this; }
316
317                    inline _ConsoleCommandManipulator& setAsInputCommand()
318                        { if (this->command_) { this->command_->setAsInputCommand(); } return *this; }
319                    inline _ConsoleCommandManipulator& keybindMode(KeybindMode::Value mode)
320                        { if (this->command_) { this->command_->keybindMode(mode); } return *this; }
321                    inline _ConsoleCommandManipulator& inputConfiguredParam(int index)
322                        { if (this->command_) { this->command_->inputConfiguredParam(index); } return *this; }
323
324                private:
325                    _ConsoleCommand* command_;
326            };
327
328        public:
329            _ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true);
330            ~_ConsoleCommand();
331
332            _ConsoleCommand& addShortcut();
333            _ConsoleCommand& addShortcut(const std::string&  name);
334            _ConsoleCommand& addGroup(const std::string& group);
335            _ConsoleCommand& addGroup(const std::string& group, const std::string&  name);
336
337            inline _ConsoleCommand setActive(bool bActive)
338                { this->bActive_ = bActive; return *this; }
339            inline _ConsoleCommand activate()
340                { return this->setActive(true); }
341            inline _ConsoleCommand deactivate()
342                { return this->setActive(false); }
343
344            inline _ConsoleCommand& setHidden(bool bHidden)
345                { this->bHidden_ = bHidden; return *this; }
346            inline _ConsoleCommand& hide()
347                { return this->setHidden(true); }
348            inline _ConsoleCommand& show()
349                { return this->setHidden(false); }
350
351            bool isActive() const;
352            bool hasAccess() const;
353            bool isHidden() const;
354
355            _ConsoleCommand& description(const std::string& description);
356            const std::string& getDescription() const;
357
358            _ConsoleCommand& descriptionParam(unsigned int param, const std::string& description);
359            const std::string& getDescriptionParam(unsigned int param) const;
360
361            _ConsoleCommand& descriptionReturnvalue(const std::string& description);
362            const std::string& getDescriptionReturnvalue(int param) const;
363
364            _ConsoleCommand& defaultValues(const MultiType& param1);
365            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2);
366            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3);
367            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4);
368            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5);
369            _ConsoleCommand& defaultValue(unsigned int index, const MultiType& param);
370
371            inline _ConsoleCommand& accessLevel(AccessLevel::Enum level)
372                { this->accessLevel_ = level; return *this; }
373            inline AccessLevel::Enum getAccessLevel() const
374                { return this->accessLevel_; }
375
376            _ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer);
377            ArgumentCompleter* getArgumentCompleter(unsigned int param) const;
378
379            void createArgumentCompletionList(unsigned int param, const std::string& param1 = "", const std::string& param2 = "", const std::string& param3 = "", const std::string& param4 = "", const std::string& param5 = "");
380            const ArgumentCompletionList& getArgumentCompletionList() const
381                { return this->argumentList_; }
382            ArgumentCompletionList::const_iterator getArgumentCompletionListBegin() const
383                { return this->argumentList_.begin(); }
384            ArgumentCompletionList::const_iterator getArgumentCompletionListEnd() const
385                { return this->argumentList_.end(); }
386
387            inline _ConsoleCommand& setAsInputCommand()
388            {
389                this->keybindMode(KeybindMode::OnHold);
390                this->defaultValue(0, Vector2(0.0f, 0.0f));
391                this->inputConfiguredParam(0);
392                return *this;
393            }
394
395            inline _ConsoleCommand& keybindMode(KeybindMode::Value mode)
396                { this->keybindMode_ = mode; return *this; }
397            inline KeybindMode::Value getKeybindMode() const
398                { return this->keybindMode_; }
399
400            inline _ConsoleCommand& inputConfiguredParam(int index)
401                { this->inputConfiguredParam_ = index; return *this; }
402            inline int getInputConfiguredParam_() const
403                { return this->inputConfiguredParam_; }
404
405            inline _ConsoleCommandManipulator getManipulator() const
406                { return this; }
407
408        private:
409            bool headersMatch(const FunctorPtr& functor);
410            bool headersMatch(const ExecutorPtr& executor);
411
412            bool setFunction(const ExecutorPtr& executor, bool bForce = false);
413            bool setFunction(const FunctorPtr& functor, bool bForce = false);
414            void pushFunction(const ExecutorPtr& executor, bool bForce = false);
415            void pushFunction(const FunctorPtr& functor, bool bForce = false);
416            void pushFunction();
417            void popFunction();
418            const ExecutorPtr& getExecutor() const;
419            const FunctorPtr& getFunctor() const;
420
421            bool setObject(void* object);
422            void pushObject(void* object);
423            void popObject();
424            void* getObject() const;
425
426            bool bActive_;
427            bool bHidden_;
428            AccessLevel::Enum accessLevel_;
429            std::string baseName_;
430            ExecutorPtr baseExecutor_;
431
432            ExecutorPtr executor_;
433            std::stack<Command> commandStack_;
434            std::stack<void*> objectStack_;
435
436            ArgumentCompleter* argumentCompleter_[5];
437            ArgumentCompletionList argumentList_;
438
439            KeybindMode::Value keybindMode_;
440            int inputConfiguredParam_;
441
442            LanguageEntryLabel description_;
443            LanguageEntryLabel descriptionReturnvalue_;
444            LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];
445
446        public:
447            static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
448                { return _ConsoleCommand::getCommandMap(); }
449
450            static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
451                { return _ConsoleCommand::getCommand("", name, bPrintError); }
452            static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
453
454        private:
455            static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
456            static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
457            static void unregisterCommand(_ConsoleCommand* command);
458    };
459
460    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, const ExecutorPtr& executor, bool bInitialized = true)
461        { return new _ConsoleCommand("", name, executor, bInitialized); }
462    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true)
463        { return new _ConsoleCommand(group, name, executor, bInitialized); }
464
465    inline _ConsoleCommand::_ConsoleCommandManipulator _ModifyConsoleCommand(const std::string& name)
466        { return _ConsoleCommand::getCommand(name, true); }
467    inline _ConsoleCommand::_ConsoleCommandManipulator _ModifyConsoleCommand(const std::string& group, const std::string& name)
468        { return _ConsoleCommand::getCommand(group, name, true); }
469}
470
471#endif /* _ConsoleCommand_H__ */
Note: See TracBrowser for help on using the repository browser.