diff --git a/.clang-tidy b/.clang-tidy index 890eb18608..98a1568e5a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -70,7 +70,6 @@ Checks: >- -modernize-use-default-member-init, -modernize-use-equals-default, -modernize-use-trailing-return-type, - -modernize-make-unique, -modernize-use-nodiscard, -mpi-*, -objc-*, @@ -114,6 +113,10 @@ CheckOptions: value: llvm - key: modernize-use-nullptr.NullMacros value: 'NULL' + - key: modernize-make-unique.MakeSmartPtrFunction + value: 'make_unique' + - key: modernize-make-unique.MakeSmartPtrFunctionHeader + value: 'esphome/core/helpers.h' - key: readability-identifier-naming.LocalVariableCase value: 'lower_case' - key: readability-identifier-naming.ClassCase diff --git a/esphome/components/nfc/ndef_message.cpp b/esphome/components/nfc/ndef_message.cpp index 147392940c..b1554f41ae 100644 --- a/esphome/components/nfc/ndef_message.cpp +++ b/esphome/components/nfc/ndef_message.cpp @@ -83,11 +83,11 @@ bool NdefMessage::add_text_record(const std::string &text) { return this->add_te bool NdefMessage::add_text_record(const std::string &text, const std::string &encoding) { std::string payload = to_string(text.length()) + encoding + text; - return this->add_record(std::unique_ptr{new NdefRecord(TNF_WELL_KNOWN, "T", payload)}); + return this->add_record(make_unique(TNF_WELL_KNOWN, "T", payload)); } bool NdefMessage::add_uri_record(const std::string &uri) { - return this->add_record(std::unique_ptr{new NdefRecord(TNF_WELL_KNOWN, "U", uri)}); + return this->add_record(make_unique(TNF_WELL_KNOWN, "U", uri)); } std::vector NdefMessage::encode() { diff --git a/esphome/components/nfc/nfc_tag.h b/esphome/components/nfc/nfc_tag.h index 2c8b0a5f21..ab6ca650e4 100644 --- a/esphome/components/nfc/nfc_tag.h +++ b/esphome/components/nfc/nfc_tag.h @@ -31,13 +31,13 @@ class NfcTag { NfcTag(std::vector &uid, const std::string &tag_type, std::vector &ndef_data) { this->uid_ = uid; this->tag_type_ = tag_type; - this->ndef_message_ = std::unique_ptr(new NdefMessage(ndef_data)); + this->ndef_message_ = make_unique(ndef_data); }; NfcTag(const NfcTag &rhs) { uid_ = rhs.uid_; tag_type_ = rhs.tag_type_; if (rhs.ndef_message_ != nullptr) - ndef_message_ = std::unique_ptr(new NdefMessage(*rhs.ndef_message_)); + ndef_message_ = make_unique(*rhs.ndef_message_); } std::vector &get_uid() { return this->uid_; }; diff --git a/esphome/components/pn532/pn532.cpp b/esphome/components/pn532/pn532.cpp index c28ce8d503..1c4160539a 100644 --- a/esphome/components/pn532/pn532.cpp +++ b/esphome/components/pn532/pn532.cpp @@ -1,4 +1,6 @@ #include "pn532.h" + +#include #include "esphome/core/log.h" // Based on: @@ -104,7 +106,7 @@ void PN532::loop() { if (!success) { // Something failed if (!this->current_uid_.empty()) { - auto tag = std::unique_ptr{new nfc::NfcTag(this->current_uid_)}; + auto tag = make_unique(this->current_uid_); for (auto *trigger : this->triggers_ontagremoved_) trigger->process(tag); } @@ -117,7 +119,7 @@ void PN532::loop() { if (num_targets != 1) { // no tags found or too many if (!this->current_uid_.empty()) { - auto tag = std::unique_ptr{new nfc::NfcTag(this->current_uid_)}; + auto tag = make_unique(this->current_uid_); for (auto *trigger : this->triggers_ontagremoved_) trigger->process(tag); } @@ -281,9 +283,9 @@ std::unique_ptr PN532::read_tag_(std::vector &uid) { return this->read_mifare_ultralight_tag_(uid); } else if (type == nfc::TAG_TYPE_UNKNOWN) { ESP_LOGV(TAG, "Cannot determine tag type"); - return std::unique_ptr{new nfc::NfcTag(uid)}; + return make_unique(uid); } else { - return std::unique_ptr{new nfc::NfcTag(uid)}; + return make_unique(uid); } } diff --git a/esphome/components/pn532/pn532_mifare_classic.cpp b/esphome/components/pn532/pn532_mifare_classic.cpp index f4bd11d49f..81d135d8e6 100644 --- a/esphome/components/pn532/pn532_mifare_classic.cpp +++ b/esphome/components/pn532/pn532_mifare_classic.cpp @@ -1,3 +1,5 @@ +#include + #include "pn532.h" #include "esphome/core/log.h" @@ -15,15 +17,15 @@ std::unique_ptr PN532::read_mifare_classic_tag_(std::vector data; if (this->read_mifare_classic_block_(current_block, data)) { if (!nfc::decode_mifare_classic_tlv(data, message_length, message_start_index)) { - return std::unique_ptr{new nfc::NfcTag(uid, nfc::ERROR)}; + return make_unique(uid, nfc::ERROR); } } else { ESP_LOGE(TAG, "Failed to read block %d", current_block); - return std::unique_ptr{new nfc::NfcTag(uid, nfc::MIFARE_CLASSIC)}; + return make_unique(uid, nfc::MIFARE_CLASSIC); } } else { ESP_LOGV(TAG, "Tag is not NDEF formatted"); - return std::unique_ptr{new nfc::NfcTag(uid, nfc::MIFARE_CLASSIC)}; + return make_unique(uid, nfc::MIFARE_CLASSIC); } uint32_t index = 0; @@ -51,7 +53,7 @@ std::unique_ptr PN532::read_mifare_classic_tag_(std::vector{new nfc::NfcTag(uid, nfc::MIFARE_CLASSIC, buffer)}; + return make_unique(uid, nfc::MIFARE_CLASSIC, buffer); } bool PN532::read_mifare_classic_block_(uint8_t block_num, std::vector &data) { diff --git a/esphome/components/pn532/pn532_mifare_ultralight.cpp b/esphome/components/pn532/pn532_mifare_ultralight.cpp index 24e97ab95c..1b91ae919e 100644 --- a/esphome/components/pn532/pn532_mifare_ultralight.cpp +++ b/esphome/components/pn532/pn532_mifare_ultralight.cpp @@ -1,3 +1,5 @@ +#include + #include "pn532.h" #include "esphome/core/log.h" @@ -9,25 +11,25 @@ static const char *const TAG = "pn532.mifare_ultralight"; std::unique_ptr PN532::read_mifare_ultralight_tag_(std::vector &uid) { if (!this->is_mifare_ultralight_formatted_()) { ESP_LOGD(TAG, "Not NDEF formatted"); - return std::unique_ptr{new nfc::NfcTag(uid, nfc::NFC_FORUM_TYPE_2)}; + return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } uint8_t message_length; uint8_t message_start_index; if (!this->find_mifare_ultralight_ndef_(message_length, message_start_index)) { - return std::unique_ptr{new nfc::NfcTag(uid, nfc::NFC_FORUM_TYPE_2)}; + return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } ESP_LOGVV(TAG, "message length: %d, start: %d", message_length, message_start_index); if (message_length == 0) { - return std::unique_ptr{new nfc::NfcTag(uid, nfc::NFC_FORUM_TYPE_2)}; + return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } std::vector data; for (uint8_t page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; page < nfc::MIFARE_ULTRALIGHT_MAX_PAGE; page++) { std::vector page_data; if (!this->read_mifare_ultralight_page_(page, page_data)) { ESP_LOGE(TAG, "Error reading page %d", page); - return std::unique_ptr{new nfc::NfcTag(uid, nfc::NFC_FORUM_TYPE_2)}; + return make_unique(uid, nfc::NFC_FORUM_TYPE_2); } data.insert(data.end(), page_data.begin(), page_data.end()); @@ -38,7 +40,7 @@ std::unique_ptr PN532::read_mifare_ultralight_tag_(std::vector{new nfc::NfcTag(uid, nfc::NFC_FORUM_TYPE_2, data)}; + return make_unique(uid, nfc::NFC_FORUM_TYPE_2, data); } bool PN532::read_mifare_ultralight_page_(uint8_t page_num, std::vector &data) { diff --git a/esphome/components/socket/bsd_sockets_impl.cpp b/esphome/components/socket/bsd_sockets_impl.cpp index 1ad520a099..6fb00ce22d 100644 --- a/esphome/components/socket/bsd_sockets_impl.cpp +++ b/esphome/components/socket/bsd_sockets_impl.cpp @@ -1,5 +1,6 @@ #include "socket.h" #include "esphome/core/defines.h" +#include "esphome/core/helpers.h" #ifdef USE_SOCKET_IMPL_BSD_SOCKETS @@ -39,7 +40,7 @@ class BSDSocketImpl : public Socket { int fd = ::accept(fd_, addr, addrlen); if (fd == -1) return {}; - return std::unique_ptr{new BSDSocketImpl(fd)}; + return make_unique(fd); } int bind(const struct sockaddr *addr, socklen_t addrlen) override { return ::bind(fd_, addr, addrlen); } int close() override { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 86c70088d4..6c0ee8399e 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -88,10 +88,15 @@ template T clamp(T val, T min, T max); */ float lerp(float completion, float start, float end); -/// std::make_unique +// Not all platforms we support target C++14 yet, so we can't unconditionally use std::make_unique. Provide our own +// implementation if needed, and otherwise pull std::make_unique into scope so that we have a uniform API. +#if __cplusplus >= 201402L +using std::make_unique; +#else template std::unique_ptr make_unique(Args &&...args) { return std::unique_ptr(new T(std::forward(args)...)); } +#endif /// Return a random 32 bit unsigned integer. uint32_t random_uint32();