GCC Code Coverage Report


Directory: ./
File: src/Subscription.h
Date: 2024-11-05 15:23:15
Exec Total Coverage
Lines: 36 36 100.0%
Branches: 9 14 64.3%

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 145 Subscription(
41 std::shared_ptr<const Variable> variable,
42 PdCom::Subscription *This,
43 PdCom::Subscriber &subscriber,
44 145 std::weak_ptr<Process> process) :
45 145 variable_(std::move(variable)),
46 This_(This),
47 subscriber_(subscriber),
48 290 process_(process)
49 145 {}
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 145 virtual ~Subscription() = default;
63
64 // this member method commits suicide!
65 42 void replaceImpl(std::shared_ptr<Subscription> impl)
66 {
67 // assume that we don't need to unregister ourselves
68 42 process_.reset();
69
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 if (impl)
70 42 impl->path_ = path_;
71 42 std::swap(impl, This_->pimpl);
72 42 }
73
74 32 void newValues(std::chrono::nanoseconds time_ns)
75 {
76 32 subscriber_.newValues(time_ns);
77 32 }
78
79 272 class SubscriberNotifier
80 {
81 std::unordered_map<Subscriber *, std::chrono::nanoseconds> subscribers_;
82
83 public:
84 62 void clear() { subscribers_.clear(); }
85 69 void insert(Subscriber &s, std::chrono::nanoseconds ts)
86 {
87
1/2
✓ Branch 4 taken 69 times.
✗ Branch 5 not taken.
69 subscribers_[&s] = ts;
88 69 }
89
90 62 void notify()
91 {
92
2/2
✓ Branch 6 taken 52 times.
✓ Branch 7 taken 62 times.
114 for (const auto s : subscribers_)
93
1/2
✓ Branch 6 taken 52 times.
✗ Branch 7 not taken.
52 s.first->newValues(s.second);
94 62 }
95 };
96
97 860 struct PendingStateChange
98 {
99 172 PendingStateChange(
100 const std::shared_ptr<Subscription> &sub,
101 172 PdCom::Subscription::State st) :
102 172 subscription_(sub), state_(st)
103 {
104 172 sub->This_->state_ = st;
105 172 }
106 std::weak_ptr<Subscription> subscription_;
107 PdCom::Subscription::State state_;
108 172 void execute() const
109 {
110
2/2
✓ Branch 3 taken 168 times.
✓ Branch 4 taken 4 times.
344 if (const auto sub = subscription_.lock())
111
1/2
✓ Branch 4 taken 168 times.
✗ Branch 5 not taken.
168 if (sub->This_) {
112 168 sub->This_->state_ = state_;
113
1/2
✓ Branch 15 taken 168 times.
✗ Branch 16 not taken.
168 sub->subscriber_.stateChanged(*sub->This_);
114 }
115 172 }
116 };
117 };
118
119
120 }} // namespace PdCom::impl
121
122
123 #endif // PDCOM5_SUBSCRIPTION_IMPL_H
124