Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Creating call back functions for a library
Forum Updated to NodeBB v4.3 + New Features

Creating call back functions for a library

Scheduled Pinned Locked Moved Solved C++ Gurus
18 Posts 5 Posters 2.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yaswanth
    wrote on 1 May 2019, 17:58 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 May 2019, 18:47 last edited by
      #2

      Hi,

      And what is the signature of these functions ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Y 1 Reply Last reply 2 May 2019, 06:51
      0
      • S SGaist
        1 May 2019, 18:47

        Hi,

        And what is the signature of these functions ?

        Y Offline
        Y Offline
        Yaswanth
        wrote on 2 May 2019, 06:51 last edited by
        #3

        @SGaist
        int someFunction(void *data);

        J 1 Reply Last reply 2 May 2019, 08:24
        0
        • Y Yaswanth
          2 May 2019, 06:51

          @SGaist
          int someFunction(void *data);

          J Offline
          J Offline
          JonB
          wrote on 2 May 2019, 08:24 last edited by
          #4

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

          Y 1 Reply Last reply 2 May 2019, 08:43
          0
          • J JonB
            2 May 2019, 08:24

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

            Y Offline
            Y Offline
            Yaswanth
            wrote on 2 May 2019, 08:43 last edited by
            #5

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

            J 1 Reply Last reply 2 May 2019, 08:46
            0
            • Y Yaswanth
              2 May 2019, 08:43

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

              J Offline
              J Offline
              JonB
              wrote on 2 May 2019, 08:46 last edited by JonB 5 Feb 2019, 08:48
              #6

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

              Y 1 Reply Last reply 2 May 2019, 09:29
              0
              • J JonB
                2 May 2019, 08:46

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

                Y Offline
                Y Offline
                Yaswanth
                wrote on 2 May 2019, 09:29 last edited by
                #7

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

                J 1 Reply Last reply 2 May 2019, 09:51
                0
                • Y Yaswanth
                  2 May 2019, 09:29

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 2 May 2019, 09:51 last edited by
                  #8

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

                  Y 1 Reply Last reply 2 May 2019, 10:07
                  1
                  • J JonB
                    2 May 2019, 09:51

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

                    Y Offline
                    Y Offline
                    Yaswanth
                    wrote on 2 May 2019, 10:07 last edited by Yaswanth 5 Feb 2019, 10:31
                    #9

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

                    J 1 Reply Last reply 2 May 2019, 10:59
                    0
                    • Y Yaswanth
                      2 May 2019, 10:07

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

                      J Offline
                      J Offline
                      JonB
                      wrote on 2 May 2019, 10:59 last edited by JonB 5 Feb 2019, 11:00
                      #10

                      @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! :)

                      Y 1 Reply Last reply 2 May 2019, 11:11
                      0
                      • J JonB
                        2 May 2019, 10:59

                        @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! :)

                        Y Offline
                        Y Offline
                        Yaswanth
                        wrote on 2 May 2019, 11:11 last edited by Yaswanth 5 Feb 2019, 11:12
                        #11

                        @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 :-)

                        J kshegunovK 2 Replies Last reply 2 May 2019, 11:15
                        0
                        • Y Yaswanth
                          2 May 2019, 11:11

                          @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 :-)

                          J Offline
                          J Offline
                          JonB
                          wrote on 2 May 2019, 11:15 last edited by
                          #12

                          @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!

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 2 May 2019, 20:04 last edited by
                            #13

                            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 ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • Y Yaswanth
                              2 May 2019, 11:11

                              @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 :-)

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on 2 May 2019, 20:47 last edited by kshegunov 5 Feb 2019, 20:53
                              #14

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

                              Read and abide by the Qt Code of Conduct

                              J 1 Reply Last reply 3 May 2019, 07:53
                              1
                              • kshegunovK kshegunov
                                2 May 2019, 20:47

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

                                J Offline
                                J Offline
                                JonB
                                wrote on 3 May 2019, 07:53 last edited by
                                #15

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

                                kshegunovK 1 Reply Last reply 3 May 2019, 21:21
                                0
                                • fcarneyF Offline
                                  fcarneyF Offline
                                  fcarney
                                  wrote on 3 May 2019, 20:08 last edited by
                                  #16

                                  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.

                                  C++ is a perfectly valid school of magic.

                                  1 Reply Last reply
                                  0
                                  • J JonB
                                    3 May 2019, 07:53

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

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on 3 May 2019, 21:21 last edited by
                                    #17

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

                                    Read and abide by the Qt Code of Conduct

                                    1 Reply Last reply
                                    1
                                    • Y Offline
                                      Y Offline
                                      Yaswanth
                                      wrote on 7 May 2019, 10:18 last edited by
                                      #18

                                      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.

                                      1 Reply Last reply
                                      0

                                      1/18

                                      1 May 2019, 17:58

                                      • Login

                                      • Login or register to search.
                                      1 out of 18
                                      • First post
                                        1/18
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved