Use inheritance for string implementation
This commit is contained in:
@ -390,14 +390,18 @@ public:
|
|||||||
auto data(this auto&& self)
|
auto data(this auto&& self)
|
||||||
{
|
{
|
||||||
using return_type = un_ref_t<copy_cref_t<decltype(self), T>>*;
|
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)
|
if constexpr (kInlineCapacity == 0)
|
||||||
{
|
{
|
||||||
return return_type{ self.m_data };
|
return return_type{ buffer.m_data };
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
return buffer.is_on_heap()
|
||||||
|
? return_type{ buffer.m_data }
|
||||||
// NOLINTNEXTLINE(*-reinterpret-cast)
|
// NOLINTNEXTLINE(*-reinterpret-cast)
|
||||||
return self.is_on_heap() ? return_type{ self.m_data } : reinterpret_cast<return_type>(&self);
|
: reinterpret_cast<return_type>(&buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,12 +11,12 @@ namespace asl
|
|||||||
{
|
{
|
||||||
|
|
||||||
template<allocator Allocator = DefaultAllocator>
|
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) :
|
explicit constexpr string(buffer<char, Allocator>&& b) :
|
||||||
m_buffer{std::move(buffer)}
|
Base{std::move(b)}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<allocator A>
|
template<allocator A>
|
||||||
@ -24,31 +24,26 @@ class string
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr string() requires default_constructible<Allocator> = default;
|
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*)
|
// NOLINTNEXTLINE(*explicit*)
|
||||||
constexpr string(string_view sv)
|
constexpr string(string_view sv)
|
||||||
requires default_constructible<Allocator>
|
requires default_constructible<Allocator>
|
||||||
: m_buffer{sv.as_span()}
|
: Base{sv.as_span()}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
constexpr string(string_view sv, Allocator allocator)
|
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;
|
using Base::size;
|
||||||
|
|
||||||
constexpr string(const string&) requires copy_constructible<Allocator> = default;
|
using Base::is_empty;
|
||||||
constexpr string(string&&) = default;
|
|
||||||
|
|
||||||
constexpr string& operator=(const string&) requires copy_assignable<Allocator> = default;
|
[[nodiscard]] constexpr const char* data() const { return Base::data(); }
|
||||||
constexpr string& operator=(string&&) = default;
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr isize_t size() const { return m_buffer.size(); }
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr bool is_empty() const { return m_buffer.is_empty(); }
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr const char* data() const { return m_buffer.data(); }
|
|
||||||
|
|
||||||
// NOLINTNEXTLINE(*explicit*)
|
// NOLINTNEXTLINE(*explicit*)
|
||||||
constexpr operator string_view() const
|
constexpr operator string_view() const
|
||||||
@ -58,7 +53,7 @@ public:
|
|||||||
|
|
||||||
[[nodiscard]] constexpr string_view as_string_view() const
|
[[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()};
|
return string_view{span.data(), span.size()};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user