PdCom  5.0
Process data communication client
Loading...
Searching...
No Matches
Subscriber.h
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * Copyright (C) 2021 Richard Hacker (lerichi at gmx dot net),
4 * Florian Pose (fp at igh dot de),
5 * Bjarne von Horn (vh at igh dot de).
6 *
7 * This file is part of the PdCom library.
8 *
9 * The PdCom library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * The PdCom library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more .
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 ****************************************************************************/
23
26#ifndef PDCOM5_SUBSCRIBER_H
27#define PDCOM5_SUBSCRIBER_H
28
29#include "pdcom5_export.h"
30
31#include <chrono>
32#include <functional>
33#include <pdcom5/Exception.h>
34
35namespace PdCom {
36
37namespace impl {
38class Subscription;
39}
40
41class Subscription;
42
44constexpr struct event_mode_tag
45{
46} event_mode;
47
49constexpr struct poll_mode_tag
50{
51} poll_mode;
52
60class PDCOM5_PUBLIC Transmission
61{
62 double interval_;
63
64 static constexpr double checkInterval(double d)
65 {
66 return d <= 0 ? throw PdCom::InvalidArgument(
67 "period must be greater than zero")
68 : d;
69 }
70
71 public:
72 constexpr double getInterval() const noexcept { return interval_; }
73 template <typename T, typename R>
74 constexpr Transmission(std::chrono::duration<T, R> d) :
75 interval_(checkInterval(
76 std::chrono::duration_cast<std::chrono::duration<double>>(d)
77 .count()))
78 {}
79 constexpr Transmission(event_mode_tag) noexcept : interval_(0) {}
80 constexpr Transmission(poll_mode_tag) noexcept : interval_(-1) {}
81 bool operator==(const Transmission &o) const noexcept
82 {
83 return o.interval_ == interval_;
84 }
85
86 static constexpr Transmission fromDouble(double d)
87 {
88 return d == 0
89 ? Transmission(event_mode)
90 : (d == -1 ? Transmission(poll_mode)
91 : Transmission(std::chrono::duration<double>(d)));
92 }
93};
94
107class PDCOM5_PUBLIC Subscriber
108{
109 Transmission td_;
110 friend class impl::Subscription;
111
112 public:
113 explicit Subscriber(const Transmission &td) noexcept : td_(td) {}
114 Subscriber(Subscriber const &) = delete;
115 Subscriber(Subscriber &&) = delete;
116 Subscriber &operator=(Subscriber const &) = delete;
117 Subscriber &operator=(Subscriber &&) = delete;
118
119 const Transmission &getTransmission() const noexcept { return td_; }
120
121 private:
125 virtual void stateChanged(PdCom::Subscription const &subscription) = 0;
126
136 virtual void newValues(std::chrono::nanoseconds time_ns) = 0;
137
138 protected:
139 ~Subscriber() = default;
140};
141
142} // namespace PdCom
143
144namespace std {
145
150template <>
151struct hash<PdCom::Transmission>
152{
153 size_t operator()(PdCom::Transmission t) const
154 noexcept(__cplusplus >= 201703L)
155 {
156 return std::hash<double>()(t.getInterval());
157 }
158};
159
160} // namespace std
161
162#endif // PDCOM5_SUBSCRIBER_H
Definition: Subscriber.h:108
PdCom Subscription interface.
Definition: Subscription.h:65
Transmission mode for subscriptions.
Definition: Subscriber.h:61
library version string as "major.minor.patch"
Definition: ClientStatistics.h:31
Definition: Exception.h:48
Tag for event-based subscription.
Definition: Subscriber.h:45
Tag for poll-based subscription.
Definition: Subscriber.h:50