[Orxonox-commit 2478] r7185 - code/branches/consolecommands3/src/libraries/core

landauf at orxonox.net landauf at orxonox.net
Thu Aug 19 02:11:28 CEST 2010


Author: landauf
Date: 2010-08-19 02:11:28 +0200 (Thu, 19 Aug 2010)
New Revision: 7185

Modified:
   code/branches/consolecommands3/src/libraries/core/ConsoleCommand.cc
   code/branches/consolecommands3/src/libraries/core/ConsoleCommand.h
   code/branches/consolecommands3/src/libraries/core/Functor.h
Log:
continued working on the new console command interface and implementation

Modified: code/branches/consolecommands3/src/libraries/core/ConsoleCommand.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/core/ConsoleCommand.cc	2010-08-18 19:46:16 UTC (rev 7184)
+++ code/branches/consolecommands3/src/libraries/core/ConsoleCommand.cc	2010-08-19 00:11:28 UTC (rev 7185)
@@ -79,9 +79,10 @@
     _SetConsoleCommand("BaseObject", "setName", &BaseObject::setName, (BaseObject*)0);
     _ConsoleCommand::_ConsoleCommandManipulator test(_ModifyConsoleCommand("BaseObject", "setName").setFunction(&BaseObject::setActive));
 
-    _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier())
+    _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier())
     {
-        this->state_ = state;
+        this->bActive_ = true;
+        this->bInitialized_ = bInitialized;
         _ConsoleCommand::registerCommand(group, name, this);
     }
 
@@ -109,87 +110,50 @@
         return *this;
     }
 
-    void _ConsoleCommand::setActive(bool bActive)
+    bool _ConsoleCommand::setFunctor(Functor* functor, bool bForce)
     {
-        if (bActive)
+        if (!functor)
         {
-            if (this->state_ == State::Inactive)
-                this->state_ = State::Active;
-            else if (this->state_ == State::UninitializedInactive)
-                this->state_ = State::UninitializedActive;
+            this->bInitialized_ = false;
+            return true;
         }
-        else
+
+        if (!bForce && !this->functionHeaderMatches(functor))
         {
-            if (this->state_ == State::Active)
-                this->state_ = State::Inactive;
-            else if (this->state_ == State::UninitializedActive)
-                this->state_ = State::UninitializedInactive;
+            COUT(1) << "Error: Couldn't assign new function to console command with name \"" << this->getName() << "\", headers don't match." << std::endl;
+            return false;
         }
+
+        this->functor_ = functor;
+        this->bInitialized_ = true;
+        return true;
     }
 
-    void _ConsoleCommand::setInitialized(bool bInitialized)
+    void _ConsoleCommand::pushFunctor(Functor* functor, bool bForce)
     {
-        if (bInitialized)
-        {
-            if (this->state_ == State::UninitializedActive)
-                this->state_ = State::Active;
-            else if (this->state_ == State::UninitializedInactive)
-                this->state_ = State::Inactive;
-        }
-        else
-        {
-            if (this->state_ == State::Active)
-                this->state_ = State::UninitializedActive;
-            else if (this->state_ == State::Inactive)
-                this->state_ = State::UninitializedInactive;
-        }
+        Functor* oldfunctor = this->getFunctor();
+
+        if (this->setFunctor(functor, bForce));
+            this->functorStack_.push(oldfunctor);
     }
 
-    void _ConsoleCommand::setFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode)
+    void _ConsoleCommand::popFunctor()
     {
-        if (!functor)
+        Functor* newfunctor = 0;
+        if (!this->functorStack_.empty())
         {
-            this->setInitialized(false);
-            return;
+            newfunctor = this->functorStack_.top();
+            this->functorStack_.pop();
         }
+        this->setFunctor(newfunctor);
+    }
 
