diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-06-03 00:13:17 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-06-03 00:13:17 +0200 |
commit | aa21fd7a24c0a14dd98d3e97e033551ed3006eb4 (patch) | |
tree | 7462b7ca39023381b03b1bab817805d5f08e42d3 /asl | |
parent | 048ab0903b8749717f4461a6abcfa023ceef0dd6 (diff) |
WIPhandle_pool
Diffstat (limited to 'asl')
-rw-r--r-- | asl/handle_pool/index_pool.hpp | 60 | ||||
-rw-r--r-- | asl/handle_pool/index_pool_tests.cpp | 10 |
2 files changed, 63 insertions, 7 deletions
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<UserType, empty> || (kUserSize_ <= size_of<UserType> * 8 && trivially_copy_constructible<UserType>) ) -class index_pool_handle_base +class index_pool_handle { static constexpr int kUserSizeComputed = same_as<UserType, empty> ? 0 : (kUserSize_ == 0 ? size_of<UserType> * 8 : kUserSize_); // NOLINT public: - using config = index_pool_config<kIndexSize_, kGenSize_, kUserSizeComputed>; + using config = index_pool_config<kIndexSize_, kGenSize_, kUserSizeComputed>; + 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<user_type_external>( - static_cast<user_type>((m_handle & kUserMask) >> kUserShift) + static_cast<user_type>((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<Slot, 1024, Allocator> m_slots; + +public: + IndexPool() requires default_constructible<Allocator> = 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<TestHandle>; ASL_TEST(test) { |