Hey,
I have a question regarding the EventManager::AddScriptListener function on page 337 of GCC3;
the code to check for duplicate eventlisteners:-
// OK, valid event type. Make sure this isn't a duplicate
ScriptEventListenerMap::const_iterator mapIter = m_ScriptEventListenerMap.find(eventID);
while(m_ScriptEventListenerMap.end() != mapIter)
{
//Iterate through and ensure no duplicates
const ScriptEventListenerPtr evtListener = mapIter->second;
const LuaObject & evtObj = evtListener->GetHandlerFunction();
if(evtObj == callbackFunction)
{
assert(0 &&"Attempted to listen to the same event handler twice!");
return false;
}
++mapIter;
}
The code finds a map entry with the a particular eventID. then it enters a while loop in which it iterates through the list to check for duplicates, this involves grabbing the entries Callback function and checking to see if it is the same as the one being added by this function.
the code then iterates through the rest of the entries/types in the ListenerMap and checks to see if the function is registered to any other entries. Does this mean that a callback function is only suppost to be registered once and is not suppost to be registered for more than 1 type?
if so why do we search for a particular event type before the loop and not just loop through the whole list?
I have a question regarding the EventManager::AddScriptListener function on page 337 of GCC3;
the code to check for duplicate eventlisteners:-
// OK, valid event type. Make sure this isn't a duplicate
ScriptEventListenerMap::const_iterator mapIter = m_ScriptEventListenerMap.find(eventID);
while(m_ScriptEventListenerMap.end() != mapIter)
{
//Iterate through and ensure no duplicates
const ScriptEventListenerPtr evtListener = mapIter->second;
const LuaObject & evtObj = evtListener->GetHandlerFunction();
if(evtObj == callbackFunction)
{
assert(0 &&"Attempted to listen to the same event handler twice!");
return false;
}
++mapIter;
}
The code finds a map entry with the a particular eventID. then it enters a while loop in which it iterates through the list to check for duplicates, this involves grabbing the entries Callback function and checking to see if it is the same as the one being added by this function.
the code then iterates through the rest of the entries/types in the ListenerMap and checks to see if the function is registered to any other entries. Does this mean that a callback function is only suppost to be registered once and is not suppost to be registered for more than 1 type?
if so why do we search for a particular event type before the loop and not just loop through the whole list?