-        if (!this->functionHeaderMatches(functor))
-        {
-            COUT(1) << "Error: Couldn't assign new function to console command with name \"" << this->getName() << "\", headers don't match." << std::endl;
-            return;
-        }
-
-        switch (mode)
-        {
-            default:
-            case _ConsoleCommand::ObjectPointer::Null:
-            {
-                this->functor_ = functor;
-            }
-            break;
-
-            case _ConsoleCommand::ObjectPointer::RawCopy:
-            {
-                void* object = (this->functor_) ? this->functor_->getRawObjectPointer() : 0;
-
-                this->functor_ = functor;
-
-                if (!this->functor_->getBaseObject())
-                    this->functor_->setRawObjectPointer(object);
-            }
-            break;
-
-            case _ConsoleCommand::ObjectPointer::CastViaBaseObject:
-            {
-                BaseObject* object = (this->functor_) ? this->functor_->getBaseObject() : 0;
-
-                this->functor_ = functor;
-
-                if (!this->functor_->getBaseObject())
-                    this->functor_->setBaseObject(object);
-            }
-            break;
-        }
+    Functor* _ConsoleCommand::getFunctor() const
+    {
+        if (this->bInitialized_)
+            return this->functor_;
+        else
+            return 0;
     }
 
     bool _ConsoleCommand::functionHeaderMatches(Functor* functor) const
@@ -197,23 +161,49 @@
         if (!this->functor_)
         {
             assert(false);
-            return false;
+            return true;
         }
-        return (functor->getHeaderIdentifier() == this->functor_->getHeaderIdentifier());
+        return (functor->getHeaderIdentifier() == this->functionHeader_);
     }
 
     void _ConsoleCommand::setObject(void* object)
     {
         if (this->functor_)
             this->functor_->setRawObjectPointer(object);
+        else if (object)
+            COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl;
     }
 
-    void _ConsoleCommand::setObject(BaseObject* object)
+    void _ConsoleCommand::pushObject(void* object)
     {
         if (this->functor_)
-            this->functor_->setBaseObject(object);
+        {
+            this->objectStack_.push(this->getObject());
+            this->setObject(object);
+        }
+        else
+            COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl;
     }
 
+    void _ConsoleCommand::popObject()
+    {
+        void* newobject = 0;
+        if (!this->objectStack_.empty())
+        {
+            newobject = this->objectStack_.top();
+            this->objectStack_.pop();
+        }
+        this->setObject(newobject);
+    }
+
+    void* _ConsoleCommand::getObject() const
+    {
+        if (this->functor_)
+            return this->functor_->getRawObjectPointer();
+        else
+            return 0;
+    }
+
     /* static */ const _ConsoleCommand* _ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError)
     {
         std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommandMap().find(group);

Modified: code/branches/consolecommands3/src/libraries/core/ConsoleCommand.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/ConsoleCommand.h	2010-08-18 19:46:16 UTC (rev 7184)
+++ code/branches/consolecommands3/src/libraries/core/ConsoleCommand.h	2010-08-19 00:11:28 UTC (rev 7185)
@@ -31,6 +31,7 @@
 
 #include "CorePrereqs.h"
 
+#include <stack>
 #include <boost/preprocessor/cat.hpp>
 #include <boost/preprocessor/facilities/expand.hpp>
 
@@ -178,7 +179,7 @@
     _DeclareConsoleCommandGeneric(group, name, functionpointer)
 
 #define _DeclareConsoleCommandGeneric(group, name, functionpointer) \
-    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), orxonox::_ConsoleCommand::State::UninitializedActive))
+    orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), false))
 
 
 #define _ModifyConsoleCommand(...) \
@@ -192,66 +193,42 @@
         friend struct _ConsoleCommandManipulator;
 
         public:
-            struct State
-            {
-                enum Enum
-                {
-                    UninitializedActive,
-                    UninitializedInactive,
-                    Active,
-                    Inactive
-                };
-            };
-
-            struct ObjectPointer
-            {
-                enum Enum
-                {
-                    Null,
-                    RawCopy,
-                    CastViaBaseObject
-                };
-            };
-
             struct _ConsoleCommandManipulator
             {
                 public:
                     _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {}
 
                     template <class F>
-                    inline _ConsoleCommandManipulator& setFunction(F function, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null)
-                        { if (this->command_) { this->command_->setFunctor(createFunctor(function), mode); } return *this; }
+                    inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false)
+                        { if (this->command_) { this->command_->setFunctor(createFunctor(function), bForce); } return *this; }
                     template <class F, class O>
