Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 3, 2009, 6:32:05 PM (15 years ago)
Author:
landauf
Message:

some cleanup in the eventsystem. not sure if everything still works, but there are more changes to come.

Location:
code/branches/core5/src/libraries/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core5/src/libraries/core/BaseObject.cc

    r5858 r5866  
    9494        if (this->isInitialized())
    9595        {
    96             for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
    97                 (*it)->unregisterEventListener(this);
    98 
    99             for (std::map<BaseObject*, std::string>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
    100                 it->first->removeEvent(this);
     96            for (std::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); )
     97                this->removeEventSource((it++)->first);
     98
     99            for (std::set<BaseObject*>::const_iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); )
     100                (*(it++))->removeEventSource(this);
    101101
    102102            for (std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.begin(); it != this->eventContainers_.end(); ++it)
     
    145145            {
    146146                std::string sectionname = (*it);
    147                 ExecutorMember<BaseObject>* loadexecutor = createExecutor(createFunctor(&BaseObject::addEvent), std::string( "BaseObject" ) + "::" + "addEvent");
    148                 ExecutorMember<BaseObject>* saveexecutor = createExecutor(createFunctor(&BaseObject::getEvent), std::string( "BaseObject" ) + "::" + "getEvent");
     147                ExecutorMember<BaseObject>* loadexecutor = createExecutor(createFunctor(&BaseObject::addEventSource), std::string( "BaseObject" ) + "::" + "addEventSource");
     148                ExecutorMember<BaseObject>* saveexecutor = createExecutor(createFunctor(&BaseObject::getEventSource), std::string( "BaseObject" ) + "::" + "getEventSource");
    149149                loadexecutor->setDefaultValue(1, sectionname);
     150                saveexecutor->setDefaultValue(1, sectionname);
    150151
    151152                XMLPortClassObjectContainer<BaseObject, BaseObject>* container = 0;
     
    224225    }
    225226
    226     void BaseObject::addEvent(BaseObject* event, const std::string& sectionname)
    227     {
    228         event->registerEventListener(this, sectionname);
    229         this->events_.push_back(event);
    230     }
    231 
    232     void BaseObject::removeEvent(BaseObject* event)
    233     {
    234         this->events_.remove(event);
    235     }
    236 
    237     BaseObject* BaseObject::getEvent(unsigned int index) const
     227    /**
     228        @brief Adds a new event source for a specific state.
     229        @param source The object which sends events to this object
     230        @param state The state of this object which will be affected by the events
     231    */
     232    void BaseObject::addEventSource(BaseObject* source, const std::string& state)
     233    {
     234        this->eventSources_[source] = state;
     235        source->registerEventListener(this);
     236    }
     237
     238    /**
     239        @brief Removes an eventsource (but doesn't unregister itself at the source).
     240    */
     241    void BaseObject::removeEventSource(BaseObject* source)
     242    {
     243        this->eventSources_.erase(source);
     244        source->unregisterEventListener(this);
     245    }
     246
     247    /**
     248        @brief Returns an eventsource with a given index.
     249    */
     250    BaseObject* BaseObject::getEventSource(unsigned int index, const std::string& state) const
    238251    {
    239252        unsigned int i = 0;
    240         for (std::list<BaseObject*>::const_iterator it = this->events_.begin(); it != this->events_.end(); ++it)
    241         {
     253        for (std::map<BaseObject*, std::string>::const_iterator it = this->eventSources_.begin(); it != this->eventSources_.end(); ++it)
     254        {
     255            if (it->second != state)
     256                continue;
     257           
    242258            if (i == index)
    243                 return (*it);
     259                return it->first;
    244260            ++i;
    245261        }
     
    268284    }
    269285
     286    /**
     287        @brief Fires an event (without a state).
     288    */
    270289    void BaseObject::fireEvent()
    271290    {
     
    274293    }
    275294
     295    /**
     296        @brief Fires an event which activates or deactivates a state.
     297    */
    276298    void BaseObject::fireEvent(bool activate)
    277299    {
     
    279301    }
    280302
     303    /**
     304        @brief Fires an event which activates or deactivates a state with agiven originator (the object which sends the event).
     305    */
    281306    void BaseObject::fireEvent(bool activate, BaseObject* originator)
    282307    {
    283308        Event event(activate, originator);
    284309
    285         for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
    286         {
    287             event.sectionname_ = it->second;
    288             it->first->processEvent(event);
    289         }
    290     }
    291 
     310        for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
     311        {
     312            event.sectionname_ = (*it)->eventSources_[this];
     313            (*it)->processEvent(event);
     314        }
     315    }
     316
     317    /**
     318        @brief Fires an event, using the Event struct.
     319    */
    292320    void BaseObject::fireEvent(Event& event)
    293321    {
    294         for (std::map<BaseObject*, std::string>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
    295             it->first->processEvent(event);
     322        for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
     323            (*it)->processEvent(event);
    296324    }
    297325
  • code/branches/core5/src/libraries/core/BaseObject.h

    r5839 r5866  
    152152            virtual void changedGametype() {}
    153153
     154            void addEventSource(BaseObject* source, const std::string& state);
     155            void removeEventSource(BaseObject* source);
     156            BaseObject* getEventSource(unsigned int index, const std::string& state) const;
     157
    154158            void fireEvent();
    155159            void fireEvent(bool activate);
     
    159163            virtual void processEvent(Event& event);
    160164
    161             inline void registerEventListener(BaseObject* object, const std::string& sectionname)
    162                 { this->eventListeners_[object] = sectionname; }
    163             inline void unregisterEventListener(BaseObject* object)
    164                 { this->eventListeners_.erase(object); }
    165 
    166             void addEvent(BaseObject* event, const std::string& sectionname);
    167             void removeEvent(BaseObject* event);
    168             BaseObject* getEvent(unsigned int index) const;
    169 
    170             void addEventContainer(const std::string& sectionname, EventContainer* container);
    171             EventContainer* getEventContainer(const std::string& sectionname) const;
    172 
    173165            /** @brief Sets the indentation of the debug output in the Loader. @param indentation The indentation */
    174166            inline void setLoaderIndentation(const std::string& indentation) { this->loaderIndentation_ = indentation; }
     
    177169
    178170        protected:
     171            /** @brief Adds an object which listens to the events of this object. */
     172            inline void registerEventListener(BaseObject* object)
     173                { this->eventListeners_.insert(object); }
     174            /** @brief Removes an event listener from this object. */
     175            inline void unregisterEventListener(BaseObject* object)
     176                { this->eventListeners_.erase(object); }
     177
     178            void addEventContainer(const std::string& sectionname, EventContainer* container);
     179            EventContainer* getEventContainer(const std::string& sectionname) const;
     180
    179181            std::string name_;                                 //!< The name of the object
    180182            std::string oldName_;                              //!< The old name of the object
     
    197199            BaseObject*            creator_;
    198200            SmartPtr<Scene>        scene_;
     201            uint32_t               sceneID_;
    199202            SmartPtr<Gametype>     gametype_;
    200203            Gametype*              oldGametype_;
    201204            std::set<Template*>    templates_;
    202             std::map<BaseObject*,  std::string> eventListeners_;
    203             std::list<BaseObject*> events_;
    204             std::map<std::string, EventContainer*> eventContainers_;
    205             uint32_t               sceneID_;
     205           
     206            std::map<BaseObject*, std::string>      eventSources_;      //!< List of objects which send events to this object, mapped to the state which they affect
     207            std::set<BaseObject*>                   eventListeners_;    //!< List of objects which listen to the events of this object
     208            std::map<std::string, EventContainer*>  eventContainers_;
    206209    };
    207210
  • code/branches/core5/src/libraries/core/Event.cc

    r5738 r5866  
    3030
    3131#include "BaseObject.h"
    32 #include "Executor.h"
    3332
    3433namespace orxonox
  • code/branches/core5/src/libraries/core/Event.h

    r5738 r5866  
    4848    {
    4949        public:
    50             EventContainer(const std::string& eventname, Executor* eventfunction, Identifier* subclass) : bActive_(false), eventname_(eventname), eventfunction_(eventfunction), subclass_(subclass), activeEvents_(0) {}
     50            EventContainer(const std::string& eventname, Functor* eventfunction, Identifier* subclass) : bActive_(false), eventname_(eventname), eventfunction_(eventfunction), subclass_(subclass), activeEvents_(0) {}
    5151            virtual ~EventContainer();
    5252
     
    5656            bool bActive_;
    5757            std::string eventname_;
    58             Executor* eventfunction_;
     58            Functor* eventfunction_;
    5959            Identifier* subclass_;
    6060
  • code/branches/core5/src/libraries/core/EventIncludes.h

    r5829 r5866  
    4040
    4141#define ORXONOX_SET_SUBCLASS_EVENT(classname, eventname, functionname, event, subclassname) \
     42    event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    4243    ORXONOX_SET_EVENT_GENERIC(eventcontainer##classname##functionname, classname, eventname, functionname, event, subclassname)
    4344
    4445#define ORXONOX_SET_SUBCLASS_EVENT_TEMPLATE(classname, eventname, functionname, event, subclassname, ...) \
     46    event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    4547    ORXONOX_SET_EVENT_GENERIC_TEMPLATE(eventcontainer##classname##functionname, classname, eventname, functionname, event, subclassname, __VA_ARGS__)
    4648
     
    4951    if (!containername) \
    5052    { \
    51         ExecutorMember<classname>* executor = orxonox::createExecutor(orxonox::createFunctor(&classname::functionname, this), std::string( #classname ) + "::" + #functionname); \
    52         containername = new orxonox::EventContainer(std::string(eventname), executor, orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
     53        Functor* functor = orxonox::createFunctor(&classname::functionname, this); \
     54        containername = new orxonox::EventContainer(std::string(eventname), functor, orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
    5355        this->addEventContainer(eventname, containername); \
    5456    } \
    55     event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    5657    containername->process(this, event)
    5758
     
    6061    if (!containername) \
    6162    { \
    62         ExecutorMember<classname>* executor = orxonox::createExecutor(orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::functionname, this), std::string( #classname ) + "::" + #functionname); \
    63         containername = new orxonox::EventContainer(std::string(eventname), executor, orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
     63        Functor* functor = orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::functionname, this); \
     64        containername = new orxonox::EventContainer(std::string(eventname), functor, orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
    6465        this->addEventContainer(eventname, containername); \
    6566    } \
    66     event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    6767    containername->process(this, event)
    6868
  • code/branches/core5/src/libraries/core/Identifier.h

    r5783 r5866  
    261261            inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapEnd() const { return this->xmlportObjectContainers_.end(); }
    262262
     263            void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
     264            XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname);
     265
     266            void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
     267            XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
     268
     269
     270            //////////////////
     271            ///// Events /////
     272            //////////////////
    263273            /** @brief Returns the map that stores all XMLPort events. @return The const_iterator */
    264274            inline const std::map<std::string, XMLPortObjectContainer*>& getXMLPortEventMap() const { return this->xmlportEventContainers_; }
     
    267277            /** @brief Returns a const_iterator to the end of the map that stores all XMLPort events. @return The const_iterator */
    268278            inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortEventMapEnd() const { return this->xmlportEventContainers_.end(); }
    269 
    270             void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
    271             XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname);
    272 
    273             void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
    274             XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
    275279
    276280            void addXMLPortEventContainer(const std::string& eventname, XMLPortObjectContainer* container);
Note: See TracChangeset for help on using the changeset viewer.