GCC Code Coverage Report


Directory: ./
File: QtPdCom1/Transmission.h
Date: 2024-03-07 15:13:58
Exec Total Coverage
Lines: 4 7 57.1%
Branches: 1 8 12.5%

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.
42 constexpr struct poll_mode_tag
43 {
44 } poll_mode;
45
46 class QTPDCOM_PUBLIC Poll
47 {
48 public:
49 template <typename T, typename R>
50 constexpr Poll(std::chrono::duration<T, R> d):
51 interval_(checkInterval(
52 std::chrono::duration_cast<std::chrono::duration<double>>(d)))
53 {}
54
55 constexpr std::chrono::duration<double> getInterval() const {
56 return interval_;
57 }
58
59 private:
60 std::chrono::duration<double> interval_;
61
62 static constexpr std::chrono::duration<double> checkInterval(
63 std::chrono::duration<double> d)
64 {
65 return d.count() <= 0 ? throw std::invalid_argument(
66 "Poll period must be greater than zero")
67 : d;
68 }
69 };
70
71 /** Transmission mode for subscriptions.
72 *
73 * This class specifies whether a subscription should be updated periodically,
74 * event-based or by polling only.
75 *
76 */
77 class QTPDCOM_PUBLIC Transmission
78 {
79
80 public:
81 constexpr double getInterval() const noexcept { return interval_; }
82
83 template <typename T, typename R>
84 constexpr Transmission(std::chrono::duration<T, R> d) :
85 mode_(Continuous),
86 interval_(checkInterval(
87 std::chrono::duration_cast<std::chrono::duration<double>>(d)
88 .count()))
89 {}
90
91 6 constexpr Transmission(event_mode_tag) noexcept :
92 6 mode_(Event), interval_(0.0) {}
93
94 constexpr Transmission(poll_mode_tag, double interval) :
95 mode_(Poll), interval_(checkInterval(interval)) {}
96
97 constexpr Transmission(const Poll &poll):
98 mode_(Poll), interval_(poll.getInterval().count()) {}
99
100 bool operator==(const Transmission &o) const noexcept
101 {
102 return o.interval_ == interval_ and o.mode_ == mode_;
103 }
104
105 constexpr bool isContinuous() const
106 {
107 return mode_ == Continuous and interval_ > 0.0;
108 }
109
110 6 constexpr bool isPoll() const
111 {
112
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 return mode_ == Poll and interval_ > 0.0;
113 }
114
115 PdCom::Transmission toPdCom() const;
116 QString toString() const;
117
118 private:
119 enum {
120 Poll = -1,
121 Event,
122 Continuous
123 } mode_;
124
125 double interval_;
126
127 static constexpr double checkInterval(double d)
128 {
129 return d <= 0 ? throw std::invalid_argument(
130 "Interval must be greater than zero")
131 : d;
132 }
133
134 };
135
136 /****************************************************************************/
137
138 } // namespace
139
140 #endif
141