knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.84k stars 1.47k forks source link

Callback signature #117

Open tomkcook opened 8 years ago

tomkcook commented 8 years ago

Is there some particular reason not to use C++11's std::function<void(char*, uint8_t*, uint32_t)> as the callback type instead of a bare function pointer? That would then allow passing pointer to member, lambda, functors etc instead of just ordinary functions.

If there's no objection, I'll create a pull request.

knolleary commented 8 years ago

I'll refer you to this comment I made over the weekend: https://github.com/knolleary/pubsubclient/issues/115#issuecomment-174217440

As long as this is fully backwards compatible, so any existing sketch will still work, then go ahead.

tomkcook commented 8 years ago

I'm not sure if all the arduino toolchains have enough C++11 to make this work. I'll experiment and see.

tomkcook commented 8 years ago

On a quick survey, no other Arduino board supports std::function<> (or indeed the <functional> header at all). I'll submit a pull request that makes this change ESP8266-specific, but see what you think.

marcoschwartz commented 8 years ago

Hello, since the last update of the library I am having the following error in my library (aREST) when used with the ESP8266:

/Users/marco/Dropbox/Arduino/libraries/aREST/aREST.h:1051: undefined reference to `PubSubClient::setCallback(std::function<void (char, unsigned char, unsigned int)>)'

With Arduino-based sketches, no problems. How to fix this? I think it is linked to this issue. Thanks!

knolleary commented 8 years ago

@marcoschwartz hmmm, ok. Will need input from @tomkcook on this as this was his change. We can rollback if it has regressed behaviour. Do you have a pointer to your code? I see the aREST library on github doesn't reference setCallback anywhere so I'm guessing this is a development version?

marcoschwartz commented 8 years ago

Thanks for the fast answer! Indeed it's not in the library directly, but I do a client.setCallback(callback); in all the code using PubSubClient.

tomkcook commented 8 years ago

I'll have a look.

tomkcook commented 8 years ago

@marcoschwartz I can't see the problem. Can you double-check what you have on line 573 of PubSubClient.cpp? ie that you have 21b75a2c4aad100a3fc2cb1f04e6a9b147d69a3d in your source tree.

marcoschwartz commented 8 years ago

Thanks for the support! Actually I had to revert to 2.4.0 and then update again from the Arduino library manager, and now it works!