5#ifndef MOLYBDEN_SUBSCRIPTION_IMPL_HPP
6#define MOLYBDEN_SUBSCRIPTION_IMPL_HPP
13#include "molybden/base/subscription.hpp"
18class SubscriptionObserver {
20 virtual ~SubscriptionObserver() =
default;
22 virtual void onCanceled(int32_t
id){};
26class SubscriptionImpl :
public Subscription {
28 SubscriptionImpl(int32_t
id, std::function<
void(
const T& e)> observer);
30 void notify(
const T& event);
34 void addObserver(SubscriptionObserver* observer);
36 void removeObserver(SubscriptionObserver* observer);
40 std::function<void(
const T& e)> observer_;
42 std::vector<SubscriptionObserver*> observers_;
46SubscriptionImpl<T>::SubscriptionImpl(int32_t
id,
47 std::function<
void(
const T&)> observer)
48 : id_(id), observer_(std::move(observer)) {}
51void SubscriptionImpl<T>::notify(
const T& event) {
58void SubscriptionImpl<T>::cancel() {
60 for (
auto* observer : observers_) {
61 observer->onCanceled(id_);
66void SubscriptionImpl<T>::addObserver(SubscriptionObserver* observer) {
67 observers_.push_back(observer);
71void SubscriptionImpl<T>::removeObserver(SubscriptionObserver* observer) {
72 auto it = std::find(observers_.begin(), observers_.end(), observer);
73 if (it != observers_.end()) {
virtual void cancel()=0
Cancels this subscription.