Creating call back functions for a library



  • I am creating a c++ library. This library has to invoke the call back function in the application which is using this library. I don't want to create static functions and externs functions in my application.How to create callback function in the application?
    Thanks for your reply.


  • Lifetime Qt Champion

    Hi,

    And what is the signature of these functions ?



  • @SGaist
    int someFunction(void *data);



  • @Yaswanth

    I don't want to create static functions and externs functions in my application.How to create callback function in the application?

    @SGaist will correct me if I'm wrong, but if you say you don't want your app to have static/externs yet still have a callable function won't you have to pass a pointer to the function from the app to the library as the only way to access it?



  • @JonB Thanks for your reply,
    I could pass reference of static function to the library. But I have thousands of such functions. So I wanted better way to do this.



  • @Yaswanth
    Something feels "wrong" if you say you have "thousands of such functions". Whether you pass references or use externs, that's not going to scale well, and might need some kind of rethink....

    BTW, can your library be Qt-aware (include Qt headers etc.)? Maybe signals/slots might be an alternative approach?

    But you could await a Qt expert better than I to reply....



  • @JonB
    My application and library are not Qt Applications.
    Using interfaces may help here. If anybody is having suggestions post here.



  • @Yaswanth said in Creating call back functions for a library:

    My application and library are not Qt Applications.

    If neither is Qt, then why are you asking this on a Qt forum? Although this is a "Special Interests Groups > C++ Gurus" forum, we are not a generic C++ question place if Qt is not relevant to you. For that I would suggest www.stackoverflow.com ?



  • @JonB
    I have seen couple of c++ specific questions and posted my question here.(Group name 'c++ gurus').
    Thanks for the suggestion to use stackoverflow(I did that).
    If this question is not allowed to ask in this forum I will delete. Somebody from Qt please confirm this.



  • @Yaswanth
    I/we would not have appreciated that you were interested in a C++ issue but have no interest in Qt. I guess you just came across us through some random Googling. It's not so much that you're not "allowed" to ask that here, it's just that it's not a good place for such a question, for one thing any answers are likely to be skewed to Qt usage. [Don't worry about deleting your post, doesn't really matter.]

    Truly you will find stackoverflow is great for any/all such C++ questions, it is just the right place for what you want, trust me! :)



  • @JonB
    I have been using Qt from long time and posting my questions and answers into the Qt forum. I felt, when I have Qt Forum to answer me, I can put my question here. Anyways thanks for your time. I considered ur concern for me :-)



  • @Yaswanth
    Well, far be it for me to put you off, that was not my intention, nor is it my place (I am only a member like you). This is the first time you have said you do use Qt, I had not appreciated that. Perhaps it would have been clearer if your question had explained you are a Qt user but this is a question about a C++ project which has nothing to do with Qt. People would at least then understand the background for the answer you are seeking.

    Anyway, ignore me now. It's not my place. I still think you may get good, generic answer from stackoverflow, but perhaps someone here will also chime in. I hope I have not misled you or put you off asking here!


  • Lifetime Qt Champion

    Take a look at this interesting stack overflow thread.

    By the way, as @JonB, I am a bit perplexed when you write "thousands of such functions". Are you going to write thousands of classes ?


  • Qt Champions 2017

    Functors to the rescue:

    class Callback
    {
    public:
        virtual ~Callback();
    };
    
    class CallbackWithInt : public Callback
    {
    public:
        virtual void operator () (int) const  = 0;
    };
    
    class CallbackWithDouble : public Callback
    {
    public:
        virtual void operator () (double) const = 0;
    };
    

    Use references/pointers to implementations of such interfaces in the library to get type-safe callback functionality.

    ... polymorphism is dead I'm told ...



  • @kshegunov
    Correct me if I'm wrong: in the OP's "thousands" of functions, he will have "hundreds" of functions whose signatures differ by the numbers & types of the parameters. So you're going to write hundreds of these corresponding CallbackWith... methods? And who knows where he might get types known to the main program from....



  • Well if the library is well defined and you have a lot of functions. Wouldn't it be prudent to find a way to parse the interface and generate boiler plate code for this? I don't know how they work, but aren't there utilities for doing this kind of work? Like clang, awk, bison? Or even write a python script and use regular expressions to sort out the interface.


  • Qt Champions 2017

    @JonB said in Creating call back functions for a library:

    Correct me if I'm wrong: in the OP's "thousands" of functions, he will have "hundreds" of functions whose signatures differ by the numbers & types of the parameters. So you're going to write hundreds of these corresponding CallbackWith... methods? And who knows where he might get types known to the main program from....

    That's a problem of the "why" so many callbacks are needed. The point is that with a functor you get type safety, which a C function taking void * doesn't give you. Imagine you have some minuscule error in some table to retrieve the function address, and you get the wrong function for the given data ... what you get is ::abort() being called. C is very low level and rather primitive when it comes to type safety, however with objects (in C++) you can safely typecast the pointer to the desired type, and you get nullptr if it's not of that type; so you can tell if you screwed up.



  • I could use member function of class as a callback.
    I have sent function object which is returned by std::bind to the library . In library used std::function to hold bind expression.
    So as per the data, respective callback functions can be called.


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.