Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

use a base-functor instead of a base-executor in ConsoleCommand because the functor of a static executor changes if new functions are assigned to the command.

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