Here’s a simple events system I’ve made using multimaps; When I use the CEvents::Add(..) method, it should insert and entry into the multimap. The thing is, when I trigger those events, the multimap appears to be empty. I’m sure I didn’t call the delete method [CEvents::Remove]. Here’s the code:
//Code:
..
CEvents Ev;
Ev.Add("onButtonBReleased",OutputFST);
..
// "CEvents.h"
class CEvents
{
public:
void Add ( string EventName, void(*fn)(void));
void Remove ( string EventName, void(*fn)(void));
void Trigger ( string EventName );
//protected:
bool Found;
std::multimap<string,void(*)(void)> EventsMap;
std::multimap<string,void(*)(void)>::iterator EvMapIt;
};
//CEvents.cpp
void CEvents::Add (string EventName, void (*fn)(void))
{
if (!EventsMap.empty())
{
Found = false;
for (EvMapIt = EventsMap.begin(); EvMapIt != EventsMap.end(); EvMapIt++)
{
if ((EvMapIt->first == EventName) && (EvMapIt->second == fn))
{
CTools tools;
tools.ErrorOut("Function already bound to same event... Not registering event");
Found = true;
}
}
if (!Found)
{
EventsMap.insert(std::pair<string,void(*)(void)>(EventName,fn));
std::cout<<"Added, with size "<<(int) EventsMap.size()<<std::endl; //Getting 1
}
}
else
{
EventsMap.insert (std::pair<string,void(*)(void)>(EventName,fn));
std::cout<<"Added, with size "<<(int) EventsMap.size()<<std::endl; //Getting 1
}
}
void CEvents::Trigger (string EventName)
{
std::cout<<"Triggering init"<<std::endl;
std::cout<<(int) EventsMap.size()<<std::endl; //Getting 0
for (EvMapIt = EventsMap.begin(); EvMapIt != EventsMap.end(); EvMapIt++)
{
std::cout<<"Triggering proc"<<std::endl;
if (EvMapIt->first == EventName)
EvMapIt->second();
}
}
Ok, finally solved it; For future reference:
What happened is that every time an instance is declared, it redefines the multimap.
The solution is to create a global pointer to the class.
Thanks to everyone who answered!