Hi, I've tried to pass an std::function to a template class but it fails to compile and it's the raison why I don't want to use an std::function.
template <typename ...A> class FastDelegate0 : public Delegate {
friend class FastDelegate;
FastDelegate0 (void(*f)(A...)) {
typedef void(*CB)(Function<void(A...)>, A...);
CB cb = &callback;
funct = new Functor<void(Function<void(A...)>, A...)> (cb, Function<void(A...)>(f));
stdFunct = nullptr;
}
FastDelegate0 (std::function<void(A...)> func) {
typedef void(*CB)(std::function<void(A...)>, A...);
CB cb = &callback;
stdFunct = new Functor<void(std::function<void(A...)>, A...)> (cb, func);
funct = nullptr;
}
};
template <typename R=void> class FastDelegate {
template <typename ...A> FastDelegate(void(*f)(A...)) {
delegate = new FastDelegate0<A...>(f);
}
template <typename ...A> FastDelegate(std::function<void(A...)> func) {
delegate = new FastDelegate0<A...>(func);
}
};
std::function<bool(Vector2f)> sigFunc = [](Vector2f mousePos){
BoundingRectangle br (0, 0, 100, 100);
if (br.isPointInside(Vec2f(mousePos.x, mousePos.y))) {
return true;
}
return false;};
std::function<void(Vector2f)> slotFunc = [](Vector2f mousePos){
std::cout<<"Mouse inside : "<<mousePos.x<<" "<<mousePos.y<<std::endl;};
listener.connect("MouseInside", FastDelegate<bool>(sigFunc), FastDelegate<void>(slotFunc));
And I don't want to have an headache anymore with template arguments.
I don't also want to pass the std::function as a template argument, because I need to reduce the template arguments of my FastDelegate class to store my fast delegates objects into an std::map.
The only inconvenient of that it's that annonymous functions could'nt be used for signal and slot but I don't thing that's a real problem anyway.
PS : In C# there is a way to do that with the FastDelegate class event with anonymous functions but I really don't know how to do that in c++.