#pragma once #include "fwd.h" #include "future.h" #include #include namespace NThreading { template class TLegacyFuture: public IThreadFactory::IThreadAble, TNonCopyable { public: typedef TR(TFunctionSignature)(); using TFunctionObjectType = std::function; using TResult = typename TFunctionObjectType::result_type; private: TFunctionObjectType Func_; TPromise Result_; THolder Thread_; public: inline TLegacyFuture(const TFunctionObjectType func, IThreadFactory* pool = SystemThreadFactory()) : Func_(func) , Result_(NewPromise()) , Thread_(pool->Run(this)) { } inline ~TLegacyFuture() override { this->Join(); } inline TResult Get() { this->Join(); return Result_.GetValue(); } private: inline void Join() { if (Thread_) { Thread_->Join(); Thread_.Destroy(); } } template struct TExecutor { static void SetPromise(TPromise& promise, const TFunctionObjectType& func) { if (IgnoreException_) { try { promise.SetValue(func()); } catch (...) { } } else { promise.SetValue(func()); } } }; template struct TExecutor { static void SetPromise(TPromise& promise, const TFunctionObjectType& func) { if (IgnoreException_) { try { func(); promise.SetValue(); } catch (...) { } } else { func(); promise.SetValue(); } } }; void DoExecute() override { TExecutor::SetPromise(Result_, Func_); } }; }