-                    inline _ConsoleCommandManipulator& setFunction(F function, O* object)
-                        { if (this->command_) { this->command_->setFunctor(createFunctor(function, object)); } return *this; }
-                    inline _ConsoleCommandManipulator& setFunction(Functor* functor)
-                        { if (this->command_) { this->command_->setFunctor(functor); } return *this; }
-                    inline _ConsoleCommandManipulator& setFunction(const _ConsoleCommand* command)
-                        { if (this->command_) { this->command_->setFunctor(command->functor_); } return *this; }
-                    inline _ConsoleCommandManipulator& setFunction(const _ConsoleCommandManipulator& manipulator)
-                        { if (this->command_) { this->command_->setFunctor(manipulator.command_->functor_); } return *this; }
+                    inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false)
+                        { if (this->command_) { this->command_->setFunctor(createFunctor(function, object), bForce); } return *this; }
+                    inline _ConsoleCommandManipulator& setFunction(Functor* functor, bool bForce = false)
+                        { if (this->command_) { this->command_->setFunctor(functor, bForce); } return *this; }
 
                     template <class F>
-                    inline _ConsoleCommandManipulator& pushFunction(F function, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null)
-                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function), mode); } return *this; }
+                    inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false)
+                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function), bForce); } return *this; }
                     template <class F, class O>
-                    inline _ConsoleCommandManipulator& pushFunction(F function, O* object)
-                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object)); } return *this; }
-                    inline _ConsoleCommandManipulator& pushFunction(Functor* functor)
-                        { if (this->command_) { this->command_->pushFunctor(functor); } return *this; }
-                    inline _ConsoleCommandManipulator& pushFunction(const _ConsoleCommand* command)
-                        { if (this->command_) { this->command_->pushFunctor(command->functor_); } return *this; }
-                    inline _ConsoleCommandManipulator& pushFunction(const _ConsoleCommandManipulator& manipulator)
-                        { if (this->command_) { this->command_->pushFunctor(manipulator.command_->functor_); } return *this; }
+                    inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false)
+                        { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object), bForce); } return *this; }
+                    inline _ConsoleCommandManipulator& pushFunction(Functor* functor, bool bForce = false)
+                        { if (this->command_) { this->command_->pushFunctor(functor, bForce); } return *this; }
 
                     inline _ConsoleCommandManipulator& popFunction()
                         { if (this->command_) { this->command_->popFunctor(); } return *this; }
 
                     inline _ConsoleCommandManipulator& setObject(void* object)
                         { if (this->command_) { this->command_->setObject(object); } return *this; }
-                    inline _ConsoleCommandManipulator& setObject(BaseObject* object)
-                        { if (this->command_) { this->command_->setObject(object); } return *this; }
+                    inline _ConsoleCommandManipulator& pushObject(void* object)
+                        { if (this->command_) { this->command_->pushObject(object); } return *this; }
+                    inline _ConsoleCommandManipulator& popObject()
+                        { if (this->command_) { this->command_->popObject(); } return *this; }
 
+                    inline void* getObject() const
+                        { if (this->command_) { return this->command_->getObject(); } else { return 0; } }
+
                     inline _ConsoleCommandManipulator& setActive(bool bActive)
                         { if (this->command_) { this->command_->setActive(bActive); } return *this; }
 
@@ -260,19 +237,21 @@
             };
 
         public:
-            _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, State::Enum state = State::Active);
+            _ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true);
 
             _ConsoleCommand& addShortcut();
             _ConsoleCommand& addShortcut(const std::string&  name);
             _ConsoleCommand& addGroup(const std::string& group);
             _ConsoleCommand& addGroup(const std::string& group, const std::string&  name);
 
-            void setActive(bool bActive);
-            inline State::Enum getState() const
-                { return this->state_; }
+            inline void setActive(bool bActive)
+                { this->bActive_ = bActive; }
             inline bool isActive() const
-                { return (this->state_ == State::Active); }
+                { return (this->bActive_ && this->bInitialized_); }
 
+            inline _ConsoleCommandManipulator getManipulator() const
+                { return this; }
+
             static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands()
                 { return _ConsoleCommand::getCommandMap(); }
 
@@ -280,35 +259,37 @@
                 { return _ConsoleCommand::getCommand("", name, bPrintError); }
             static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false);
 
-            inline _ConsoleCommandManipulator getManipulator() const
-                { return this; }
-
         private:
             static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap();
             static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command);
 
