From aa21fd7a24c0a14dd98d3e97e033551ed3006eb4 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Tue, 3 Jun 2025 00:13:17 +0200 Subject: WIP --- asl/handle_pool/index_pool.hpp | 60 +++++++++++++++++++++++++++++++----- asl/handle_pool/index_pool_tests.cpp | 10 ++++++ 2 files changed, 63 insertions(+), 7 deletions(-) (limited to 'asl') diff --git a/asl/handle_pool/index_pool.hpp b/asl/handle_pool/index_pool.hpp index 377f6f4..245ac24 100644 --- a/asl/handle_pool/index_pool.hpp +++ b/asl/handle_pool/index_pool.hpp @@ -10,6 +10,8 @@ namespace asl { +// @Todo Uniquely represented for the handle? + template< int kIndexSize_, int kGenSize_, @@ -43,19 +45,19 @@ template< requires ( same_as || (kUserSize_ <= size_of * 8 && trivially_copy_constructible) ) -class index_pool_handle_base +class index_pool_handle { static constexpr int kUserSizeComputed = same_as ? 0 : (kUserSize_ == 0 ? size_of * 8 : kUserSize_); // NOLINT public: - using config = index_pool_config; + using config = index_pool_config; + using user_type_external = UserType; private: using handle_type = config::handle_type; using user_type = config::user_type; - using user_type_external = UserType; - + static constexpr handle_type kHasValueMask = ~(~handle_type{0} >> 1); static constexpr handle_type kIndexMask = (handle_type{1} << config::kIndexSize) - 1; @@ -68,20 +70,64 @@ private: handle_type m_handle{}; + [[nodiscard]] constexpr handle_type index(this index_pool_handle self) + { + return self.m_handle & kIndexMask; + } + + [[nodiscard]] constexpr handle_type gen(this index_pool_handle self) + { + return (self.m_handle & kGenMask) >> kGenShift; + } + public: - [[nodiscard]] constexpr bool has_value() const { return m_handle & kHasValueMask; } + index_pool_handle() = default; - [[nodiscard]] constexpr user_type_external user() const + [[nodiscard]] constexpr bool has_value(this index_pool_handle self) + { + return self.m_handle & kHasValueMask; + } + + [[nodiscard]] constexpr user_type_external user(this index_pool_handle self) requires config::kHasUser { return bit_cast( - static_cast((m_handle & kUserMask) >> kUserShift) + static_cast((self.m_handle & kUserMask) >> kUserShift) ); } + + constexpr bool operator==(this index_pool_handle self, index_pool_handle other) + { + return self.m_handle == other.m_handle; + } }; +template< + typename H, + typename Payload = empty, + allocator Allocator = DefaultAllocator> class IndexPool { + using config = H::config; + + struct Slot + { + H handle; + ASL_NO_UNIQUE_ADDRESS Payload payload; + }; + + asl::chunked_buffer m_slots; + +public: + IndexPool() requires default_constructible = default; + + explicit IndexPool(Allocator allocator) : m_slots{allocator} {} + + H acquire() requires (!H::kHasUser) + { + } + + H acquire(H::user_type_external }; } // namespace asl diff --git a/asl/handle_pool/index_pool_tests.cpp b/asl/handle_pool/index_pool_tests.cpp index 1dd2816..50becfe 100644 --- a/asl/handle_pool/index_pool_tests.cpp +++ b/asl/handle_pool/index_pool_tests.cpp @@ -3,6 +3,16 @@ // SPDX-License-Identifier: BSD-3-Clause #include "asl/testing/testing.hpp" +#include "asl/handle_pool/index_pool.hpp" + +enum TestHandleType : uint8_t +{ + kType1, + kType2, +}; + +using TestHandle = asl::index_pool_handle<10, 14, TestHandleType>; +using Pool = asl::IndexPool; ASL_TEST(test) { -- cgit