mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 11:56:11 +08:00
[mqtt] Replace std::bind with lambdas in CustomMQTTDevice (#14964)
This commit is contained in:
@@ -189,28 +189,33 @@ class CustomMQTTDevice {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void CustomMQTTDevice::subscribe(const std::string &topic,
|
void CustomMQTTDevice::subscribe(const std::string &topic,
|
||||||
void (T::*callback)(const std::string &, const std::string &), uint8_t qos) {
|
void (T::*callback)(const std::string &, const std::string &), uint8_t qos) {
|
||||||
auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2);
|
auto *obj = static_cast<T *>(this);
|
||||||
global_mqtt_client->subscribe(topic, f, qos);
|
global_mqtt_client->subscribe(
|
||||||
|
topic, [obj, callback](const std::string &t, const std::string &payload) { (obj->*callback)(t, payload); }, qos);
|
||||||
}
|
}
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos) {
|
void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos) {
|
||||||
auto f = std::bind(callback, (T *) this, std::placeholders::_2);
|
auto *obj = static_cast<T *>(this);
|
||||||
global_mqtt_client->subscribe(topic, f, qos);
|
global_mqtt_client->subscribe(
|
||||||
|
topic, [obj, callback](const std::string &, const std::string &payload) { (obj->*callback)(payload); }, qos);
|
||||||
}
|
}
|
||||||
template<typename T> void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos) {
|
template<typename T> void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos) {
|
||||||
auto f = std::bind(callback, (T *) this);
|
auto *obj = static_cast<T *>(this);
|
||||||
global_mqtt_client->subscribe(topic, f, qos);
|
global_mqtt_client->subscribe(
|
||||||
|
topic, [obj, callback](const std::string &, const std::string &) { (obj->*callback)(); }, qos);
|
||||||
}
|
}
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject),
|
void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject),
|
||||||
uint8_t qos) {
|
uint8_t qos) {
|
||||||
auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2);
|
auto *obj = static_cast<T *>(this);
|
||||||
global_mqtt_client->subscribe_json(topic, f, qos);
|
global_mqtt_client->subscribe_json(
|
||||||
|
topic, [obj, callback](const std::string &t, JsonObject root) { (obj->*callback)(t, root); }, qos);
|
||||||
}
|
}
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(JsonObject), uint8_t qos) {
|
void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(JsonObject), uint8_t qos) {
|
||||||
auto f = std::bind(callback, (T *) this, std::placeholders::_2);
|
auto *obj = static_cast<T *>(this);
|
||||||
global_mqtt_client->subscribe_json(topic, f, qos);
|
global_mqtt_client->subscribe_json(
|
||||||
|
topic, [obj, callback](const std::string &, JsonObject root) { (obj->*callback)(root); }, qos);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace esphome::mqtt
|
} // namespace esphome::mqtt
|
||||||
|
|||||||
Reference in New Issue
Block a user