diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-06-15 22:46:01 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-06-18 00:05:33 +0200 |
commit | 92f908ee1b1385c1c3e924e3349b55682238232e (patch) | |
tree | 950fe524111e3eb3d1b4b83c66d3dc6da256c9f9 /asl/handle_pool/index_pool.hpp | |
parent | 30237bb78f00a2aeb277bc8f011d285908fa8b41 (diff) |
Add index_pool_handle
Diffstat (limited to 'asl/handle_pool/index_pool.hpp')
-rw-r--r-- | asl/handle_pool/index_pool.hpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/asl/handle_pool/index_pool.hpp b/asl/handle_pool/index_pool.hpp index f874386..1d359d5 100644 --- a/asl/handle_pool/index_pool.hpp +++ b/asl/handle_pool/index_pool.hpp @@ -52,5 +52,69 @@ struct index_pool_config static constexpr HandleType kUserMask = ((HandleType{1} << kUserBits) - 1) << kUserShift; }; +template< + int kIndexBits_, + int kGenBits_, + typename UserType_ = empty, + int kUserBits_ = 0 +> +class index_pool_handle +{ + // using config = index_pool_config<5, 5>; + using config = index_pool_config<kIndexBits_, kGenBits_, UserType_, kUserBits_>; + + config::HandleType m_handle{}; + +public: + constexpr index_pool_handle() = default; + + constexpr index_pool_handle(uint64_t index, uint64_t gen) + requires (!config::kHasUser) + : m_handle{static_cast<config::HandleType>( + config::kValidMask | + (index & config::kIndexMask) | + ((gen << config::kGenShift) & config::kGenMask))} + { + ASL_ASSERT((index & uint64_t{config::kIndexMask}) == index); + ASL_ASSERT((gen & (uint64_t{config::kGenMask} >> config::kGenShift)) == gen); + } + + constexpr index_pool_handle(uint64_t index, uint64_t gen, config::UserType user) + requires config::kHasUser + : m_handle{static_cast<config::HandleType>( + config::kValidMask | + (index & config::kIndexMask) | + ((gen << config::kGenShift) & config::kGenMask) | + ((static_cast<config::HandleType>(bit_cast<typename config::PrimitiveUserType>(user)) << config::kUserShift) & config::kUserMask))} + { + ASL_ASSERT((index & uint64_t{config::kIndexMask}) == index); + ASL_ASSERT((gen & (uint64_t{config::kGenMask} >> config::kGenShift)) == gen); + ASL_ASSERT((bit_cast<typename config::PrimitiveUserType>(user) & (uint64_t{config::kUserMask} >> config::kUserShift)) == bit_cast<typename config::PrimitiveUserType>(user)); + } + + constexpr bool is_valid(this index_pool_handle self) + { + return self.m_handle & config::kValidMask; + } + + constexpr uint64_t index(this index_pool_handle self) + { + return self.m_handle & config::kIndexMask; + } + + constexpr uint64_t gen(this index_pool_handle self) + { + return (self.m_handle & config::kGenMask) >> config::kGenShift; + } + + constexpr config::UserType user(this index_pool_handle self) + { + return bit_cast<typename config::UserType>(static_cast<config::PrimitiveUserType>( + ((self.m_handle & config::kUserMask) >> config::kUserShift))); + } + + constexpr bool operator==(this index_pool_handle self, index_pool_handle other) = default; +}; + } // namespace asl |