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>)>;
36 using Tuple = std::tuple<typename std::decay<Func>::type,
37 typename std::decay<Args>::type...>;
45 : FuncWithArguments(std::move(FuncWithArguments)) {}
48 template <std::size_t... Indexes,
class... RestArgs>
49 auto CallImpl(llvm::integer_sequence<std::size_t, Indexes...> Seq,
51 -> decltype(std::get<0>(this->FuncWithArguments)(
52 std::forward<Args>(std::get<Indexes + 1>(this->FuncWithArguments))...,
53 std::forward<RestArgs>(Rest)...)) {
55 std::forward<Args>(std::get<Indexes + 1>(this->FuncWithArguments))...,
56 std::forward<RestArgs>(Rest)...);
60 template <
class... RestArgs>
62 -> decltype(this->CallImpl(llvm::index_sequence_for<Args...>(),
63 std::forward<RestArgs>(Rest)...)) {
66 assert(!WasCalled &&
"Can only call result of Bind once.");
69 return CallImpl(llvm::index_sequence_for<Args...>(),
70 std::forward<RestArgs>(Rest)...);
80 template <
class Func,
class... Args>
83 std::make_tuple(std::forward<Func>(F), std::forward<Args>(As)...));
87 template <
typename T>
class Event {
90 using Listener = std::function<void(const T &)>;
99 : Parent(Parent), ListenerID(ListenerID) {}
105 *
this = std::move(Other);
110 std::lock_guard<std::recursive_mutex>(Parent->ListenersMu);
111 llvm::erase_if(Parent->Listeners,
112 [&](
const std::pair<Listener, unsigned> &P) {
113 return P.second == ListenerID;
117 std::tie(Parent, ListenerID) = std::tie(Other.Parent, Other.ListenerID);
118 Other.Parent =
nullptr;
132 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
133 Listeners.push_back({std::move(L), ++ListenerCount});
134 return Subscription(
this, ListenerCount);
141 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
142 for (
const auto &L : Listeners)
147 std::lock_guard<std::recursive_mutex> Lock(ListenersMu);
148 assert(Listeners.empty());
152 static_assert(std::is_same<
typename std::decay<T>::type, T>::value,
153 "use a plain type: event values are always passed by const&");
155 std::recursive_mutex ListenersMu;
156 bool IsBroadcasting =
false;
157 std::vector<std::pair<Listener, unsigned>> Listeners;
158 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)
ForwardBinder< Func, Args... > Bind(Func F, Args &&... As)
Creates an object that stores a callable (F) and first arguments to the callable (As) and allows to c...
ForwardBinder(Tuple FuncWithArguments)
std::tuple< typename std::decay< Func >::type, typename std::decay< Args >::type... > Tuple
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Stores a callable object (Func) and arguments (Args) and allows to call the callable with provided ar...
auto operator()(RestArgs &&... Rest) -> decltype(this->CallImpl(llvm::index_sequence_for< Args... >(), std::forward< RestArgs >(Rest)...))
Subscription observe(Listener L)