12#include <userver/utils/impl/internal_tag.hpp>
14USERVER_NAMESPACE_BEGIN
19enum class UnsubscribingKind;
21template <
typename... Args>
22class AsyncEventSource;
26class AsyncEventSourceBase {
28 virtual void RemoveListener(FunctionId, UnsubscribingKind)
noexcept = 0;
32 ~AsyncEventSourceBase();
39class FunctionId
final {
41 constexpr FunctionId() =
default;
43 template <
typename Class>
44 explicit FunctionId(Class* obj) : FunctionId(obj,
typeid(Class)) {}
46 explicit operator
bool()
const;
48 bool operator==(
const FunctionId& other)
const;
51 std::size_t operator()(FunctionId id)
const noexcept;
55 FunctionId(
void* ptr,
const std::type_info& type);
58 const std::type_info* type_{
nullptr};
61enum class UnsubscribingKind { kManual, kAutomatic };
70class [[nodiscard]] AsyncEventSubscriberScope
final {
72 AsyncEventSubscriberScope() =
default;
74 AsyncEventSubscriberScope(AsyncEventSubscriberScope&& scope)
noexcept;
75 AsyncEventSubscriberScope& operator=(AsyncEventSubscriberScope&& other)
noexcept;
76 ~AsyncEventSubscriberScope();
84 template <
typename... Args>
85 AsyncEventSubscriberScope(
utils::impl::InternalTag, AsyncEventSource<Args...>& channel, FunctionId id)
86 : AsyncEventSubscriberScope(
static_cast<impl::AsyncEventSourceBase&>(channel), id) {}
90 AsyncEventSubscriberScope(impl::AsyncEventSourceBase& channel, FunctionId id);
92 impl::AsyncEventSourceBase* channel_{
nullptr};
102template <
typename... Args>
103class AsyncEventSource :
public impl::AsyncEventSourceBase {
105 using Function = std::function<
void(Args... args)>;
107 virtual ~AsyncEventSource() =
default;
129 template <
class Class>
130 AsyncEventSubscriberScope
AddListener(Class* obj, std::string_view name,
void (Class::*func)(Args...)) {
131 return AddListener(FunctionId(obj), name, [obj, func](Args... args) { (obj->*func)(args...); });
139 AsyncEventSubscriberScope
AddListener(FunctionId id, std::string_view name, Function&& func) {
140 return DoAddListener(id, name, std::move(func));
147 virtual AsyncEventSubscriberScope DoAddListener(FunctionId id, std::string_view name, Function&& func) = 0;