EduArt Sensor Ring Library 4.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 sensorring {
20
21namespace subscription {
22
33public:
37 Subscription() noexcept = default;
38
44 Subscription(SubscriberToken token, std::function<void()> unsubscribe)
45 : _token(token)
46 , _unsubscribe(std::move(unsubscribe)) {}
47
49 Subscription(Subscription&& other) noexcept : _token(other._token), _unsubscribe(std::move(other._unsubscribe)) {
50 other._token = SubscriberToken{};
51 other._unsubscribe = nullptr;
52 }
53
55 Subscription& operator=(Subscription&& other) noexcept {
56 if (this != &other) {
57 cancel();
58 _token = other._token;
59 _unsubscribe = std::move(other._unsubscribe);
60 other._token = SubscriberToken{};
61 other._unsubscribe = nullptr;
62 }
63 return *this;
64 }
65
66 Subscription(const Subscription&) = delete;
67 Subscription& operator=(const Subscription&) = delete;
68
73
77 void cancel() noexcept {
78 if (_unsubscribe) {
79 _unsubscribe();
80 _unsubscribe = nullptr;
81 }
82 _token = SubscriberToken{};
83 }
84
89 bool isActive() const noexcept { return _token.isValid(); }
90
95 SubscriberToken token() const noexcept { return _token; }
96
97private:
98 SubscriberToken _token;
99 std::function<void()> _unsubscribe;
100};
101
102} // namespace subscription
103
104} // namespace sensorring
105
106} // 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:32
Subscription(Subscription &&other) noexcept
Move constructor.
Definition Subscription.hpp:49
~Subscription()
Destructor — automatically cancels an active subscription.
Definition Subscription.hpp:72
bool isActive() const noexcept
Check whether this subscription is still active.
Definition Subscription.hpp:89
Subscription & operator=(Subscription &&other) noexcept
Move assignment operator.
Definition Subscription.hpp:55
SubscriberToken token() const noexcept
Access the underlying token (e.g. for logging or debugging).
Definition Subscription.hpp:95
Subscription() noexcept=default
Default-construct an inactive Subscription.
void cancel() noexcept
Cancel the subscription. Safe to call multiple times (idempotent).
Definition Subscription.hpp:77
Opaque token identifying a subscription (state or device group).
Definition SubscriberToken.hpp:25