From d1bb5a83f6dc697ff0f506374b42ba32f6db89a1 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 14 Mar 2025 18:53:39 +0100 Subject: Use inheritance for string implementation --- asl/containers/buffer.hpp | 10 +++++++--- asl/strings/string.hpp | 33 ++++++++++++++------------------- 2 files changed, 21 insertions(+), 22 deletions(-) (limited to 'asl') diff --git a/asl/containers/buffer.hpp b/asl/containers/buffer.hpp index bb32f89..c3d865f 100644 --- a/asl/containers/buffer.hpp +++ b/asl/containers/buffer.hpp @@ -390,14 +390,18 @@ public: auto data(this auto&& self) { using return_type = un_ref_t>*; + // NOLINTNEXTLINE(*-reinterpret-cast) + auto&& buffer = reinterpret_cast>(self); if constexpr (kInlineCapacity == 0) { - return return_type{ self.m_data }; + return return_type{ buffer.m_data }; } else { - // NOLINTNEXTLINE(*-reinterpret-cast) - return self.is_on_heap() ? return_type{ self.m_data } : reinterpret_cast(&self); + return buffer.is_on_heap() + ? return_type{ buffer.m_data } + // NOLINTNEXTLINE(*-reinterpret-cast) + : reinterpret_cast(&buffer); } } diff --git a/asl/strings/string.hpp b/asl/strings/string.hpp index c95e151..8c2f528 100644 --- a/asl/strings/string.hpp +++ b/asl/strings/string.hpp @@ -11,12 +11,12 @@ namespace asl { template -class string +class string : protected buffer { - buffer m_buffer; + using Base = buffer; - explicit constexpr string(buffer&& buffer) : - m_buffer{std::move(buffer)} + explicit constexpr string(buffer&& b) : + Base{std::move(b)} {} template @@ -24,31 +24,26 @@ class string public: constexpr string() requires default_constructible = default; - explicit constexpr string(Allocator allocator) : m_buffer{std::move(allocator)} {} + + explicit constexpr string(Allocator allocator) + : Base{std::move(allocator)} + {} // NOLINTNEXTLINE(*explicit*) constexpr string(string_view sv) requires default_constructible - : m_buffer{sv.as_span()} + : Base{sv.as_span()} {} constexpr string(string_view sv, Allocator allocator) - : m_buffer{sv.as_span(), std::move(allocator)} + : Base{sv.as_span(), std::move(allocator)} {} - constexpr ~string() = default; - - constexpr string(const string&) requires copy_constructible = default; - constexpr string(string&&) = default; - - constexpr string& operator=(const string&) requires copy_assignable = default; - constexpr string& operator=(string&&) = default; - - [[nodiscard]] constexpr isize_t size() const { return m_buffer.size(); } + using Base::size; - [[nodiscard]] constexpr bool is_empty() const { return m_buffer.is_empty(); } + using Base::is_empty; - [[nodiscard]] constexpr const char* data() const { return m_buffer.data(); } + [[nodiscard]] constexpr const char* data() const { return Base::data(); } // NOLINTNEXTLINE(*explicit*) constexpr operator string_view() const @@ -58,7 +53,7 @@ public: [[nodiscard]] constexpr string_view as_string_view() const { - auto span = m_buffer.as_span(); + auto span = Base::as_span(); return string_view{span.data(), span.size()}; } -- cgit