From eb285643ed5dab8125e9c6bc94abd7ef562096a5 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Thu, 27 Feb 2025 23:58:57 +0100 Subject: Finish work on deducing this, for now --- asl/containers/buffer.hpp | 55 ++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) (limited to 'asl/containers/buffer.hpp') diff --git a/asl/containers/buffer.hpp b/asl/containers/buffer.hpp index 76562d3..3954d35 100644 --- a/asl/containers/buffer.hpp +++ b/asl/containers/buffer.hpp @@ -381,39 +381,31 @@ public: return *init; } - // @Todo(C++23) Use deducing this - const T* data() const + auto data(this auto&& self) { + using return_type = un_ref_t>*; if constexpr (kInlineCapacity == 0) { - return m_data; + return return_type{ self.m_data }; } else { - return is_on_heap() ? m_data : reinterpret_cast(this); + return self.is_on_heap() ? return_type{ self.m_data } : reinterpret_cast(&self); } } - T* data() + constexpr auto begin(this auto&& self) { - if constexpr (kInlineCapacity == 0) - { - return m_data; - } - else - { - return is_on_heap() ? m_data : reinterpret_cast(this); - } + using type = un_ref_t>; + return contiguous_iterator{self.data()}; } - // @Todo(C++23) Use deducing this - constexpr contiguous_iterator begin() const { return contiguous_iterator{data()}; } - constexpr contiguous_iterator end() const { return contiguous_iterator{data() + size()}; } - - constexpr contiguous_iterator begin() { return contiguous_iterator{data()}; } - constexpr contiguous_iterator end() { return contiguous_iterator{data() + size()}; } + constexpr auto end(this auto&& self) + { + using type = un_ref_t>; + return contiguous_iterator{self.data() + self.size()}; + } - // @Todo(C++23) Deducing this constexpr operator span() const // NOLINT(*-explicit-conversions) { return as_span(); @@ -424,27 +416,16 @@ public: return as_span(); } - constexpr span as_span() const - { - return span{data(), size()}; - } - - constexpr span as_span() - { - return span{data(), size()}; - } - - // @Todo(C++23) Use deducing this - constexpr T& operator[](isize_t i) + constexpr auto as_span(this auto&& self) { - ASL_ASSERT(i >= 0 && i <= size()); - return data()[i]; + using type = un_ref_t>; + return span{self.data(), self.size()}; } - constexpr const T& operator[](isize_t i) const + constexpr auto&& operator[](this auto&& self, isize_t i) { - ASL_ASSERT(i >= 0 && i <= size()); - return data()[i]; + ASL_ASSERT(i >= 0 && i <= self.size()); + return ASL_FWD_LIKE(decltype(self), ASL_FWD(self).data()[i]); } template -- cgit