Solved Creating call back functions for a library
-
@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!
-
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 ?
-
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 correspondingCallbackWith...
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.
-
@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 getnullptr
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.