Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7185


Ignore:
Timestamp:
Aug 19, 2010, 2:11:28 AM (14 years ago)
Author:
landauf
Message:

continued working on the new console command interface and implementation

Location:
code/branches/consolecommands3/src/libraries/core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/consolecommands3/src/libraries/core/ConsoleCommand.cc

    r7179 r7185  
    8080    _ConsoleCommand::_ConsoleCommandManipulator test(_ModifyConsoleCommand("BaseObject", "setName").setFunction(&BaseObject::setActive));
    8181
    82     _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier())
    83     {
    84         this->state_ = state;
     82    _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier())
     83    {
     84        this->bActive_ = true;
     85        this->bInitialized_ = bInitialized;
    8586        _ConsoleCommand::registerCommand(group, name, this);
    8687    }
     
    110111    }
    111112
    112     void _ConsoleCommand::setActive(bool bActive)
    113     {
    114         if (bActive)
    115         {
    116             if (this->state_ == State::Inactive)
    117                 this->state_ = State::Active;
    118             else if (this->state_ == State::UninitializedInactive)
    119                 this->state_ = State::UninitializedActive;
    120         }
    121         else
    122         {
    123             if (this->state_ == State::Active)
    124                 this->state_ = State::Inactive;
    125             else if (this->state_ == State::UninitializedActive)
    126                 this->state_ = State::UninitializedInactive;
    127         }
    128     }
    129 
    130     void _ConsoleCommand::setInitialized(bool bInitialized)
    131     {
    132         if (bInitialized)
    133         {
    134             if (this->state_ == State::UninitializedActive)
    135                 this->state_ = State::Active;
    136             else if (this->state_ == State::UninitializedInactive)
    137                 this->state_ = State::Inactive;
    138         }
    139         else
    140         {
    141             if (this->state_ == State::Active)
    142                 this->state_ = State::UninitializedActive;
    143             else if (this->state_ == State::Inactive)
    144                 this->state_ = State::UninitializedInactive;
    145         }
    146     }
    147 
    148     void _ConsoleCommand::setFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode)
     113    bool _ConsoleCommand::setFunctor(Functor* functor, bool bForce)
    149114    {
    150115        if (!functor)
    151116        {
    152             this->setInitialized(false);
    153             return;
    154         }
    155 
    156         if (!this->functionHeaderMatches(functor))
     117            this->bInitialized_ = false;
     118            return true;
     119        }
     120
     121        if (!bForce && !this->functionHeaderMatches(functor))
    157122        {
    158123            COUT(1) << "Error: Couldn't assign new function to console command with name \"" << this->getName() << "\", headers don't match." << std::endl;
    159             return;
    160         }
    161 
    162         switch (mode)
    163         {
    164             default:
    165             case _ConsoleCommand::ObjectPointer::Null:
    166             {
    167                 this->functor_ = functor;
    168             }
    169             break;
    170 
    171             case _ConsoleCommand::ObjectPointer::RawCopy:
    172             {
    173                 void* object = (this->functor_) ? this->functor_->getRawObjectPointer() : 0;
    174 
    175                 this->functor_ = functor;
    176 
    177                 if (!this->functor_->getBaseObject())
    178                     this->functor_->setRawObjectPointer(object);
    179             }
    180             break;
    181 
    182             case _ConsoleCommand::ObjectPointer::CastViaBaseObject:
    183             {
    184                 BaseObject* object = (this->functor_) ? this->functor_->getBaseObject() : 0;
    185 
    186                 this->functor_ = functor;
    187 
    188                 if (!this->functor_->getBaseObject())
    189                     this->functor_->setBaseObject(object);
    190             }
    191             break;
    192         }
     124            return false;
     125        }
     126
     127        this->functor_ = functor;
     128        this->bInitialized_ = true;
     129        return true;
     130    }
     131
     132    void _ConsoleCommand::pushFunctor(Functor* functor, bool bForce)
     133    {
     134        Functor* oldfunctor = this->getFunctor();
     135
     136        if (this->setFunctor(functor, bForce));
     137            this->functorStack_.push(oldfunctor);
     138    }
     139
     140    void _ConsoleCommand::popFunctor()
     141    {
     142        Functor* newfunctor = 0;
     143        if (!this->functorStack_.empty())
     144        {
     145            newfunctor = this->functorStack_.top();
     146            this->functorStack_.pop();
     147        }
     148        this->setFunctor(newfunctor);
     149    }
     150
     151    Functor* _ConsoleCommand::getFunctor() const
     152    {
     153        if (this->bInitialized_)
     154            return this->functor_;
     155        else
     156            return 0;
    193157    }
    194158
     
    198162        {
    199163            assert(false);
    200             return false;
    201         }
    202         return (functor->getHeaderIdentifier() == this->functor_->getHeaderIdentifier());
     164            return true;
     165        }
     166        return (functor->getHeaderIdentifier() == this->functionHeader_);
    203167    }
    204168
     
    207171        if (this->functor_)
    208172            this->functor_->setRawObjectPointer(object);
    209     }
    210 
    211     void _ConsoleCommand::setObject(BaseObject* object)
     173        else if (object)
     174            COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl;
     175    }
     176
     177    void _ConsoleCommand::pushObject(void* object)
    212178    {
    213179        if (this->functor_)
    214             this->functor_->setBaseObject(object);
     180        {
     181            this->objectStack_.push(this->getObject());
     182            this->setObject(object);
     183        }
     184        else
     185            COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl;
     186    }
     187
     188    void _ConsoleCommand::popObject()
     189    {
     190        void* newobject = 0;
     191        if (!this->objectStack_.empty())
     192        {
     193            newobject = this->objectStack_.top();
     194            this->objectStack_.pop();
     195        }
     196        this->setObject(newobject);
     197    }
     198
     199    void* _ConsoleCommand::getObject() const
     200    {
     201        if (this->functor_)
     202            return this->functor_->getRawObjectPointer();
     203        else
     204            return 0;
    215205    }
    216206
  • code/branches/consolecommands3/src/libraries/core/ConsoleCommand.h

    r7179 r7185  
    3232#include "CorePrereqs.h"
    3333
     34#include <stack>
    3435#include <boost/preprocessor/cat.hpp>
    3536#include <boost/preprocessor/facilities/expand.hpp>
     
    179180
    180181#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    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), false))
    182183
    183184
     
    193194
    194195        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 
    216196            struct _ConsoleCommandManipulator
    217197            {
     
    220200
    221201                    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; }
     202                    inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
     203                        { if (this->command_) { this->command_->setFunctor(createFunctor(function), bForce); } return *this; }
    224204                    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; }
     205                    inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false)
     206                        { if (this->command_) { this->command_->setFunctor(createFunctor(function, object), bForce); } return *this; }
     207                    inline _ConsoleCommandManipulator& setFunction(Functor* functor, bool bForce = false)
     208                        { if (this->command_) { this->command_->setFunctor(functor, bForce); } return *this; }
    233209
    234210                    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; }
     211                    inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
     212                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function), bForce); } return *this; }
    237213                    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; }
     214                    inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false)
     215                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object), bForce); } return *this; }
     216                    inline _ConsoleCommandManipulator& pushFunction(Functor* functor, bool bForce = false)
     217                        { if (this->command_) { this->command_->pushFunctor(functor, bForce); } return *this; }
    246218
    247219                    inline _ConsoleCommandManipulator& popFunction()
     
    250222                    inline _ConsoleCommandManipulator& setObject(void* object)
    251223                        { 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; }
     224                    inline _ConsoleCommandManipulator& pushObject(void* object)
     225                        { if (this->command_) { this->command_->pushObject(object); } return *this; }
     226                    inline _ConsoleCommandManipulator& popObject()
     227                        { if (this->command_) { this->command_->popObject(); } return *this; }
     228
     229                    inline void* getObject() const
     230                        { if (this->command_) { return this->command_->getObject(); } else { return 0; } }
    254231
    255232                    inline _ConsoleCommandManipulator& setActive(bool bActive)
     
    261238
    262239        public:
    263             _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state = State::Active);
     240            _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true);
    264241
    265242            _ConsoleCommand& addShortcut();
     
    268245            _ConsoleCommand& addGroup(const std::string& group, const std::string&  name);
    269246
    270             void setActive(bool bActive);
    271             inline State::Enum getState() const
    272                 { return this->state_; }
     247            inline void setActive(bool bActive)
     248                { this->bActive_ = bActive; }
    273249            inline bool isActive() const
    274                 { return (this->state_ == State::Active); }
     250                { return (this->bActive_ && this->bInitialized_); }
     251
     252            inline _ConsoleCommandManipulator getManipulator() const
     253                { return this; }
    275254
    276255            static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
     
    281260            static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
    282261
    283             inline _ConsoleCommandManipulator getManipulator() const
    284                 { return this; }
    285 
    286262        private:
    287263            static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
    288264            static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
    289265
    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);
     266            bool setFunctor(Functor* functor, bool bForce = false);
     267            void pushFunctor(Functor* functor, bool bForce = false);
    294268            void popFunctor();
     269            Functor* getFunctor() const;
     270
    295271            bool functionHeaderMatches(Functor* functor) const;
    296272
    297273            void setObject(void* object);
    298             void setObject(BaseObject* object);
    299 
    300             State::Enum state_;
     274            void pushObject(void* object);
     275            void popObject();
     276            void* getObject() const;
     277
     278            bool bActive_;
     279            bool bInitialized_;
    301280            const std::type_info& functionHeader_;
     281            std::stack<Functor*> functorStack_;
     282            std::stack<void*> objectStack_;
    302283    };
    303284
    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);
     285    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, bool bInitialized = true)
     286    {
     287        return new _ConsoleCommand("", name, functor, bInitialized);
     288    }
     289
     290    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true)
     291    {
     292        return new _ConsoleCommand(group, name, functor, bInitialized);
    312293    }
    313294}
  • code/branches/consolecommands3/src/libraries/core/Functor.h

    r7180 r7185  
    3838#include "util/Debug.h"
    3939#include "util/MultiType.h"
    40 #include "BaseObject.h"
    4140
    4241namespace orxonox
     
    116115            virtual void evaluateParam(unsigned int index, MultiType& param) const = 0;
    117116
    118             virtual void setBaseObject(BaseObject* object) {}
    119             virtual void setBaseObject(const BaseObject* object) {}
    120             virtual BaseObject* getBaseObject() const { return 0; }
    121 
    122117            virtual void setRawObjectPointer(void* object) {}
    123118            virtual void* getRawObjectPointer() const { return 0; }
     
    183178            }
    184179
    185             void setBaseObject(BaseObject* object)
    186             {
    187                 this->object_ = dynamic_cast<T*>(object);
    188                 this->constObject_ = 0;
    189             }
    190 
    191             void setBaseObject(const BaseObject* object)
    192             {
    193                 this->object_ = 0;
    194                 this->constObject_ = dynamic_cast<const T*>(object);
    195             }
    196 
    197             BaseObject* getBaseObject() const
    198             {
    199                 if (this->object_)
    200                     return upcast<BaseObject*>(this->object_);
    201                 else
    202                     return const_cast<BaseObject*>(upcast<const BaseObject*>(this->constObject_));
    203             }
    204 
    205180            void setRawObjectPointer(void* object)
    206181            {
     
    237212
    238213
    239     template <int r, class R, class P1, class P2, class P3, class P4, class P5>
     214    template <class R, class P1, class P2, class P3, class P4, class P5>
    240215    struct FunctorHeaderIdentifier {};
    241216
     
    354329
    355330#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES##numparams(returnvalue)
    356 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void>
    357 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void>
    358 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void>
    359 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void>
    360 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void>
    361 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5>
     331#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void>
     332#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void>
     333#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void>
     334#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void>
     335#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void>
     336#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5>
    362337
    363338#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE##returnvalue
Note: See TracChangeset for help on using the changeset viewer.