EduArt Sensor Ring Library 3.0.0
Loading...
Searching...
No Matches
Subscription.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 EduArt Robotik GmbH
2
9
10#pragma once
11
12#include <functional>
13#include <vector>
14
16
17namespace eduart {
18
19namespace subscription {
20
31public:
35 Subscription() noexcept = default;
36
42 Subscription(SubscriberToken token, std::function<void()> unsubscribe)
43 : _token(token)
44 , _unsubscribe(std::move(unsubscribe)) {}
45
47 Subscription(Subscription&& other) noexcept : _token(other._token), _unsubscribe(std::move(other._unsubscribe)) {
48 other._token = SubscriberToken{};
49 other._unsubscribe = nullptr;
50 }
51
53 Subscription& operator=(Subscription&& other) noexcept {
54 if (this != &other) {
55 cancel();
56 _token = other._token;
57 _unsubscribe = std::move(other._unsubscribe);
58 other._token = SubscriberToken{};
59 other._unsubscribe = nullptr;
60 }
61 return *this;
62 }
63
64 Subscription(const Subscription&) = delete;
65 Subscription& operator=(const Subscription&) = delete;
66
71
75 void cancel() noexcept {
76 if (_unsubscribe) {
77 _unsubscribe();
78 _unsubscribe = nullptr;
79 }
80 _token = SubscriberToken{};
81 }
82
87 bool isActive() const noexcept { return _token.isValid(); }
88
93 SubscriberToken token() const noexcept { return _token; }
94
95private:
96 SubscriberToken _token;
97 std::function<void()> _unsubscribe;
98};
99
100} // namespace subscription
101
102} // namespace eduart
Opaque token identifying a subscription.
Move-only RAII wrapper that pairs a SubscriberToken with an unsubscribe callable. Destroying a Subscr...
Definition Subscription.hpp:30
void cancel() noexcept
Cancel the subscription. Safe to call multiple times (idempotent).
Definition Subscription.hpp:75
Subscription & operator=(Subscription &&other) noexcept
Move assignment operator.
Definition Subscription.hpp:53
Subscription(Subscription &&other) noexcept
Move constructor.
Definition Subscription.hpp:47
bool isActive() const noexcept
Check whether this subscription is still active.
Definition Subscription.hpp:87
~Subscription()
Destructor — automatically cancels an active subscription.
Definition Subscription.hpp:70
SubscriberToken token() const noexcept
Access the underlying token (e.g. for logging or debugging).
Definition Subscription.hpp:93
Subscription() noexcept=default
Default-construct an inactive Subscription.
Opaque token identifying a subscription (state or device group).
Definition SubscriberToken.hpp:22