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
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            inline ConsoleCommand& description(const std::string& description)
93                { this->Executor::setDescription(description); return (*this); }
94            inline ConsoleCommand& descriptionParam(int param, const std::string& description)
95                { this->Executor::setDescriptionParam(param, description); return (*this); }
96            inline ConsoleCommand& descriptionReturnvalue(const std::string& description)
97                { this->Executor::setDescriptionReturnvalue(description); return (*this); }
98            inline ConsoleCommand& defaultValues(const MultiType& param1)
99                { this->Executor::setDefaultValues(param1); return (*this); }
100            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2)
101                { this->Executor::setDefaultValues(param1, param2); return (*this); }
102            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
103                { this->Executor::setDefaultValues(param1, param2, param3); return (*this); }
104            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
105                { this->Executor::setDefaultValues(param1, param2, param3, param4); return (*this); }
106            inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
107                { this->Executor::setDefaultValues(param1, param2, param3, param4, param5); return (*this); }
108            inline ConsoleCommand& defaultValue(unsigned int index, const MultiType& param)
109                { this->Executor::setDefaultValue(index, param); return (*this); }
110
111            inline ConsoleCommand& accessLevel(AccessLevel::Value level)
112                { this->accessLevel_ = level; return (*this); }
113            inline AccessLevel::Value getAccessLevel() const
114                { return this->accessLevel_; }
115
116            ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer);
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
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
135            inline ConsoleCommand& keybindMode(KeybindMode::Value mode)
136                { this->keybindMode_ = mode; return *this; }
137            inline KeybindMode::Value getKeybindMode() const
138                { return this->keybindMode_; }
139
140            inline ConsoleCommand& inputConfiguredParam(int index)
141                { this->inputConfiguredParam_ = index; return *this; }
142            inline int getInputConfiguredParam_() const
143                { return this->inputConfiguredParam_; }
144
145        private:
146            AccessLevel::Value accessLevel_;
147            ArgumentCompleter* argumentCompleter_[5];
148            ArgumentCompletionList argumentList_;
149
150            KeybindMode::Value keybindMode_;
151            int inputConfiguredParam_;
152    };
153
154    inline ConsoleCommand* createConsoleCommand(Functor* functor, const std::string& name = "")
155    {
156        return new ConsoleCommand(functor, name);
157    }
158}
159
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) \
182    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), false))
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>
202                    inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
203                        { if (this->command_) { this->command_->setFunctor(createFunctor(function), bForce); } return *this; }
204                    template <class F, class O>
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; }
209
210                    template <class F>
211                    inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
212                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function), bForce); } return *this; }
213                    template <class F, class O>
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; }
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; }
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; }
228
229                    inline void* getObject() const
230                        { if (this->command_) { return this->command_->getObject(); } else { return 0; } }
231
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:
240            _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true);
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
247            inline void setActive(bool bActive)
248                { this->bActive_ = bActive; }
249            inline bool isActive() const
250                { return (this->bActive_ && this->bInitialized_); }
251
252            inline _ConsoleCommandManipulator getManipulator() const
253                { return this; }
254
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
266            bool setFunctor(Functor* functor, bool bForce = false);
267            void pushFunctor(Functor* functor, bool bForce = false);
268            void popFunctor();
269            Functor* getFunctor() const;
270
271            bool functionHeaderMatches(Functor* functor) const;
272
273            void setObject(void* object);
274            void pushObject(void* object);
275            void popObject();
276            void* getObject() const;
277
278            bool bActive_;
279            bool bInitialized_;
280            const std::type_info& functionHeader_;
281            std::stack<Functor*> functorStack_;
282            std::stack<void*> objectStack_;
283    };
284
285    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, bool bInitialized = true)
286    {
287        return new _ConsoleCommand("", name, functor, bInitialized);
288    }
289
290    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true)
291    {
292        return new _ConsoleCommand(group, name, functor, bInitialized);
293    }
294}
295
296#endif /* _ConsoleCommand_H__ */
Note: See TracBrowser for help on using the repository browser.