Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands2/src/libraries/core/ConsoleCommand.h @ 6452

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

merged current state of the new cc system to the updated branch

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