-            void setInitialized(bool bInitialized);
-
-            void setFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null);
-            void pushFunctor(Functor* functor, _ConsoleCommand::ObjectPointer::Enum mode = _ConsoleCommand::ObjectPointer::Null);
+            bool setFunctor(Functor* functor, bool bForce = false);
+            void pushFunctor(Functor* functor, bool bForce = false);
             void popFunctor();
+            Functor* getFunctor() const;
+
             bool functionHeaderMatches(Functor* functor) const;
 
             void setObject(void* object);
-            void setObject(BaseObject* object);
+            void pushObject(void* object);
+            void popObject();
+            void* getObject() const;
 
-            State::Enum state_;
+            bool bActive_;
+            bool bInitialized_;
             const std::type_info& functionHeader_;
+            std::stack<Functor*> functorStack_;
+            std::stack<void*> objectStack_;
     };
 
-    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, _ConsoleCommand::State::Enum state = _ConsoleCommand::State::Active)
+    inline _ConsoleCommand* _createConsoleCommand(const std::string& name, Functor* functor, bool bInitialized = true)
     {
-        return new _ConsoleCommand("", name, functor, state);
+        return new _ConsoleCommand("", name, functor, bInitialized);
     }
 
-    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, _ConsoleCommand::State::Enum state = _ConsoleCommand::State::Active)
+    inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized = true)
     {
-        return new _ConsoleCommand(group, name, functor, state);
+        return new _ConsoleCommand(group, name, functor, bInitialized);
     }
 }
 

Modified: code/branches/consolecommands3/src/libraries/core/Functor.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/Functor.h	2010-08-18 19:46:16 UTC (rev 7184)
+++ code/branches/consolecommands3/src/libraries/core/Functor.h	2010-08-19 00:11:28 UTC (rev 7185)
@@ -37,7 +37,6 @@
 #include "util/Convert.h"
 #include "util/Debug.h"
 #include "util/MultiType.h"
-#include "BaseObject.h"
 
 namespace orxonox
 {
@@ -115,10 +114,6 @@
 
             virtual void evaluateParam(unsigned int index, MultiType& param) const = 0;
 
-            virtual void setBaseObject(BaseObject* object) {}
-            virtual void setBaseObject(const BaseObject* object) {}
-            virtual BaseObject* getBaseObject() const { return 0; }
-
             virtual void setRawObjectPointer(void* object) {}
             virtual void* getRawObjectPointer() const { return 0; }
 
@@ -182,26 +177,6 @@
                 return this;
             }
 
-            void setBaseObject(BaseObject* object)
-            {
-                this->object_ = dynamic_cast<T*>(object);
-                this->constObject_ = 0;
-            }
-
-            void setBaseObject(const BaseObject* object)
-            {
-                this->object_ = 0;
-                this->constObject_ = dynamic_cast<const T*>(object);
-            }
-
-            BaseObject* getBaseObject() const
-            {
-                if (this->object_)
-                    return upcast<BaseObject*>(this->object_);
-                else
-                    return const_cast<BaseObject*>(upcast<const BaseObject*>(this->constObject_));
-            }
-
             void setRawObjectPointer(void* object)
             {
                 this->object_ = (T*)object;
@@ -236,7 +211,7 @@
 
 
 
-    template <int r, class R, class P1, class P2, class P3, class P4, class P5>
+    template <class R, class P1, class P2, class P3, class P4, class P5>
     struct FunctorHeaderIdentifier {};
 
 
@@ -353,12 +328,12 @@
 
 
 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES(returnvalue, numparams) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES##numparams(returnvalue)
-#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void>
-#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void>
-#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void>
-#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void>
-#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void>
-#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <returnvalue, FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5>
+#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES0(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), void, void, void, void, void>
+#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES1(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, void, void, void, void>
+#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES2(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, void, void, void>
+#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES3(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, void, void>
+#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES4(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, void>
+#define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES5(returnvalue) <FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue), P1, P2, P3, P4, P5>
 
 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE(returnvalue) FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE##returnvalue
 #define FUNCTOR_HEADER_IDENTIFIER_TEMPLATE_CLASSES_RETURNVALUE0 void




More information about the Orxonox-commit mailing list