libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::ObjectPool< Data >::Handle Class Reference

可写句柄:独占一个槽位,只能移动。 Writable handle: exclusive owner of one slot, move-only. More...

#include <object_pool.hpp>

Public Member Functions

 Handle ()=default
 构造空句柄 / Construct an empty handle.
 
 Handle (const Handle &)=delete
 禁止拷贝构造 / Non-copyable.
 
Handle & operator= (const Handle &)=delete
 禁止拷贝赋值 / Non-copy-assignable.
 
 Handle (Handle &&other) noexcept
 转移槽位并清空源句柄 / Transfer the slot and empty the source.
 
Handle & operator= (Handle &&other) noexcept
 转移槽位并释放原有槽位 / Transfer the slot and release the previous one.
 
 ~Handle ()
 释放槽位 / Release the slot.
 
bool Valid () const
 是否持有槽位 / Whether this handle owns a slot.
 
Data & Get ()
 访问负载,句柄必须有效 / Access the payload; requires a valid handle.
 
const Data & Get () const
 只读访问负载,句柄必须有效 / Read-only payload access; requires a valid handle.
 
Data * operator-> ()
 返回负载指针 / Return the payload pointer.
 
const Data * operator-> () const
 返回只读负载指针 / Return a read-only payload pointer.
 
Data & operator* ()
 解引用 / Dereference.
 
const Data & operator* () const
 只读解引用 / Dereference read-only.
 
IndexType Index () const
 返回有效句柄的槽索引 / Return the slot index of a valid handle.
 
void Reset ()
 清空句柄并释放槽位;空句柄无操作 / Empty and release; no-op on an empty handle.
 

Friends

class ObjectPool
 
class ConstHandle
 

Detailed Description

template<typename Data>
class LibXR::ObjectPool< Data >::Handle

可写句柄:独占一个槽位,只能移动。 Writable handle: exclusive owner of one slot, move-only.

Acquire 得到可写句柄;写完后移动为 ConstHandle 共享出去。 Acquire yields a writable handle; after writing, move it into a ConstHandle to share the slot.

Definition at line 100 of file object_pool.hpp.

Constructor & Destructor Documentation

◆ Handle()

template<typename Data >
LibXR::ObjectPool< Data >::Handle::Handle ( Handle && other)
inlinenoexcept

转移槽位并清空源句柄 / Transfer the slot and empty the source.

Definition at line 112 of file object_pool.hpp.

113 : pool_(std::exchange(other.pool_, nullptr)),
114 index_(std::exchange(other.index_, IndexType{}))
115 {
116 }
uint32_t IndexType
槽索引类型 / Slot index type.

◆ ~Handle()

template<typename Data >
LibXR::ObjectPool< Data >::Handle::~Handle ( )
inline

释放槽位 / Release the slot.

Definition at line 131 of file object_pool.hpp.

131{ Reset(); }
void Reset()
清空句柄并释放槽位;空句柄无操作 / Empty and release; no-op on an empty handle.

Member Function Documentation

◆ Get() [1/2]

template<typename Data >
Data & LibXR::ObjectPool< Data >::Handle::Get ( )
inlinenodiscard

访问负载,句柄必须有效 / Access the payload; requires a valid handle.

Definition at line 137 of file object_pool.hpp.

138 {
139 ASSERT(Valid());
140 return pool_->slots_[index_].data_;
141 }
bool Valid() const
是否持有槽位 / Whether this handle owns a slot.

◆ Get() [2/2]

template<typename Data >
const Data & LibXR::ObjectPool< Data >::Handle::Get ( ) const
inlinenodiscard

只读访问负载,句柄必须有效 / Read-only payload access; requires a valid handle.

Definition at line 145 of file object_pool.hpp.

146 {
147 ASSERT(Valid());
148 return pool_->slots_[index_].data_;
149 }

◆ Index()

template<typename Data >
IndexType LibXR::ObjectPool< Data >::Handle::Index ( ) const
inlinenodiscard

返回有效句柄的槽索引 / Return the slot index of a valid handle.

Definition at line 161 of file object_pool.hpp.

162 {
163 ASSERT(Valid());
164 return index_;
165 }

◆ operator*() [1/2]

template<typename Data >
Data & LibXR::ObjectPool< Data >::Handle::operator* ( )
inlinenodiscard

解引用 / Dereference.

Definition at line 156 of file object_pool.hpp.

156{ return Get(); }
Data & Get()
访问负载,句柄必须有效 / Access the payload; requires a valid handle.

◆ operator*() [2/2]

template<typename Data >
const Data & LibXR::ObjectPool< Data >::Handle::operator* ( ) const
inlinenodiscard

只读解引用 / Dereference read-only.

Definition at line 158 of file object_pool.hpp.

158{ return Get(); }

◆ operator->() [1/2]

template<typename Data >
Data * LibXR::ObjectPool< Data >::Handle::operator-> ( )
inlinenodiscard

返回负载指针 / Return the payload pointer.

Definition at line 152 of file object_pool.hpp.

152{ return &Get(); }

◆ operator->() [2/2]

template<typename Data >
const Data * LibXR::ObjectPool< Data >::Handle::operator-> ( ) const
inlinenodiscard

返回只读负载指针 / Return a read-only payload pointer.

Definition at line 154 of file object_pool.hpp.

154{ return &Get(); }

◆ operator=()

template<typename Data >
Handle & LibXR::ObjectPool< Data >::Handle::operator= ( Handle && other)
inlinenoexcept

转移槽位并释放原有槽位 / Transfer the slot and release the previous one.

Definition at line 119 of file object_pool.hpp.

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

◆ Reset()

template<typename Data >
void LibXR::ObjectPool< Data >::Handle::Reset ( )
inline

清空句柄并释放槽位;空句柄无操作 / Empty and release; no-op on an empty handle.

Definition at line 169 of file object_pool.hpp.

170 {
171 ObjectPool* pool = std::exchange(pool_, nullptr);
172 const IndexType index = std::exchange(index_, IndexType{});
173 if (pool != nullptr)
174 {
175 pool->Release(index);
176 }
177 }

◆ Valid()

template<typename Data >
bool LibXR::ObjectPool< Data >::Handle::Valid ( ) const
inlinenodiscard

是否持有槽位 / Whether this handle owns a slot.

Definition at line 134 of file object_pool.hpp.

134{ return pool_ != nullptr; }

Friends And Related Symbol Documentation

◆ ConstHandle

template<typename Data >
friend class ConstHandle
friend

Definition at line 181 of file object_pool.hpp.

◆ ObjectPool

template<typename Data >
friend class ObjectPool
friend

Definition at line 180 of file object_pool.hpp.


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