summaryrefslogtreecommitdiff
path: root/asl/handle_pool/index_pool.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'asl/handle_pool/index_pool.hpp')
-rw-r--r--asl/handle_pool/index_pool.hpp64
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