arduino-libraries / ArduinoMqttClient

ArduinoMqttClient Library for Arduino
GNU Lesser General Public License v2.1
192 stars 75 forks source link

Add support for using std::function callbacks instead of C-pointers #35

Closed chris-hatton closed 2 years ago

chris-hatton commented 4 years ago

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 of MyMqttIntegration 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 using std::functions. 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:

MqttClient::MessageCallback callback = [this](MqttClient *client, int messageSize) {
    Serial.print("Received message with topic: " + client->messageTopic());
};
mqttClient->onMessage(callback);

Including a pointer to the source MqttClient affords a little extra flexibility in case multiple MqttClients are used which share the same callback function.

CLAassistant commented 3 years ago

CLA assistant check
All committers have signed the CLA.

chris-hatton commented 3 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.

aentinger commented 3 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.

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.

JDuchniewicz commented 3 years ago

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

chris-hatton commented 2 years ago

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?

aentinger commented 2 years ago

Okay. You've got me there. Merging it in 👍

Johboh commented 1 year ago

Thank you for this!