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

Discussion in 'C++' started by Angus, Feb 27, 2009.

  1. Angus

    Angus Guest

    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
     
    Angus, Feb 27, 2009
    #1
    1. Advertising

  2. Re: How to deal with calling a member function from a static callback C type function

    Angus <> 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__
     
    Pascal J. Bourguignon, Feb 27, 2009
    #2
    1. Advertising

  3. Angus

    Christopher Guest

    Re: How to deal with calling a member function from a static callbackC type function

    On Feb 27, 4:25 am, Angus <> wrote:
    > 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
     
    Christopher, Feb 27, 2009
    #3
  4. Angus

    James Kanze Guest

    Re: How to deal with calling a member function from a static callbackC type function

    On Feb 27, 11:52 am, (Pascal J. Bourguignon)
    wrote:
    > Angus <> 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Mar 2, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    569
    Duncan Smith
    Dec 28, 2005
  2. Evan Burkitt
    Replies:
    2
    Views:
    1,038
    Evan Burkitt
    Aug 24, 2007
  3. dolphin
    Replies:
    3
    Views:
    1,362
    Pete Becker
    Dec 5, 2007
  4. Dat Chu
    Replies:
    8
    Views:
    2,665
    MarkyMark101
    Aug 28, 2009
  5. legard_new
    Replies:
    2
    Views:
    744
    legard_new
    Aug 1, 2010
Loading...

Share This Page