(17) 类 _Packaged_state<ret(args…)>、_Packaged_state<ret&(args…)>、_Packaged_state<void(args…)> , 这也是上一篇 _Packaged_state<_Ty> 的子类:
// 模板参数的类型为线程中函数的类型 Ret(_ArgTypes...)、Ret&(_ArgTypes...)、void(_ArgTypes...)
template <class> // 这应该是泛型类的泛型版本,若不需要实例化,可以只声明,不定义
class _Packaged_state; // 本父类系列模板没有修改爷爷类中的虚函数
template <class _Ret, class... _ArgTypes> // 本类是 _Associated_state<_Ret> 的子类
class _Packaged_state<_Ret(_ArgTypes...)> : public _Associated_state<_Ret>
{
private: // function 对象,包裹可调用对象
function<_Ret(_ArgTypes...)> _Fn; // 传递来的 _Fake_no_copy_callable_adapter 是可调用对象
public:
using _Mybase = _Associated_state<_Ret>; // 父类
using _Mydel = typename _Mybase::_Mydel;
template <class _Fty2>
_Packaged_state(const _Fty2& _Fnarg) : _Fn(_Fnarg) {} // 有参构造函数
template <class _Fty2> // 采用的是这个万能引用的版本, 有参构造函数
_Packaged_state(_Fty2&& _Fnarg) : _Fn(_STD forward<_Fty2>(_Fnarg)) {}
const function<_Ret(_ArgTypes...)>& _Get_fn() { return _Fn; } // 返回左值引用
// 每种类型的函数(返回值的类型不同), 都有延迟执行和立即执行的两种情况
void _Call_deferred(_ArgTypes... _Args) // set deferred call
{
_TRY_BEGIN // call function object and catch exceptions
this->_Set_value(_Fn(_STD forward<_ArgTypes>(_Args)...), true);
_CATCH_ALL // function object threw exception; record result
this->_Set_exception(_STD current_exception(), true);
_CATCH_END
}
// 父类的各个版本的成函 _Set_value(...) 的最后一个参数都是 bool _At_thread_exit
void _Call_immediate(_ArgTypes... _Args) // call function object
{
_TRY_BEGIN //= try {
this->_Set_value(_Fn(_STD forward<_ArgTypes>(_Args)...), false);
_CATCH_ALL //= } catch (...) { // 这是可变参数包类型
this->_Set_exception(_STD current_exception(), false);
_CATCH_END
} // 本函中,把线程里的函数执行了
};
// 本类是 _Associated_state<_Ret*> 的子类 ,注意定义的时候对函数的返回值类型做了转换为指针
template <class _Ret, class... _ArgTypes>
class _Packaged_state<_Ret& (_ArgTypes...)> : public _Associated_state<_Ret*>
{
private:
function<_Ret& (_ArgTypes...)> _Fn;
public:
using _Mybase = _Associated_state<_Ret*>;
using _Mydel = typename _Mybase::_Mydel;
template <class _Fty2> // 有参构造函数
_Packaged_state(const _Fty2& _Fnarg) : _Fn(_Fnarg) {}
template <class _Fty2> // 有参构造函数
_Packaged_state(_Fty2&& _Fnarg) : _Fn(_STD forward<_Fty2>(_Fnarg)) {}
const function<_Ret& (_ArgTypes...)>& _Get_fn() { return _Fn; }
void _Call_deferred(_ArgTypes... _Args) // set deferred call
{
_TRY_BEGIN // 对函数的返回值取地址
this->_Set_value(_STD addressof(_Fn(_STD forward<_ArgTypes>(_Args)...)), true);
_CATCH_ALL
this->_Set_exception(_STD current_exception(), true);
_CATCH_END
}
void _Call_immediate(_ArgTypes... _Args) // call function object
{
_TRY_BEGIN // 父类中的 void _Set_value ( bool _At_thread_exit )
this->_Set_value(_STD addressof(_Fn(_STD forward<_ArgTypes>(_Args)...)), false);
_CATCH_ALL
this->_Set_exception(_STD current_exception(), false);
_CATCH_END
}
};
template <class... _ArgTypes> //注意对函数的返回值类型转换 void 为 int
class _Packaged_state<void(_ArgTypes...)> : public _Associated_state<int>
{
private:
function<void(_ArgTypes...)> _Fn;
public:
using _Mybase = _Associated_state<int>;
using _Mydel = typename _Mybase::_Mydel;
template <class _Fty2> // 有参构造函数
_Packaged_state(const _Fty2& _Fnarg) : _Fn(_Fnarg) {}
template <class _Fty2> // 有参构造函数
_Packaged_state(_Fty2&& _Fnarg) : _Fn(_STD forward<_Fty2>(_Fnarg)) {}
const function<void(_ArgTypes...)>& _Get_fn() { return _Fn; } // 返回左值引用
void _Call_deferred(_ArgTypes... _Args) // set deferred call
{
_TRY_BEGIN
_Fn(_STD forward<_ArgTypes>(_Args)...);
this->_Set_value(1, true); // 可见,只要函数成功执行,就存入返回值为 1
_CATCH_ALL
this->_Set_exception(_STD current_exception(), true);
_CATCH_END
}
void _Call_immediate(_ArgTypes... _Args)
{
_TRY_BEGIN
_Fn(_STD forward<_ArgTypes>(_Args)...);
this->_Set_value(1, false);
_CATCH_ALL
this->_Set_exception(_STD current_exception(), false);
_CATCH_END
}
};
(18)
谢谢
1229

被折叠的 条评论
为什么被折叠?



