3#include <llvm/ADT/FunctionExtras.h>
4#include <llvm/Support/Error.h>
14using Callback = llvm::unique_function<void(llvm::Expected<T>)>;
17template <
typename T>
class Event {
20 using Listener = std::function<void(
const T &)>;
29 : Parent(Parent), ListenerID(ListenerID) {}
35 *
this = std::move(Other);
40 std::lock_guard<std::recursive_mutex> Lock(Parent->ListenersMu);
41 llvm::erase_if(Parent->Listeners,
42 [&](
const std::pair<Listener, unsigned> &P) {
43 return P.second == ListenerID;
47 std::tie(Parent, ListenerID) = std::tie(Other.Parent, Other.ListenerID);
48 Other.Parent =
nullptr;
62 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
63 Listeners.push_back({std::move(L), ++ListenerCount});
71 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
72 for (
const auto &L : Listeners)
77 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
78 assert(Listeners.empty());
82 static_assert(std::is_same<std::decay_t<T>, T>::value,
83 "use a plain type: event values are always passed by const&");
85 std::recursive_mutex ListenersMu;
86 bool IsBroadcasting =
false;
87 std::vector<std::pair<Listener, unsigned>> Listeners;
88 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)
std::function< void(const T &)> Listener
Subscription observe(Listener L)
Whether current platform treats paths case insensitively.
llvm::unique_function< void(llvm::Expected< T >)> Callback