Directory: | ./ |
---|---|
File: | qtpdcom/QtPdCom1/Transmission.h |
Date: | 2025-06-08 04:08:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 3 | 7 | 42.9% |
Branches: | 4 | 12 | 33.3% |
Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
36 | /// Tag for event-based subscription. | ||
37 | constexpr struct event_mode_tag | ||
38 | { | ||
39 | } event_mode; | ||
40 | |||
41 | /// Tag for poll-based subscription with timer. | ||
42 | constexpr struct poll_mode_tag | ||
43 | { | ||
44 | } poll_mode; | ||
45 | |||
46 | /// Tag for poll-based subscription without timer. | ||
47 | constexpr struct manual_poll_mode_tag | ||
48 | { | ||
49 | } manual_poll_mode; | ||
50 | |||
51 | class QTPDCOM_PUBLIC Poll | ||
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>>( | ||
58 | d))) | ||
59 | {} | ||
60 | |||
61 | constexpr std::chrono::duration<double> getInterval() const | ||
62 | { | ||
63 | return interval_; | ||
64 | } | ||
65 | |||
66 | private: | ||
67 | std::chrono::duration<double> interval_; | ||
68 | |||
69 | static constexpr std::chrono::duration<double> | ||
70 | checkInterval(std::chrono::duration<double> d) | ||
71 | { | ||
72 | return d.count() <= 0 | ||
73 | ? throw std::invalid_argument( | ||
74 | "Poll period must be greater than zero") | ||
75 | : d; | ||
76 | } | ||
77 | }; | ||
78 | |||
79 | /** Transmission mode for subscriptions. | ||
80 | * | ||
81 | * This class specifies whether a subscription should be updated periodically, | ||
82 | * event-based or by polling only. | ||
83 | * | ||
84 | */ | ||
85 | class QTPDCOM_PUBLIC Transmission | ||
86 | { | ||
87 | public: | ||
88 | Transmission() = delete; | ||
89 | 1 | constexpr double getInterval() const noexcept { return interval_; } | |
90 | |||
91 | template <typename T, typename R> | ||
92 | constexpr Transmission(std::chrono::duration<T, R> d): | ||
93 | mode_(Continuous), | ||
94 | interval_(checkInterval( | ||
95 | std::chrono::duration_cast<std::chrono::duration<double>>( | ||
96 | d) | ||
97 | .count())) | ||
98 | {} | ||
99 | |||
100 | ✗ | constexpr Transmission(event_mode_tag) noexcept: | |
101 | mode_(Event), | ||
102 | ✗ | interval_(0.0) | |
103 | {} | ||
104 | |||
105 | constexpr Transmission(manual_poll_mode_tag) noexcept: | ||
106 | mode_(ManualPoll), | ||
107 | interval_(0.0) | ||
108 | {} | ||
109 | |||
110 | constexpr Transmission(poll_mode_tag, double interval): | ||
111 | mode_(Poll), | ||
112 | interval_(checkInterval(interval)) | ||
113 | {} | ||
114 | |||
115 | constexpr Transmission(const Poll &poll): | ||
116 | mode_(Poll), | ||
117 | interval_(poll.getInterval().count()) | ||
118 | {} | ||
119 | |||
120 | bool operator==(const Transmission &o) const noexcept | ||
121 | { | ||
122 | return o.interval_ == interval_ and o.mode_ == mode_; | ||
123 | } | ||
124 | |||
125 | ✗ | constexpr bool isContinuous() const | |
126 | { | ||
127 | ✗ | return mode_ == Continuous and interval_ > 0.0; | |
128 | } | ||
129 | |||
130 | 2 | constexpr bool isPoll() const | |
131 | { | ||
132 |
4/6✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
2 | return mode_ == Poll and interval_ > 0.0; |
133 | } | ||
134 | |||
135 | PdCom::Transmission toPdCom() const; | ||
136 | QString toString() const; | ||
137 | |||
138 | private: | ||
139 | enum { Poll = -1, ManualPoll, Event, Continuous } mode_; | ||
140 | |||
141 | double interval_; | ||
142 | |||
143 | static constexpr double checkInterval(double d) | ||
144 | { | ||
145 | return d <= 0 ? throw std::invalid_argument( | ||
146 | "Interval must be greater than zero") | ||
147 | : d; | ||
148 | } | ||
149 | }; | ||
150 | |||
151 | /****************************************************************************/ | ||
152 | |||
153 | } // namespace QtPdCom | ||
154 | |||
155 | #endif | ||
156 |