-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use std::function to define callbacks? #142
Comments
Agree. I made a static function and then a dispatcher via a std::unordered_map from uint32_t to my std::functions. if uint32_t had been uint64_t, or void*, it'd be a bit easier as you could use a pointer to the function or something, but having the full std::function would be even better, then we could use lambdas as well. |
Hi, could you please elaborate or provide a code snippet to show how this could be done? Did you made the dispatcher a static function and pass it as the callback? I'm having the same issue. I tried to pass in the class pointer as uint32_t, but then found out that the pointer is 64 bit long and the callback could not find my class member variables because the pointer got shortened. |
@Sssswl use a map instance like std::map<uint32_t, SomeClass*> callbacks. For a code snippet see a little wrapper that I made some time ago for Qt/QML
I hope that is helpful for you |
@stefanbesler Your code was very helpful! My main problem was just where to put this std::map. I didn't realize I could also make it static. Everything's working now. Thank you very much for your help! |
ADS/AdsLib/AdsNotificationOOI.h
Line 5 in 0e0f73f
The current callback typedef is a bit "stiff". I have to provide a static method (correct me if I'm wrong) or a lambda with nothing piped in. If I pipe anything into the callback lambda, I get compiler errors.
If it was replaced with std::function it would be more flexible. I could use non-static methods or lambdas where I can pipe data into it.
The new suggested callback typedef would then be:
typedef std::function<void(const AmsAddr* pAddr, const AdsNotificationHeader* pNotification, uint32_t hUser)> PAdsNotificationFuncExConst;
The text was updated successfully, but these errors were encountered: