GCC Code Coverage Report


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