Changeset 3330
- Timestamp:
- 01/30/10 13:18:29 (7 weeks ago)
- Location:
- Core/branches/0075_TR_SCRIPTING/Event
- Files:
-
- 9 modified
-
I_Event.hpp (modified) (2 diffs)
-
I_EventQueue.hpp (modified) (2 diffs)
-
I_EventService.hpp (modified) (2 diffs)
-
src/Event.cpp (modified) (6 diffs)
-
src/EventQueue.cpp (modified) (2 diffs)
-
src/EventQueue.hpp (modified) (3 diffs)
-
src/EventService.cpp (modified) (5 diffs)
-
src/EventService.hpp (modified) (4 diffs)
-
src/Event_impl.hpp (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
Core/branches/0075_TR_SCRIPTING/Event/I_Event.hpp
r3305 r3330 38 38 class I_Connection; 39 39 class I_Action; 40 class I_EventQueue; 40 41 41 42 /// @brief Event interface … … 56 57 public: 57 58 /// 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. 58 62 /// @return I_Connection that represents the connected event and action. 59 63 /// 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; 61 65 62 66 /// Fire an event. -
Core/branches/0075_TR_SCRIPTING/Event/I_EventQueue.hpp
r3309 r3330 44 44 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 45 45 class I_Action; 46 class I_Event;47 46 48 47 class EVENT_DLL_LINK I_EventQueue … … 62 61 /// @{ 63 62 public: 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 70 63 /// Dispatch the next event in this queue. 71 64 /// Call this from the thread that needs to dispatch this -
Core/branches/0075_TR_SCRIPTING/Event/I_EventService.hpp
r3308 r3330 43 43 class I_EventQueue; 44 44 class I_ActionMap; 45 class I_Event; 45 46 46 47 /// Event Service. … … 62 63 /// @{ 63 64 public: 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 64 71 /// 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. 65 79 virtual I_EventQueue& getEventQueue(const std::string& _queueName) = 0; 66 80 -
Core/branches/0075_TR_SCRIPTING/Event/src/Event.cpp
r3308 r3330 34 34 35 35 #include "EventQueue.hpp" 36 #include "EventService.hpp" 36 37 37 38 #include <Zen/Core/Scripting/forward_declarations.hpp> … … 44 45 namespace Event { 45 46 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 46 Event_impl::Event_impl(Event Queue& _queue)47 : m_ queue(_queue)47 Event_impl::Event_impl(EventService& _service) 48 : m_service(_service) 48 49 , m_connections() 49 50 , m_pMutex(Threading::MutexFactory::create()) … … 114 115 Event_impl::getScriptModule() 115 116 { 116 return m_ queue.getScriptModule();117 return m_service.getScriptModule(); 117 118 } 118 119 119 120 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 120 121 I_Connection& 121 Event_impl::connect(pAction_type _pAction )122 Event_impl::connect(pAction_type _pAction, I_EventQueue* _pQueue) 122 123 { 123 124 pConnection_type pConnection = new Connection_type(this, _pAction); 124 125 125 126 Threading::CriticalSection guard(m_pMutex); 127 126 128 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 } 127 143 128 144 return *pConnection; … … 133 149 Event_impl::disconnect(Connection_impl* _pConnection) 134 150 { 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 135 155 Threading::CriticalSection guard(m_pMutex); 136 156 … … 146 166 Event_impl::fireEvent(boost::any _argument) 147 167 { 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 } 149 174 } 150 175 … … 182 207 Event_impl::connectScript(boost::any _scriptObject, boost::any _scriptFunction) 183 208 { 184 ScriptAction* pRawAction = new ScriptAction(m_ queue.getScriptObject()->getModule(),209 ScriptAction* pRawAction = new ScriptAction(m_service.getScriptObject()->getModule(), 185 210 _scriptObject, _scriptFunction); 186 211 187 212 pAction_type pAction(pRawAction, boost::bind(&Event_impl::destroyScriptAction, this, _1)); 188 return connect(pAction); 213 214 return connect(pAction, &m_service.getEventQueue("script")); 189 215 } 190 216 -
Core/branches/0075_TR_SCRIPTING/Event/src/EventQueue.cpp
r3309 r3330 46 46 , m_pScriptObject(NULL) 47 47 , m_eventQueue() 48 , m_pEventsMutex(Threading::MutexFactory::create())49 48 { 50 49 } … … 53 52 EventQueue::~EventQueue() 54 53 { 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 if63 // 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 else85 {86 return *iter->second;87 }88 54 } 89 55 -
Core/branches/0075_TR_SCRIPTING/Event/src/EventQueue.hpp
r3309 r3330 55 55 typedef std::pair<Event_impl*, boost::any> Event_type; 56 56 typedef Threading::SynchronizedQueue<Event_type> EventQueue_type; 57 typedef std::map<std::string, Event_impl*> Events_type;58 57 59 58 typedef Memory::managed_ptr<Scripting::I_ScriptModule> pScriptModule_type; … … 63 62 /// @{ 64 63 public: 65 virtual I_Event& createEvent(const std::string& _name);66 virtual I_Event& getEvent(const std::string& _name);67 64 virtual bool dispatchOneEvent(); 68 65 virtual void dispatchAllEvents(bool _wait); … … 101 98 EventQueue_type m_eventQueue; 102 99 103 /// Collection of all registered events.104 Events_type m_events;105 106 /// Mutex for guarding m_events107 Threading::I_Mutex* m_pEventsMutex;108 100 // @} 109 101 -
Core/branches/0075_TR_SCRIPTING/Event/src/EventService.cpp
r3308 r3330 47 47 EventService::EventService() 48 48 : m_pScriptObject(NULL) 49 , m_pEventsMutex(Threading::MutexFactory::create()) 49 50 { 50 51 } … … 70 71 } 71 72 } 73 74 Threading::MutexFactory::destroy(m_pEventsMutex); 72 75 } 73 76 … … 117 120 118 121 module.addType<I_EventService>(getScriptTypeName(), "Event Service") 122 .addMethod("createEvent", &I_EventService::createEvent) 123 .addMethod("getEvent", &I_EventService::getEvent) 119 124 .addMethod("getEventQueue", &I_EventService::getEventQueue) 120 125 .addMethod("getActionMap", &I_EventService::getActionMap) … … 123 128 124 129 // 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) 130 133 ; 131 134 … … 159 162 160 163 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 164 I_Event& 165 EventService::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 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 173 I_Event& 174 EventService::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 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 161 196 I_EventQueue& 162 197 EventService::getEventQueue(const std::string& _queueName) -
Core/branches/0075_TR_SCRIPTING/Event/src/EventService.hpp
r3308 r3330 39 39 class ActionMap; 40 40 class EventQueue; 41 class Event_impl; 41 42 42 43 /// Event Service implementation. … … 51 52 typedef std::map<std::string, EventQueue*> EventQueues_type; 52 53 typedef std::map<std::string, ActionMap*> ActionMaps_type; 54 typedef std::map<std::string, Event_impl*> Events_type; 53 55 /// @} 54 56 … … 69 71 /// @{ 70 72 public: 73 virtual I_Event& createEvent(const std::string& _name); 74 virtual I_Event& getEvent(const std::string& _name); 71 75 virtual I_EventQueue& getEventQueue(const std::string& _queueName); 72 76 virtual I_ActionMap& getActionMap(const std::string& _actionMapName); … … 96 100 EventQueues_type m_eventQueues; 97 101 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 98 109 /// @} 99 110 -
Core/branches/0075_TR_SCRIPTING/Event/src/Event_impl.hpp
r3305 r3330 27 27 #include "../I_Event.hpp" 28 28 29 #include <set> 30 29 31 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 30 32 namespace Zen { … … 48 50 typedef std::list<pConnection_type> container_type; 49 51 typedef Zen::Memory::managed_ptr<Scripting::I_ScriptModule> pScriptModule_type; 52 typedef std::set<EventQueue*> Queues_type; 50 53 /// @} 51 54 … … 53 56 /// @{ 54 57 public: 55 virtual I_Connection& connect(pAction_type _pAction );58 virtual I_Connection& connect(pAction_type _pAction, I_EventQueue* _pQueue = NULL); 56 59 virtual void fireEvent(boost::any _argument); 57 60 /// @} … … 88 91 /// @{ 89 92 protected: 90 Event_impl(EventQueue& _queue); 93 friend class EventService; 94 Event_impl(EventService& _service); 91 95 virtual ~Event_impl(); 92 96 /// @} … … 95 99 /// @{ 96 100 private: 97 /// Queue that owns this event.98 Event Queue& m_queue;101 /// Service that owns this event. 102 EventService& m_service; 99 103 100 104 /// Connections for this event. 101 105 container_type m_connections; 102 106 103 /// Mutex to guard m_connections. 107 Queues_type m_queues; 108 109 /// Mutex to guard m_connections and m_queues 104 110 pMutex_type m_pMutex; 105 111
