//////////////////////////////////////////////////////////////////////////////// /// DISCLAIMER /// /// Copyright 2018 ArangoDB GmbH, Cologne, Germany /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is ArangoDB GmbH, Cologne, Germany /// /// @author Simon Grätzer //////////////////////////////////////////////////////////////////////////////// #ifndef ARANGOD_FUTURES_UTILITIES_H #define ARANGOD_FUTURES_UTILITIES_H 1 #include "Futures/Future.h" #include "Futures/Unit.h" namespace arangodb { namespace futures { template Future makeFuture(Try&& t) { return Future(detail::SharedState::make(std::move(t))); } Future makeFuture() { return Future(unit); } template Future::type> makeFuture(T&& t) { return makeFuture(Try::type>(std::forward(t))); } /// Make a failed Future from an std::exception_ptr. template Future makeFuture(std::exception_ptr const& e) { return makeFuture(Try(e)); } /// Make a Future from an exception type E that can be passed to /// std::make_exception_ptr(). template typename std::enable_if::value, Future>::type makeFuture(E const& e) { return makeFuture(Try(std::make_exception_ptr(e))); } // makeFutureWith(Future()) -> Future template > typename std::enable_if::value, R>::type makeFutureWith(F&& func) { using InnerType = typename isFuture::inner; try { return std::forward(func)(); } catch (...) { return makeFuture(std::current_exception()); } } // makeFutureWith(T()) -> Future // makeFutureWith(void()) -> Future template > typename std::enable_if::value, Future>::type makeFutureWith(F&& func) { return makeFuture( makeTryWith([&func]() mutable { return std::forward(func)(); })); } } // namespace futures } // namespace arangodb #endif // ARANGOD_FUTURES_UTILITIES_H