Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7621 was 6454, checked in by rgrieder, 15 years ago

3 build fixes for MSVC.

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