GCC Code Coverage Report


Directory: ./
File: src/Subscription.h
Date: 2024-03-27 13:09:52
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 141 Subscription(
41 std::shared_ptr<const Variable> variable,
42 PdCom::Subscription *This,
43 PdCom::Subscriber &subscriber,
44 141 std::weak_ptr<Process> process) :
45 141 variable_(std::move(variable)),
46 This_(This),
47 subscriber_(subscriber),
48 282 process_(process)
49 141 {}
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 141 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 264 class SubscriberNotifier
80 {
81 std::unordered_map<Subscriber *, std::chrono::nanoseconds> subscribers_;
82
83 public:
84 60 void clear() { subscribers_.clear(); }
85 66 void insert(Subscriber &s, std::chrono::nanoseconds ts)
86 {
87
1/2
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
66 subscribers_[&s] = ts;
88 66 }
89
90 60 void notify()
91 {
92
2/2
✓ Branch 6 taken 50 times.
✓ Branch 7 taken 60 times.
110 for (const auto s : subscribers_)
93
1/2
✓ Branch 6 taken 50 times.
✗ Branch 7 not taken.
50 s.first->newValues(s.second);
94 60 }
95 };
96
97 830 struct PendingStateChange
98 {
99 166 PendingStateChange(
100 const std::shared_ptr<Subscription> &sub,
101 166 PdCom::Subscription::State st) :
102 166 subscription_(sub), state_(st)
103 {
104 166 sub->This_->state_ = st;
105 166 }
106 std::weak_ptr<Subscription> subscription_;
107 PdCom::Subscription::State state_;
108 166 void execute() const
109 {
110
2/2
✓ Branch 3 taken 162 times.
✓ Branch 4 taken 4 times.
332 if (const auto sub = subscription_.lock())
111
1/2
✓ Branch 4 taken 162 times.
✗ Branch 5 not taken.
162 if (sub->This_) {
112 162 sub->This_->state_ = state_;
113
1/2
✓ Branch 15 taken 162 times.
✗ Branch 16 not taken.
162 sub->subscriber_.stateChanged(*sub->This_);
114 }
115 166 }
116 };
117 };
118
119
120 }} // namespace PdCom::impl
121
122
123 #endif // PDCOM5_SUBSCRIPTION_IMPL_H
124