I have a list of boost::function objects and am trying to find a specific one so I can remove it from the list. Effectively a function is registered (pushed onto a vector) and I wish to be able to unregister it (search the vector and remove the matching function pointer). Here is the code:
#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
class DummyClass
{
public:
std::string Data;
};
typedef boost::shared_ptr<DummyClass> DummyClassPtrType;
class UpdaterClass
{
public:
void handle(DummyClassPtrType Dummy);
};
class ManagerClass
{
public:
typedef boost::function<void (DummyClassPtrType Dummy)> HandlerFunctionType;
typedef std::vector<HandlerFunctionType> HandlerFunctionListType;
//
HandlerFunctionListType HandlerFunctionList;
void registerHandler(HandlerFunctionType Handler)
{
HandlerFunctionList.push_back(Handler);
}
void unRegister(HandlerFunctionType Handler)
{
// find the function pointer in the list and delete it from the list if found
HandlerFunctionListType::iterator HandlerIter = HandlerFunctionList.begin();
while (HandlerIter != HandlerFunctionList.end())
{
if (*HandlerIter == Handler) // error C2666: 'boost::operator ==' : 4 overloads have similar conversions
{
HandlerIter = HandlerFunctionList.erase(HandlerIter);
break;
}
else
{
++HandlerIter;
}
}
}
};
int main()
{
ManagerClass Manager;
UpdaterClass Updater;
Manager.registerHandler(boost::bind(&UpdaterClass::handle, &Updater, _1));
Manager.unRegister(boost::bind(&UpdaterClass::handle, &Updater, _1));
return 0;
}
The compiler (VS2008 SP1) doesn't like the line:
if (*HandlerIter == Handler)
and I can't figure out how to achieve this.