12USERVER_NAMESPACE_BEGIN
17enum class UnsubscribingKind;
19template <
typename... Args>
20class AsyncEventSource;
24class AsyncEventSourceBase {
26 virtual void RemoveListener(FunctionId, UnsubscribingKind)
noexcept = 0;
30 ~AsyncEventSourceBase();
37class FunctionId
final {
39 constexpr FunctionId() =
default;
41 template <
typename Class>
42 explicit FunctionId(Class* obj) : ptr_(obj), type_(&
typeid(Class)) {}
44 explicit operator
bool()
const;
46 bool operator==(
const FunctionId& other)
const;
49 std::size_t operator()(FunctionId id)
const noexcept;
54 const std::type_info* type_{
nullptr};
57enum class UnsubscribingKind { kManual, kAutomatic };
66class [[nodiscard]] AsyncEventSubscriberScope
final {
68 AsyncEventSubscriberScope() =
default;
70 template <
typename... Args>
71 AsyncEventSubscriberScope(AsyncEventSource<Args...>& channel, FunctionId id)
72 : AsyncEventSubscriberScope(
73 static_cast<impl::AsyncEventSourceBase&>(channel), id) {}
75 AsyncEventSubscriberScope(AsyncEventSubscriberScope&& scope)
noexcept;
77 AsyncEventSubscriberScope& operator=(
78 AsyncEventSubscriberScope&& other)
noexcept;
80 ~AsyncEventSubscriberScope();
87 AsyncEventSubscriberScope(impl::AsyncEventSourceBase& channel, FunctionId id);
89 impl::AsyncEventSourceBase* channel_{
nullptr};
99template <
typename... Args>
100class AsyncEventSource :
public impl::AsyncEventSourceBase {
102 using Function = std::function<
void(Args... args)>;
104 virtual ~AsyncEventSource() =
default;
126 template <
class Class>
127 AsyncEventSubscriberScope
AddListener(Class* obj, std::string_view name,
128 void (Class::*func)(Args...)) {
129 return AddListener(FunctionId(obj), name,
130 [obj, func](Args... args) { (obj->*func)(args...); });
138 AsyncEventSubscriberScope
AddListener(FunctionId id, std::string_view name,
140 return DoAddListener(id, name, std::move(func));
147 virtual AsyncEventSubscriberScope DoAddListener(FunctionId id,
148 std::string_view name,
149 Function&& func) = 0;