|
libxr
1.0
Want to be the best embedded framework
|
基于空闲索引队列的 RAII 槽池。 More...
#include <object_pool.hpp>
Data Structures | |
| class | Handle |
| 槽索引必须是整数类型。 Slot indices must use an integral type. More... | |
Public Types | |
| using | ValueType = Data |
| 槽内对象类型。 Slot object type. | |
| using | QueueType = FreeQueue |
| 空闲索引队列类型。 Free-index queue type. | |
| using | IndexType = typename FreeQueue::ValueType |
| 槽索引类型。 Slot index type. | |
Public Member Functions | |
| template<typename T = Data> requires std::is_default_constructible_v<T> | |
| BasicObjectPool (size_t slot_count) | |
| 用内部 queue 和内部 slots 构造 pool。 | |
| BasicObjectPool (size_t slot_count, Data *slots) | |
| 用内部 queue 和外部 slots 构造 pool。 | |
| template<typename T = Data> requires std::is_default_constructible_v<T> | |
| BasicObjectPool (FreeQueue &free_queue, size_t slot_count) | |
| 用外部 queue 和内部 slots 构造 pool。 | |
| BasicObjectPool (FreeQueue &free_queue, size_t slot_count, Data *slots) | |
| 用外部 queue 和外部 slots 构造 pool。 | |
| ~BasicObjectPool () | |
| 析构对象池,并释放其拥有的内部资源。 | |
| BasicObjectPool (const BasicObjectPool &)=delete | |
| 禁止拷贝构造。 Non-copyable. | |
| BasicObjectPool & | operator= (const BasicObjectPool &)=delete |
| 禁止拷贝赋值。 Non-copy-assignable. | |
| BasicObjectPool (BasicObjectPool &&)=delete | |
| 禁止移动构造。 Non-movable. | |
| BasicObjectPool & | operator= (BasicObjectPool &&)=delete |
| 禁止移动赋值。 Non-move-assignable. | |
| ErrorCode | Acquire (Handle &handle) |
| 获取一个槽位 handle。 | |
| size_t | EmptySize () const |
| 返回当前仍可获取的空闲槽位数。 | |
| size_t | Size () const |
| 返回对象池总槽位数。 | |
| Data & | UnsafeAt (size_t index) |
| 通过槽位索引直接访问对象,不参与所有权检查。 | |
| const Data & | UnsafeAt (size_t index) const |
| 通过槽位索引只读直接访问对象,不参与所有权检查。 | |
Private Member Functions | |
| ErrorCode | Release (IndexType index) |
| 把指定槽位索引归还到空闲队列。 | |
| void | ConstructOwnedQueue (size_t slot_count) |
| 在内部存储区里构造一个自拥有空闲队列。 | |
| void | AllocateOwnedSlots () |
| 申请并拥有内部槽数组。 | |
| void | InitializeFreeQueue () |
用 0 .. slot_count - 1 初始化空闲索引队列。 | |
Private Attributes | |
| std::byte | free_queue_storage_ [sizeof(FreeQueue)] = {} |
| 内部自拥有 queue 的原地构造存储区。 In-place storage for an internally owned queue. | |
| FreeQueue * | free_queue_ |
| 空闲索引队列指针。 Pointer to the free-index queue. | |
| const size_t | slot_count_ |
| 槽位总数。 Total slot count. | |
| Data * | slots_ = nullptr |
| 槽数组指针。 Pointer to slot storage. | |
| bool | owns_free_queue_ |
| 是否拥有内部 queue。 Whether this pool owns the free queue. | |
| bool | owns_slots_ |
| 是否拥有内部 slots。 Whether this pool owns the slot storage. | |
基于空闲索引队列的 RAII 槽池。
RAII slot pool backed by a free-index queue.
该池使用一个索引队列管理空闲槽位;成功获取后返回 move-only Handle,其析构会 自动把槽位索引归还到空闲队列。这样上层只依赖四类队列共享的最小 typed 接口。
This pool uses one index queue to manage free slots. Successful acquisition returns one move-only Handle, whose destructor automatically returns the slot index back to the free queue. This keeps the upper layer dependent only on the minimal typed interface shared by the queue family.
| Data | 槽内对象类型。 Slot object type. |
| FreeQueue | 空闲索引队列类型。 Free-index queue type. |
Definition at line 58 of file object_pool.hpp.
| using LibXR::BasicObjectPool< Data, FreeQueue >::IndexType = typename FreeQueue::ValueType |
槽索引类型。 Slot index type.
Definition at line 63 of file object_pool.hpp.
| using LibXR::BasicObjectPool< Data, FreeQueue >::QueueType = FreeQueue |
空闲索引队列类型。 Free-index queue type.
Definition at line 62 of file object_pool.hpp.
| using LibXR::BasicObjectPool< Data, FreeQueue >::ValueType = Data |
槽内对象类型。 Slot object type.
Definition at line 61 of file object_pool.hpp.
|
inlineexplicit |
用内部 queue 和内部 slots 构造 pool。
Construct the pool with an internal queue and internal slots.
| slot_count | 槽位数量。 Number of slots. |
Definition at line 237 of file object_pool.hpp.
|
inline |
用内部 queue 和外部 slots 构造 pool。
Construct the pool with an internal queue and external slots.
| slot_count | 槽位数量。 Number of slots. |
| slots | 外部槽数组。 Caller-provided slot storage. |
slots 指向至少 slot_count 个 Data 槽位。 The caller must ensure that slots points to at least slot_count Data slots. Definition at line 255 of file object_pool.hpp.
|
inline |
用外部 queue 和内部 slots 构造 pool。
Construct the pool with an external queue and internal slots.
| free_queue | 外部空闲索引队列。 Caller-provided free-index queue. |
| slot_count | 槽位数量。 Number of slots. |
free_queue 必须是空队列,并且只供当前 pool 独占使用。 The caller-provided free_queue must be empty and dedicated to this pool. free_queue 的容量至少为 slot_count。 The caller must ensure that free_queue capacity is at least slot_count. Definition at line 276 of file object_pool.hpp.
|
inline |
用外部 queue 和外部 slots 构造 pool。
Construct the pool with an external queue and external slots.
| free_queue | 外部空闲索引队列。 Caller-provided free-index queue. |
| slot_count | 槽位数量。 Number of slots. |
| slots | 外部槽数组。 Caller-provided slot storage. |
free_queue 必须是空队列,并且只供当前 pool 独占使用。 The caller-provided free_queue must be empty and dedicated to this pool. free_queue 的容量至少为 slot_count。 The caller must ensure that free_queue capacity is at least slot_count. slots 指向至少 slot_count 个 Data 槽位。 The caller must ensure that slots points to at least slot_count Data slots. Definition at line 300 of file object_pool.hpp.
|
inline |
析构对象池,并释放其拥有的内部资源。
Destroy the object pool and release owned internal resources.
Definition at line 313 of file object_pool.hpp.
|
inlinenodiscard |
获取一个槽位 handle。
Acquire one slot handle.
| handle | 用于接收成功获取的 handle。 Handle receiving the acquired slot. |
ErrorCode::OK,池空返回 ErrorCode::EMPTY。 Returns ErrorCode::OK on success and ErrorCode::EMPTY when the pool is empty. Definition at line 345 of file object_pool.hpp.
|
inlineprivate |
申请并拥有内部槽数组。
Allocate and own the internal slot storage.
Definition at line 440 of file object_pool.hpp.
|
inlineprivate |
在内部存储区里构造一个自拥有空闲队列。
Construct an owned free queue inside internal storage.
| slot_count | 槽位数量,同时也是初始化时要压入的索引数量。 Slot count and thus the number of indices to preload. |
Definition at line 430 of file object_pool.hpp.
|
inlinenodiscard |
返回当前仍可获取的空闲槽位数。
Return the number of currently acquirable free slots.
Definition at line 366 of file object_pool.hpp.
|
inlineprivate |
用 0 .. slot_count - 1 初始化空闲索引队列。
Initialize the free-index queue with 0 .. slot_count - 1.
Definition at line 450 of file object_pool.hpp.
|
inlineprivate |
把指定槽位索引归还到空闲队列。
Return the given slot index back to the free queue.
| index | 待归还的槽位索引。 Slot index to return. |
Definition at line 418 of file object_pool.hpp.
|
inlinenodiscard |
返回对象池总槽位数。
Return the total number of slots in the pool.
Definition at line 373 of file object_pool.hpp.
|
inlinenodiscard |
通过槽位索引直接访问对象,不参与所有权检查。
Access an object by slot index without ownership checks.
| index | 槽位索引。 Slot index. |
Acquire() / Handle 的所有权语义,只适合调试、检查 外部存储区或其他明确知道槽位状态的场景。 This API bypasses the ownership semantics of Acquire() / Handle, and is intended only for debugging, external-storage inspection, or other cases where the caller already knows the slot state. Definition at line 387 of file object_pool.hpp.
|
inlinenodiscard |
通过槽位索引只读直接访问对象,不参与所有权检查。
Access an object by slot index as const without ownership checks.
| index | 槽位索引。 Slot index. |
Acquire() / Handle 的所有权语义,只适合调试、检查 外部存储区或其他明确知道槽位状态的场景。 This API bypasses the ownership semantics of Acquire() / Handle, and is intended only for debugging, external-storage inspection, or other cases where the caller already knows the slot state. Definition at line 405 of file object_pool.hpp.
|
private |
空闲索引队列指针。 Pointer to the free-index queue.
Definition at line 464 of file object_pool.hpp.
|
private |
内部自拥有 queue 的原地构造存储区。 In-place storage for an internally owned queue.
Definition at line 463 of file object_pool.hpp.
|
private |
是否拥有内部 queue。 Whether this pool owns the free queue.
Definition at line 468 of file object_pool.hpp.
|
private |
是否拥有内部 slots。 Whether this pool owns the slot storage.
Definition at line 470 of file object_pool.hpp.
|
private |
槽位总数。 Total slot count.
Definition at line 466 of file object_pool.hpp.
|
private |
槽数组指针。 Pointer to slot storage.
Definition at line 467 of file object_pool.hpp.