13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_FUNCTION_H
14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_FUNCTION_H
16 #include "llvm/ADT/FunctionExtras.h"
17 #include "llvm/Support/Error.h"
28 using Callback = llvm::unique_function<void(llvm::Expected<T>)>;
31 template <
typename T>
class Event {
34 using Listener = std::function<void(
const T &)>;
49 *
this = std::move(Other);
54 std::lock_guard<std::recursive_mutex>(
Parent->ListenersMu);
55 llvm::erase_if(
Parent->Listeners,
56 [&](
const std::pair<Listener, unsigned> &P) {
57 return P.second == ListenerID;
61 std::tie(
Parent, ListenerID) = std::tie(Other.Parent, Other.ListenerID);
62 Other.Parent =
nullptr;
76 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
77 Listeners.push_back({std::move(L), ++ListenerCount});
78 return Subscription(
this, ListenerCount);
85 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
86 for (
const auto &L : Listeners)
91 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
92 assert(Listeners.empty());
96 static_assert(std::is_same<
typename std::decay<T>::type, T>::value,
97 "use a plain type: event values are always passed by const&");
99 std::recursive_mutex ListenersMu;
100 bool IsBroadcasting =
false;
101 std::vector<std::pair<Listener, unsigned>> Listeners;
102 unsigned ListenerCount = 0;