Game Development Reference
In-Depth Information
void EventManager::SendEventToHandler(EventID eventId, EventHandler& eventHandler)
{
EventMapIterator result = m_eventMap.find(eventId);
if (result != m_eventMap.end())
{
assert(result->second);
if (result->second)
{
result->second->SendToHandler(eventHandler);
}
}
}
The destructor for the
EventManager
class is also interesting. It is responsible for cleaning up
the
Event
objects in the
unordered_map
. As they were created using the
new
keyword, they must
be destroyed using the
delete
keyword (I cover this in detail in Chapter 24). The
EventManager
destructor is given in Listing 21-13.
Listing 21-13. The
EventManager
Destructor
EventManager::~EventManager()
{
for (EventMapIterator iter = m_eventMap.begin(); iter != m_eventMap.end(); ++iter)
{
Event* pEvent = iter->second;
if (pEvent)
{
delete pEvent;
iter->second = nullptr;
}
}
m_eventMap.clear();
}
The destructor iterates over the
m_eventMap
variable, deletes each
Event
, and sets the second
member on the
iterator
to be
nullptr
.
Now I can show you the
public friend
functions that wrap the
EventManager private
methods.
Listing 21-14 shows the code for the
SendEvent friend
function.
Listing 21-14. The
SendEvent friend
Function
inline void SendEvent(EventID eventId)
{
EventManager* pEventManager = EventManager::GetSingletonPtr();
assert(pEventManager);
if (pEventManager)
{
pEventManager->SendEvent(eventId);
}
}