Skip to content
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

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

Merged

Conversation

chris-hatton
Copy link
Contributor

@chris-hatton chris-hatton commented Apr 10, 2020

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
Copy link

CLAassistant commented Apr 9, 2021

CLA assistant check
All committers have signed the CLA.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@chris-hatton
Copy link
Contributor Author

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
Copy link
Contributor

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.

@JDuchniewicz
Copy link

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
Copy link
Contributor Author

chris-hatton commented Oct 26, 2022

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
Copy link
Contributor

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

@aentinger aentinger merged commit 761ee79 into arduino-libraries:master Oct 27, 2022
@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Oct 27, 2022
@Johboh
Copy link

Johboh commented Dec 10, 2022

Thank you for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants