diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-02-27 23:58:57 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-02-28 00:30:34 +0100 |
commit | eb285643ed5dab8125e9c6bc94abd7ef562096a5 (patch) | |
tree | 99bb67ae08b15d4de39a8a98b0f9c051dce97546 /asl/containers | |
parent | 38ab48b1882f36ed7eb7e50c4fb46ce5d376fbc3 (diff) |
Finish work on deducing this, for now
Diffstat (limited to 'asl/containers')
-rw-r--r-- | asl/containers/buffer.hpp | 55 | ||||
-rw-r--r-- | asl/containers/hash_map.hpp | 25 | ||||
-rw-r--r-- | asl/containers/intrusive_list.hpp | 31 | ||||
-rw-r--r-- | asl/containers/intrusive_list_tests.cpp | 2 |
4 files changed, 37 insertions, 76 deletions
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<copy_cref_t<decltype(self), T>>*; if constexpr (kInlineCapacity == 0) { - return m_data; + return return_type{ self.m_data }; } else { - return is_on_heap() ? m_data : reinterpret_cast<const T*>(this); + return self.is_on_heap() ? return_type{ self.m_data } : reinterpret_cast<return_type>(&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<T*>(this); - } + using type = un_ref_t<copy_cref_t<decltype(self), T>>; + return contiguous_iterator<type>{self.data()}; } - // @Todo(C++23) Use deducing this - constexpr contiguous_iterator<const T> begin() const { return contiguous_iterator{data()}; } - constexpr contiguous_iterator<const T> end() const { return contiguous_iterator{data() + size()}; } - - constexpr contiguous_iterator<T> begin() { return contiguous_iterator{data()}; } - constexpr contiguous_iterator<T> end() { return contiguous_iterator{data() + size()}; } + constexpr auto end(this auto&& self) + { + using type = un_ref_t<copy_cref_t<decltype(self), T>>; + return contiguous_iterator<type>{self.data() + self.size()}; + } - // @Todo(C++23) Deducing this constexpr operator span<const T>() const // NOLINT(*-explicit-conversions) { return as_span(); @@ -424,27 +416,16 @@ public: return as_span(); } - constexpr span<const T> as_span() const - { - return span<const T>{data(), size()}; - } - - constexpr span<T> as_span() - { - return span<T>{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<copy_cref_t<decltype(self), T>>; + return span<type>{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<typename H> diff --git a/asl/containers/hash_map.hpp b/asl/containers/hash_map.hpp index 104bd6a..a59e580 100644 --- a/asl/containers/hash_map.hpp +++ b/asl/containers/hash_map.hpp @@ -147,31 +147,18 @@ public: // NOLINTEND(*-pointer-arithmetic) } - // @Todo(C++23) Deducing this template<typename U> - requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, K, U> - const V* get(const U& value) const + auto get(this auto&& self, const U& value) + requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, K, U> { - isize_t index = Base::find_slot_lookup(value); + using return_type = un_ref_t<copy_cref_t<decltype(self), V>>*; + isize_t index = self.find_slot_lookup(value); if (index >= 0) { // NOLINTNEXTLINE(*-pointer-arithmetic) - return &Base::m_values[index].as_init_unsafe().value; + return return_type{ &self.m_values[index].as_init_unsafe().value }; } - return nullptr; - } - - template<typename U> - requires key_hasher<KeyHasher, U> && key_comparator<KeyComparator, K, U> - V* get(const U& value) - { - isize_t index = Base::find_slot_lookup(value); - if (index >= 0) - { - // NOLINTNEXTLINE(*-pointer-arithmetic) - return &Base::m_values[index].as_init_unsafe().value; - } - return nullptr; + return return_type{ nullptr }; } }; diff --git a/asl/containers/intrusive_list.hpp b/asl/containers/intrusive_list.hpp index dcf0508..99509b8 100644 --- a/asl/containers/intrusive_list.hpp +++ b/asl/containers/intrusive_list.hpp @@ -76,14 +76,16 @@ public: } } - constexpr T* head() const + constexpr auto head(this auto&& self) { - return m_head; + using return_type = un_ref_t<copy_cref_t<decltype(self), T>>*; + return return_type{ self.m_head }; } - constexpr T* tail() const + constexpr auto tail(this auto&& self) { - return m_head != nullptr ? m_head->m_prev : nullptr; + using return_type = un_ref_t<copy_cref_t<decltype(self), T>>*; + return return_type{ self.m_head != nullptr ? self.m_head->m_prev : nullptr }; } void detach(T* node) @@ -169,25 +171,16 @@ public: using iterator = generic_iterator<T>; using const_iterator = generic_iterator<const T>; - // @Todo(C++23) Deduplicate with deducing-this maybe - const_iterator begin() const + auto begin(this auto&& self) { - return const_iterator{ head(), is_empty() }; - } - - const_iterator end() const - { - return const_iterator{ head(), true }; + using iterator_type = select_t<is_const<un_ref_t<decltype(self)>>, const_iterator, iterator>; + return iterator_type{ self.head(), self.is_empty() }; } - iterator begin() - { - return iterator{ head(), is_empty() }; - } - - iterator end() + auto end(this auto&& self) { - return iterator{ head(), true }; + using iterator_type = select_t<is_const<un_ref_t<decltype(self)>>, const_iterator, iterator>; + return iterator_type{ self.head(), true }; } }; diff --git a/asl/containers/intrusive_list_tests.cpp b/asl/containers/intrusive_list_tests.cpp index ceb54a6..373913c 100644 --- a/asl/containers/intrusive_list_tests.cpp +++ b/asl/containers/intrusive_list_tests.cpp @@ -13,7 +13,7 @@ struct IntNode : public asl::intrusive_list_node<IntNode> ASL_TEST(empty_list) { - asl::IntrusiveList<IntNode> list; + const asl::IntrusiveList<IntNode> list; ASL_TEST_EXPECT(list.is_empty()); ASL_TEST_EXPECT(list.head() == nullptr); ASL_TEST_EXPECT(list.tail() == nullptr); |