Game Development Reference
In-Depth Information
Listing 21-7. The
Event::Send
Method
void Event::Send()
{
for (auto& listener : m_listeners)
{
if (listener != nullptr)
{
listener->HandleEvent(this);
}
}
}
The
Send
method is straightforward. It simply loops all over the
m_listeners vector
and calls the
HandleEvent
method on each valid entry.
The
SendToHandler
method is given in Listing 21-8. It sends the message to a single
EventHandler
rather than all of them.
Listing 21-8. The
Event::SendToHandler
Method
void Event::SendToHandler(EventHandler& eventHandler)
{
auto found = std::find(m_listeners.begin(), m_listeners.end(), &eventHandler);
if (found != m_listeners.end())
{
(*found)->HandleEvent(this);
}
}
This time we use the find STL algorithm to get an
iterator
to the entry that stores our
EventHandler
and then call
HandleEvent
if it is found.
Now that we have an interface for
EventHandlers
and a class to represent
Events
, we can look at
how the
EventManager
class is implemented.
The
EventManager
Implementation
Listing 21-9 shows the member variables and methods contained in the
EventManager
class.
Listing 21-9. The
EventManager
Members and Methods
class EventManager
: public Singleton<EventManager>
{
friend void SendEvent(EventID eventId);
template <typename T>
friend void SendEventToHandler(EventID eventId, T* eventHandler);
friend bool RegisterEvent(EventID eventId);