Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

continued working on the new console command interface and implementation

  • Property svn:eol-style set to native
File size: 14.3 KB
RevLine 
[1505]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 "CorePrereqs.h"
33
[7185]34#include <stack>
[3304]35#include <boost/preprocessor/cat.hpp>
[7179]36#include <boost/preprocessor/facilities/expand.hpp>
[3304]37
[7179]38#include "util/VA_NARGS.h"
[3196]39#include "ArgumentCompletionFunctions.h"
40#include "CommandExecutor.h"
[1505]41#include "Executor.h"
[1549]42#include "Identifier.h"
[1505]43
44
[1747]45#define SetConsoleCommand(classname, function, bCreateShortcut) \
[3304]46    SetConsoleCommandGeneric(classname, function, #function, bCreateShortcut)
[1747]47#define SetConsoleCommandAlias(classname, function, name, bCreateShortcut) \
[3304]48    SetConsoleCommandGeneric(classname, function, name, bCreateShortcut)
[1505]49
[3304]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)
[1505]52
53
54#define SetConsoleCommandShortcut(classname, function) \
[3304]55    SetConsoleCommandShortcutAliasGeneric(classname, function, #function)
[1747]56#define SetConsoleCommandShortcutAlias(classname, function, name) \
[3304]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))
[1505]60
61#define SetConsoleCommandShortcutExtern(function) \
[3304]62    SetConsoleCommandShortcutExternAliasGeneric(function, #function)
[1747]63#define SetConsoleCommandShortcutExternAlias(function, name) \
[3304]64    SetConsoleCommandShortcutExternAliasGeneric(function, name)
65#define SetConsoleCommandShortcutExternAliasGeneric(function, name) \
66    SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&function), name))
[1505]67
68#define SetConsoleCommandShortcutGeneric(fakevariable, command) \
[2662]69    orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command, true)
[1505]70
71
72namespace orxonox
73{
74    namespace AccessLevel
75    {
[3280]76        enum Value
[1505]77        {
78            None,
79            User,
80            Admin,
81            Offline,
82            Debug,
83            Disabled
84        };
85    }
86
[1755]87    class _CoreExport ConsoleCommand : public Executor
[1505]88    {
89        public:
[1755]90            ConsoleCommand(Functor* functor, const std::string& name = "");
[1505]91
[1747]92            inline ConsoleCommand& description(const std::string& description)
[1755]93                { this->Executor::setDescription(description); return (*this); }
[1747]94            inline ConsoleCommand& descriptionParam(int param, const std::string& description)
[1755]95                { this->Executor::setDescriptionParam(param, description); return (*this); }
[1747]96            inline ConsoleCommand& descriptionReturnvalue(const std::string& description)
[1755]97                { this->Executor::setDescriptionReturnvalue(description); return (*this); }
[1747]98            inline ConsoleCommand& defaultValues(const MultiType& param1)
[1755]99                { this->Executor::setDefaultValues(param1); return (*this); }
[1747]100            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2)
[1755]101                { this->Executor::setDefaultValues(param1, param2); return (*this); }
[1747]102            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
[1755]103                { this->Executor::setDefaultValues(param1, param2, param3); return (*this); }
[1747]104            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
[1755]105                { this->Executor::setDefaultValues(param1, param2, param3, param4); return (*this); }
[1747]106            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
[1755]107                { this->Executor::setDefaultValues(param1, param2, param3, param4, param5); return (*this); }
[1747]108            inline ConsoleCommand& defaultValue(unsigned int index, const MultiType& param)
[1755]109                { this->Executor::setDefaultValue(index, param); return (*this); }
[1505]110
[3280]111            inline ConsoleCommand& accessLevel(AccessLevel::Value level)
[1505]112                { this->accessLevel_ = level; return (*this); }
[3280]113            inline AccessLevel::Value getAccessLevel() const
[1505]114                { return this->accessLevel_; }
115
[1747]116            ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer);
[1505]117            ArgumentCompleter* getArgumentCompleter(unsigned int param) const;
118
119            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 = "");
120            const ArgumentCompletionList& getArgumentCompletionList() const
121                { return this->argumentList_; }
122            ArgumentCompletionList::const_iterator getArgumentCompletionListBegin() const
123                { return this->argumentList_.begin(); }
124            ArgumentCompletionList::const_iterator getArgumentCompletionListEnd() const
125                { return this->argumentList_.end(); }
126
[2087]127            inline ConsoleCommand& setAsInputCommand()
128            {
129                this->keybindMode(KeybindMode::OnHold);
130                this->defaultValue(0, Vector2(0.0f, 0.0f));
131                this->inputConfiguredParam(0);
132                return *this;
133            }
134
[3280]135            inline ConsoleCommand& keybindMode(KeybindMode::Value mode)
[1505]136                { this->keybindMode_ = mode; return *this; }
[3280]137            inline KeybindMode::Value getKeybindMode() const
[1505]138                { return this->keybindMode_; }
139
[2087]140            inline ConsoleCommand& inputConfiguredParam(int index)
141                { this->inputConfiguredParam_ = index; return *this; }
142            inline int getInputConfiguredParam_() const
143                { return this->inputConfiguredParam_; }
[1505]144
145        private:
[3280]146            AccessLevel::Value accessLevel_;
[1505]147            ArgumentCompleter* argumentCompleter_[5];
148            ArgumentCompletionList argumentList_;
149
[3280]150            KeybindMode::Value keybindMode_;
[2087]151            int inputConfiguredParam_;
[1505]152    };
153
[1755]154    inline ConsoleCommand* createConsoleCommand(Functor* functor, const std::string& name = "")
[1505]155    {
156        return new ConsoleCommand(functor, name);
157    }
158}
159
[7179]160
161#define _SetConsoleCommand(...) \
162    BOOST_PP_EXPAND(BOOST_PP_CAT(_SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__))
163#define _SetConsoleCommand2(name, functionpointer) \
164    _SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer))
165#define _SetConsoleCommand3(group, name, functionpointer) \
166    _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer))
167#define _SetConsoleCommand4(group, name, functionpointer, object) \
168    _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object))
169
170#define _SetConsoleCommandGeneric(group, name, functor) \
171    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, functor))
172
173
174#define _DeclareConsoleCommand(...) \
175    BOOST_PP_CAT(_DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)
176#define _DeclareConsoleCommand2(name, functionpointer) \
177    _DeclareConsoleCommandGeneric("", name, functionpointer)
178#define _DeclareConsoleCommand3(group, name, functionpointer) \
179    _DeclareConsoleCommandGeneric(group, name, functionpointer)
180
181#define _DeclareConsoleCommandGeneric(group, name, functionpointer) \
[7185]182    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), false))
[7179]183
184
185#define _ModifyConsoleCommand(...) \
186    orxonox::_ConsoleCommand::getCommand(__VA_ARGS__, true)->getManipulator()
187
188
189namespace orxonox
190{
191    class _CoreExport _ConsoleCommand : protected Executor
192    {
193        friend struct _ConsoleCommandManipulator;
194
195        public:
196            struct _ConsoleCommandManipulator
197            {
198                public:
199                    _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {}
200
201                    template <class F>
[7185]202                    inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
203                        { if (this->command_) { this->command_->setFunctor(createFunctor(function), bForce); } return *this; }
[7179]204                    template <class F, class O>
[7185]205                    inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false)
206                        { if (this->command_) { this->command_->setFunctor(createFunctor(function, object), bForce); } return *this; }
207                    inline _ConsoleCommandManipulator& setFunction(Functor* functor, bool bForce = false)
208                        { if (this->command_) { this->command_->setFunctor(functor, bForce); } return *this; }
[7179]209
210                    template <class F>
[7185]211                    inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
212                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function), bForce); } return *this; }
[7179]213                    template <class F, class O>
[7185]214                    inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false)
215                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object), bForce); } return *this; }
216                    inline _ConsoleCommandManipulator& pushFunction(Functor* functor, bool bForce = false)
217                        { if (this->command_) { this->command_->pushFunctor(functor, bForce); } return *this; }
[7179]218
219                    inline _ConsoleCommandManipulator& popFunction()
220                        { if (this->command_) { this->command_->popFunctor(); } return *this; }
221
222                    inline _ConsoleCommandManipulator& setObject(void* object)
223                        { if (this->command_) { this->command_->setObject(object); } return *this; }
[7185]224                    inline _ConsoleCommandManipulator& pushObject(void* object)
225                        { if (this->command_) { this->command_->pushObject(object); } return *this; }
226                    inline _ConsoleCommandManipulator& popObject()
227                        { if (this->command_) { this->command_->popObject(); } return *this; }
[7179]228
[7185]229                    inline void* getObject() const
230                        { if (this->command_) { return this->command_->getObject(); } else { return 0; } }
231
[7179]232                    inline _ConsoleCommandManipulator& setActive(bool bActive)
233                        { if (this->command_) { this->command_->setActive(bActive); } return *this; }
234
235                private:
236                    _ConsoleCommand* command_;
237            };
238
239        public:
[7185]240            _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true);
[7179]241
242            _ConsoleCommand& addShortcut();
243            _ConsoleCommand& addShortcut(const std::string&  name);
244            _ConsoleCommand& addGroup(const std::string& group);
245            _ConsoleCommand& addGroup(const std::string& group, const std::string&  name);
246
[7185]247            inline void setActive(bool bActive)
248                { this->bActive_ = bActive; }
[7179]249            inline bool isActive() const
[7185]250                { return (this->bActive_ && this->bInitialized_); }
[7179]251
[7185]252            inline _ConsoleCommandManipulator getManipulator() const
253                { return this; }
254
[7179]255            static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
256                { return _ConsoleCommand::getCommandMap(); }
257
258            static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
259                { return _ConsoleCommand::getCommand("", name, bPrintError); }
260            static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
261
262        private:
263            static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
264            static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
265
[7185]266            bool setFunctor(Functor* functor, bool bForce = false);
267            void pushFunctor(Functor* functor, bool bForce = false);
268            void popFunctor();
269            Functor* getFunctor() const;
[7179]270
271            bool functionHeaderMatches(Functor* functor) const;
272
273            void setObject(void* object);
[7185]274            void pushObject(void* object);
275            void popObject();
276            void* getObject() const;
[7179]277
[7185]278            bool bActive_;
279            bool bInitialized_;
[7179]280            const std::type_info& functionHeader_;
[7185]281            std::stack<Functor*> functorStack_;
282            std::stack<void*> objectStack_;
[7179]283    };
284
[7185]285    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, bool bInitialized = true)
[7179]286    {
[7185]287        return new _ConsoleCommand("", name, functor, bInitialized);
[7179]288    }
289
[7185]290    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true)
[7179]291    {
[7185]292        return new _ConsoleCommand(group, name, functor, bInitialized);
[7179]293    }
294}
295
[1505]296#endif /* _ConsoleCommand_H__ */
Note: See TracBrowser for help on using the repository browser.