QtPdCom  1.4.1
Transmission.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2022 Florian Pose <fp@igh.de>
4  *
5  * This file is part of the QtPdCom library.
6  *
7  * The QtPdCom library is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * The QtPdCom library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  * License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19  *
20  ****************************************************************************/
21 
22 #ifndef QTPDCOM_TRANSMISSION_H
23 #define QTPDCOM_TRANSMISSION_H
24 
25 #include <pdcom5/Subscriber.h> // PdCom::Transmission
26 
27 #include "Export.h"
28 
29 #include <chrono>
30 #include <stdexcept>
31 
32 namespace QtPdCom {
33 
34 /****************************************************************************/
35 
37 constexpr struct event_mode_tag
38 {
39 } event_mode;
40 
42 constexpr struct poll_mode_tag
43 {
44 } poll_mode;
45 
47 constexpr struct manual_poll_mode_tag
48 {
50 
52 {
53  public:
54  template <typename T, typename R>
55  constexpr Poll(std::chrono::duration<T, R> d):
56  interval_(checkInterval(
57  std::chrono::duration_cast<std::chrono::duration<double>>(d)))
58  {}
59 
60  constexpr std::chrono::duration<double> getInterval() const {
61  return interval_;
62  }
63 
64  private:
65  std::chrono::duration<double> interval_;
66 
67  static constexpr std::chrono::duration<double> checkInterval(
68  std::chrono::duration<double> d)
69  {
70  return d.count() <= 0 ? throw std::invalid_argument(
71  "Poll period must be greater than zero")
72  : d;
73  }
74 };
75 
83 {
84 
85  public:
86  constexpr double getInterval() const noexcept { return interval_; }
87 
88  template <typename T, typename R>
89  constexpr Transmission(std::chrono::duration<T, R> d) :
90  mode_(Continuous),
91  interval_(checkInterval(
92  std::chrono::duration_cast<std::chrono::duration<double>>(d)
93  .count()))
94  {}
95 
96  constexpr Transmission(event_mode_tag) noexcept :
97  mode_(Event), interval_(0.0) {}
98 
99  constexpr Transmission(manual_poll_mode_tag) noexcept :
100  mode_(ManualPoll), interval_(0.0) {}
101 
102  constexpr Transmission(poll_mode_tag, double interval) :
103  mode_(Poll), interval_(checkInterval(interval)) {}
104 
105  constexpr Transmission(const Poll &poll):
106  mode_(Poll), interval_(poll.getInterval().count()) {}
107 
108  bool operator==(const Transmission &o) const noexcept
109  {
110  return o.interval_ == interval_ and o.mode_ == mode_;
111  }
112 
113  constexpr bool isContinuous() const
114  {
115  return mode_ == Continuous and interval_ > 0.0;
116  }
117 
118  constexpr bool isPoll() const
119  {
120  return mode_ == Poll and interval_ > 0.0;
121  }
122 
123  PdCom::Transmission toPdCom() const;
124  QString toString() const;
125 
126  private:
127  enum {
128  Poll = -1,
131  Continuous
132  } mode_;
133 
134  double interval_;
135 
136  static constexpr double checkInterval(double d)
137  {
138  return d <= 0 ? throw std::invalid_argument(
139  "Interval must be greater than zero")
140  : d;
141  }
142 
143 };
144 
145 /****************************************************************************/
146 
147 } // namespace
148 
149 #endif
static constexpr double checkInterval(double d)
Definition: Transmission.h:136
constexpr double getInterval() const noexcept
Definition: Transmission.h:86
Tag for event-based subscription.
Definition: Transmission.h:37
Transmission mode for subscriptions.
Definition: Transmission.h:82
constexpr bool isContinuous() const
Definition: Transmission.h:113
Definition: BroadcastModel.h:32
constexpr Transmission(manual_poll_mode_tag) noexcept
Definition: Transmission.h:99
constexpr Poll(std::chrono::duration< T, R > d)
Definition: Transmission.h:55
Definition: Transmission.h:129
constexpr Transmission(const Poll &poll)
Definition: Transmission.h:105
#define QTPDCOM_PUBLIC
Definition: Export.h:30
std::chrono::duration< double > interval_
Definition: Transmission.h:65
static constexpr std::chrono::duration< double > checkInterval(std::chrono::duration< double > d)
Definition: Transmission.h:67
Tag for poll-based subscription without timer.
Definition: Transmission.h:47
Definition: Transmission.h:130
constexpr bool isPoll() const
Definition: Transmission.h:118
double interval_
Definition: Transmission.h:134
Tag for poll-based subscription with timer.
Definition: Transmission.h:42
constexpr std::chrono::duration< double > getInterval() const
Definition: Transmission.h:60
constexpr Transmission(poll_mode_tag, double interval)
Definition: Transmission.h:102
constexpr Transmission(event_mode_tag) noexcept
Definition: Transmission.h:96
Definition: Transmission.h:51
bool operator==(const Transmission &o) const noexcept
Definition: Transmission.h:108
constexpr struct QtPdCom::poll_mode_tag poll_mode
constexpr struct QtPdCom::manual_poll_mode_tag manual_poll_mode
constexpr Transmission(std::chrono::duration< T, R > d)
Definition: Transmission.h:89
constexpr struct QtPdCom::event_mode_tag event_mode