/*T ->return type, E -> error type, D -> parameter type */
template<typename T, typename E, typename D = void>
using result_t = typename internal::result_t<T, E, D>::type;
第一个参数类型T是返回的Result<1, 2>中参数1,第二个参数类型参数E为参数2,参数D为函数参数的类型.
template<typename T>
struct State
{
std::mutex mutex;
std::experimental::optional<T> value;
std::unique_ptr<detail::ContinuationBase<T&>> continuation; // 注意这里并不是一个引用类型, 而是一个 ContinuationBase<int&> class类似, ContinuationBase是不用做引用类型的初始化.
}
template<typename T>
class Promise
{
.....
private:
std::shared_ptr<typename detail::State<T>> _state;
};
template<typename T>
class Future
{
private:
std::shared_ptr<detail::State<T>> _state;
friend class Promise<T>;
};
Promise 和 Future 通过 std::shared_ptr<detail::State<T>> _state;连接.
template<typename... Args>
class ContinuationBase
{
public:
virtual ~ContinuationBase() {}
virtual void operator () (Args&&... args) = 0;
};
template<typename F, typename... Args>
class Continuation : public ContinuationBase<Args...>
{
static_assert(!std::is_reference<F> {}, "F cannot be a reference");
public:
typedef result_of<F, Args...> result_type;
Continuation(const F& fun) : _fun(fun)
{
}
Continuation(F&& fun) : _fun(std::move(fun))
{
}
}