Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5879


Ignore:
Timestamp:
Oct 5, 2009, 5:02:25 PM (15 years ago)
Author:
landauf
Message:

More changes in the event-system: processEvent() is now locally executed in BaseObject. The event states (like activity, visibility, …) are now defined in XMLEventPort, a function which closely resembles XMLPort. This function is used to define event states and to parse event sources from XML.

Connected the main-state directly with the event-system. After a state was declared as the "main state", the Functor from the corresponding EventState-object is used to call the function. This reduces the redundancy of declaring event-states and main-states separately. Of course only boolean event-states (like activity or visibility) can be used as main-state, while memoryless states (like spawn in ParticleSpawner) and individual states which need the triggering object (like execute in QuestEffectBeacon) won't work.

Location:
code/branches/core5/src
Files:
20 edited

Legend:

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

    r5866 r5879  
    6363        this->bVisible_ = true;
    6464        this->oldGametype_ = 0;
     65        this->bRegisteredEventStates_ = false;
    6566
    6667        this->lastLoadedXMLElement_ = 0;
    6768
    68         this->functorSetMainState_ = 0;
    69         this->functorGetMainState_ = 0;
     69        this->mainStateFunctor_ = 0;
    7070
    7171        this->setCreator(creator);
     
    100100                (*(it++))->removeEventSource(this);
    101101
    102             for (std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.begin(); it != this->eventContainers_.end(); ++it)
     102            for (std::map<std::string, EventState*>::const_iterator it = this->eventStates_.begin(); it != this->eventStates_.end(); ++it)
    103103                delete it->second;
    104 
    105             if (this->functorSetMainState_)
    106                 delete this->functorSetMainState_;
    107             if (this->functorGetMainState_)
    108                 delete this->functorGetMainState_;
    109104        }
    110105    }
     
    114109        @param xmlelement The XML-element
    115110        @param loading Loading (true) or saving (false)
    116         @return The XML-element
    117111    */
    118112    void BaseObject::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     
    124118
    125119        XMLPortObjectTemplate(BaseObject, Template, "templates", addTemplate, getTemplate, xmlelement, mode, Template*);
    126 
    127         Element* events = xmlelement.FirstChildElement("events", false);
    128 
     120       
     121        Element* events = 0;
     122        if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
     123            events = xmlelement.FirstChildElement("events", false);
     124        else if (mode == XMLPort::SaveObject)
     125            ;
    129126        if (events)
    130         {
    131             std::list<std::string> eventnames;
    132 
    133             if (mode == XMLPort::LoadObject || mode == XMLPort::ExpandObject)
    134             {
    135                 for (ticpp::Iterator<ticpp::Element> child = events->FirstChildElement(false); child != child.end(); child++)
    136                     eventnames.push_back(child->Value());
    137             }
    138             else if (mode == XMLPort::SaveObject)
    139             {
    140                 for (std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->getIdentifier()->getXMLPortEventMapBegin(); it != this->getIdentifier()->getXMLPortEventMapEnd(); ++it)
    141                     eventnames.push_back(it->first);
    142             }
    143 
    144             for (std::list<std::string>::iterator it = eventnames.begin(); it != eventnames.end(); ++it)
    145             {
    146                 std::string sectionname = (*it);
    147                 ExecutorMember<BaseObject>* loadexecutor = createExecutor(createFunctor(&BaseObject::addEventSource), std::string( "BaseObject" ) + "::" + "addEventSource");
    148                 ExecutorMember<BaseObject>* saveexecutor = createExecutor(createFunctor(&BaseObject::getEventSource), std::string( "BaseObject" ) + "::" + "getEventSource");
    149                 loadexecutor->setDefaultValue(1, sectionname);
    150                 saveexecutor->setDefaultValue(1, sectionname);
    151 
    152                 XMLPortClassObjectContainer<BaseObject, BaseObject>* container = 0;
    153                 container = static_cast<XMLPortClassObjectContainer<BaseObject, BaseObject>*>(this->getIdentifier()->getXMLPortEventContainer(sectionname));
    154                 if (!container)
    155                 {
    156                     container = new XMLPortClassObjectContainer<BaseObject, BaseObject>(sectionname, this->getIdentifier(), loadexecutor, saveexecutor, false, true);
    157                     this->getIdentifier()->addXMLPortEventContainer(sectionname, container);
    158                 }
    159                 container->port(this, *events, mode);
    160             }
    161         }
     127            this->XMLEventPort(*events, mode);
     128    }
     129
     130    /**
     131        @brief Defines the possible event states of this object and parses eventsources from an XML file.
     132        @param xmlelement The XML-element
     133        @param loading Loading (true) or saving (false)
     134    */
     135    void BaseObject::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
     136    {
     137        XMLPortEventState(BaseObject, BaseObject, "activity", setActive, xmlelement, mode);
     138        XMLPortEventState(BaseObject, BaseObject, "visibility", setVisible, xmlelement, mode);
     139       
     140        this->bRegisteredEventStates_ = true;
    162141    }
    163142
     
    263242    }
    264243
    265     void BaseObject::addEventContainer(const std::string& sectionname, EventContainer* container)
    266     {
    267         std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname);
    268         if (it != this->eventContainers_.end())
    269         {
    270             COUT(2) << "Warning: Overwriting EventContainer in class " << this->getIdentifier()->getName() << "." << std::endl;
     244    void BaseObject::addEventState(const std::string& name, EventState* state)
     245    {
     246        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name);
     247        if (it != this->eventStates_.end())
     248        {
     249            COUT(2) << "Warning: Overwriting EventState in class " << this->getIdentifier()->getName() << "." << std::endl;
    271250            delete (it->second);
    272251        }
    273252
    274         this->eventContainers_[sectionname] = container;
    275     }
    276 
    277     EventContainer* BaseObject::getEventContainer(const std::string& sectionname) const
    278     {
    279         std::map<std::string, EventContainer*>::const_iterator it = this->eventContainers_.find(sectionname);
    280         if (it != this->eventContainers_.end())
     253        this->eventStates_[name] = state;
     254    }
     255
     256    EventState* BaseObject::getEventState(const std::string& name) const
     257    {
     258        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(name);
     259        if (it != this->eventStates_.end())
    281260            return ((*it).second);
    282261        else
     
    302281
    303282    /**
    304         @brief Fires an event which activates or deactivates a state with agiven originator (the object which sends the event).
     283        @brief Fires an event which activates or deactivates a state with agiven originator (the object which triggered the event).
    305284    */
    306285    void BaseObject::fireEvent(bool activate, BaseObject* originator)
     
    310289        for (std::set<BaseObject*>::iterator it = this->eventListeners_.begin(); it != this->eventListeners_.end(); ++it)
    311290        {
    312             event.sectionname_ = (*it)->eventSources_[this];
     291            event.statename_ = (*it)->eventSources_[this];
    313292            (*it)->processEvent(event);
    314293        }
     
    324303    }
    325304
     305    /**
     306        @brief Processing an event by calling the right main state.
     307        @param event The event struct which contains the information about the event
     308    */
    326309    void BaseObject::processEvent(Event& event)
    327310    {
    328         ORXONOX_SET_EVENT(BaseObject, "activity", setActive, event);
    329         ORXONOX_SET_EVENT(BaseObject, "visibility", setVisible, event);
    330     }
    331 
    332     void BaseObject::setMainStateName(const std::string& name)
    333     {
    334         if (this->mainStateName_ != name)
    335         {
    336             this->mainStateName_ = name;
    337             if (this->functorSetMainState_)
    338                 delete this->functorSetMainState_;
    339             if (this->functorGetMainState_)
    340                 delete this->functorGetMainState_;
    341             this->changedMainState();
    342             if (!this->functorSetMainState_)
    343                 COUT(2) << "Warning: \"" << name << "\" is not a valid MainState." << std::endl;
    344         }
    345     }
    346 
     311        this->registerEventStates();
     312       
     313        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(event.statename_);
     314        if (it != this->eventStates_.end())
     315            it->second->process(event, this);
     316        else if (event.statename_ != "")
     317            COUT(2) << "Warning: \"" << event.statename_ << "\" is not a valid state in object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << std::endl;
     318        else
     319            COUT(2) << "Warning: Event with invalid source sent to object \"" << this->getName() << "\" of class " << this->getIdentifier()->getName() << "." << std::endl;
     320    }
     321
     322    /**
     323        @brief Sets the main state of the object to a given boolean value.
     324       
     325        Note: The main state of an object can be set with the @ref setMainStateName function.
     326        It's part of the eventsystem and used for event forwarding (when the target object can't specify a specific state,
     327        the main state is used by default).
     328    */
    347329    void BaseObject::setMainState(bool state)
    348330    {
    349         if (this->functorSetMainState_)
    350             (*this->functorSetMainState_)(state);
     331        if (this->mainStateFunctor_)
     332            (*this->mainStateFunctor_)(state);
    351333        else
    352334            COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;
    353335    }
    354336
    355     bool BaseObject::getMainState() const
    356     {
    357         if (this->functorGetMainState_)
    358         {
    359             (*this->functorGetMainState_)();
    360             return this->functorGetMainState_->getReturnvalue();
    361         }
    362         else
    363         {
    364             COUT(2) << "Warning: No MainState defined in object \"" << this->getName() << "\" (" << this->getIdentifier()->getName() << ")" << std::endl;
    365             return false;
    366         }
    367     }
    368 
    369     void BaseObject::changedMainState()
    370     {
    371         SetMainState(BaseObject, "activity",   setActive,  isActive);
    372         SetMainState(BaseObject, "visibility", setVisible, isVisible);
     337    /**
     338        @brief This function gets called if the main state name of the object changes.
     339    */
     340    void BaseObject::changedMainStateName()
     341    {
     342        this->registerEventStates();
     343       
     344        this->mainStateFunctor_ = 0;
     345       
     346        std::map<std::string, EventState*>::const_iterator it = this->eventStates_.find(this->mainStateName_);
     347        if (it != this->eventStates_.end() && it->second->getFunctor() && it->second->getFunctor()->getParamCount() == 1)
     348            this->mainStateFunctor_ = it->second->getFunctor();
     349        else
     350            COUT(2) << "Warning: \"" << this->mainStateName_ << "\" is not a valid MainState." << std::endl;
     351    }
     352   
     353    /**
     354        @brief Calls XMLEventPort with an empty XML-element to register the event states if necessary.
     355    */
     356    void BaseObject::registerEventStates()
     357    {
     358        if (!this->bRegisteredEventStates_)
     359        {
     360            Element xmlelement;
     361            this->XMLEventPort(xmlelement, XMLPort::NOP);
     362        }
    373363    }
    374364}
  • code/branches/core5/src/libraries/core/BaseObject.h

    r5866 r5879  
    3737#define _BaseObject_H__
    3838
    39 #define SetMainState(classname, statename, setfunction, getfunction) \
    40     if (this->getMainStateName() == statename) \
    41     { \
    42         this->functorSetMainState_ = createFunctor(&classname::setfunction, this); \
    43         this->functorGetMainState_ = createFunctor(&classname::getfunction, this); \
    44     }
    45 
    46 
    4739#include "CorePrereqs.h"
    4840
     
    6961            virtual ~BaseObject();
    7062            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     63            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
    7164
    7265            /** @brief Returns if the object was initialized (passed the object registration). @return True was the object is initialized */
     
    111104
    112105            void setMainState(bool state);
    113             bool getMainState() const;
    114 
    115             void setMainStateName(const std::string& name);
     106
     107            /** @brief Sets the name of the main state (used for event reactions). */
     108            void setMainStateName(const std::string& name)
     109            {
     110                if (this->mainStateName_ != name)
     111                {
     112                    this->mainStateName_ = name;
     113                    this->changedMainStateName();
     114                }
     115            }
     116            /** @brief Returns the name of the main state. */
    116117            inline const std::string& getMainStateName() const { return this->mainStateName_; }
    117             virtual void changedMainState();
     118            /** @brief This function gets called if the main state name of the object changes. */
     119            virtual void changedMainStateName();
    118120
    119121            /** @brief Sets a pointer to the xml file that loaded this object. @param file The pointer to the XMLFile */
     
    176178                { this->eventListeners_.erase(object); }
    177179
    178             void addEventContainer(const std::string& sectionname, EventContainer* container);
    179             EventContainer* getEventContainer(const std::string& sectionname) const;
     180            void addEventState(const std::string& name, EventState* container);
     181            EventState* getEventState(const std::string& name) const;
    180182
    181183            std::string name_;                                 //!< The name of the object
     
    184186            mbool       bVisible_;                             //!< True = the object is visible
    185187            std::string mainStateName_;
    186             Functor*    functorSetMainState_;
    187             Functor*    functorGetMainState_;
     188            Functor*    mainStateFunctor_;
    188189
    189190        private:
    190191            void setXMLName(const std::string& name);
    191192            Template* getTemplate(unsigned int index) const;
     193            void registerEventStates();
    192194
    193195            bool                   bInitialized_;              //!< True if the object was initialized (passed the object registration)
     
    204206            std::set<Template*>    templates_;
    205207           
    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_;
     208            std::map<BaseObject*, std::string>  eventSources_;           //!< List of objects which send events to this object, mapped to the state which they affect
     209            std::set<BaseObject*>               eventListeners_;         //!< List of objects which listen to the events of this object
     210            std::map<std::string, EventState*>  eventStates_;            //!< Maps the name of the event states to their helper objects
     211            bool                                bRegisteredEventStates_; //!< Becomes true after the object registered its event states (with XMLEventPort)
    209212    };
    210213
     
    212215    SUPER_FUNCTION(2, BaseObject, changedActivity, false);
    213216    SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
    214     SUPER_FUNCTION(4, BaseObject, processEvent, false);
    215     SUPER_FUNCTION(6, BaseObject, changedMainState, false);
    216     SUPER_FUNCTION(9, BaseObject, changedName, false);
    217     SUPER_FUNCTION(10, BaseObject, changedGametype, false);
     217    SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
     218    SUPER_FUNCTION(8, BaseObject, changedName, false);
     219    SUPER_FUNCTION(9, BaseObject, changedGametype, false);
    218220}
    219221
  • code/branches/core5/src/libraries/core/CorePrereqs.h

    r5863 r5879  
    7777        enum Mode
    7878        {
     79            NOP,
    7980            LoadObject,
    8081            SaveObject,
     
    130131    class DynLibManager;
    131132    struct Event;
    132     class EventContainer;
     133    class EventState;
    133134    class Executor;
    134135    template <class T>
  • code/branches/core5/src/libraries/core/Event.cc

    r5866 r5879  
    3030
    3131#include "BaseObject.h"
     32#include "Identifier.h"
    3233
    3334namespace orxonox
    3435{
    35     EventContainer::~EventContainer()
     36    /**
     37        @brief Destructor: Deletes the functor of the event state.
     38    */
     39    EventState::~EventState()
    3640    {
    37         delete this->eventfunction_;
     41        if (this->statefunction_)
     42            delete this->statefunction_;
    3843    }
    3944
    40     void EventContainer::process(BaseObject* object, const Event& event)
     45    /**
     46        @brief Processes an event (calls the set-function if the necessary conditions are met).
     47       
     48        @param event     The fired event
     49        @param object    The object whose state is affected by the event (only needed for debug output)
     50    */
     51    void EventState::process(const Event& event, BaseObject* object)
    4152    {
    42         if (this->bActive_)
     53        if (this->bProcessingEvent_)
    4354        {
    44             COUT(2) << "Warning: Detected Event loop in section \"" << this->eventname_ << "\" of object \"" << object->getName() << "\" and fired by \"" << event.originator_->getName() << "\"" << std::endl;
     55            COUT(2) << "Warning: Detected Event loop in section \"" << event.statename_ << "\" of object \"" << object->getName() << "\" and fired by \"" << event.originator_->getName() << "\"" << std::endl;
    4556            return;
    4657        }
    4758
    48         this->bActive_ = true;
     59        this->bProcessingEvent_ = true;
    4960
    50         if (this->eventname_ == event.sectionname_)
     61        // check if the originator is an instance of the requested class
     62        if (event.originator_->isA(this->subclass_))
    5163        {
    52             if (event.originator_->isA(this->subclass_))
     64            // actualize the activationcounter
     65            if (event.activate_)
     66                ++this->activeEvents_;
     67            else
    5368            {
    54                 if (event.activate_)
    55                     ++this->activeEvents_;
    56                 else
     69                --this->activeEvents_;
     70
     71                if (this->activeEvents_ < 0)
     72                    this->activeEvents_ = 0;
     73            }
     74
     75            if (this->statefunction_->getParamCount() == 0 && event.activate_)
     76            {
     77                // if the eventfunction doesn't have a state, just call it whenever an activation-event comes in
     78                (*this->statefunction_)();
     79            }
     80            else if ((this->activeEvents_ == 1 && event.activate_) || (this->activeEvents_ == 0 && !event.activate_))
     81            {
     82                // if the eventfunction needs a state, we just call the function if the state changed from 0 to 1 (state = true) or from 1 to 0 (state = false) [but not if activeEvents_ is > 1]
     83                if (this->statefunction_->getParamCount() == 1)
    5784                {
    58                     --this->activeEvents_;
    59 
    60                     if (this->activeEvents_ < 0)
    61                         this->activeEvents_ = 0;
     85                    // one argument: just the eventstate
     86                    (*this->statefunction_)(this->activeEvents_);
    6287                }
    63 
    64                 if (this->eventfunction_->getParamCount() == 0 && event.activate_)
    65                     (*this->eventfunction_)();
    66                 else if ((this->activeEvents_ == 1 && event.activate_) || (this->activeEvents_ == 0 && !event.activate_))
     88                else if (this->statefunction_->getParamCount() >= 2)
    6789                {
    68                     if (this->eventfunction_->getParamCount() == 1)
    69                         (*this->eventfunction_)(this->activeEvents_);
    70                     else if (this->eventfunction_->getParamCount() >= 2 && event.castedOriginator_)
    71                         (*this->eventfunction_)(this->activeEvents_, event.castedOriginator_);
     90                    // two arguments: the eventstate and the originator
     91                    if (this->subclass_->isExactlyA(ClassIdentifier<BaseObject>::getIdentifier()))
     92                    {
     93                        // if the subclass is BaseObject, we don't have to cast the pointer
     94                        (*this->statefunction_)(this->activeEvents_, event.originator_);
     95                    }
     96                    else
     97                    {
     98                        // else cast the pointer to the desired class
     99                        void* castedOriginator = event.originator_->getDerivedPointer(this->subclass_->getClassID());
     100                        (*this->statefunction_)(this->activeEvents_, castedOriginator);
     101                    }
    72102                }
    73103            }
    74104        }
    75105
    76         this->bActive_ = false;
     106        this->bProcessingEvent_ = false;
    77107    }
    78108}
  • code/branches/core5/src/libraries/core/Event.h

    r5866 r5879  
    3535namespace orxonox
    3636{
     37    /**
     38        @brief The Event struct contains information about a fired Event.
     39    */
    3740    struct _CoreExport Event
    3841    {
    39             Event(bool activate, BaseObject* originator) : activate_(activate), originator_(originator), castedOriginator_(0) {}
     42        Event(bool activate, BaseObject* originator) : activate_(activate), originator_(originator) {}
    4043
    41             bool        activate_;
    42             BaseObject* originator_;
    43             void*       castedOriginator_;
    44             std::string sectionname_;
     44        bool        activate_;   //!< True if this is an activating event (the event source was inactive before and just triggered the event) - false otherwise
     45        std::string statename_;  //!< The name of the state this event affects
     46        BaseObject* originator_; //!< The object which triggered this event
    4547    };
    4648
    47     class _CoreExport EventContainer
     49    /**
     50        @brief The EventState contains information about an event state.
     51       
     52        An event state is a state of an object, which can be changed by events.
     53        Event states are changed through functions. Possible functions headers for set event states are:
     54         - memoryless state: function()
     55         - boolean state:    function(bool state)
     56         - individual state: function(bool state, SomeClass originator)
     57         
     58        Note that SomeClass may be any class deriving from BaseObject. You will not receive events from originators of other classes.
     59        The actual class for SomeClass must be specified as the second argument of the XMLPortEventState macro.
     60       
     61        The this pointer of the affected object is hidden in the functors, because the events are processed in the BaseObject, but some
     62        statefunctions may be from child-classes.
     63    */
     64    class _CoreExport EventState
    4865    {
    4966        public:
    50             EventContainer(const std::string& eventname, Functor* eventfunction, Identifier* subclass) : bActive_(false), eventname_(eventname), eventfunction_(eventfunction), subclass_(subclass), activeEvents_(0) {}
    51             virtual ~EventContainer();
     67            EventState(Functor* statefunction, Identifier* subclass) : bProcessingEvent_(false), activeEvents_(0), statefunction_(statefunction), subclass_(subclass) {}
     68            virtual ~EventState();
    5269
    53             void process(BaseObject* object, const Event& event);
     70            void process(const Event& event, BaseObject* object);
     71           
     72            Functor* getFunctor() const
     73                { return this->statefunction_; }
    5474
    5575        private:
    56             bool bActive_;
    57             std::string eventname_;
    58             Functor* eventfunction_;
    59             Identifier* subclass_;
    60 
    61             int activeEvents_;
     76            bool        bProcessingEvent_;  //!< This becomes true while the container processes an event (used to prevent loops)
     77            int         activeEvents_;      //!< The number of events which affect this state and are currently active
     78            Functor*    statefunction_;     //!< A functor to set the state
     79            Identifier* subclass_;          //!< Originators must be an instance of this class (usually BaseObject, but some statefunctions allow a second argument with an originator of a specific class)
    6280    };
    6381}
  • code/branches/core5/src/libraries/core/EventIncludes.h

    r5866 r5879  
    3333#include "Executor.h"
    3434
    35 #define ORXONOX_SET_EVENT(classname, eventname, functionname, event) \
    36     ORXONOX_SET_EVENT_GENERIC(eventcontainer##classname##functionname, classname, eventname, functionname, event, BaseObject)
     35/**
     36    @brief Defines a new event state (a state of the object which can be changed by events).
     37   
     38    @param classname    The name of this class
     39    @param subclassname Usually BaseObject - if different, only instances of this class can send events to this object
     40    @param statename    The name (string) of this state
     41    @param function     The function which should be used to set the state
     42    @param xmlelement   Argument for XMLPort
     43    @param mode         Argument for XMLPort
     44*/
     45#define XMLPortEventState(classname, subclassname, statename, function, xmlelement, mode) \
     46    orxonox::EventState* containername##function = this->getEventState(statename); \
     47    if (!containername##function) \
     48    { \
     49        containername##function = new orxonox::EventState(orxonox::createFunctor(&classname::function, this), orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
     50        this->addEventState(statename, containername##function); \
     51    } \
     52    XMLPortEventStateIntern(xmlportevent##function, classname, statename, xmlelement, mode)
    3753
    38 #define ORXONOX_SET_EVENT_TEMPLATE(classname, eventname, functionname, event, ...) \
    39     ORXONOX_SET_EVENT_GENERIC_TEMPLATE(eventcontainer##classname##functionname, classname, eventname, functionname, event, BaseObject, __VA_ARGS__)
     54/**
     55    @brief Like XMLPortEventState but with additional template arguments to identify the function of the state (if ambiguous).
     56*/
     57#define XMLPortEventStateTemplate(classname, subclassname, statename, function, xmlelement, mode, ...) \
     58    orxonox::EventState* containername##function = this->getEventState(statename); \
     59    if (!containername##function) \
     60    { \
     61        containername##function = new orxonox::EventState(orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::function, this), orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
     62        this->addEventState(statename, containername##function); \
     63    } \
     64    XMLPortEventStateIntern(xmlportevent##function, classname, statename, xmlelement, mode)
    4065
    41 #define ORXONOX_SET_SUBCLASS_EVENT(classname, eventname, functionname, event, subclassname) \
    42     event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    43     ORXONOX_SET_EVENT_GENERIC(eventcontainer##classname##functionname, classname, eventname, functionname, event, subclassname)
    44 
    45 #define ORXONOX_SET_SUBCLASS_EVENT_TEMPLATE(classname, eventname, functionname, event, subclassname, ...) \
    46     event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \
    47     ORXONOX_SET_EVENT_GENERIC_TEMPLATE(eventcontainer##classname##functionname, classname, eventname, functionname, event, subclassname, __VA_ARGS__)
    48 
    49 #define ORXONOX_SET_EVENT_GENERIC(containername, classname, eventname, functionname, event, subclassname) \
    50     orxonox::EventContainer* containername = this->getEventContainer(eventname); \
    51     if (!containername) \
    52     { \
    53         Functor* functor = orxonox::createFunctor(&classname::functionname, this); \
    54         containername = new orxonox::EventContainer(std::string(eventname), functor, orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
    55         this->addEventContainer(eventname, containername); \
    56     } \
    57     containername->process(this, event)
    58 
    59 #define ORXONOX_SET_EVENT_GENERIC_TEMPLATE(containername, classname, eventname, functionname, event, subclassname, ...) \
    60     orxonox::EventContainer* containername = this->getEventContainer(eventname); \
    61     if (!containername) \
    62     { \
    63         Functor* functor = orxonox::createFunctor<classname, __VA_ARGS__ >(&classname::functionname, this); \
    64         containername = new orxonox::EventContainer(std::string(eventname), functor, orxonox::ClassIdentifier<subclassname>::getIdentifier()); \
    65         this->addEventContainer(eventname, containername); \
    66     } \
    67     containername->process(this, event)
    68 
     66#define XMLPortEventStateIntern(name, classname, statename, xmlelement, mode) \
     67    static orxonox::ExecutorMember<classname>* xmlsetfunctor##name = (orxonox::ExecutorMember<classname>*)&orxonox::createExecutor(orxonox::createFunctor(&classname::addEventSource), std::string( #classname ) + "::" + "addEventSource" + "(" + statename + ")")->setDefaultValue(1, statename); \
     68    static orxonox::ExecutorMember<classname>* xmlgetfunctor##name = (orxonox::ExecutorMember<classname>*)&orxonox::createExecutor(orxonox::createFunctor(&classname::getEventSource), std::string( #classname ) + "::" + "getEventSource" + "(" + statename + ")")->setDefaultValue(1, statename); \
     69    XMLPortObjectGeneric(xmlport##name, classname, orxonox::BaseObject, statename, xmlsetfunctor##name, xmlgetfunctor##name, xmlelement, mode, false, true)
     70 
    6971#endif /* _EventIncludes_H__ */
  • code/branches/core5/src/libraries/core/Identifier.cc

    r5821 r5879  
    8787            delete (it->second);
    8888        for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it)
    89             delete (it->second);
    90         for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportEventContainers_.begin(); it != this->xmlportEventContainers_.end(); ++it)
    9189            delete (it->second);
    9290    }
     
    565563
    566564    /**
    567         @brief Returns a XMLPortEventContainer that attaches an event to this class.
    568         @param sectionname The name of the section that contains the event
    569         @return The container
    570     */
    571     XMLPortObjectContainer* Identifier::getXMLPortEventContainer(const std::string& eventname)
    572     {
    573         std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportEventContainers_.find(eventname);
    574         if (it != this->xmlportEventContainers_.end())
    575             return it->second;
    576         else
    577             return 0;
    578     }
    579 
    580     /**
    581         @brief Adds a new XMLPortEventContainer that attaches an event to this class.
    582         @param sectionname The name of the section that contains the event
    583         @param container The container
    584     */
    585     void Identifier::addXMLPortEventContainer(const std::string& eventname, XMLPortObjectContainer* container)
    586     {
    587         std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportEventContainers_.find(eventname);
    588         if (it != this->xmlportEventContainers_.end())
    589         {
    590             COUT(2) << "Warning: Overwriting XMLPortEventContainer in class " << this->getName() << "." << std::endl;
    591             delete (it->second);
    592         }
    593 
    594         this->xmlportEventContainers_[eventname] = container;
    595     }
    596 
    597     /**
    598565        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
    599566        @param out The outstream
  • code/branches/core5/src/libraries/core/Identifier.h

    r5866 r5879  
    268268
    269269
    270             //////////////////
    271             ///// Events /////
    272             //////////////////
    273             /** @brief Returns the map that stores all XMLPort events. @return The const_iterator */
    274             inline const std::map<std::string, XMLPortObjectContainer*>& getXMLPortEventMap() const { return this->xmlportEventContainers_; }
    275             /** @brief Returns a const_iterator to the beginning of the map that stores all XMLPort events. @return The const_iterator */
    276             inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortEventMapBegin() const { return this->xmlportEventContainers_.begin(); }
    277             /** @brief Returns a const_iterator to the end of the map that stores all XMLPort events. @return The const_iterator */
    278             inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortEventMapEnd() const { return this->xmlportEventContainers_.end(); }
    279 
    280             void addXMLPortEventContainer(const std::string& eventname, XMLPortObjectContainer* container);
    281             XMLPortObjectContainer* getXMLPortEventContainer(const std::string& eventname);
    282 
    283 
    284270        protected:
    285271            Identifier();
     
    342328            std::map<std::string, XMLPortParamContainer*> xmlportParamContainers_;     //!< All loadable parameters
    343329            std::map<std::string, XMLPortObjectContainer*> xmlportObjectContainers_;   //!< All attachable objects
    344             std::map<std::string, XMLPortObjectContainer*> xmlportEventContainers_;    //!< All events
    345330    };
    346331
  • code/branches/core5/src/libraries/core/OrxonoxClass.h

    r5823 r5879  
    110110                Returns NULL if the no pointer was found.
    111111            */
    112             template <class T>
    113             FORCEINLINE T* getDerivedPointer(unsigned int classID)
     112            FORCEINLINE void* getDerivedPointer(unsigned int classID)
    114113            {
    115114                for (int i = this->objectPointers_.size() - 1; i >= 0; --i)
    116115                {
    117116                    if (this->objectPointers_[i].first == classID)
    118                         return static_cast<T*>(this->objectPointers_[i].second);
     117                        return this->objectPointers_[i].second;
    119118                }
    120119                return NULL;
    121120            }
    122             //! Const version of getDerivedPointer
    123             template <class T>
    124             FORCEINLINE const T* getDerivedPointer(unsigned int classID) const
    125             {
    126                 return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID);
    127             }
     121
     122            //! Version of getDerivedPointer with template
     123            template <class T> FORCEINLINE T* getDerivedPointer(unsigned int classID)
     124            {   return static_cast<T*>(this->getDerivedPointer(classID));   }
     125            //! Const version of getDerivedPointer with template
     126            template <class T> FORCEINLINE const T* getDerivedPointer(unsigned int classID) const
     127            {   return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID);   }
    128128
    129129        private:
  • code/branches/core5/src/libraries/core/Super.h

    r5738 r5879  
    250250        SUPER_NOARGS(classname, functionname)
    251251
    252     #define SUPER_processEvent(classname, functionname, ...) \
     252    #define SUPER_XMLEventPort(classname, functionname, ...) \
    253253        SUPER_ARGS(classname, functionname, __VA_ARGS__)
    254254
    255255    #define SUPER_changedScale(classname, functionname, ...) \
    256         SUPER_NOARGS(classname, functionname)
    257 
    258     #define SUPER_changedMainState(classname, functionname, ...) \
    259256        SUPER_NOARGS(classname, functionname)
    260257
     
    497494        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
    498495
    499         SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(4, processEvent, true, Event& event)
    500             (event)
     496        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(4, XMLEventPort, true, Element& xmlelement, XMLPort::Mode mode)
     497            (xmlelement, mode)
    501498        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
    502499
     
    505502        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
    506503
    507         SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(6, changedMainState, false)
    508             ()
    509         SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
    510 
    511         SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(7, changedOwner, false)
    512             ()
    513         SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
    514 
    515         SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(8, changedOverlayGroup, false)
    516             ()
    517         SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
    518 
    519         SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(9, changedName, false)
    520             ()
    521         SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
    522 
    523         SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(10, changedGametype, false)
     504        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(6, changedOwner, false)
     505            ()
     506        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
     507
     508        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(7, changedOverlayGroup, false)
     509            ()
     510        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
     511
     512        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(8, changedName, false)
     513            ()
     514        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
     515
     516        SUPER_FUNCTION_GLOBAL_DECLARATION_PART1(9, changedGametype, false)
    524517            ()
    525518        SUPER_FUNCTION_GLOBAL_DECLARATION_PART2;
     
    571564    SUPER_INTRUSIVE_DECLARATION(changedActivity);
    572565    SUPER_INTRUSIVE_DECLARATION(changedVisibility);
    573     SUPER_INTRUSIVE_DECLARATION(processEvent);
     566    SUPER_INTRUSIVE_DECLARATION(XMLEventPort);
    574567    SUPER_INTRUSIVE_DECLARATION(changedScale);
    575     SUPER_INTRUSIVE_DECLARATION(changedMainState);
    576568    SUPER_INTRUSIVE_DECLARATION(changedOwner);
    577569    SUPER_INTRUSIVE_DECLARATION(changedOverlayGroup);
  • code/branches/core5/src/libraries/core/XMLPort.h

    r5858 r5879  
    411411                    }
    412412                }
    413                 else
     413                else if (mode == XMLPort::SaveObject)
    414414                {
    415415                    if (this->saveexecutor_)
     
    628628                    }
    629629                }
    630                 else
     630                else if (mode == XMLPort::SaveObject)
    631631                {
    632632                }
  • code/branches/core5/src/modules/objects/Attacher.cc

    r5738 r5879  
    5353    void Attacher::processEvent(Event& event)
    5454    {
    55         for (std::list<WorldEntity*>::iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
    56             (*it)->fireEvent(event);
     55        if (this->target_)
     56            this->target_->processEvent(event);
    5757    }
    5858
     
    102102
    103103        for (ObjectList<WorldEntity>::iterator it = ObjectList<WorldEntity>::begin(); it != ObjectList<WorldEntity>::end(); ++it)
     104        {
    104105            if (it->getName() == this->targetname_)
     106            {
     107                this->target_ = *it;
    105108                this->attachToParent(*it);
     109            }
     110        }
    106111    }
    107112
  • code/branches/core5/src/modules/objects/triggers/EventTrigger.cc

    r5738 r5879  
    4747    }
    4848
    49     void EventTrigger::processEvent(Event& event)
     49    void EventTrigger::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
    5050    {
    51         SUPER(EventTrigger, processEvent, event);
     51        SUPER(EventTrigger, XMLEventPort, xmlelement, mode);
    5252
    53         ORXONOX_SET_EVENT(EventTrigger, "trigger", trigger, event);
     53        XMLPortEventState(EventTrigger, BaseObject, "trigger", trigger, xmlelement, mode);
    5454    }
    5555
  • code/branches/core5/src/modules/objects/triggers/EventTrigger.h

    r5738 r5879  
    4141            virtual ~EventTrigger();
    4242
    43             virtual void processEvent(Event& event);
     43            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
    4444
    4545            inline void trigger(bool bTriggered)
  • code/branches/core5/src/modules/questsystem/QuestEffectBeacon.cc

    r5738 r5879  
    7575        XMLPortObject(QuestEffectBeacon, QuestEffect, "effects", addEffect, getEffect, xmlelement, mode);
    7676
     77        XMLPortEventState(QuestEffectBeacon, PlayerTrigger, "execute", execute, xmlelement, mode);
     78
    7779        COUT(3) << "New QuestEffectBeacon created." << std::endl;
    7880    }
    7981
    80     /**
    81     @brief
    82         Processes an event for this QuestEffectBeacon.
    83     */
    84     void QuestEffectBeacon::processEvent(Event& event)
    85     {
    86         SUPER(QuestEffectBeacon, processEvent, event);
    87 
    88         ORXONOX_SET_SUBCLASS_EVENT(QuestEffectBeacon, "execute", execute, event, PlayerTrigger);
     82    void QuestEffectBeacon::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
     83    {
     84        SUPER(QuestEffectBeacon, XMLEventPort, xmlelement, mode);
     85
     86        XMLPortEventState(QuestEffectBeacon, PlayerTrigger, "execute", execute, xmlelement, mode);
    8987    }
    9088
  • code/branches/core5/src/modules/questsystem/QuestEffectBeacon.h

    r5738 r5879  
    8686
    8787            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a QuestEffectBeacon object through XML.
    88 
    89             virtual void processEvent(Event& event); //!< Processes an event for this QuestEffectBeacon.
     88            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
    9089
    9190            bool execute(bool b, PlayerTrigger* trigger); //!< Executes the QuestEffects of the QuestEffectBeacon.
  • code/branches/core5/src/orxonox/graphics/ParticleSpawner.cc

    r5857 r5879  
    7171    }
    7272
    73     void ParticleSpawner::processEvent(Event& event)
     73    void ParticleSpawner::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
    7474    {
    75         SUPER(ParticleSpawner, processEvent, event);
     75        SUPER(ParticleSpawner, XMLEventPort, xmlelement, mode);
    7676
    77         ORXONOX_SET_EVENT(ParticleSpawner, "spawn", spawn, event);
     77        XMLPortEventState(ParticleSpawner, BaseObject, "spawn", spawn, xmlelement, mode);
    7878    }
    7979
  • code/branches/core5/src/orxonox/graphics/ParticleSpawner.h

    r5831 r5879  
    4444
    4545            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    46             virtual void processEvent(Event& event);
     46            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
    4747
    4848            inline void stop(bool bDestroy)
  • code/branches/core5/src/orxonox/overlays/OrxonoxOverlay.h

    r3327 r5879  
    213213  };
    214214
    215   SUPER_FUNCTION(7, OrxonoxOverlay, changedOwner, false);
    216   SUPER_FUNCTION(8, OrxonoxOverlay, changedOverlayGroup, false);
     215  SUPER_FUNCTION(6, OrxonoxOverlay, changedOwner, false);
     216  SUPER_FUNCTION(7, OrxonoxOverlay, changedOverlayGroup, false);
    217217}
    218218
  • code/branches/core5/src/orxonox/worldentities/WorldEntity.cc

    r5801 r5879  
    175175    void WorldEntity::registerVariables()
    176176    {
    177         registerVariable(this->mainStateName_,  VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedMainState));
     177        registerVariable(this->mainStateName_,  VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedMainStateName));
    178178
    179179        registerVariable(this->bActive_,        VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
Note: See TracChangeset for help on using the changeset viewer.