Changeset 3330

Show
Ignore:
Timestamp:
01/30/10 13:18:29 (7 weeks ago)
Author:
trichards
Message:

Refactored the Event framework so that I_Event is created in EventService? instead of EventQueue?.

Location:
Core/branches/0075_TR_SCRIPTING/Event
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • Core/branches/0075_TR_SCRIPTING/Event/I_Event.hpp

    r3305 r3330  
    3838class I_Connection; 
    3939class I_Action; 
     40class I_EventQueue; 
    4041 
    4142/// @brief Event interface 
     
    5657public: 
    5758    /// Connect an action to an event. 
     59    /// @param _pAction Action that will be invoked when the event is fired. 
     60    /// @param _queue The event queue that will be used to dispatch the action. 
     61    ///         If this parameter is NULL, the default event queue is used. 
    5862    /// @return I_Connection that represents the connected event and action. 
    5963    ///         Use this to disconnect the connection. 
    60     virtual I_Connection& connect(pAction_type _pAction) = 0; 
     64    virtual I_Connection& connect(pAction_type _pAction, I_EventQueue* _pQueue = NULL) = 0; 
    6165 
    6266    /// Fire an event. 
  • Core/branches/0075_TR_SCRIPTING/Event/I_EventQueue.hpp

    r3309 r3330  
    4444//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    4545class I_Action; 
    46 class I_Event; 
    4746 
    4847class EVENT_DLL_LINK I_EventQueue 
     
    6261    /// @{ 
    6362public: 
    64     /// Create an event by name. 
    65     virtual I_Event& createEvent(const std::string& _name) = 0; 
    66  
    67     /// Get an event by name. 
    68     virtual I_Event& getEvent(const std::string& _name) = 0; 
    69  
    7063    /// Dispatch the next event in this queue. 
    7164    /// Call this from the thread that needs to dispatch this 
  • Core/branches/0075_TR_SCRIPTING/Event/I_EventService.hpp

    r3308 r3330  
    4343class I_EventQueue; 
    4444class I_ActionMap; 
     45class I_Event; 
    4546 
    4647/// Event Service. 
     
    6263    /// @{ 
    6364public: 
     65    /// Create an event by name. 
     66    virtual I_Event& createEvent(const std::string& _name) = 0; 
     67 
     68    /// Get an event by name. 
     69    virtual I_Event& getEvent(const std::string& _name) = 0; 
     70 
    6471    /// Get an event queue by name. 
     72    /// There are two special event queues. "default" and "script". 
     73    /// The default event queue is used if I_Event::connect() has a NULL 
     74    /// for the event queue.  The script event queue is used as the default 
     75    /// event queue for scripted actions.  If either of these 
     76    /// event queues are created by either of these use cases, make 
     77    /// sure you process events on those queues, otherwise the queues will 
     78    /// fill up and consume memory. 
    6579    virtual I_EventQueue& getEventQueue(const std::string& _queueName) = 0; 
    6680 
  • Core/branches/0075_TR_SCRIPTING/Event/src/Event.cpp

    r3308 r3330  
    3434 
    3535#include "EventQueue.hpp" 
     36#include "EventService.hpp" 
    3637 
    3738#include <Zen/Core/Scripting/forward_declarations.hpp> 
     
    4445namespace Event { 
    4546//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    46 Event_impl::Event_impl(EventQueue& _queue) 
    47 :   m_queue(_queue) 
     47Event_impl::Event_impl(EventService& _service) 
     48:   m_service(_service) 
    4849,   m_connections() 
    4950,   m_pMutex(Threading::MutexFactory::create()) 
     
    114115Event_impl::getScriptModule() 
    115116{ 
    116     return m_queue.getScriptModule(); 
     117    return m_service.getScriptModule(); 
    117118} 
    118119 
    119120//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    120121I_Connection& 
    121 Event_impl::connect(pAction_type _pAction) 
     122Event_impl::connect(pAction_type _pAction, I_EventQueue* _pQueue) 
    122123{ 
    123124    pConnection_type pConnection = new Connection_type(this, _pAction); 
    124125 
    125126    Threading::CriticalSection guard(m_pMutex); 
     127 
    126128    m_connections.push_back(pConnection); 
     129    if (_pQueue) 
     130    { 
     131        EventQueue* pQueue = dynamic_cast<EventQueue*>(_pQueue); 
     132 
     133        if (pQueue) 
     134        { 
     135            m_queues.insert(pQueue); 
     136        } 
     137    } 
     138    else 
     139    { 
     140        // Dispatch to the default queue. 
     141        m_queues.insert(dynamic_cast<EventQueue*>(&m_service.getEventQueue("default"))); 
     142    } 
    127143 
    128144    return *pConnection; 
     
    133149Event_impl::disconnect(Connection_impl* _pConnection) 
    134150{ 
     151    // TODO Keep a refrence count for the queues and 
     152    // when the last connection is disconnected for a 
     153    // given queue, remove it from the set. 
     154 
    135155    Threading::CriticalSection guard(m_pMutex); 
    136156 
     
    146166Event_impl::fireEvent(boost::any _argument) 
    147167{ 
    148     m_queue.queueEvent(this, _argument); 
     168    Threading::CriticalSection guard(m_pMutex); 
     169 
     170    for(Queues_type::iterator iter = m_queues.begin(); iter != m_queues.end(); iter++) 
     171    { 
     172        (*iter)->queueEvent(this, _argument); 
     173    }     
    149174} 
    150175 
     
    182207Event_impl::connectScript(boost::any _scriptObject, boost::any _scriptFunction) 
    183208{ 
    184     ScriptAction* pRawAction = new ScriptAction(m_queue.getScriptObject()->getModule(), 
     209    ScriptAction* pRawAction = new ScriptAction(m_service.getScriptObject()->getModule(), 
    185210        _scriptObject, _scriptFunction); 
    186211 
    187212    pAction_type pAction(pRawAction, boost::bind(&Event_impl::destroyScriptAction, this, _1)); 
    188     return connect(pAction); 
     213 
     214    return connect(pAction, &m_service.getEventQueue("script")); 
    189215} 
    190216 
  • Core/branches/0075_TR_SCRIPTING/Event/src/EventQueue.cpp

    r3309 r3330  
    4646,   m_pScriptObject(NULL) 
    4747,   m_eventQueue() 
    48 ,   m_pEventsMutex(Threading::MutexFactory::create()) 
    4948{ 
    5049} 
     
    5352EventQueue::~EventQueue() 
    5453{ 
    55     Threading::MutexFactory::destroy(m_pEventsMutex); 
    56 } 
    57  
    58 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    59 I_Event& 
    60 EventQueue::createEvent(const std::string& _name) 
    61 { 
    62     // Since getEvent() will create the event if 
    63     // it doesn't exist, just call it. 
    64     return getEvent(_name); 
    65 } 
    66  
    67 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    68 I_Event& 
    69 EventQueue::getEvent(const std::string& _name) 
    70 { 
    71     Threading::CriticalSection guard(m_pEventsMutex); 
    72  
    73     // TODO Make sure the event has been created. 
    74     Events_type::iterator iter = m_events.find(_name); 
    75      
    76     if (iter == m_events.end()) 
    77     { 
    78         Event_impl* pEvent = new Event_impl(*this); 
    79  
    80         m_events[_name] = pEvent; 
    81  
    82         return *pEvent; 
    83     } 
    84     else 
    85     { 
    86         return *iter->second; 
    87     } 
    8854} 
    8955 
  • Core/branches/0075_TR_SCRIPTING/Event/src/EventQueue.hpp

    r3309 r3330  
    5555    typedef std::pair<Event_impl*, boost::any>              Event_type; 
    5656    typedef Threading::SynchronizedQueue<Event_type>        EventQueue_type; 
    57     typedef std::map<std::string, Event_impl*>              Events_type; 
    5857 
    5958    typedef Memory::managed_ptr<Scripting::I_ScriptModule>  pScriptModule_type; 
     
    6362    /// @{ 
    6463public: 
    65     virtual I_Event& createEvent(const std::string& _name); 
    66     virtual I_Event& getEvent(const std::string& _name); 
    6764    virtual bool dispatchOneEvent(); 
    6865    virtual void dispatchAllEvents(bool _wait); 
     
    10198    EventQueue_type                 m_eventQueue; 
    10299 
    103     /// Collection of all registered events. 
    104     Events_type                     m_events; 
    105  
    106     /// Mutex for guarding m_events 
    107     Threading::I_Mutex*             m_pEventsMutex; 
    108100    // @} 
    109101 
  • Core/branches/0075_TR_SCRIPTING/Event/src/EventService.cpp

    r3308 r3330  
    4747EventService::EventService() 
    4848:   m_pScriptObject(NULL) 
     49,   m_pEventsMutex(Threading::MutexFactory::create()) 
    4950{ 
    5051} 
     
    7071        } 
    7172    } 
     73 
     74    Threading::MutexFactory::destroy(m_pEventsMutex); 
    7275} 
    7376 
     
    117120 
    118121    module.addType<I_EventService>(getScriptTypeName(), "Event Service") 
     122        .addMethod("createEvent", &I_EventService::createEvent) 
     123        .addMethod("getEvent", &I_EventService::getEvent) 
    119124        .addMethod("getEventQueue", &I_EventService::getEventQueue) 
    120125        .addMethod("getActionMap", &I_EventService::getActionMap) 
     
    123128 
    124129    // EventQueue 
    125     module.addType<I_EventQueue>("EventQueue", "Event Queue") 
    126         .addMethod("createEvent", &I_EventQueue::createEvent) 
    127         .addMethod("getEvent", &I_EventQueue::getEvent) 
    128         .addMethod("dispatchOneEvent", &I_EventQueue::dispatchOneEvent) 
    129         .addMethod("dispatchAllEvents", &I_EventQueue::dispatchAllEvents) 
     130    module.addType<EventQueue>("EventQueue", "Event Queue") 
     131        .addMethod("dispatchOneEvent", &EventQueue::dispatchOneEvent) 
     132        .addMethod("dispatchAllEvents", &EventQueue::dispatchAllEvents) 
    130133    ; 
    131134 
     
    159162 
    160163//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     164I_Event& 
     165EventService::createEvent(const std::string& _name) 
     166{ 
     167    // Since getEvent() will create the event if 
     168    // it doesn't exist, just call it. 
     169    return getEvent(_name); 
     170} 
     171 
     172//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     173I_Event& 
     174EventService::getEvent(const std::string& _name) 
     175{ 
     176    Threading::CriticalSection guard(m_pEventsMutex); 
     177 
     178    // TODO Make sure the event has been created. 
     179    Events_type::iterator iter = m_events.find(_name); 
     180     
     181    if (iter == m_events.end()) 
     182    { 
     183        Event_impl* pEvent = new Event_impl(*this); 
     184 
     185        m_events[_name] = pEvent; 
     186 
     187        return *pEvent; 
     188    } 
     189    else 
     190    { 
     191        return *iter->second; 
     192    } 
     193} 
     194 
     195//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    161196I_EventQueue& 
    162197EventService::getEventQueue(const std::string& _queueName) 
  • Core/branches/0075_TR_SCRIPTING/Event/src/EventService.hpp

    r3308 r3330  
    3939class ActionMap; 
    4040class EventQueue; 
     41class Event_impl; 
    4142 
    4243/// Event Service implementation. 
     
    5152    typedef std::map<std::string, EventQueue*>          EventQueues_type; 
    5253    typedef std::map<std::string, ActionMap*>           ActionMaps_type; 
     54    typedef std::map<std::string, Event_impl*>          Events_type; 
    5355    /// @} 
    5456 
     
    6971    /// @{ 
    7072public: 
     73    virtual I_Event& createEvent(const std::string& _name); 
     74    virtual I_Event& getEvent(const std::string& _name); 
    7175    virtual I_EventQueue& getEventQueue(const std::string& _queueName); 
    7276    virtual I_ActionMap& getActionMap(const std::string& _actionMapName); 
     
    96100    EventQueues_type                    m_eventQueues; 
    97101    ActionMaps_type                     m_actionMaps; 
     102 
     103    /// Collection of all registered events. 
     104    Events_type                         m_events; 
     105 
     106    /// Mutex for guarding m_events 
     107    Threading::I_Mutex*                 m_pEventsMutex; 
     108 
    98109    /// @} 
    99110 
  • Core/branches/0075_TR_SCRIPTING/Event/src/Event_impl.hpp

    r3305 r3330  
    2727#include "../I_Event.hpp" 
    2828 
     29#include <set> 
     30 
    2931//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    3032namespace Zen { 
     
    4850    typedef std::list<pConnection_type>                 container_type; 
    4951    typedef Zen::Memory::managed_ptr<Scripting::I_ScriptModule> pScriptModule_type; 
     52    typedef std::set<EventQueue*>                       Queues_type; 
    5053    /// @} 
    5154 
     
    5356    /// @{ 
    5457public: 
    55     virtual I_Connection& connect(pAction_type _pAction); 
     58    virtual I_Connection& connect(pAction_type _pAction, I_EventQueue* _pQueue = NULL); 
    5659    virtual void fireEvent(boost::any _argument); 
    5760    /// @} 
     
    8891    /// @{ 
    8992protected: 
    90              Event_impl(EventQueue& _queue); 
     93    friend class EventService; 
     94             Event_impl(EventService& _service); 
    9195    virtual ~Event_impl(); 
    9296    /// @} 
     
    9599    /// @{ 
    96100private: 
    97     /// Queue that owns this event. 
    98     EventQueue&                         m_queue; 
     101    /// Service that owns this event. 
     102    EventService&                       m_service; 
    99103 
    100104    /// Connections for this event. 
    101105    container_type                      m_connections; 
    102106 
    103     /// Mutex to guard m_connections. 
     107    Queues_type                         m_queues; 
     108 
     109    /// Mutex to guard m_connections and m_queues 
    104110    pMutex_type                         m_pMutex; 
    105111