f



How to deal with calling a member function from a static callback C type function

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
0
anguscomber (130)
2/27/2009 10:25:20 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
818 Views

Similar Articles

[PageSpeed] 56

Angus <anguscomber@gmail.com> writes:

> 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>?

-- 
__Pascal Bourguignon__
0
pjb (7869)
2/27/2009 10:52:55 AM
On Feb 27, 4:25=A0am, Angus <anguscom...@gmail.com> wrote:
> Hi all
>
> I am using a C style function which requires a 'callback' function to
> be defined. =A0This 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? =A0At present I have
> a global pointer to the class which gets initialised when I create the
> class. =A0But 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


0
cpisz (479)
2/27/2009 11:52:39 AM
On Feb 27, 11:52 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Angus <anguscom...@gmail.com> writes:

> > 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.

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.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
0
james.kanze (9769)
3/2/2009 12:43:03 AM
Reply: