#pragma once #include #include #include #include #include #include // probably this thing should have been called TFuture template class TAsyncResult : TNonCopyable { private: TMutex Mutex; TCondVar CondVar; TMaybe Result; typedef void TOnResult(const T&); std::function OnResult; public: void SetResult(const T& result) { TGuard guard(Mutex); Y_ABORT_UNLESS(!Result, "cannot set result twice"); Result = result; CondVar.BroadCast(); if (!!OnResult) { OnResult(result); } } const T& GetResult() { TGuard guard(Mutex); while (!Result) { CondVar.Wait(Mutex); } return *Result; } template void AndThen(const TFunc& onResult) { TGuard guard(Mutex); if (!!Result) { onResult(*Result); } else { Y_ASSERT(!OnResult); OnResult = std::function(onResult); } } };