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 {
36namespace impl {
37class Subscription;
38}
39
40class Subscription;
41
43constexpr struct event_mode_tag
44{
45} event_mode;
47constexpr struct poll_mode_tag
48{
49} poll_mode;
50
57class PDCOM5_PUBLIC Transmission
58{
59 double interval_;
60
61 static constexpr double checkInterval(double d)
62 {
63 return d <= 0 ? throw PdCom::InvalidArgument(
64 "period must be greater than zero")
65 : d;
66 }
67
68 public:
69 constexpr double getInterval() const noexcept { return interval_; }
70 template <typename T, typename R>
71 constexpr Transmission(std::chrono::duration<T, R> d) :
72 interval_(checkInterval(
73 std::chrono::duration_cast<std::chrono::duration<double>>(d)
74 .count()))
75 {}
76 constexpr Transmission(event_mode_tag) noexcept : interval_(0) {}
77 constexpr Transmission(poll_mode_tag) noexcept : interval_(-1) {}
78 bool operator==(const Transmission &o) const noexcept
79 {
80 return o.interval_ == interval_;
81 }
82
83 static constexpr Transmission fromDouble(double d)
84 {
85 return d == 0
86 ? Transmission(event_mode)
87 : (d == -1 ? Transmission(poll_mode)
88 : Transmission(std::chrono::duration<double>(d)));
89 }
90};
91
92
101class PDCOM5_PUBLIC Subscriber
102{
103 Transmission td_;
104 friend class impl::Subscription;
105
106 public:
107 explicit Subscriber(const Transmission &td) noexcept : td_(td) {}
108 Subscriber(Subscriber const &) = delete;
109 Subscriber(Subscriber &&) = delete;
110 Subscriber &operator=(Subscriber const &) = delete;
111 Subscriber &operator=(Subscriber &&) = delete;
112
113 const Transmission &getTransmission() const noexcept { return td_; }
114
115 private:
119 virtual void stateChanged(PdCom::Subscription const &subscription) = 0;
126 virtual void newValues(std::chrono::nanoseconds time_ns) = 0;
127
128 protected:
129 ~Subscriber() = default;
130};
131
132} // namespace PdCom
133
134namespace std {
135
140template <>
141struct hash<PdCom::Transmission>
142{
143 size_t operator()(PdCom::Transmission t) const
144 noexcept(__cplusplus >= 201703L)
145 {
146 return std::hash<double>()(t.getInterval());
147 }
148};
149} // namespace std
150
151#endif // PDCOM5_SUBSCRIBER_H
Definition: Subscriber.h:102
PdCom Subscription interface.
Definition: Subscription.h:66
Transmission mode for subscriptions.
Definition: Subscriber.h:58
library version string as "major.minor.patch"
Definition: ClientStatistics.h:31
Definition: Exception.h:48
Tag for event-based subscription.
Definition: Subscriber.h:44
Tag for poll-based subscription.
Definition: Subscriber.h:48