-
Notifications
You must be signed in to change notification settings - Fork 82
Add support for using std::function callbacks instead of C-pointers #35
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add support for using std::function callbacks instead of C-pointers #35
Conversation
I signed the CLA a month ago and checks all pass - do you need anything else from me to be able to merge this? Thanks. |
Hi @chris-hatton 👋 Doing both does not guarantee a merge into that repository. How do you intend to provide this define to the library? It's unproblematic for e.g. platform.io but not available in the Arduino tooling - by design. |
Relying on a standalone function as a callback is a sound solution albeit limited. In order to circumvent this I have to rely to heavy hacks on an embedded platform which does not have |
@aentinger Yes, my own use case is with platform.io - perhaps including this optional improvement for the benefit of other platform.io users, while not impacting Arduino users, can be considered? Also wondering; is the 'define' restriction still present in Arduino IDE 2.0? |
Okay. You've got me there. Merging it in 👍 |
Thank you for this! |
When writing more structured Arduino code, it may be desirable to have an instance of
MqttClient
be encapsulated inside another object e.g.MyMqttIntegration
. In this case, the exclusive use of C-pointers for message callbacks becomes a problem since a member function ofMyMqttIntegration
cannot be presented as a C-pointer. It is possible to cast a captureless lambda as a C-pointer, but this has it's own limitations.This PR provides a solution: the current C-pointer API remains the default, but by defining
MQTT_CLIENT_STD_FUNCTION_CALLBACK
the library swaps to usingstd::function
s.Such a callback can then be defined by a lambda or by using
std::bind
on a member function.Example setting of message callback when
MQTT_CLIENT_STD_FUNCTION_CALLBACK
is defined:Including a pointer to the source
MqttClient
affords a little extra flexibility in case multipleMqttClient
s are used which share the same callback function.