Use std::function for callbacks
Moved from bug 3012
Partially addresses #194 (closed)
I spent some time to modernize the implementation of callbacks by using C++11 std::function. My goals were to:
- make the code in callback.h more readable and compact
- allow the creation of callbacks pointing to lambdas and objects returned by std::bind
- remove the limitation on 3 bound parameters
The overall design is mostly untouched. Instead of having a CallbackImpl<> class and 5 subclasses, we have a single CallbackImpl<> class that stores any kind of callable objects as a std::function. Given that std::function objects cannot be compared, I thought that CallbackImpl<> could also keep a vector of callback "components": the original function and every bound argument. Callbacks are compared by comparing such components one by one. Partial specialization of the CallbackComponent class enables to avoid the comparison of lambdas (not allowed by compilers) and always return false.
Still, it is possible to check when two callbacks point to the same lambda. The traced-callback test has been modified to show that this can be done by simply storing a CallbackBase object (slicing works because the implementation is kept).
Apart from three nasty attempts at casting 0 to a Callback, no other changes are required. Changes to the tests are mainly additions to showcase what can be done with callbacks.
All the tests and examples run fine I verified that there are no memory leaks and I also successfully scanned the python bindings .
Switching to C++14 would further simplify this patch, but I leave it for a subsequent MR.