Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed old console command implementation (doesn't compile)

  • Property svn:eol-style set to native
File size: 18.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 "core/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 "Executor.h"
41
42
43#define _SetConsoleCommand(...) \
44    BOOST_PP_EXPAND(BOOST_PP_CAT(_SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__))
45#define _SetConsoleCommand2(name, functionpointer) \
46    _SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer))
47#define _SetConsoleCommand3(group, name, functionpointer) \
48    _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer))
49#define _SetConsoleCommand4(group, name, functionpointer, object) \
50    _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object))
51
52#define _SetConsoleCommandGeneric(group, name, functor) \
53    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createExecutor(functor)))
54
55
56#define _DeclareConsoleCommand(...) \
57    BOOST_PP_CAT(_DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)
58#define _DeclareConsoleCommand2(name, functionpointer) \
59    _DeclareConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer))
60#define _DeclareConsoleCommand3(group, name, functionpointer) \
61    _DeclareConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer))
62#define _DeclareConsoleCommand4(group, name, functionpointer, object) \
63    _DeclareConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object))
64
65#define _DeclareConsoleCommandGeneric(group, name, functor) \
66    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = orxonox::_createConsoleCommand(group, name, orxonox::createExecutor(functor), false)
67
68
69namespace orxonox
70{
71    class _CoreExport _ConsoleCommand
72    {
73        friend struct _ConsoleCommandManipulator;
74
75        struct Command
76        {
77            ExecutorPtr executor_;
78            FunctorPtr functor_;
79        };
80
81        struct AccessLevel
82        {
83            enum Enum
84            {
85                All,
86                Standalone,
87                Master,
88                Server,
89                Client,
90                Online,
91                Offline,
92                None
93            };
94        };
95
96        public:
97            struct _ConsoleCommandManipulator
98            {
99                public:
100                    _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {}
101
102                    template <class F>
103                    inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
104                        {
105                            if (this->command_)
106                            {
107                                if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
108                                {
109                                    FunctorPointer<F>* functor = static_cast<FunctorPointer<F>*>(this->command_->getExecutor()->getFunctor().get());
110                                    functor->setFunction(function);
111                                    return *this;
112                                }
113                                this->command_->setFunction(createFunctor(function), bForce);
114                            }
115                            return *this;
116                        }
117                    template <class F, class O>
118                    inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false)
119                        {
120                            if (this->command_)
121                            {
122                                if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F))
123                                {
124                                    FunctorPointer<F, O>* functor = static_cast<FunctorPointer<F, O>*>(this->command_->getExecutor()->getFunctor().get());
125                                    functor->setFunction(function);
126                                    functor->setObject(object);
127                                    return *this;
128                                }
129                                this->command_->setFunction(createFunctor(function, object), bForce);
130                            }
131                            return *this;
132                        }
133                    inline _ConsoleCommandManipulator& setFunction(const FunctorPtr& functor, bool bForce = false)
134                        { if (this->command_) { this->command_->setFunction(functor, bForce); } return *this; }
135                    inline _ConsoleCommandManipulator& setFunction(const ExecutorPtr& executor, bool bForce = false)
136                        { if (this->command_) { this->command_->setFunction(executor, bForce); } return *this; }
137
138                    template <class F>
139                    inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
140                        { if (this->command_) { this->command_->pushFunction(createFunctor(function), bForce); } return *this; }
141                    template <class F, class O>
142                    inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false)
143                        { if (this->command_) { this->command_->pushFunction(createFunctor(function, object), bForce); } return *this; }
144                    inline _ConsoleCommandManipulator& pushFunction(const FunctorPtr& functor, bool bForce = false)
145                        { if (this->command_) { this->command_->pushFunction(functor, bForce); } return *this; }
146                    inline _ConsoleCommandManipulator& pushFunction(const ExecutorPtr& executor, bool bForce = false)
147                        { if (this->command_) { this->command_->pushFunction(executor, bForce); } return *this; }
148
149                    inline _ConsoleCommandManipulator& popFunction()
150                        { if (this->command_) { this->command_->popFunction(); } return *this; }
151
152                    inline _ConsoleCommandManipulator& setObject(void* object)
153                        { if (this->command_) { this->command_->setObject(object); } return *this; }
154                    inline _ConsoleCommandManipulator& pushObject(void* object)
155                        { if (this->command_) { this->command_->pushObject(object); } return *this; }
156                    inline _ConsoleCommandManipulator& popObject()
157                        { if (this->command_) { this->command_->popObject(); } return *this; }
158
159                    inline _ConsoleCommandManipulator& setActive(bool bActive)
160                        { if (this->command_) { this->command_->setActive(bActive); } return *this; }
161                    inline _ConsoleCommandManipulator& activate()
162                        { return this->setActive(true); }
163                    inline _ConsoleCommandManipulator& deactivate()
164                        { return this->setActive(false); }
165
166                    inline _ConsoleCommandManipulator& setHidden(bool bHidden)
167                        { if (this->command_) { this->command_->setHidden(bHidden); } return *this; }
168                    inline _ConsoleCommandManipulator& hide()
169                        { return this->setHidden(true); }
170                    inline _ConsoleCommandManipulator& show()
171                        { return this->setHidden(false); }
172
173                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1)
174                        { if (this->command_) { this->command_->defaultValues(param1); } return *this; }
175                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2)
176                        { if (this->command_) { this->command_->defaultValues(param1, param2); } return *this; }
177                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
178                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3); } return *this; }
179                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
180                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4); } return *this; }
181                    inline _ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
182                        { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4, param5); } return *this; }
183                    inline _ConsoleCommandManipulator& defaultValue(unsigned int index, const MultiType& param)
184                        { if (this->command_) { this->command_->defaultValue(index, param); } return *this; }
185
186                    inline _ConsoleCommandManipulator& accessLevel(_ConsoleCommand::AccessLevel::Enum level)
187                        { if (this->command_) { this->command_->accessLevel(level); } return *this; }
188
189                    inline _ConsoleCommandManipulator& argumentCompleter(unsigned int param, ArgumentCompleter* completer)
190                        { if (this->command_) { this->command_->argumentCompleter(param, completer); } return *this; }
191
192                    inline _ConsoleCommandManipulator& setAsInputCommand()
193                        { if (this->command_) { this->command_->setAsInputCommand(); } return *this; }
194                    inline _ConsoleCommandManipulator& keybindMode(KeybindMode::Value mode)
195                        { if (this->command_) { this->command_->keybindMode(mode); } return *this; }
196                    inline _ConsoleCommandManipulator& inputConfiguredParam(int index)
197                        { if (this->command_) { this->command_->inputConfiguredParam(index); } return *this; }
198
199                private:
200                    _ConsoleCommand* command_;
201            };
202
203        public:
204            _ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true);
205            ~_ConsoleCommand();
206
207            _ConsoleCommand& addShortcut();
208            _ConsoleCommand& addShortcut(const std::string&  name);
209            _ConsoleCommand& addGroup(const std::string& group);
210            _ConsoleCommand& addGroup(const std::string& group, const std::string&  name);
211
212            inline _ConsoleCommand setActive(bool bActive)
213                { this->bActive_ = bActive; return *this; }
214            inline _ConsoleCommand activate()
215                { return this->setActive(true); }
216            inline _ConsoleCommand deactivate()
217                { return this->setActive(false); }
218
219            inline _ConsoleCommand& setHidden(bool bHidden)
220                { this->bHidden_ = bHidden; return *this; }
221            inline _ConsoleCommand& hide()
222                { return this->setHidden(true); }
223            inline _ConsoleCommand& show()
224                { return this->setHidden(false); }
225
226            bool isActive() const;
227            bool hasAccess() const;
228            bool isHidden() const;
229
230            _ConsoleCommand& description(const std::string& description);
231            const std::string& getDescription() const;
232
233            _ConsoleCommand& descriptionParam(unsigned int param, const std::string& description);
234            const std::string& getDescriptionParam(unsigned int param) const;
235
236            _ConsoleCommand& descriptionReturnvalue(const std::string& description);
237            const std::string& getDescriptionReturnvalue(int param) const;
238
239            _ConsoleCommand& defaultValues(const MultiType& param1);
240            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2);
241            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3);
242            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4);
243            _ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5);
244            _ConsoleCommand& defaultValue(unsigned int index, const MultiType& param);
245
246            inline _ConsoleCommand& accessLevel(AccessLevel::Enum level)
247                { this->accessLevel_ = level; return *this; }
248            inline AccessLevel::Enum getAccessLevel() const
249                { return this->accessLevel_; }
250
251            _ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer);
252            ArgumentCompleter* getArgumentCompleter(unsigned int param) const;
253
254            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 = "");
255            const ArgumentCompletionList& getArgumentCompletionList() const
256                { return this->argumentList_; }
257            ArgumentCompletionList::const_iterator getArgumentCompletionListBegin() const
258                { return this->argumentList_.begin(); }
259            ArgumentCompletionList::const_iterator getArgumentCompletionListEnd() const
260                { return this->argumentList_.end(); }
261
262            inline _ConsoleCommand& setAsInputCommand()
263            {
264                this->keybindMode(KeybindMode::OnHold);
265                this->defaultValue(0, Vector2(0.0f, 0.0f));
266                this->inputConfiguredParam(0);
267                return *this;
268            }
269
270            inline _ConsoleCommand& keybindMode(KeybindMode::Value mode)
271                { this->keybindMode_ = mode; return *this; }
272            inline KeybindMode::Value getKeybindMode() const
273                { return this->keybindMode_; }
274
275            inline _ConsoleCommand& inputConfiguredParam(int index)
276                { this->inputConfiguredParam_ = index; return *this; }
277            inline int getInputConfiguredParam_() const
278                { return this->inputConfiguredParam_; }
279
280            inline _ConsoleCommandManipulator getManipulator() const
281                { return this; }
282
283        private:
284            bool headersMatch(const FunctorPtr& functor);
285            bool headersMatch(const ExecutorPtr& executor);
286
287            bool setFunction(const ExecutorPtr& executor, bool bForce = false);
288            bool setFunction(const FunctorPtr& functor, bool bForce = false);
289            void pushFunction(const ExecutorPtr& executor, bool bForce = false);
290            void pushFunction(const FunctorPtr& functor, bool bForce = false);
291            void pushFunction();
292            void popFunction();
293            const ExecutorPtr& getExecutor() const;
294            const FunctorPtr& getFunctor() const;
295
296            bool setObject(void* object);
297            void pushObject(void* object);
298            void popObject();
299            void* getObject() const;
300
301            bool bActive_;
302            bool bHidden_;
303            AccessLevel::Enum accessLevel_;
304            std::string baseName_;
305            ExecutorPtr baseExecutor_;
306
307            ExecutorPtr executor_;
308            std::stack<Command> commandStack_;
309            std::stack<void*> objectStack_;
310
311            ArgumentCompleter* argumentCompleter_[5];
312            ArgumentCompletionList argumentList_;
313
314            KeybindMode::Value keybindMode_;
315            int inputConfiguredParam_;
316
317            LanguageEntryLabel description_;
318            LanguageEntryLabel descriptionReturnvalue_;
319            LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS];
320
321        public:
322            static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
323                { return _ConsoleCommand::getCommandMap(); }
324
325            static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false)
326                { return _ConsoleCommand::getCommand("", name, bPrintError); }
327            static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
328
329            static void destroyAll();
330
331        private:
332            static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
333            static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
334            static void unregisterCommand(_ConsoleCommand* command);
335    };
336
337    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, const ExecutorPtr& executor, bool bInitialized = true)
338        { return new _ConsoleCommand("", name, executor, bInitialized); }
339    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true)
340        { return new _ConsoleCommand(group, name, executor, bInitialized); }
341
342    inline _ConsoleCommand::_ConsoleCommandManipulator _ModifyConsoleCommand(const std::string& name)
343        { return _ConsoleCommand::getCommand(name, true); }
344    inline _ConsoleCommand::_ConsoleCommandManipulator _ModifyConsoleCommand(const std::string& group, const std::string& name)
345        { return _ConsoleCommand::getCommand(group, name, true); }
346}
347
348#endif /* _ConsoleCommand_H__ */
Note: See TracBrowser for help on using the repository browser.