diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-03-14 18:53:39 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-03-14 18:56:58 +0100 |
commit | d1bb5a83f6dc697ff0f506374b42ba32f6db89a1 (patch) | |
tree | 07d0138926dd950ab5452275079d9c53ae00c173 /asl | |
parent | 4630cb5237aea7749ac9938c738146c798e305cb (diff) |
Use inheritance for string implementation
Diffstat (limited to 'asl')
-rw-r--r-- | asl/containers/buffer.hpp | 10 | ||||
-rw-r--r-- | asl/strings/string.hpp | 33 |
2 files changed, 21 insertions, 22 deletions
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<copy_cref_t<decltype(self), T>>*; + // NOLINTNEXTLINE(*-reinterpret-cast) + auto&& buffer = reinterpret_cast<copy_cref_t<decltype(self), class buffer>>(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<return_type>(&self); + return buffer.is_on_heap() + ? return_type{ buffer.m_data } + // NOLINTNEXTLINE(*-reinterpret-cast) + : reinterpret_cast<return_type>(&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<allocator Allocator = DefaultAllocator> -class string +class string : protected buffer<char, Allocator> { - buffer<char, Allocator> m_buffer; + using Base = buffer<char, Allocator>; - explicit constexpr string(buffer<char, Allocator>&& buffer) : - m_buffer{std::move(buffer)} + explicit constexpr string(buffer<char, Allocator>&& b) : + Base{std::move(b)} {} template<allocator A> @@ -24,31 +24,26 @@ class string public: constexpr string() requires default_constructible<Allocator> = 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<Allocator> - : 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<Allocator> = default; - constexpr string(string&&) = default; - - constexpr string& operator=(const string&) requires copy_assignable<Allocator> = 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()}; } |