mirror of
https://github.com/esphome/esphome.git
synced 2026-02-28 09:54:19 -07:00
Address review feedback: add explicit includes and clarify comments
- Add #include <type_traits> to both .cpp and .h for static_asserts - Clarify CompactString comment: explicitly note it is not trivially copyable, and that memcpy safety relies on validated layout property - Use memcpy_fn indirection to suppress both GCC -Wclass-memaccess and clang-tidy bugprone-undefined-memory-manipulation without platform-specific pragma guards
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <cassert>
|
||||
#include <cinttypes>
|
||||
#include <cmath>
|
||||
#include <type_traits>
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#if (ESP_IDF_VERSION_MAJOR >= 5 && ESP_IDF_VERSION_MINOR >= 1)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_LIBRETINY
|
||||
@@ -219,12 +220,14 @@ class CompactString {
|
||||
};
|
||||
|
||||
static_assert(sizeof(CompactString) == 20, "CompactString must be exactly 20 bytes");
|
||||
// CompactString is safe to memcpy for permutation-based sorting (no ownership duplication).
|
||||
// Unlike libstdc++ std::string which uses a self-referential pointer (_M_p -> _M_local_buf)
|
||||
// in SSO mode, CompactString stores either inline data or an external heap pointer in
|
||||
// storage_[] — never a pointer to itself. These asserts guard that property.
|
||||
static_assert(std::is_standard_layout<CompactString>::value, "CompactString must be standard layout for memcpy safety");
|
||||
static_assert(!std::is_polymorphic<CompactString>::value, "CompactString must not have vtable for memcpy safety");
|
||||
// CompactString is not trivially copyable (non-trivial destructor/copy for heap case).
|
||||
// However, its layout has no self-referential pointers: storage_[] contains either inline
|
||||
// data or an external heap pointer — never a pointer to itself. This is unlike libstdc++
|
||||
// std::string SSO where _M_p points to _M_local_buf within the same object.
|
||||
// This property allows memcpy-based permutation sorting where each element ends up in
|
||||
// exactly one slot (no ownership duplication). These asserts document that layout property.
|
||||
static_assert(std::is_standard_layout<CompactString>::value, "CompactString must be standard layout");
|
||||
static_assert(!std::is_polymorphic<CompactString>::value, "CompactString must not have vtable");
|
||||
|
||||
class WiFiAP {
|
||||
friend class WiFiComponent;
|
||||
|
||||
Reference in New Issue
Block a user