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.9k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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
    0
    • SGaistS SGaist

      Hi,

      And what is the signature of these functions ?

      Y Offline
      Y Offline
      Yaswanth
      wrote on last edited by
      #3

      @SGaist
      int someFunction(void *data);

      JonBJ 1 Reply Last reply
      0
      • Y Yaswanth

        @SGaist
        int someFunction(void *data);

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        0
        • JonBJ JonB

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

          JonBJ 1 Reply Last reply
          0
          • Y Yaswanth

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #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
            0
            • JonBJ JonB

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

              JonBJ 1 Reply Last reply
              0
              • Y Yaswanth

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                1
                • JonBJ JonB

                  @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 last edited by Yaswanth
                  #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.

                  JonBJ 1 Reply Last reply
                  0
                  • Y Yaswanth

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #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
                    0
                    • JonBJ JonB

                      @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 last edited by Yaswanth
                      #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 :-)

                      JonBJ kshegunovK 2 Replies Last reply
                      0
                      • Y Yaswanth

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 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

                            @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 last edited by kshegunov
                            #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

                            JonBJ 1 Reply Last reply
                            1
                            • kshegunovK kshegunov

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

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on 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
                              0
                              • fcarneyF Offline
                                fcarneyF Offline
                                fcarney
                                wrote on 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
                                • JonBJ JonB

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

                                    • Login

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