Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

using object stack and function stack together works now

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