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) : FunctionId(obj,
typeid(Class)) {}
44 explicit operator
bool()
const;
46 bool operator==(
const FunctionId& other)
const;
49 std::size_t operator()(FunctionId id)
const noexcept;
53 FunctionId(
void* ptr,
const std::type_info& type);
56 const std::type_info* type_{
nullptr};
59enum class UnsubscribingKind { kManual, kAutomatic };
68class [[nodiscard]] AsyncEventSubscriberScope
final {
70 AsyncEventSubscriberScope() =
default;
72 template <
typename... Args>
73 AsyncEventSubscriberScope(AsyncEventSource<Args...>& channel, FunctionId id)
74 : AsyncEventSubscriberScope(
static_cast<impl::AsyncEventSourceBase&>(channel), id) {}
76 AsyncEventSubscriberScope(AsyncEventSubscriberScope&& scope)
noexcept;
78 AsyncEventSubscriberScope& operator=(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,
void (Class::*func)(Args...)) {
128 return AddListener(FunctionId(obj), name, [obj, func](Args... args) { (obj->*func)(args...); });
136 AsyncEventSubscriberScope
AddListener(FunctionId id, std::string_view name, Function&& func) {
137 return DoAddListener(id, name, std::move(func));
144 virtual AsyncEventSubscriberScope DoAddListener(FunctionId id, std::string_view name, Function&& func) = 0;