PdCom  5.3
Process data communication client
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 <chrono>
30 #include <functional>
31 #include <pdcom5/Exception.h>
32 #include <pdcom5_export.h>
33 
34 namespace PdCom {
35 
36 namespace impl {
37 class Subscription;
38 }
39 
40 class Subscription;
41 
43 constexpr struct event_mode_tag
44 {
45 } event_mode;
46 
48 constexpr struct poll_mode_tag
49 {
50 } poll_mode;
51 
59 class PDCOM5_PUBLIC Transmission
60 {
61  double interval_;
62 
63  static constexpr double checkInterval(double d)
64  {
65  return d <= 0 ? throw PdCom::InvalidArgument(
66  "period must be greater than zero")
67  : d;
68  }
69 
70  public:
71  constexpr double getInterval() const noexcept { return interval_; }
72  template <typename T, typename R>
73  constexpr Transmission(std::chrono::duration<T, R> d) :
74  interval_(checkInterval(
75  std::chrono::duration_cast<std::chrono::duration<double>>(d)
76  .count()))
77  {}
78  constexpr Transmission(event_mode_tag) noexcept : interval_(0) {}
79  constexpr Transmission(poll_mode_tag) noexcept : interval_(-1) {}
80  bool operator==(const Transmission &o) const noexcept
81  {
82  return o.interval_ == interval_;
83  }
84 
85  static constexpr Transmission fromDouble(double d)
86  {
87  return d == 0
88  ? Transmission(event_mode)
89  : (d == -1 ? Transmission(poll_mode)
90  : Transmission(std::chrono::duration<double>(d)));
91  }
92 };
93 
106 class PDCOM5_PUBLIC Subscriber
107 {
108  Transmission td_;
109  friend class impl::Subscription;
110 
111  public:
112  explicit Subscriber(const Transmission &td) noexcept : td_(td) {}
113  Subscriber(Subscriber const &) = delete;
114  Subscriber(Subscriber &&) = delete;
115  Subscriber &operator=(Subscriber const &) = delete;
116  Subscriber &operator=(Subscriber &&) = delete;
117 
118  const Transmission &getTransmission() const noexcept { return td_; }
119 
120  private:
124  virtual void stateChanged(PdCom::Subscription const &subscription) = 0;
125 
135  virtual void newValues(std::chrono::nanoseconds time_ns) = 0;
136 
137  protected:
138  ~Subscriber() = default;
139 };
140 
141 } // namespace PdCom
142 
143 namespace std {
144 
149 template <>
150 struct hash<PdCom::Transmission>
151 {
152  size_t operator()(PdCom::Transmission t) const
153  noexcept(__cplusplus >= 201703L)
154  {
155  return std::hash<double>()(t.getInterval());
156  }
157 };
158 
159 } // namespace std
160 
161 #endif // PDCOM5_SUBSCRIBER_H
Definition: Future.h:128
Tag for event-based subscription.
Definition: Subscriber.h:43
PdCom Subscription interface.
Definition: Subscription.h:64
Definition: Subscriber.h:106
Definition: ClientStatistics.h:31
Transmission mode for subscriptions.
Definition: Subscriber.h:59
Tag for poll-based subscription.
Definition: Subscriber.h:48
Definition: Exception.h:46