(40) _Associated_state 中函数返回值的专用删除器 _State_deleter<T , Derived , Alloc>。一般用通用 delete 即可:
template <class _Ty> // abstract base class for managing deletion of state objects
struct __declspec(novtable) _Deleter_base
{
virtual void _Delete(_Associated_state<_Ty>*) = 0; // 纯虚函数,形参是父类指针指向子类对象
virtual ~_Deleter_base() noexcept {} // 虚析构函数
};
template <class _Ty, class _Derived, class _Alloc> // manage allocator and deletion state objects
struct _State_deleter : _Deleter_base<_Ty> // 仅当为 _Associated_state<T> 中的 T指定分配器与删除器时才有用
{
_Alloc _My_alloc;
_State_deleter(const _Alloc& _Al) : _My_alloc(_Al) {} // 构造函数
_State_deleter(const _State_deleter&) = delete; // 禁止 copy 构造函数
_State_deleter& operator=(const _State_deleter&) = delete; // 禁止 copy 赋值运算符函数
template <class _Ty, class _Derived, class _Alloc> // delete _State and this using stored allocator
void _Delete( _Associated_state<_Ty>* _State) override // 重写父类中的虚函数
{
using _State_allocator = _Rebind_alloc_t<_Alloc, _Derived>;
_State_allocator _St_alloc(_My_alloc);
using _Deleter_allocator = _Rebind_alloc_t<_Alloc, _State_deleter>;
_Deleter_allocator _Del_alloc(_My_alloc);
_Derived* _Ptr = static_cast<_Derived*>(_State);
/*
template <class _Alloc> // deallocate a plain pointer using an allocator 回收内存
void _Deallocate_plain(_Alloc& _Al, typename _Alloc::value_type* const _Ptr) noexcept
{
using _Alloc_traits = allocator_traits<_Alloc>;
if constexpr (is_same_v<_Alloc_ptr_t<_Alloc>, typename _Alloc::value_type*>)
_Alloc_traits::deallocate(_Al, _Ptr, 1);
else
{
using _Ptr_traits = pointer_traits<_Alloc_ptr_t<_Alloc>>;
_Alloc_traits::deallocate(_Al, _Ptr_traits::pointer_to(*_Ptr), 1);
}
}
// destroy *_Ptr in place, then deallocate _Ptr using _Al;
// used for internal container types the user didn't name
// 将 *_Ptr 销毁到位,然后使用 _Al 释放 _Ptr ;用于内部用户未命名的容器类型
template <class _Alloc> // 总之就是先执行 _Ty 类型对象的析构函数,再回收内存。
void _Delete_plain_internal(_Alloc& _Al, typename _Alloc::value_type* const _Ptr) noexcept
{
using _Ty = typename _Alloc::value_type;
_Ptr->~_Ty();
_Deallocate_plain(_Al, _Ptr);
}
*/
_Delete_plain_internal(_St_alloc, _Ptr);
_Delete_plain_internal(_Del_alloc, this);
}
};
(41) 在 _Associated_state 中的使用情形如下:
template <class _Ty>
class _Associated_state
{
public:
using _State_type = _Ty; // 函数返回值则 _Ty是对象类型; 返回左值引用则 _Ty是地址; 返回 void则 _Ty是 int
using _Mydel = _Deleter_base<_Ty>;
private:
_Atomic_counter_t _Refs; // using _Atomic_counter_t = unsigned long;
// future 的父类 _State_manager 的 copy 构造与赋值运算符函数,就是仅仅将 _Refs 加一。堆区仅有此一份结果。
_Mydel* _Deleter; // 初始化为 nullptr ,表程序员未使用自己编写的 删除器 ,用的系统默认版本。
void _Delete_this() // delete this object 销毁本模板类的对象自身(也存在父类指针指向子类对象)
{
if (_Deleter) _Deleter->_Delete(this);
else delete this;
}
public:
_Associated_state(_Mydel* _Dp = nullptr) : _Deleter(_Dp) { }
virtual ~_Associated_state() noexcept // registered for release at thread exit 析构函数
{ // 本类存储了线程中函数的返回值。若本类析构时函数返回值没 ready,则不再执行条件变量的唤醒操作
if (_Has_stored_result && !_Ready) { _Cond._Unregister(_Mtx); }
}
void _Retain() { _MT_INCR(_Refs); } // increment reference count 原子性的递增对本对象的引用计数
// decrement reference count and destroy when zero 原子性的递减变量的值
void _Release() { if (_MT_DECR(_Refs) == 0) _Delete_this(); }
// 当没有别处继续引用本对象时,销毁、析构本储存函数返回值的堆区对象。
};
(42)
谢谢