List of pointers to methods of derived classes - c++

I'm trying to create the following in C++:
class A {
SpecialContainer<SpecialType> list_of_callables;
void add_to_container(SpecialType *method); // adds method to the SpecialContainer
};
class B : public A {
void method_1();
};
such that a pointer to method_1 can be inserted into the container using, e.g.,
a.add_to_container(&B::method_1)
and consequently be called in an iteration inside A using for instance
# pseudo code
for item in container
(this->*item)();
(Notice that A does not define method_1).
My question is: is this even possible to do, even if with void*, a boost lib or some
C/C++ hack? If yes, how?
(I'm sorry if the answer is obvious, I came back from Python recently).
So far, I tried using
typedef void (A::*SpecialType)();
and SpecialContainer a std::vector, but with no success since method_1
is obviously from B, i.e. I would have to call
add_to_container(&B::method_1)
which is an invalid compilation.

add_to_container(&B::method_1)
This only works if method_1 was a static member of B. The following code works for me (apart from linking, and std::vector instead of your container class):
class A {
public:
typedef void (*SpecialType)();
std::vector<SpecialType> list_of_callables;
void add_to_container(SpecialType method);
};
class B : public A {
public:
static void method_1();
};
int main()
{
A a;
a.add_to_container(&B::method_1);
return 0;
}
If you want functions bound to specific instances of B check out std::bind if you're using C++11, or boost::bind for older C++.

You can't mix pointers to functions with pointers to member functions, so you cannot implement your callback list in that way.
I have implemented this kind of list many times, in form of a event class. My solution to that problem is to dispatch global handlers and member handlers in two distinct ways, and to use dynamic binding when calling the handlers:
//C#-like event class. It supports global functions and member functions as handlers
template<typename SENDER , typename ARGUMMENTS = void>
class event
{
public:
typedef SENDER& sender_param_type;
typedef ARGUMMENTS& argumments_param_type;
private:
struct handler_data
{
virtual void dispatch( sender_param_type , argumments_param_type ) = 0;
virtual ~handler_data() {}
};
struct global_handler_data : public handler_data
{
typedef std::function<void(sender_param_type , argumments_param_type)> global_handler_type;
global_handler_type handler;
global_handler_data( const global_handler_type& handlerrr ) : handler( handlerrr ) {}
void dispatch( sender_param_type sender , argumments_param_type argumments )
{
handler( sender , argumments );
}
};
template<typename HANDLER_CLASS>
struct member_handler_data : public handler_data
{
typedef void(HANDLER_CLASS::*member_handler_type)( sender_param_type , argumments_param_type);
member_handler_type handler;
HANDLER_CLASS& handler_instance;
member_handler_data( HANDLER_CLASS& handlerrr_instance , const member_handler_type& handlerrr ) : handler_instance( handlerrr_instance ) , handler( handlerrr ) {}
void dispatch( sender_param_type sender , argumments_param_type argumments )
{
(handler_instance.*handler)( sender , argumments );
}
};
std::vector<std::unique_ptr<handler_data>> _handlers;
public:
void add_handler( const typename global_handler_data::global_handler_type& handler )
{
_handlers.push_back( std::unique_ptr<handler_data>( new global_handler_data( handler ) ) );
}
template<typename HANDLER_CLASS>
void add_handler( HANDLER_CLASS& handler_instance , const typename member_handler_data<HANDLER_CLASS>::member_handler_type& handler )
{
_handlers.push_back( std::unique_ptr<handler_data>( new member_handler_data<HANDLER_CLASS>( handler_instance , handler ) ) );
}
void raise_event( sender_param_type sender , argumments_param_type argumments )
{
for(auto& handler : _handlers )
{
handler->dispatch( sender , argumments );
}
}
};
//Non-args evets specialization:
template<typename SENDER>
class event<SENDER,void>
{
public:
typedef SENDER& sender_param_type;
private:
struct handler_data
{
virtual void dispatch( sender_param_type ) = 0;
virtual ~handler_data() {}
};
struct global_handler_data : public handler_data
{
typedef std::function<void(sender_param_type)> global_handler_type;
global_handler_type handler;
global_handler_data( const global_handler_type& handlerrr ) : handler( handlerrr ) {}
void dispatch( sender_param_type sender )
{
handler( sender );
}
};
template<typename HANDLER_CLASS>
struct member_handler_data : public handler_data
{
typedef void(HANDLER_CLASS::*member_handler_type)( sender_param_type );
member_handler_type handler;
HANDLER_CLASS& handler_instance;
member_handler_data( HANDLER_CLASS& handlerrr_instance , const member_handler_type& handlerrr ) : handler_instance( handlerrr_instance ) , handler( handlerrr ) {}
void dispatch( sender_param_type sender )
{
(handler_instance.*handler)( sender );
}
};
std::vector<std::unique_ptr<handler_data>> _handlers;
public:
void add_handler( const typename global_handler_data::global_handler_type& handler )
{
_handlers.push_back( std::unique_ptr<handler_data>( new global_handler_data( handler ) ) );
}
template<typename HANDLER_CLASS>
void add_handler( HANDLER_CLASS& handler_instance , const typename member_handler_data<HANDLER_CLASS>::member_handler_type& handler )
{
_handlers.push_back( std::unique_ptr<handler_data>( new member_handler_data<HANDLER_CLASS>( handler_instance , handler ) ) );
}
void raise_event( sender_param_type sender )
{
for(auto& handler : _handlers )
{
handler->dispatch( sender );
}
}
};
As you can see, the class is designed to dispatch events with two parameters: A reference to the object which raised the event, and the event parameters.
You could use C++11 variadic-templates instead of one aggregate event parameter only, but I have done in this manner because the class had to be compatible with MSVC11.
The class provides exactly the same interface to manage global handlers and member handlers, the add_handler() function is overloaded in that way. The only difference is that member handlers need one object to be called with, so the handler of the event stores a reference to a caller object specified by the user when registers the member handler.
Finally, the class is specialised to allow the user create events without parameters.
Here is an example of its usage:
class rabbit
{
void jump()
{
up.raise
}
}

Related

Polymorphism and function binding

For an event system i'm writing i want to bind callbacks to a list of functions.
Here is a basic example of what i want to do:
#include <iostream>
#include <functional>
#include <string>
class Base {
public:
virtual std::string getType() const = 0;
};
class Derived : public Base {
protected:
int some_data;
public:
Derived(int some_data): some_data(some_data) {}
virtual std::string getType() const {
return "Derived";
}
int getData() const {
return this->some_data;
}
};
class DerivedTwo : public Base {
protected:
double some_data;
public:
DerivedTwo(double some_data): some_data(some_data) {}
virtual std::string getType() const {
return "DerivedTwo";
}
// The type of data is not always the same.
double getData() const {
return this->some_data;
}
};
// The type of member should ALWAYS be Derived but then i can't store it in <callback>
void onDerivedEvent(Base& member) {
std::cout << member.getType() << std::endl;
// This is obviously not possible with member being a base class object
// member.getData();
}
// The type of member should ALWAYS be DerivedTwo but then i can't store it in <callback>
void onDerivedTwoEvent(Base& member) {
std::cout << member.getType() << std::endl;
}
int main() {
std::function<void(Base&)> callback;
callback = std::bind(onDerivedEvent, std::placeholders::_1);
callback(Derived(2));
callback = std::bind(onDerivedTwoEvent, std::placeholders::_1);
callback(DerivedTwo(3.0));
return 0;
}
The only thing i would like to change is that onCallback() should take a derived class member as argument instead of a reference to a base object, so i can call getData() for example.
In this example this would mean:
void onCallback(Derived& derived);
However, if i do this, i can no longer bind() the method to callback because the argument types are not matching.
Does anyone know how to make this work?
// EDIT
Sorry for the confusion here, i updated the source code with some more specifics and examples to maybe clarify what im doing here.
Note:
Since it seems like this is very relevant, here is the specific use case for what i'm trying to do here:
It's part of an event system for an engine i'm building. There are basic events pre-defined but it should be extendable with more specific events by a user using this engine. So there is not definitive list of derived classes. Then some object can subscribe to a specific event type and whenever the central event bus recieves such an event, it calls all subscribed callback functions with the event as argument. The reason i am not adding a one and for all handle function in the derived class is, the events an be used in multiple ways.
Answers to some questions from the comments:
What should happen if you pass onCallback an object that isn't that specific Derived&? (ie, add a Derived2 which has a doStuff2. Pass it to callback. What do you want to happen?
That should not be possible.
I might have not calrified that and also had a misleading information at the beginning which i have editted since then. The type of the passed derived class is always known beforehand. For example: onKeyEvent will always recieve a KeyEvent object and not a base class object or any other derived variants.
However, the variable to which this function is bound should be able to store functions which accept different derived classes from Base
This is my storage for all events:
std::map<EventType, std::list<std::function<void(const Event&)>>> listener_map;
Why isn't onCallback a method in Base that Derived overrides
I answered this in a comment. ...The reason i am not adding a one and for all handle function in the derived class is, the events an be used in multiple ways...
Meaning, i might have an KeyEvent which has the data to a key (which key, is it pressed/released/held) and the listening function(s) can use this data for whatever it wants. (Check if some specific key is pressed, chech if any random key is pressed and so on.) Some other events might not have any data at all and just notify a listener that something happened or have multiple sets of data etc.
Is there, or can there be, a finite, bounded at compile time, central list of all of the types that derive from Base at any point in your code?
In theory yes. During compilation there will be a finite number of Derived classes. However these might be different for the compilation of the library and the compilation of the project using this library.
template<class Base>
struct poly_callback {
template<class T>
static poly_callback make( std::function<void(T&)> f ) {
return { std::function<void(void*)>( [f]( void* ptr ) { f(*static_cast<T*>(static_cast<Base*>(ptr))); }) };
}
template<class T>
poly_callback( void(*pf)(T&) ):poly_callback( make<T>( pf ) ) {}
poly_callback( poly_callback const& ) = default;
poly_callback( poly_callback && ) = default;
void operator()( Base& b ) {
return type_erased( static_cast<void*>(std::addressof(b)) );
}
private:
std::function<void(void*)> type_erased;
poly_callback( std::function<void(void*)> t ):type_erased(std::move(t)) {}
};
A poly_callback<Event> can store a callable with signature compatible to void(Derived&), where Derived is derived from Event. It must be called with exactly an instance of the Derived& type or undefined behavior results as it blindly downcasts.
Stop using std::bind, it is functionally obsolete.
class Base {
public:
virtual std::string getType() const = 0;
};
class Derived : public Base {
protected:
int some_data;
public:
Derived(int some_data): some_data(some_data) {}
virtual std::string getType() const {
return "Derived";
}
int getData() const {
return this->some_data;
}
};
class DerivedTwo : public Base {
protected:
double some_data;
public:
DerivedTwo(double some_data): some_data(some_data) {}
virtual std::string getType() const {
return "DerivedTwo";
}
// The type of data is not always the same.
double getData() const {
return this->some_data;
}
};
// The type of member should ALWAYS be Derived but then i can't store it in <callback>
void onDerivedEvent(Derived& member) {
std::cout << member.getType() << "\n";
std::cout << member.getData() << "\n";
}
// The type of member should ALWAYS be DerivedTwo but then i can't store it in <callback>
void onDerivedTwoEvent(DerivedTwo& member) {
std::cout << member.getType() << "\n";
std::cout << member.getData() << "\n";
}
struct callbacks {
std::unordered_map< std::string, std::vector< poly_callback<Base> > > events;
void invoke( std::string const& name, Base& item ) {
auto it = events.find(name);
if (it == events.end())
return;
for (auto&& f : it->second)
f( item );
}
template<class Derived>
void connect( std::string const& name, void(*pf)(Derived&) )
{
events[name].push_back( pf );
}
template<class Derived>
void connect_T( std::string const& name, std::function<void(Derived&)> f )
{
events[name].push_back( std::move(f) );
}
};
int main() {
callbacks cb;
cb.connect("one", onDerivedEvent );
cb.connect("two", onDerivedTwoEvent );
Derived d(7);
DerivedTwo d2(3.14);
cb.invoke( "one", d );
cb.invoke( "two", d2 );
return 0;
}
Live example.
This can be tweaked for safety and usability. For example, check that the typeid actually matches.
Output is:
Derived
7
DerivedTwo
3.14
and as you can see, the callback functions take Derived& and DerivedTwo& objects.
In my experience this is a bad plan.
Instead, have a broadcaster<KeyboardEvent> keyboard; and don't look up your event registry systems with strings.
A map from string-to-callback only makes sense if there is some way to treat the callbacks uniformly. And you don't want to treat these callbacks uniformly. Even if you chose to store them uniformly for efficiency sake (useful in ridiculously huge frameworks), I'd want type-safe APIs not a map.

Counting how many times a function is called

I want to count how many times a function is called, so that if it is called more than once, some error is notified to the developers (via logging, or asserting, and so on). I would like this code to be well defined and isolated so that it is easily portable across many function and member functions as well. Something as easy as:
void function( )
{
if( is_called_more_than_once( ) )
{
// do something to handle the error
}
}
void AClass::method( )
{
if( is_called_more_than_once( ) )
{
// do something to handle the error
}
}
Is it possible in C++, since it does not have reflection, to achieve something like that somehow?
For standalone functions, or static class methods, you can use a static local variable:
void function()
{
static int num_called = 0;
if( ++num_called > 1 )
{
// do something to handle the error
}
...
}
For non-static class methods, use a class data member for each method, allowing individual instances of the class to do their own tracking:
class AClass
{
private:
int num_method1_called;
int num_method2_called;
public:
AClass();
void method1();
void method2();
...
};
AClass::AClass() :
num_method1_called(0),
num_method2_called(0)
{
}
void AClass::method1()
{
if( ++num_method1_called > 1 )
{
// do something to handle the error
}
...
}
void AClass::method2()
{
if( ++num_method2_called > 1 )
{
// do something to handle the error
}
...
}
If the error handling is always the same, consider extracting it to a reusable helper:
struct callTracker
{
int counter;
callTracker() : counter(0) {}
void called()
{
if( ++counter > 1 )
{
// do something to handle the error
}
}
};
void function( )
{
static callTracker tracker;
tracker.called();
...
}
class AClass
{
private:
callTracker method1_tracker;
callTracker method2_tracker;
public:
void method1();
void method2();
...
};
void AClass::method1()
{
method1_tracker.called();
...
}
void AClass::method2()
{
method2_tracker.called();
...
}
Alternatively:
struct singleCallTracker
{
int counter;
singleCallTracker() : counter(0) {}
void called()
{
if( ++counter > 1 )
{
// do something to handle the error
}
}
};
struct multiCallTracker
{
std::map<std::string, singleCallTracker> trackers;
void called(const std::string &name)
{
trackers[name].called();
}
};
void function()
{
static singleCallTracker tracker;
tracker.called();
...
}
class AClass
{
private:
multiCallTracker method_tracker;
public:
void method1();
void method2();
...
};
void AClass::method1()
{
method_tracker.called(__FUNC__);
...
}
void AClass::method2()
{
method_tracker.called(__FUNC__);
...
}
Reflection isn't necessary, because the caller is known as compile time. C++11 has a built-in __func__ which evaluates as a plain-C string that is the unadorned name of the function. There is also typeid(*this).name() to get the mangled name of the class behind *this.
So define a class that maintains a set of strings and has a single method, announce_call. Put an instance of that class in every class you define, possibly by inheriting from someone that has a protected instance.
Call announce_call providing the function name and class name. If the function name is already within the set, log an error with it and the class name. Otherwise add it to the set.
Provide a global instance if desired, for functions that do not belong to classes.
So net per-class syntax cost is: adding an extra base class, adding an extra line at the start of every counted function.
Inheritance provides the main caveat: since this always points to an instance of the thing the function belongs to, if B inherits from A and C owns an instance B but calls a method from A twice, the log will show a double call to A rather than to B.
I think this is as close as you can get with a single macro:
#define if_called_more_than_once() \
static int s_nTimesCalled = 0; if (++s_nTimesCalled > 1)
void function( )
{
if_called_more_than_once()
{
// do something to handle the error
}
}

Does C++0x support Anonymous Inner Classes?

Say I have listeners built in C++98, they are abstract and must for example implement ActionPerformed. In C++0x is there a way to do similar to Java:
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
// do something.
}
});
Thanks
Not exactly, but you can do something close with Lambdas.
i.e.:
class ActionListener
{
public:
typedef std::function<void(ActionEvent&)> ActionCallback;
public:
ActionListener( ActionCallback cb )
:_callback(cb)
{}
void fire(ActionEvent& e )
{
_callback(e);
}
private:
ActionCallback _callback;
};
..
button.addActionListener( new ActionListener(
[]( ActionEvent& e )
{
...
}
));
No you can't do that.
If you give up on "similar to Java", though, and just use a functor, you'll find C++11 lambdas very helpful.
This is C++, not Java, so writing C++ like Java won't work well.
Anyway, you could create an adaptor function. Suppose
typedef int ActionEvent; // <-- just for testing
class ActionListener
{
public:
virtual void actionPerformed(const ActionEvent& event) = 0;
};
Then we could write a templated subclass of ActionListener that wraps a function object:
#include <memory>
template <typename F>
class ActionListenerFunctor final : public ActionListener
{
public:
template <typename T>
ActionListenerFunctor(T&& function)
: _function(std::forward<T>(function)) {}
virtual void actionPerformed(const ActionEvent& event)
{
_function(event);
}
private:
F _function;
};
template <typename F>
std::unique_ptr<ActionListenerFunctor<F>> make_action_listener(F&& function)
{
auto ptr = new ActionListenerFunctor<F>(std::forward<F>(function));
return std::unique_ptr<ActionListenerFunctor<F>>(ptr);
}
and then use make_action_listener to wrap a lambda, e.g ( http://ideone.com/SQaLz ).
#include <iostream>
void addActionListener(std::shared_ptr<ActionListener> listener)
{
ActionEvent e = 12;
listener->actionPerformed(e);
}
int main()
{
addActionListener(make_action_listener([](const ActionEvent& event)
{
std::cout << event << std::endl;
}));
}
Note that this is far from idiomatic C++, where in addActionListener() you should simply take a const std::function<void(const ActionEvent&)>&, or even a template parameter for maximum efficiency, and supply the lambda directly.
I think we can do this in C++ using lambdas
button.addActionListener([]()->ActionListener*{ struct A: ActionListener {
void actionPerfored(ActionEvent e)
{
// do something.
}
}; return new A;}());
It should be easy to wrap this up in a macro.

multi-signatures signal management in user classes

I'm very familiar with Qt and I know that we cannot have a similar synthax because we don't have the MOC part here. However I'm trying to have a signal creation management to simplify the declaration of a signal and the connection to it, inside my classes.
this is schematicly what I'm doing now
class Foo
{
public:
void connectMove(boost::signal<void(int)>::slot_type slot)
void connectRotate(boost::signal<void(double)>::slot_type slot)
private:
boost::signal<void(int)> m_signalMove;
boost::signal<void(double)> m_signalRotate;
};
and this is basicaly what I would like to do (UPPERCASE = missing part)
class SignalManager
{
public:
typedef boost::unrodered_map<std::string, GENERIC_SIGNAL *> MapSignal;
public:
template <typename Sig>
bool connect(const std::string& strSignalName, boost::signal<Sig>::slot_type slot)
{
// simplyfied... :
(*m_mapSignal.find(strSignalName))->connect(slot);
}
template <typename Sig>
bool disconnect(const std::string& strSignalName, boost::signal<Sig>::slot_type slot)
{
// simplyfied... :
(*m_mapSignal.find(strSignalName))->disconnect(slot);
}
protected:
bool call(const std::string& strSignalName, SIGNAL_ARGS)
{
(*m_mapSignal.find(strSignalName))(SIGNAL_ARGS);
}
template <typename Sig>
void delareSignal(const std::string& strSignalName)
{
m_mapSignals.insert(MapSignal::value_type(strSignalName, new boost::signal<Sig>()));
}
void destroySignal(const std::string& strSignalName)
{
// simplyfied... :
auto it = m_mapSignal.find(strSignalName);
delete *it;
m_mapSignal.erase(it);
}
private:
MapSignal m_mapSignals;
};
class Foo : public SignalManager
{
public:
Foo(void)
{
this->declareSignal<void(int)>("Move");
this->declareSignal<void(double)>("Rotate");
}
};
class Other : public boost::signals::trackable
{
public:
Other(Foo *p)
{
p->connect("Move", &Other::onMove);
p->connect("Rotate", &Other::onRotate);
}
void onMove(int i)
{
/* ... */
}
void onRotate(double d)
{
/* ... */
}
};
I think I could resolve the "SIGNAL_ARGS" part with boost::functions_traits<>, but i don't know how to go around the abstract signal type.
1/ Is what I want even possible ?
2/ Is this a good approach ? (I know I will have some overhead due to the unordered_map.find, esspecily when I use this->call("signalname", ...), but I think it shouldn't be too significant)
3/ If this is not possible or not a good approach, do you have any other suggestions ?
I resolved my problem by wrapping boost::signals and having a boost::shared_ptr<IWrapperSignal> instead of my GENERIC_SIGNAL.
The arguments probnlem was also resolved using boost::function_traits<T>::arg_type.
I don't know if it's the best way to do that, but it's working fine, and it is more simple for the user to declare signals in the classes that inherit this SignalManager.

How to define a general member function pointer

I have created a Timer class that must call a callback method when the timer has expired. Currently I have it working with normal function pointers (they are declared as void (*)(void), when the Elapsed event happens the function pointer is called.
Is possible to do the same thing with a member function that has also the signature void (AnyClass::*)(void)?
Thanks mates.
EDIT: This code has to work on Windows and also on a real-time OS (VxWorks) so not using external libraries would be great.
EDIT2: Just to be sure, what I need is to have a Timer class that take an argument at the Constructor of tipe "AnyClass.AnyMethod" without arguments and returning void. I have to store this argument and latter in a point of the code just execute the method pointed by this variable. Hope is clear.
Dependencies, dependencies... yeah, sure boost is nice, so is mem_fn, but you don't need them. However, the syntax of calling member functions is evil, so a little template magic helps:
class Callback
{
public:
void operator()() { call(); };
virtual void call() = 0;
};
class BasicCallback : public Callback
{
// pointer to member function
void (*function)(void);
public:
BasicCallback(void(*_function)(void))
: function( _function ) { };
virtual void call()
{
(*function)();
};
};
template <class AnyClass>
class ClassCallback : public Callback
{
// pointer to member function
void (AnyClass::*function)(void);
// pointer to object
AnyClass* object;
public:
ClassCallback(AnyClass* _object, void(AnyClass::*_function)(void))
: object( _object ), function( _function ) { };
virtual void call()
{
(*object.*function)();
};
};
Now you can just use Callback as a callback storing mechanism so:
void set_callback( Callback* callback );
set_callback( new ClassCallback<MyClass>( my_class, &MyClass::timer ) );
And
Callback* callback = new ClassCallback<MyClass>( my_class, &MyClass::timer ) );
(*callback)();
// or...
callback->call();
The best solution I have used for that same purpose was boost::signal or boost::function libraries (depending on whether you want a single callback or many of them), and boost::bind to actually register the callbacks.
class X {
public:
void callback() {}
void with_parameter( std::string const & x ) {}
};
int main()
{
X x1, x2;
boost::function< void () > callback1;
callback1 = boost::bind( &X::callback, &x1 );
callback1(); // will call x1.callback()
boost::signal< void () > multiple_callbacks;
multiple_callbacks.connect( boost::bind( &X::callback, &x1 ) );
multiple_callbacks.connect( boost::bind( &X::callback, &x2 ) );
// even inject parameters:
multiple_callbacks.connect( boost::bind( &X::with_parameter, &x1, "Hi" ) );
multiple_callbacks(); // will call x1.callback(), x2.callback and x1.with_parameter("Hi") in turn
}
Maybe the standard mem_fun is already good enough for what you want. It's part of STL.
boost::function looks like a perfect fit here.
I'm assuming an interface like this:
void Timer::register_callback( void(*callback)(void*user_data), void* user_data );
template<typename AnyClass, (AnyClass::*Func_Value)(void)>
void wrap_method_callback( void* class_pointer )
{
AnyClass*const self = reinterpret_cast<AnyClass*>(class_pointer);
(self->*Func_Value)();
}
class A
{
public:
void callback()
{ std::cout << m_i << std::endl; }
int m_i;
};
int main ()
{
Timer t;
A a = { 10 };
t.register_callback( &wrap_method_callback<A,&A::callback>, &a );
}
I think a better solution would be to upgrade call you callback to either use boost::function or a homegrown version (like Kornel's answer). However this require real C++ developers to get involved, otherwise you are very likely to introduce bugs.
The advantage of my solution is that it is just one template function. Not a whole lot can go wrong. One disadvantage of my solution is it may slice your class with cast to void* and back. Be careful that only AnyClass* pointers are passes as void* to the callback registration.