[clangd] Replace UniqueFunction with llvm::unique_function.
One implementation of this ought to be enough for everyone.
llvm-svn: 336228
diff --git a/clang-tools-extra/clangd/Function.h b/clang-tools-extra/clangd/Function.h
index 975134b..88a5bc0 100644
--- a/clang-tools-extra/clangd/Function.h
+++ b/clang-tools-extra/clangd/Function.h
@@ -7,83 +7,25 @@
//
//===----------------------------------------------------------------------===//
//
-// This file provides an analogue to std::function that supports move semantics.
+// This file provides utilities for callable objects.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FUNCTION_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FUNCTION_H
-#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/FunctionExtras.h"
#include "llvm/Support/Error.h"
-#include <cassert>
-#include <memory>
#include <tuple>
-#include <type_traits>
#include <utility>
namespace clang {
namespace clangd {
-/// A move-only type-erasing function wrapper. Similar to `std::function`, but
-/// allows to store move-only callables.
-template <class> class UniqueFunction;
/// A Callback<T> is a void function that accepts Expected<T>.
/// This is accepted by ClangdServer functions that logically return T.
-template <typename T> using Callback = UniqueFunction<void(llvm::Expected<T>)>;
-
-template <class Ret, class... Args> class UniqueFunction<Ret(Args...)> {
-public:
- UniqueFunction() = default;
- UniqueFunction(std::nullptr_t) : UniqueFunction(){};
-
- UniqueFunction(UniqueFunction const &) = delete;
- UniqueFunction &operator=(UniqueFunction const &) = delete;
-
- UniqueFunction(UniqueFunction &&) noexcept = default;
- UniqueFunction &operator=(UniqueFunction &&) noexcept = default;
-
- template <class Callable,
- /// A sfinae-check that Callable can be called with Args... and
- class = typename std::enable_if<std::is_convertible<
- decltype(std::declval<Callable>()(std::declval<Args>()...)),
- Ret>::value>::type>
- UniqueFunction(Callable &&Func)
- : CallablePtr(llvm::make_unique<
- FunctionCallImpl<typename std::decay<Callable>::type>>(
- std::forward<Callable>(Func))) {}
-
- explicit operator bool() { return bool(CallablePtr); }
-
- Ret operator()(Args... As) {
- assert(CallablePtr);
- return CallablePtr->Call(std::forward<Args>(As)...);
- }
-
-private:
- class FunctionCallBase {
- public:
- virtual ~FunctionCallBase() = default;
- virtual Ret Call(Args... As) = 0;
- };
-
- template <class Callable>
- class FunctionCallImpl final : public FunctionCallBase {
- static_assert(
- std::is_same<Callable, typename std::decay<Callable>::type>::value,
- "FunctionCallImpl must be instanstiated with std::decay'ed types");
-
- public:
- FunctionCallImpl(Callable Func) : Func(std::move(Func)) {}
-
- Ret Call(Args... As) override { return Func(std::forward<Args>(As)...); }
-
- private:
- Callable Func;
- };
-
- std::unique_ptr<FunctionCallBase> CallablePtr;
-};
+template <typename T>
+using Callback = llvm::unique_function<void(llvm::Expected<T>)>;
/// Stores a callable object (Func) and arguments (Args) and allows to call the
/// callable with provided arguments later using `operator ()`. The arguments