mardi 21 mars 2023

C++ function pointer to C pointer in a general way

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);

Aucun commentaire:

Enregistrer un commentaire