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"
28using Callback = llvm::unique_function<void(llvm::Expected<T>)>;
31template <
typename T>
class Event {
34 using Listener = std::function<void(
const T &)>;
49 *
this = std::move(Other);
54 std::lock_guard<std::recursive_mutex> Lock(
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});
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<std::decay_t<T>, T>::value,
97 "use a plain type: event values are always passed by const&");
99 std::recursive_mutex ListenersMu;
100 std::vector<std::pair<Listener, unsigned>> Listeners;
101 unsigned ListenerCount = 0;
Subscription(Subscription &&Other)
Subscription & operator=(Subscription &&Other)
An Event<T> allows events of type T to be broadcast to listeners.
void broadcast(const T &V)
Subscription observe(Listener L)
std::function< void(const T &)> Listener
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//