(31) 特化版本 shared_future,其泛化版本在上一篇:
template <> // class that defines a copyable asynchronous return object that does not hold a value
class shared_future<void> : public _State_manager<int>
{
using _Mybase = _State_manager<int>;
public:
shared_future() noexcept {}
shared_future(future<void>&& _Other) noexcept : _Mybase(_STD forward<_Mybase>(_Other)) {}
shared_future(const shared_future& _Other) noexcept : _Mybase(_Other) {}
shared_future& operator=(const shared_future& _Right) noexcept
{
_Mybase::operator=(_Right);
return *this;
}
shared_future(shared_future&& _Other) noexcept : _Mybase(_STD move(_Other)) {}
shared_future& operator=(shared_future&& _Right)
{
_Mybase::operator=(_STD move(_Right));
return *this;
}
~shared_future() noexcept {}
void get() const // block until ready then return or throw the stored exception
{
this->_Get_value();
}
};
(32)类 _Promise《T》,该类包含了 _State_manager<_Ty> 这个对象数据成员,源码如下。本模板类也出现在 async 函数里,所以要先学习一下:
template <class _Ty> // 注意前导的下划线 _Promise
class _Promise
{
private:
// _State_manager<T> { _Associated_state<T>* _Assoc_state; bool _Get_only_once; };
_State_manager<_Ty> _State; // 该类也有子类,future 与 share_future,但本 _Promise 没有使用
bool _Future_retrieved; // 函数的返回值是否已交给 future 管理,初始化为 false,后改为 true
public:
_Promise(_Associated_state<_Ty>* _State_ptr) // 有参构造函数
: _State(_State_ptr, false), _Future_retrieved(false) {}
_Promise(const _Promise&) = delete; // 禁止 copy 构造函数
_Promise& operator=(const _Promise&) = delete; // 禁止 copy 赋值运算符函数
_Promise(_Promise&& _Other) // 移动构造函数
: _State(_STD move(_Other._State)), _Future_retrieved(_Other._Future_retrieved) {}
_Promise& operator=(_Promise&& _Other) // 移动赋值运算符函数
{
_State = _STD move(_Other._State);
_Future_retrieved = _Other._Future_retrieved;
return *this;
}
// _State_manager<T> { ~_State_manager() { if (_Assoc_state) _Assoc_state->_Release(); } };
~_Promise() noexcept {} // 空的析构函数
bool _Is_valid() { return _State.valid(); }
/*
_State_manager<T>
{
~_State_manager() { if (_Assoc_state) _Assoc_state->_Release(); }
bool valid() { return _Assoc_state && !( _Get_only_once && _Assoc_state->_Retrieved ); }
bool _Is_ready() { return _Assoc_state && _Assoc_state->_Is_ready(); }
bool _Is_ready_at_thread_exit() { return _Assoc_state && _Assoc_state->_Ready_at_thread_exit; }
};
*/
bool _Is_ready() { return _State._Is_ready(); } // 这几个简单的函数采用了集中注释
bool _Is_ready_at_thread_exit() { return _State._Is_ready_at_thread_exit(); }
void _Swap(_Promise& _Other)
{
_State._Swap(_Other._State);
_STD swap(_Future_retrieved, _Other._Future_retrieved);
}
const _State_manager<_Ty>& _Get_state() const { return _State; } // 注意:返回的是左值引用
_State_manager<_Ty>& _Get_state() { return _State; } // 返回结果是左值引用
_State_manager<_Ty>& _Get_state_for_set() // 返回结果是左值引用
{
if (!_State.valid()) // 给出本类的数据成员 _State_manager<_Ty> 是为了设置函数返回值
_Throw_future_error(make_error_code(future_errc::no_state));
return _State;
}
_State_manager<_Ty>& _Get_state_for_future() // 返回结果是左值引用
{
if (!_State.valid())
_Throw_future_error(make_error_code(future_errc::no_state));
if ( _Future_retrieved )
_Throw_future_error(make_error_code(future_errc::future_already_retrieved));
_Future_retrieved = true; // 给 future 后,就不能再通过本 _Promise 使用函数返回值
return _State;
}
};
(33) 可变参模板类 _Fake_no_copy_callable_adapter<T…> , 本模板类,是当前的 STL 版本中的用以完善功能以自圆其说的类,看名字,适配器。以后的 STL 库代码,可能会更改此写法。本类也用于 全局 async 函数。本模板类,包含了要在线程中执行的函数及其实参,以为将来的函数执行做准备:
template <class... _Types, size_t... _Indices> // invoke() a tuple with explicit parameter ordering
auto _Invoke_stored_explicit(tuple<_Types...>&& _Tuple, index_sequence<_Indices...>)
-> decltype(_STD invoke(_STD get<_Indices>(_STD move(_Tuple))...))
{
return _STD invoke(_STD get<_Indices>(_STD move(_Tuple))...);
}
// 系统定义的 全局 invoke() 函数,有一个强大的功能,就是可以识别出其形参分别是可调用的函数与其参数,
// invoke() 可以把其所有的参数,整合成一个完成的函数再次运行起来。
template <class... _Types>
auto _Invoke_stored(tuple<_Types...>&& _Tuple) // invoke() a tuple
-> decltype(_Invoke_stored_explicit(_STD move(_Tuple), index_sequence_for<_Types...>{}))
{
return _Invoke_stored_explicit(_STD move(_Tuple), index_sequence_for<_Types...>{});
}
template <class... _Types> // 本类型是一个可调用对象,定义了 括号( )运算符函数
class _Fake_no_copy_callable_adapter // 核心是包括了一个 tuple 元组
{
private:
using _Storaget = tuple<decay_t<_Types>...>; // 使用了 tuple 元组
mutable _Storaget _Storage;
public:
explicit _Fake_no_copy_callable_adapter(_Types&&... _Vals) // 有参构造函数
: _Storage(_STD forward<_Types>(_Vals)...) {}
_Fake_no_copy_callable_adapter(const _Fake_no_copy_callable_adapter& _Other) // copy 构造函数
: _Storage(_STD move(_Other._Storage)) { _STD terminate(); } // Very Bad Things
_Fake_no_copy_callable_adapter(_Fake_no_copy_callable_adapter&& _Other) = default; // 默认移动构造函数
//禁止 copy赋值运算符函数 与 移动赋值运算符函数
_Fake_no_copy_callable_adapter& operator=(const _Fake_no_copy_callable_adapter&) = delete;
_Fake_no_copy_callable_adapter& operator=(_Fake_no_copy_callable_adapter&&) = delete;
auto operator()() -> decltype(_Invoke_stored(_STD move(_STD declval<_Storaget&>())))
{ // 定义了没有参数的括号 () 运算符函数
return _Invoke_stored(_STD move(_Storage)); // 看意思是返回形参中函数的返回值
}
};
(34)
谢谢