Molybden API
Loading...
Searching...
No Matches
subscription_impl.hpp
1// Copyright (c) 2000-2023 TeamDev Ltd. All rights reserved.
2// TeamDev PROPRIETARY and CONFIDENTIAL.
3// Use is subject to license terms.
4
5#ifndef MOLYBDEN_SUBSCRIPTION_IMPL_HPP
6#define MOLYBDEN_SUBSCRIPTION_IMPL_HPP
7
8#include <algorithm>
9#include <functional>
10#include <utility>
11#include <vector>
12
13#include "molybden/base/subscription.hpp"
14
15namespace molybden {
16namespace internal {
17
18class SubscriptionObserver {
19 public:
20 virtual ~SubscriptionObserver() = default;
21
22 virtual void onCanceled(int32_t id){};
23};
24
25template <typename T>
26class SubscriptionImpl : public Subscription {
27 public:
28 SubscriptionImpl(int32_t id, std::function<void(const T& e)> observer);
29
30 void notify(const T& event);
31
32 void cancel() override;
33
34 void setObserver(SubscriptionObserver* observer);
35
36 private:
37 int32_t id_;
38 std::function<void(const T& e)> event_observer_;
39 SubscriptionObserver* subscription_observer_;
40};
41
42template <typename T>
43SubscriptionImpl<T>::SubscriptionImpl(int32_t id,
44 std::function<void(const T&)> observer)
45 : id_(id), event_observer_(std::move(observer)) {}
46
47template <typename T>
48void SubscriptionImpl<T>::notify(const T& event) {
49 if (event_observer_) {
50 event_observer_(event);
51 }
52}
53
54template <typename T>
55void SubscriptionImpl<T>::cancel() {
56 event_observer_ = {};
57 if (subscription_observer_) {
58 subscription_observer_->onCanceled(id_);
59 }
60}
61
62template <typename T>
63void SubscriptionImpl<T>::setObserver(SubscriptionObserver* observer) {
64 subscription_observer_ = observer;
65}
66
67} // namespace internal
68} // namespace molybden
69
70#endif // MOLYBDEN_SUBSCRIPTION_IMPL_HPP
virtual void cancel()=0
Cancels this subscription.