/** * Copyright (C) 2022-present MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the Server Side Public License, version 1, * as published by MongoDB, Inc. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Server Side Public License for more details. * * You should have received a copy of the Server Side Public License * along with this program. If not, see * . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the Server Side Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #pragma once #include "mongo/base/error_codes.h" #include "mongo/stdx/type_traits.h" #include "mongo/util/future.h" namespace mongo { namespace packaged_task_detail { /** * SigHelper is a family of types helpful for deducing the type signature of the callable wrapped by * a PackagedTask. */ template struct SigHelper {}; // Function Type template struct SigHelper : stdx::type_identity {}; // Member Function Pointers template struct SigHelper : stdx::type_identity {}; template struct SigHelper : stdx::type_identity {}; template struct SigHelper : stdx::type_identity {}; template struct SigHelper : stdx::type_identity {}; template using getCallOperator = decltype(&T::operator()); template constexpr bool hasCallOperator = stdx::is_detected_v; template using SigFor = typename std::conditional_t, SigHelper, SigHelper>::type; } // namespace packaged_task_detail /** * A PackagedTask wraps anything Callable, but packages the return value of the Callable in a Future * that can be accessed before the Callable is run. Construct a PackagedTask by giving it a * Callable. Once the PackagedTask is constructed, you can extract a Future that will contain the * result of running the packaged task. The PackagedTask can be invoked as if it was the Callable * that it wraps. */ template class PackagedTask; template class PackagedTask { using ReturnType = FutureContinuationResult, Args...>; public: template explicit PackagedTask(F&& f) : _f(std::forward(f)) {} PackagedTask(const PackagedTask&) = delete; PackagedTask& operator=(const PackagedTask&) = delete; PackagedTask(PackagedTask&&) = default; PackagedTask& operator=(PackagedTask&&) = default; /** * Invokes the Callable wrapped by this PackagedTask. This can only be called once, as a * PackagedTask produces at most one result obtained from running the wrapped Callable at most * one time. It is invalid to call this more than once. */ void operator()(Args... args) { _p.setWith([&] { return _f(std::forward(args)...); }); } /** * Returns a Future that represents the (possibly-deferred) result of the wrapped task. Because * running the task will produce exactly one result, it is safe to call getFuture() at most once * on any PackagedTask; subsequent calls will throw a DBException set with * ErrorCodes::FutureAlreadyRetrieved. */ Future getFuture() { if (_futureExtracted) { iasserted(ErrorCodes::FutureAlreadyRetrieved, "Attempted to extract more than one future from a PackagedTask"); } _futureExtracted = true; return std::move(_fut); } private: unique_function _f; Promise _p{NonNullPromiseTag{}}; Future _fut{_p.getFuture()}; bool _futureExtracted{false}; }; template > PackagedTask(F&& f) -> PackagedTask; template PackagedTask(R (*)(Args...)) -> PackagedTask; } // namespace mongo