13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FUNCTION_H 14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_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 &)>;
43 :
Parent(Parent), ListenerID(ListenerID) {}
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;
std::function< void(const std::vector< std::string > &)> Listener
Subscription(Subscription &&Other)
An Event<T> allows events of type T to be broadcast to listeners.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Subscription & operator=(Subscription &&Other)
void broadcast(const T &V)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Subscription observe(Listener L)