libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::BasicObjectPool< Data, FreeQueue >::Handle Class Reference

槽索引必须是整数类型。 Slot indices must use an integral type. More...

#include <object_pool.hpp>

Collaboration diagram for LibXR::BasicObjectPool< Data, FreeQueue >::Handle:
[legend]

Public Member Functions

 Handle ()=default
 构造一个空 handle。
 
 Handle (BasicObjectPool *pool, IndexType index)
 用指定 pool 和槽位索引构造 handle。
 
 Handle (const Handle &)=delete
 禁止拷贝构造。 Non-copyable.
 
Handleoperator= (const Handle &)=delete
 禁止拷贝赋值。 Non-copy-assignable.
 
 Handle (Handle &&other) noexcept
 移动构造 handle,并转移槽位所有权。
 
Handleoperator= (Handle &&other) noexcept
 移动赋值 handle,并转移槽位所有权。
 
 ~Handle ()
 析构 handle,并自动归还槽位。
 
bool Valid () const
 判断当前 handle 是否持有有效槽位。
 
Data & Get ()
 返回当前槽位对象的可写引用。
 
const Data & Get () const
 返回当前槽位对象的只读引用。
 
Data * operator-> ()
 以指针形式访问当前槽位对象。
 
const Data * operator-> () const
 以只读指针形式访问当前槽位对象。
 
Data & operator* ()
 解引用当前槽位对象。
 
const Data & operator* () const
 只读解引用当前槽位对象。
 
IndexType Index () const
 返回当前持有的槽位索引。
 
void Reset ()
 主动归还当前槽位,并使 handle 失效。
 

Private Attributes

BasicObjectPoolpool_ = nullptr
 所属对象池。 Owning object pool.
 
IndexType index_ = {}
 当前槽位索引。 Current slot index.
 

Detailed Description

template<typename Data, PoolIndexQueue FreeQueue>
class LibXR::BasicObjectPool< Data, FreeQueue >::Handle

槽索引必须是整数类型。 Slot indices must use an integral type.

槽索引必须是无符号整数类型。 Slot indices must use an unsigned integral type.

对象池槽位的 move-only RAII 句柄。

Move-only RAII handle for one object-pool slot.

句柄持有一个槽位索引及所属 pool 指针;析构时会自动把索引归还到空闲队列。 The handle stores one slot index plus its owning pool pointer, and returns the index to the free queue automatically on destruction.

Definition at line 81 of file object_pool.hpp.

Constructor & Destructor Documentation

◆ Handle() [1/3]

template<typename Data , PoolIndexQueue FreeQueue>
LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Handle ( )
default

构造一个空 handle。

Construct an empty handle.

◆ Handle() [2/3]

template<typename Data , PoolIndexQueue FreeQueue>
LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Handle ( BasicObjectPool * pool,
IndexType index )
inline

用指定 pool 和槽位索引构造 handle。

Construct a handle from the given pool and slot index.

Parameters
pool所属对象池。 Owning object pool.
index槽位索引。 Slot index.

Definition at line 96 of file object_pool.hpp.

96: pool_(pool), index_(index) {}
BasicObjectPool * pool_
所属对象池。 Owning object pool.
IndexType index_
当前槽位索引。 Current slot index.

◆ Handle() [3/3]

template<typename Data , PoolIndexQueue FreeQueue>
LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Handle ( Handle && other)
inlinenoexcept

移动构造 handle,并转移槽位所有权。

Move-construct the handle and transfer slot ownership.

Parameters
other被转移的源 handle。 Source handle being moved from.

Definition at line 108 of file object_pool.hpp.

109 : pool_(std::exchange(other.pool_, nullptr)),
110 index_(std::exchange(other.index_, IndexType{}))
111 {
112 }
typename FreeQueue::ValueType IndexType
槽索引类型。 Slot index type.

◆ ~Handle()

template<typename Data , PoolIndexQueue FreeQueue>
LibXR::BasicObjectPool< Data, FreeQueue >::Handle::~Handle ( )
inline

析构 handle,并自动归还槽位。

Destroy the handle and return the slot automatically.

Definition at line 137 of file object_pool.hpp.

137{ Reset(); }
void Reset()
主动归还当前槽位,并使 handle 失效。

Member Function Documentation

◆ Get() [1/2]

template<typename Data , PoolIndexQueue FreeQueue>
Data & LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Get ( )
inlinenodiscard

返回当前槽位对象的可写引用。

Return a writable reference to the current slot object.

Returns
当前槽位对象引用。 Reference to the current slot object.

Definition at line 152 of file object_pool.hpp.

153 {
154 ASSERT(pool_ != nullptr);
155 return pool_->slots_[index_];
156 }
Data * slots_
槽数组指针。 Pointer to slot storage.

◆ Get() [2/2]

template<typename Data , PoolIndexQueue FreeQueue>
const Data & LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Get ( ) const
inlinenodiscard

返回当前槽位对象的只读引用。

