0

I have an embedded C generated source file that requires a function pointer. I need to give it a member function pointer from my C++ code. One of the solution I encountered was: Convert C++ function pointer to c function pointer . Specifically this one:

class MyCallbackClass
{

    void non_static_func(/* args */);

public:
    static void static_func(MyClass *ptr, /* other args */) {
        non_static_func(/* other args */);
    }
};

and giving the C function : c_library_function(MyCallabckClass::static_func)

This suits my system, as I am using c++11 , but can not involve any external libraries (std, bind), or heap allocation.

I would like to make it more general to any C callback I might need in the future.

How can I build MyCallbackClass according to the C typedef of the function pointer? so I can give it my C++ member function (of the same type) ? e.g.,

typedef int32_t (*Write_Func)(uint16_t, uint16_t, const uint8_t*, uint16_t);
273K
  • 29,503
  • 10
  • 41
  • 64
  • 5
    Unless you have some way to pass a "user data" pointer to the callback, I don't see a nice way to do it. – Some programmer dude Mar 21 '23 at 11:00
  • 2
    Callback functions in C often take a `void*` as one of the arguments. Does your C function do that? – Ted Lyngmo Mar 21 '23 at 11:04
  • Unfortunately not, what is the void for? – Shanti Schul Mar 21 '23 at 11:06
  • 1
    How is C going to execute member functions when it doesn't have the concept of members or classes to begin with? Though I think some Danish guy designed a "C with classes language" at some point, maybe worth checking out :) – Lundin Mar 21 '23 at 11:07
  • On a serious note, the common way to deal with this _is_ to pass along a `static` member function. That can be called from C just fine. Though I'm not sure how you'll sort out the C++ code `non_static_func` from a `static` member in the first place. – Lundin Mar 21 '23 at 11:09
  • 2
    @ShantiSchul The `void*` is for used to cast into some user defined type. It's very common to supply a `static` C++ member function, then cast the `void*` to the class type and call a non-`static` member function – Ted Lyngmo Mar 21 '23 at 11:09
  • @Someprogrammerdude , maybe something similar I can use ? I don't want to have to write a new class for each callback.. – Shanti Schul Mar 21 '23 at 11:11
  • @TedLyngmo Thanks, can a C code somehow run a static member function without the void ptr of the class? Or must it have ptr->static_func to run? – Shanti Schul Mar 21 '23 at 11:15
  • 1
    @ShantiSchul It can run a `static` member function ([like this](https://godbolt.org/z/bxa3P7bqG)) and if it's supposed to use non-`static` member data, it must have a way to access the actual instance of the class (like the example shows). Without supplying a pointer to the instance you could use a global pointer that you use inside the function, but that is ugly and doesn't work well if you want to be able to use multiple instances. – Ted Lyngmo Mar 21 '23 at 11:17
  • @TedLyngmo appreciate the detailed answer, is there a possibility to save the user data as a static as well? static void ptr = this? – Shanti Schul Mar 21 '23 at 11:25
  • @TedLyngmo i see the problem then, all classes instances will have the same void ptr if I make it static... Any way to change a static between classes...I guess not ... – Shanti Schul Mar 21 '23 at 11:28
  • If designing a C wrapper "class" using opaque pointers, then you can allocate one wrapper per C++ object and initialize it through a void pointer obtained from a C++ getter returning `this`. The C wrapper could then pass this pointer along to a callback corresponding to a static member. With that design you'd allow for multiple instances and it would be thread safe. But it's needlessly complicated and a last resort. – Lundin Mar 21 '23 at 11:46
  • Where's the _"C generated source file"_ coming from? Can you convince the creator to make it possible to supply user data via a `void*`? – Ted Lyngmo Mar 21 '23 at 11:51
  • @TedLyngmo STsemiconductors..., I have the code, but if I change it , and they future change it, then it can be a mess. – Shanti Schul Mar 21 '23 at 12:19
  • @Lundin, not sure but if I template the class according to the C typedef? I can store the instance of the member function as static inside my callback class? – Shanti Schul Mar 21 '23 at 12:22
  • @ShantiSchul Given the limitations you provided (no bind, lambda et.c) the example you posted is the only way to do this. – no more sigsegv Mar 21 '23 at 13:28
  • Ok, will leave this to be . thanks all – Shanti Schul Mar 21 '23 at 18:30

0 Answers0