GCC Code Coverage Report


Directory: ./
File: pdcom5/src/Subscription.h
Date: 2023-11-12 04:06:57
Exec Total Coverage
Lines: 36 36 100.0%
Branches: 8 14 57.1%

Line Branch Exec Source
1 /*****************************************************************************
2 * vim:tw=78
3 *
4 * Copyright (C) 2021 Richard Hacker (lerichi at gmx dot net),
5 * Florian Pose (fp at igh dot de),
6 * Bjarne von Horn (vh at igh dot de).
7 *
8 * This file is part of the PdCom library.
9 *
10 * The PdCom library is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The PdCom library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
22 *
23 *****************************************************************************/
24
25 #ifndef PDCOM5_SUBSCRIPTION_IMPL_H
26 #define PDCOM5_SUBSCRIPTION_IMPL_H
27
28 #include <memory>
29 #include <pdcom5/Subscriber.h>
30 #include <pdcom5/Subscription.h>
31 #include <unordered_map>
32
33 namespace PdCom { namespace impl {
34 class Process;
35 class Variable;
36
37 class Subscription
38 {
39 public:
40 63 Subscription(
41 std::shared_ptr<const Variable> variable,
42 PdCom::Subscription *This,
43 PdCom::Subscriber &subscriber,
44 63 std::weak_ptr<Process> process) :
45 63 variable_(std::move(variable)),
46 This_(This),
47 subscriber_(subscriber),
48 126 process_(process)
49 63 {}
50
51
52 virtual void poll() = 0;
53 virtual const void *getData() const = 0;
54
55 std::weak_ptr<const Variable> const variable_;
56 PdCom::Subscription *This_ = nullptr;
57 PdCom::Subscriber &subscriber_;
58 std::weak_ptr<Process> process_;
59 std::string path_;
60
61
62 63 virtual ~Subscription() = default;
63
64 // this member method commits suicide!
65 10 void replaceImpl(std::shared_ptr<Subscription> impl)
66 {
67 // assume that we don't need to unregister ourselves
68 10 process_.reset();
69
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if (impl)
70 10 impl->path_ = path_;
71 10 std::swap(impl, This_->pimpl);
72 10 }
73
74 75 void newValues(std::chrono::nanoseconds time_ns)
75 {
76 75 subscriber_.newValues(time_ns);
77 75 }
78
79 548 class SubscriberNotifier
80 {
81 std::unordered_map<Subscriber *, std::chrono::nanoseconds> subscribers_;
82
83 public:
84 94 void clear() { subscribers_.clear(); }
85 98 void insert(Subscriber &s, std::chrono::nanoseconds ts)
86 {
87
1/2
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
98 subscribers_[&s] = ts;
88 98 }
89
90 94 void notify()
91 {
92
2/2
✓ Branch 6 taken 98 times.
✓ Branch 7 taken 94 times.
192 for (const auto s : subscribers_)
93
1/2
✓ Branch 6 taken 98 times.
✗ Branch 7 not taken.
98 s.first->newValues(s.second);
94 94 }
95 };
96
97 380 struct PendingStateChange
98 {
99 76 PendingStateChange(
100 const std::shared_ptr<Subscription> &sub,
101 76 PdCom::Subscription::State st) :
102 76 subscription_(sub), state_(st)
103 {
104 76 sub->This_->state_ = st;
105 76 }
106 std::weak_ptr<Subscription> subscription_;
107 PdCom::Subscription::State state_;
108 76 void execute() const
109 {
110
1/2
✓ Branch 3 taken 76 times.
✗ Branch 4 not taken.
152 if (const auto sub = subscription_.lock())
111
1/2
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
76 if (sub->This_) {
112 76 sub->This_->state_ = state_;
113
1/2
✓ Branch 15 taken 76 times.
✗ Branch 16 not taken.
76 sub->subscriber_.stateChanged(*sub->This_);
114 }
115 76 }
116 };
117 };
118
119
120 }} // namespace PdCom::impl
121
122
123 #endif // PDCOM5_SUBSCRIPTION_IMPL_H
124