10#include "libxr_def.hpp"
31template <
typename QueueType>
33 requires(QueueType queue,
const typename QueueType::ValueType& index_const,
34 typename QueueType::ValueType& index_mut) {
35 typename QueueType::ValueType;
36 { queue.Push(index_const) } -> std::same_as<ErrorCode>;
37 { queue.Pop(index_mut) } -> std::same_as<ErrorCode>;
38 { queue.Size() } -> std::convertible_to<size_t>;
57template <
typename Data, PoolIndexQueue FreeQueue>
66 static_assert(std::is_integral_v<IndexType>,
67 "BasicObjectPool requires an integral index queue");
69 static_assert(std::is_unsigned_v<IndexType>,
70 "BasicObjectPool requires an unsigned index queue");
109 :
pool_(std::exchange(other.pool_,
nullptr)),
128 pool_ = std::exchange(other.pool_,
nullptr);
145 [[nodiscard]]
bool Valid()
const {
return pool_ !=
nullptr; }
154 ASSERT(
pool_ !=
nullptr);
163 [[nodiscard]]
const Data&
Get()
const
165 ASSERT(
pool_ !=
nullptr);
204 ASSERT(
pool_ !=
nullptr);
214 if (
pool_ ==
nullptr)
235 template <
typename T = Data>
236 requires std::is_default_constructible_v<T>
258 ASSERT(
slots_ !=
nullptr);
274 template <
typename T = Data>
275 requires std::is_default_constructible_v<T>
280 ASSERT(free_queue.Size() == 0);
304 ASSERT(
slots_ !=
nullptr);
305 ASSERT(free_queue.Size() == 0);
347 ASSERT(!handle.
Valid());
357 handle =
Handle(
this, index);
405 [[nodiscard]]
const Data&
UnsafeAt(
size_t index)
const
453 ASSERT(
slot_count_ - 1 <=
static_cast<size_t>(std::numeric_limits<IndexType>::max()));
454 for (
size_t index = 0; index <
slot_count_; ++index)
480template <
typename Data,
typename IndexType = u
int32_t>
489template <
typename Data,
typename IndexType = u
int32_t>
498template <
typename Data,
typename IndexType = u
int32_t>
槽索引必须是整数类型。 Slot indices must use an integral type.
BasicObjectPool * pool_
所属对象池。 Owning object pool.
Handle & operator=(const Handle &)=delete
禁止拷贝赋值。 Non-copy-assignable.
Data & operator*()
解引用当前槽位对象。
Data & Get()
返回当前槽位对象的可写引用。
void Reset()
主动归还当前槽位,并使 handle 失效。
IndexType index_
当前槽位索引。 Current slot index.
Handle & operator=(Handle &&other) noexcept
移动赋值 handle,并转移槽位所有权。
const Data & operator*() const
只读解引用当前槽位对象。
const Data & Get() const
返回当前槽位对象的只读引用。
bool Valid() const
判断当前 handle 是否持有有效槽位。
Handle(BasicObjectPool *pool, IndexType index)
用指定 pool 和槽位索引构造 handle。
Handle(const Handle &)=delete
禁止拷贝构造。 Non-copyable.
Handle()=default
构造一个空 handle。
const Data * operator->() const
以只读指针形式访问当前槽位对象。
Data * operator->()
以指针形式访问当前槽位对象。
IndexType Index() const
返回当前持有的槽位索引。
~Handle()
析构 handle,并自动归还槽位。
Handle(Handle &&other) noexcept
移动构造 handle,并转移槽位所有权。
BasicObjectPool(size_t slot_count)
用内部 queue 和内部 slots 构造 pool。
BasicObjectPool(const BasicObjectPool &)=delete
禁止拷贝构造。 Non-copyable.
BasicObjectPool(size_t slot_count, Data *slots)
用内部 queue 和外部 slots 构造 pool。
BasicObjectPool & operator=(BasicObjectPool &&)=delete
禁止移动赋值。 Non-move-assignable.
Data * slots_
槽数组指针。 Pointer to slot storage.
FreeQueue * free_queue_
空闲索引队列指针。 Pointer to the free-index queue.
size_t EmptySize() const
返回当前仍可获取的空闲槽位数。
void AllocateOwnedSlots()
申请并拥有内部槽数组。
bool owns_slots_
是否拥有内部 slots。 Whether this pool owns the slot storage.
void ConstructOwnedQueue(size_t slot_count)
在内部存储区里构造一个自拥有空闲队列。
typename FreeQueue::ValueType IndexType
槽索引类型。 Slot index type.
const size_t slot_count_
槽位总数。 Total slot count.
size_t Size() const
返回对象池总槽位数。
ErrorCode Release(IndexType index)
把指定槽位索引归还到空闲队列。
BasicObjectPool(BasicObjectPool &&)=delete
禁止移动构造。 Non-movable.
const Data & UnsafeAt(size_t index) const
通过槽位索引只读直接访问对象,不参与所有权检查。
BasicObjectPool(FreeQueue &free_queue, size_t slot_count)
用外部 queue 和内部 slots 构造 pool。
ErrorCode Acquire(Handle &handle)
获取一个槽位 handle。
~BasicObjectPool()
析构对象池,并释放其拥有的内部资源。
FreeQueue QueueType
空闲索引队列类型。 Free-index queue type.
Data ValueType
槽内对象类型。 Slot object type.
bool owns_free_queue_
是否拥有内部 queue。 Whether this pool owns the free queue.
BasicObjectPool & operator=(const BasicObjectPool &)=delete
禁止拷贝赋值。 Non-copy-assignable.
BasicObjectPool(FreeQueue &free_queue, size_t slot_count, Data *slots)
用外部 queue 和外部 slots 构造 pool。
std::byte free_queue_storage_[sizeof(FreeQueue)]
内部自拥有 queue 的原地构造存储区。 In-place storage for an internally owned queue.
Data & UnsafeAt(size_t index)
通过槽位索引直接访问对象,不参与所有权检查。
void InitializeFreeQueue()
用 0 .. slot_count - 1 初始化空闲索引队列。
@ OK
操作成功 | Operation successful