diff options
| author | Ilya Biryukov <ibiryukov@google.com> | 2017-12-13 12:51:22 +0000 |
|---|---|---|
| committer | Ilya Biryukov <ibiryukov@google.com> | 2017-12-13 12:51:22 +0000 |
| commit | 940901e8b169b6d797762e8188e96d7d0446cc3d (patch) | |
| tree | 8894fb8dd512ee03f9f682a2f115ace6850ab0a7 /clang-tools-extra/clangd/Function.h | |
| parent | 845e5dce835d24a30d876411fa3153115141ae89 (diff) | |
| download | bcm5719-llvm-940901e8b169b6d797762e8188e96d7d0446cc3d.tar.gz bcm5719-llvm-940901e8b169b6d797762e8188e96d7d0446cc3d.zip | |
[clangd] Implemented logging using Context
Reviewers: sammccall, ioeric, hokein
Reviewed By: sammccall
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D40486
llvm-svn: 320576
Diffstat (limited to 'clang-tools-extra/clangd/Function.h')
| -rw-r--r-- | clang-tools-extra/clangd/Function.h | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/clang-tools-extra/clangd/Function.h b/clang-tools-extra/clangd/Function.h index d1bc94b3436..dfb951df489 100644 --- a/clang-tools-extra/clangd/Function.h +++ b/clang-tools-extra/clangd/Function.h @@ -39,7 +39,11 @@ public: UniqueFunction(UniqueFunction &&) noexcept = default; UniqueFunction &operator=(UniqueFunction &&) noexcept = default; - template <class Callable> + 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>>( @@ -133,6 +137,40 @@ ForwardBinder<Func, Args...> BindWithForward(Func F, Args &&... As) { std::make_tuple(std::forward<Func>(F), std::forward<Args>(As)...)); } +namespace detail { +/// Runs provided callback in destructor. Use onScopeExit helper function to +/// create this object. +template <class Func> struct ScopeExitGuard { + static_assert(std::is_same<typename std::decay<Func>::type, Func>::value, + "Func must be decayed"); + + ScopeExitGuard(Func F) : F(std::move(F)) {} + ~ScopeExitGuard() { + if (!F) + return; + (*F)(); + } + + // Move-only. + ScopeExitGuard(const ScopeExitGuard &) = delete; + ScopeExitGuard &operator=(const ScopeExitGuard &) = delete; + + ScopeExitGuard(ScopeExitGuard &&Other) = default; + ScopeExitGuard &operator=(ScopeExitGuard &&Other) = default; + +private: + llvm::Optional<Func> F; +}; +} // namespace detail + +/// Creates a RAII object that will run \p F in its destructor. +template <class Func> +auto onScopeExit(Func &&F) + -> detail::ScopeExitGuard<typename std::decay<Func>::type> { + return detail::ScopeExitGuard<typename std::decay<Func>::type>( + std::forward<Func>(F)); +} + } // namespace clangd } // namespace clang |

