From 252381640be00b7f380052bba022e5bfae633696 Mon Sep 17 00:00:00 2001 From: Karsten Sperling <113487422+ksperling-apple@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:36:58 +1300 Subject: [PATCH] Make (Fixed)Span::data_equals a plain method (not a template) (#30479) This allows implicit conversion (e.g. from std::array) to take place. --- src/lib/support/Span.h | 22 ++-------------------- src/lib/support/tests/TestSpan.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/lib/support/Span.h b/src/lib/support/Span.h index 2554cd4a695219..a6f63c7c59f33e 100644 --- a/src/lib/support/Span.h +++ b/src/lib/support/Span.h @@ -130,13 +130,10 @@ class Span reference front() const { return (*this)[0]; } reference back() const { return (*this)[size() - 1]; } - template , std::remove_const_t>::value>> - bool data_equal(const Span & other) const + bool data_equal(const Span & other) const { return (size() == other.size()) && (empty() || (memcmp(data(), other.data(), size() * sizeof(T)) == 0)); } - template , std::remove_const_t>::value>> - inline bool data_equal(const FixedSpan & other) const; Span SubSpan(size_t offset, size_t length) const { @@ -329,15 +326,7 @@ class FixedSpan reference front() const { return (*this)[0]; } reference back() const { return (*this)[size() - 1]; } - // Allow data_equal for spans that are over the same type up to const-ness. - template , std::remove_const_t>::value>> - bool data_equal(const FixedSpan & other) const - { - return (memcmp(data(), other.data(), N * sizeof(T)) == 0); - } - - template , std::remove_const_t>::value>> - bool data_equal(const Span & other) const + bool data_equal(const Span & other) const { return (N == other.size() && memcmp(data(), other.data(), N * sizeof(T)) == 0); } @@ -359,13 +348,6 @@ template constexpr Span::Span(const FixedSpan & other) : mDataBuf(other.data()), mDataLen(other.size()) {} -template -template -inline bool Span::data_equal(const FixedSpan & other) const -{ - return other.data_equal(*this); -} - template [[deprecated("Use !empty()")]] inline bool IsSpanUsable(const Span & span) { diff --git a/src/lib/support/tests/TestSpan.cpp b/src/lib/support/tests/TestSpan.cpp index e1ff254a35b0a0..37e8d97de3488c 100644 --- a/src/lib/support/tests/TestSpan.cpp +++ b/src/lib/support/tests/TestSpan.cpp @@ -345,6 +345,15 @@ static void TestConversionConstructors(nlTestSuite * inSuite, void * inContext) ([](FixedSpan f) {})(constArray); ([](Span f) {})(constArray); + NL_TEST_ASSERT(inSuite, span10.data_equal(span10)); + NL_TEST_ASSERT(inSuite, span10.data_equal(span9)); + NL_TEST_ASSERT(inSuite, span10.data_equal(array)); + NL_TEST_ASSERT(inSuite, span10.data_equal(constArray)); + NL_TEST_ASSERT(inSuite, span9.data_equal(span9)); + NL_TEST_ASSERT(inSuite, span9.data_equal(span10)); + NL_TEST_ASSERT(inSuite, span9.data_equal(array)); + NL_TEST_ASSERT(inSuite, span9.data_equal(constArray)); + // The following should not compile // Span error1 = std::array(); // Span would point into a temporary value }