Using a C++ class member function as a C callback function
You can do that if the member function is static.
Non-static member functions of class A have an implicit first parameter of type class A*
which corresponds to this pointer. That's why you could only register them if the signature of the callback also had the first parameter of class A*
type.
How to use a C++ member function as the callback function for a C framework
Does callback function need to be declared under extern "C"?
NO. extern "C" is necessary only when you are calling a C++ function directly, without the use of function pointers, from C. If function pointers are used, extern "C" is not required.
Can I use non-static member functions as a callback?
NO. Non-static member functions of class A have an implicit first parameter corresponding to this pointer.
Can I use static member functions as a callback?
YES, as long as signature matches with that of the callback.
Does it matter if the function is a template function or not?
NO, template function can be used as callbacks as long as the signature of the instantiated template matches with the callback.
Map C++ member function to C callback
Use the user_data
pointer to give you back a pointer to the class that owns the callback. For example.
void MyCallback(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data)
{
static_cast<Example *>(user_data)->Callback(port, interrupt_mask, value_mask);
}
class Example
{
public:
Example()
{
//setup callback here - i dont know what library you're using but you should be able to pass a this pointer as user_data
init_lib(fictionalparameter1, fictionalparameter2, this);
}
private:
void Callback(char port, uint8_t interrupt_mask, uint8_t value_mask)
{
//callback handler here...
}
friend void MyCallback(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data);
};
How can I pass a class member function as a callback?
That doesn't work because a member function pointer cannot be handled like a normal function pointer, because it expects a "this" object argument.
Instead you can pass a static member function as follows, which are like normal non-member functions in this regard:
m_cRedundencyManager->Init(&CLoggersInfra::Callback, this);
The function can be defined as follows
static void Callback(int other_arg, void * this_pointer) {
CLoggersInfra * self = static_cast<CLoggersInfra*>(this_pointer);
self->RedundencyManagerCallBack(other_arg);
}
Calling C++ class methods from C using callbacks
The classical C way of passing callbacks is to pass two values: a pointer to the callback itself, and an opaque pointer which will be passed to the callback as an additional argument (take a look at qsort_r
for example). When interfacing with C++, that opaque value may be used as instance pointer; you will only need to write a thin wrapper:
class B {
void my_callback(int arg);
static void my_callback_wrapper(int arg, void *u) {
((B*)u)->my_callback(arg);
}
};
// or even:
extern "C" void my_callback_wrapper(int arg, void *u) {
((B*)u)->my_callback(arg);
}
and pass a pointer to the wrapper, along with a pointer to the object, to the C part. Be careful to use the exact same class type on both sides and not base/derived classes, for example.
Note that while it may be possible to get a pointer to the (non-static) method itself, on some compilers (tested on MSVC a long time ago) they have a special calling convention so that the pointer will not be compatible with any normal function pointer.
call a C++ method from a C callback
A library will usually allow you to provide a void *
value as a "context", which is then passed to the callback. If the library doesn't provide that functionality (huh?), you can use a thread-local variable (also known as a global variable in single-threaded programs) instead.
This is how the code will often look like.
class A {
public:
static void static_callback(void * ctx) {
static_cast<A*>(ctx)->callback();
}
void callback();
};
extern void library_function(void (*callback)(void *), void * ctx);
int main()
{
A a;
library_function(&A::static_callback, &a);
}
How to pass a pointer to a member function to a C function?
Kindly refer to detailed topic about
How to Implement a Callback to a static C++ Member Function ?
How to Implement a Callback to a non-static C++ Member Function ?
http://www.newty.de/fpt/callback.html
Related Topics
Why Doesn't Adl Find Function Templates
Deleting Elements from Std::Set While Iterating
What Constitutes a Valid State For a "Moved From" Object in C++11
Should the Trailing Return Type Syntax Style Become the Default For New C++11 Programs
How to Turn on (Literally) All of Gcc'S Warnings
C++ Syntax For Explicit Specialization of a Template Function in a Template Class
How to Compile For Windows on Linux With Gcc/G++
Why Can't We Declare a Std::Vector≪Abstractclass≫
Const VS Constexpr on Variables
Accessing Inherited Variable from Templated Parent Class
How to Reassign the Reference in C++
Is Main() Really Start of a C++ Program
When Were the 'And' and 'Or' Alternative Tokens Introduced in C++