Closed chris-hatton closed 2 years ago
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.
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 :wave: 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 std::function
It's unproblematic for e.g. platform.io but not available in the Arduino tooling - by design.
@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 usingstd::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.