PdCom  5.0
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 "visibility.h"
30 
31 #include <chrono>
32 #include <stdexcept>
33 
34 namespace PdCom {
35 namespace impl {
36 class Subscription;
37 }
38 
39 class Subscription;
40 
42 constexpr struct event_mode_tag
43 {
44 } event_mode;
46 constexpr struct poll_mode_tag
47 {
48 } poll_mode;
49 
56 class PDCOM5_PUBLIC Transmission
57 {
58  double interval_;
59 
60  static constexpr double checkInterval(double d)
61  {
62  return d <= 0 ? throw std::invalid_argument(
63  "period must be greater than zero")
64  : d;
65  }
66 
67  public:
68  constexpr double getInterval() const noexcept { return interval_; }
69  template <typename T, typename R>
70  constexpr Transmission(std::chrono::duration<T, R> d) :
71  interval_(checkInterval(
72  std::chrono::duration_cast<std::chrono::duration<double>>(d)
73  .count()))
74  {}
75  constexpr Transmission(event_mode_tag) noexcept : interval_(0) {}
76  constexpr Transmission(poll_mode_tag) noexcept : interval_(-1) {}
77  bool operator==(const Transmission &o) const noexcept
78  {
79  return o.interval_ == interval_;
80  }
81 };
82 
83 
91 class PDCOM5_PUBLIC Subscriber
92 {
93  Transmission td_;
94  friend class impl::Subscription;
95 
96  public:
97  explicit Subscriber(const Transmission &td) noexcept : td_(td) {}
98  Subscriber(Subscriber const &) = delete;
99  Subscriber(Subscriber &&) = delete;
100  Subscriber &operator=(Subscriber const &) = delete;
101  Subscriber &operator=(Subscriber &&) = delete;
102 
103  const Transmission &getTransmission() const noexcept { return td_; }
104 
105  private:
109  virtual void stateChanged(PdCom::Subscription const &subscription) = 0;
116  virtual void newValues(std::chrono::nanoseconds time_ns) = 0;
117 
118  protected:
119  ~Subscriber() = default;
120 };
121 
122 } // namespace PdCom
123 
124 #endif // PDCOM5_SUBSCRIBER_H
Tag for event-based subscription.
Definition: Subscriber.h:42
PdCom Subscription interface.
Definition: Subscription.h:93
Definition: Subscriber.h:91
library version string as "major.minor.patch"
Definition: details.h:37
Transmission mode for subscriptions.
Definition: Subscriber.h:56
Tag for poll-based subscription.
Definition: Subscriber.h:46