Return a read-only reference to the current slot object.

Returns
当前槽位对象常量引用。 Const reference to the current slot object.

Definition at line 163 of file object_pool.hpp.

164 {
165 ASSERT(pool_ != nullptr);
166 return pool_->slots_[index_];
167 }

◆ Index()

template<typename Data , PoolIndexQueue FreeQueue>
IndexType LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Index ( ) const
inlinenodiscard

返回当前持有的槽位索引。

Return the currently owned slot index.

Returns
当前槽位索引。 Current slot index.

Definition at line 202 of file object_pool.hpp.

203 {
204 ASSERT(pool_ != nullptr);
205 return index_;
206 }

◆ operator*() [1/2]

template<typename Data , PoolIndexQueue FreeQueue>
Data & LibXR::BasicObjectPool< Data, FreeQueue >::Handle::operator* ( )
inlinenodiscard

解引用当前槽位对象。

Dereference the current slot object.

Returns
当前槽位对象引用。 Reference to the current slot object.

Definition at line 188 of file object_pool.hpp.

188{ return Get(); }
Data & Get()
返回当前槽位对象的可写引用。

◆ operator*() [2/2]

template<typename Data , PoolIndexQueue FreeQueue>
const Data & LibXR::BasicObjectPool< Data, FreeQueue >::Handle::operator* ( ) const
inlinenodiscard

只读解引用当前槽位对象。

Dereference the current slot object as const.

Returns
当前槽位对象常量引用。 Const reference to the current slot object.

Definition at line 195 of file object_pool.hpp.

195{ return Get(); }

◆ operator->() [1/2]

template<typename Data , PoolIndexQueue FreeQueue>
Data * LibXR::BasicObjectPool< Data, FreeQueue >::Handle::operator-> ( )
inlinenodiscard

以指针形式访问当前槽位对象。

Access the current slot object as a pointer.

Returns
指向当前槽位对象的指针。 Pointer to the current slot object.

Definition at line 174 of file object_pool.hpp.

174{ return &Get(); }

◆ operator->() [2/2]

template<typename Data , PoolIndexQueue FreeQueue>
const Data * LibXR::BasicObjectPool< Data, FreeQueue >::Handle::operator-> ( ) const
inlinenodiscard

以只读指针形式访问当前槽位对象。

Access the current slot object as a const pointer.

Returns
指向当前槽位对象的只读指针。 Const pointer to the current slot object.

Definition at line 181 of file object_pool.hpp.

181{ return &Get(); }

◆ operator=()

template<typename Data , PoolIndexQueue FreeQueue>
Handle & LibXR::BasicObjectPool< Data, FreeQueue >::Handle::operator= ( Handle && other)
inlinenoexcept

移动赋值 handle,并转移槽位所有权。

Move-assign the handle and transfer slot ownership.

Parameters
other被转移的源 handle。 Source handle being moved from.
Returns
当前 handle 的引用。 Reference to this handle.

Definition at line 120 of file object_pool.hpp.

121 {
122 if (this == &other)
123 {
124 return *this;
125 }
126
127 Reset();
128 pool_ = std::exchange(other.pool_, nullptr);
129 index_ = std::exchange(other.index_, IndexType{});
130 return *this;
131 }

◆ Reset()

template<typename Data , PoolIndexQueue FreeQueue>
void LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Reset ( )
inline

主动归还当前槽位,并使 handle 失效。

Return the current slot explicitly and invalidate the handle.

Definition at line 212 of file object_pool.hpp.

213 {
214 if (pool_ == nullptr)
215 {
216 return;
217 }
218
219 const ErrorCode ec = pool_->Release(index_);
220 ASSERT(ec == ErrorCode::OK);
221 pool_ = nullptr;
222 index_ = IndexType{};
223 }
ErrorCode Release(IndexType index)
把指定槽位索引归还到空闲队列。
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful

◆ Valid()

template<typename Data , PoolIndexQueue FreeQueue>
bool LibXR::BasicObjectPool< Data, FreeQueue >::Handle::Valid ( ) const
inlinenodiscard

判断当前 handle 是否持有有效槽位。

Return whether this handle currently owns a valid slot.

Returns
持有有效槽位返回 true,否则返回 false。 Returns true when this handle owns a valid slot, otherwise false.

Definition at line 145 of file object_pool.hpp.

145{ return pool_ != nullptr; }

Field Documentation

◆ index_

template<typename Data , PoolIndexQueue FreeQueue>
IndexType LibXR::BasicObjectPool< Data, FreeQueue >::Handle::index_ = {}
private

当前槽位索引。 Current slot index.

Definition at line 227 of file object_pool.hpp.

227{};

◆ pool_

template<typename Data , PoolIndexQueue FreeQueue>
BasicObjectPool* LibXR::BasicObjectPool< Data, FreeQueue >::Handle::pool_ = nullptr
private

所属对象池。 Owning object pool.

Definition at line 226 of file object_pool.hpp.


The documentation for this class was generated from the following file: