<future> 注释4:类 _Packaged_state<ret(args...)>、_Packaged_state<r&(g...)>、_Packaged_state<void(g...)>

(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)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值