Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Moved ability to possess descriptions from Executor to ConsoleCommand, since no other executors use this feature. Also simplified this code a little by introducing a new shortcut in Language.h. XMLPort has to use a temporary solution for descriptions without Language support atm.

  • Property svn:eol-style set to native
File size: 14.4 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 "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 "ArgumentCompletionFunctions.h"
40#include "CommandExecutor.h"
41#include "Executor.h"
42#include "Identifier.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(Functor* 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(Functor* 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, 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, functionpointer)
185#define _DeclareConsoleCommand3(group, name, functionpointer) \
186    _DeclareConsoleCommandGeneric(group, name, functionpointer)
187
188#define _DeclareConsoleCommandGeneric(group, name, functionpointer) \
189    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), false))
190
191
192#define _ModifyConsoleCommand(...) \
193    orxonox::_ConsoleCommand::getCommand(__VA_ARGS__, true)->getManipulator()
194
195
196namespace orxonox
197{
198    class _CoreExport _ConsoleCommand : protected Executor
199    {
200        friend struct _ConsoleCommandManipulator;
201
202        public:
203            struct _ConsoleCommandManipulator
204            {
205                public:
206                    _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {}
207
208                    template <class F>
209                    inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
210                        { if (this->command_) { this->command_->setFunctor(createFunctor(function), bForce); } return *this; }
211                    template <class F, class O>
212                    inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false)
213                        { if (this->command_) { this->command_->setFunctor(createFunctor(function, object), bForce); } return *this; }
214                    inline _ConsoleCommandManipulator& setFunction(Functor* functor, bool bForce = false)
215                        { if (this->command_) { this->command_->setFunctor(functor, bForce); } return *this; }
216
217                    template <class F>
218                    inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
219                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function), bForce); } return *this; }
220                    template <class F, class O>
221                    inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false)
222                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object), bForce); } return *this; }
223                    inline _ConsoleCommandManipulator& pushFunction(Functor* functor, bool bForce = false)
224                        { if (this->command_) { this->command_->pushFunctor(functor, bForce); } return *this; }
225
226                    inline _ConsoleCommandManipulator& popFunction()
227                        { if (this->command_) { this->command_->popFunctor(); } return *this; }
228
229                    inline _ConsoleCommandManipulator& setObject(void* object)
230                        { if (this->command_) { this->command_->setObject(object); } return *this; }
231                    inline _ConsoleCommandManipulator& pushObject(void* object)
232                        { if (this->command_) { this->command_->pushObject(object); } return *this; }
233                    inline _ConsoleCommandManipulator& popObject()
234                        { if (this->command_) { this->command_->popObject(); } return *this; }
235
236                    inline void* getObject() const
237                        { if (this->command_) { return this->command_->getObject(); } else { return 0; } }
238
239                    inline _ConsoleCommandManipulator& setActive(bool bActive)
240                        { if (this->command_) { this->command_->setActive(bActive); } return *this; }
241
242                private:
243                    _ConsoleCommand* command_;
244            };
245
246        public:
247            _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true);
248
249            _ConsoleCommand& addShortcut();
250            _ConsoleCommand& addShortcut(const std::string&  name);
251            _ConsoleCommand& addGroup(const std::string& group);
252            _ConsoleCommand& addGroup(const std::string& group, const std::string&  name);
253
254            inline void setActive(bool bActive)
255                { this->bActive_ = bActive; }
256            inline bool isActive() const
257                { return (this->bActive_ && this->bInitialized_); }
258
259            inline _ConsoleCommandManipulator getManipulator() const
260                { return this; }
261
262            static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
263                { return _ConsoleCommand::getCommandMap(); }
264
265            static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
266                { return _ConsoleCommand::getCommand("", name, bPrintError); }
267            static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
268
269        private:
270            static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
271            static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
272
273            bool setFunctor(Functor* functor, bool bForce = false);
274            void pushFunctor(Functor* functor, bool bForce = false);
275            void popFunctor();
276            Functor* getFunctor() const;
277
278            bool functionHeaderMatches(Functor* functor) const;
279
280            void setObject(void* object);
281            void pushObject(void* object);
282            void popObject();
283            void* getObject() const;
284
285            bool bActive_;
286            bool bInitialized_;
287            const std::type_info& functionHeader_;
288            std::stack<Functor*> functorStack_;
289            std::stack<void*> objectStack_;
290    };
291
292    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, bool bInitialized = true)
293    {
294        return new _ConsoleCommand("", name, functor, bInitialized);
295    }
296
297    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true)
298    {
299        return new _ConsoleCommand(group, name, functor, bInitialized);
300    }
301}
302
303#endif /* _ConsoleCommand_H__ */
Note: See TracBrowser for help on using the repository browser.