How to deal with calling a member function from a static callback Ctype function

A

Angus

Hi all

I am using a C style function which requires a 'callback' function to
be defined. This callback function is called whenever the interface
requests new data.

The function is a bit like this:

create_object(<callback_function>)

I have a class, MyClass, which uses this functionality so I have an
Init() member function which calls create_object.

However the callback_function cannot be a member function - so I make
it a static.

I want the callback_function to call a member function - but a static
function contains no this pointer - so what to do? At present I have
a global pointer to the class which gets initialised when I create the
class. But this does not seem like the most elegant design.

There seems no way to get the C callback to send eg an instance
variable where I could tuck a pointer to the class.

What is the most elegant way to solve this sort of problem?

Angus
 
P

Pascal J. Bourguignon

Angus said:
Hi all

I am using a C style function which requires a 'callback' function to
be defined. This callback function is called whenever the interface
requests new data.

Any C++ function that doesn't throw can be called as a C function.
The function is a bit like this:

create_object(<callback_function>)

I have a class, MyClass, which uses this functionality so I have an
Init() member function which calls create_object.

However the callback_function cannot be a member function - so I make
it a static.

I want the callback_function to call a member function - but a static
function contains no this pointer - so what to do?

You need a closure. (If you could use gcc implementation specific stuff).

At present I have
a global pointer to the class which gets initialised when I create the
class. But this does not seem like the most elegant design.

There seems no way to get the C callback to send eg an instance
variable where I could tuck a pointer to the class.

What is the most elegant way to solve this sort of problem?

Since there is no closure in standard C, the usual way to design a
callback interface is to take in addition to the function, a pointer
that will be passed to the callback function.

If there are other parameters passed to the callback, perhaps they may
contain some user data that you could use?


Otherwise, you could use a package such as LLVM to generate closures
yourself. It's basically a trampoline function that stores the data
with itself, so you need a copy of each trampoline for each couple
(function,data) to be used as callback.


So, what is the type of <callback_function>?
 
C

Christopher

Hi all

I am using a C style function which requires a 'callback' function to
be defined.  This callback function is called whenever the interface
requests new data.

The function is a bit like this:

create_object(<callback_function>)

I have a class, MyClass, which uses this functionality so I have an
Init() member function which calls create_object.

However the callback_function cannot be a member function - so I make
it a static.

I want the callback_function to call a member function - but a static
function contains no this pointer - so what to do?  At present I have
a global pointer to the class which gets initialised when I create the
class.  But this does not seem like the most elegant design.

There seems no way to get the C callback to send eg an instance
variable where I could tuck a pointer to the class.

What is the most elegant way to solve this sort of problem?

I pass a pointer to my class, to the callback
I then make the callback a static member of a base class
I have the callback function call another non static method of the
base class whose pointer was passed
The non static method can be virtual when I want to derive from the
base class
 
J

James Kanze

Any C++ function that doesn't throw can be called as a C
function.

That's completely wrong. First of all, of course, non-static
member functions can't be called without an object, so they
can't possibly be called as a C function. Secondly, C and C++
functions (or more precisely, functions with C linkage and with
C++ linkage) have different types, so you can't use one where
the other is expected. And since a member function always has
C++ linkage, even if it is static, you can't call it as a C
function either.

In order to call a C++ function as a C function, the C++
function must be a free function or a static member, the
implementations of the two languages must use the same calling
conventions, and the call itself must either be in assembler
code, or you must take advantage of an error in the compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top