VLink  2.0.0
A high-performance communication middleware
vlink::ObjectPool< T > 模板类 参考

Thread-safe recycling pool for instances of T with RAII acquisition. 更多...

#include <object_pool.h>

类 vlink::ObjectPool< T > 继承关系图:
vlink::ObjectPool< T > 的协作图:

struct  PoolDeleter
 Custom deleter installed on every RAII handle handed out by get / get_shared. 更多...
 

Public 类型

using FactoryCallback = MoveFunction< std::unique_ptr< T >()>
 Signature of the factory used to grow the pool on demand. 更多...
 
using ResetCallback = MoveFunction< void(T &)>
 Signature of the reset hook used to scrub objects on acquire and/or release. 更多...
 
 Decides when (or whether) the user-supplied ResetCallback runs. 更多...

Public 成员函数

 ObjectPool (FactoryCallback factory_callback=get_default_factory(), size_t initial_size=0, size_t max_size=0, ResetCallback reset_callback=nullptr, Policy policy=kPolicyRelease)
 Constructs the pool and optionally pre-warms the free list. 更多...
 
std::unique_ptr< T, typename ObjectPool< T >::PoolDeleterget ()
 Acquires an object wrapped in a unique_ptr with automatic return. 更多...
 
std::shared_ptr< T > get_shared ()
 Acquires an object wrapped in a shared_ptr with automatic return. 更多...
 
T * borrow ()
 Acquires an object as a raw pointer; the caller owns the return path. 更多...
 
void give_back (T *ptr)
 Returns an object obtained from borrow() back to the pool. 更多...
 
Stats stats () const
 Captures a thread-safe snapshot of pool statistics. 更多...
 
size_t size () const
 Returns the number of objects currently idle on the free list. 更多...
 

额外继承的成员函数

详细描述

template<typename T>
class vlink::ObjectPool< T >

Thread-safe recycling pool for instances of T with RAII acquisition.

Must always live behind a std::shared_ptr because PoolDeleter holds a std::weak_ptr back to the owning pool; constructing one on the stack leaks outstanding RAII handles into a dangling pool. Use std::make_shared<ObjectPool<T>>().

模板参数
TPooled element type. Must be default-constructible unless a non-default factory callback is supplied.

成员类型定义说明

◆ FactoryCallback

template<typename T >
using vlink::ObjectPool< T >::FactoryCallback = MoveFunction<std::unique_ptr<T>()>

Signature of the factory used to grow the pool on demand.

A non-null std::unique_ptr<T> must be returned; nullptr or thrown exceptions are propagated to the caller of get / get_shared / borrow.

◆ ResetCallback

template<typename T >
using vlink::ObjectPool< T >::ResetCallback = MoveFunction<void(T&)>

Signature of the reset hook used to scrub objects on acquire and/or release.

Exceptions thrown on the release path are swallowed and the affected object is discarded. Exceptions on the acquire path return the object to the pool and rethrow to the caller.

构造及析构函数说明

◆ ObjectPool()

template<typename T >
vlink::ObjectPool< T >::ObjectPool ( FactoryCallback  factory_callback = get_default_factory(),
size_t  initial_size = 0,
size_t  max_size = 0,
ResetCallback  reset_callback = nullptr,
Policy  policy = kPolicyRelease 
)
inlineexplicit

Constructs the pool and optionally pre-warms the free list.

Details

参数
factory_callbackFactory used to grow the pool. Default: std::make_unique<T>().
initial_sizeObjects to allocate eagerly at construction time.
max_sizeCap on the number of live objects. 0 means unlimited.
reset_callbackOptional scrubbing hook driven by policy.
policyWhen the reset hook fires. Default: kPolicyRelease.
异常
std::invalid_argumentwhen initial_size exceeds a non-zero max_size.
std::runtime_errorwhen the factory returns nullptr while pre-filling.
注解
Always construct via std::make_shared<ObjectPool<T>>() so that PoolDeleter can resolve its weak_ptr.
函数调用图:

成员函数说明

◆ borrow()

template<typename T >
T * vlink::ObjectPool< T >::borrow
inline

Acquires an object as a raw pointer; the caller owns the return path.

返回
Raw pointer that must later be returned via give_back().
异常
std::runtime_errorwhen the pool is exhausted or the factory fails.

◆ get()

template<typename T >
std::unique_ptr< T, typename ObjectPool< T >::PoolDeleter > vlink::ObjectPool< T >::get
inline

Acquires an object wrapped in a unique_ptr with automatic return.

返回
RAII handle whose deleter releases the object back to the pool.
异常
std::runtime_errorwhen the pool is exhausted or the factory fails.

◆ get_shared()

template<typename T >
std::shared_ptr< T > vlink::ObjectPool< T >::get_shared
inline

Acquires an object wrapped in a shared_ptr with automatic return.

返回
RAII handle whose deleter releases the object back to the pool.
异常
std::runtime_errorwhen the pool is exhausted or the factory fails.

◆ give_back()

template<typename T >
void vlink::ObjectPool< T >::give_back ( T *  ptr)
inline

Returns an object obtained from borrow() back to the pool.

参数
ptrRaw pointer previously returned by borrow(); nullptr is ignored.

◆ size()

template<typename T >
size_t vlink::ObjectPool< T >::size
inline

Returns the number of objects currently idle on the free list.

返回
Idle object count.

◆ stats()

template<typename T >
ObjectPoolBase::Stats vlink::ObjectPool< T >::stats
inline

Captures a thread-safe snapshot of pool statistics.

返回
Filled Stats value.

该类的文档由以下文件生成: