Debian Codegraph @

src/core/components/controls/controlmodeprofilepart.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "controlmodeprofilepart.h"
5
6 #include "core/item.h"
7 #include <algorithm>
8 #include <iterator>
9 #include <unordered_map>
10 #include <utility>
11
12 class ControlModeProfilePart::Factory final
13 : public ProfilePart::Factory
14 , public ControlMode::Exporter
15 {
16 public:
17 Factory(IProfilePartProvider const &profilePartProvider,
18 ControlModeProfilePart &outer) noexcept
19 : ProfilePart::Factory(profilePartProvider)
20 , outer_(outer)
21 {
22 }
23
24 void takeProfilePart(std::unique_ptr<IProfilePart> &&part) override;
25
26 std::optional<std::reference_wrapper<Exportable::Exporter>>
27 provideExporter(Item const &i) override;
28
29 void takeActive(bool) override
30 {
31 }
32
33 void takeMode(std::string const &) override
34 {
35 }
36
37 void takeModes(std::vector<std::string> const &) override
38 {
39 }
40
41 private:
42 ControlModeProfilePart &outer_;
43 };
44
45 void ControlModeProfilePart::Factory::takeProfilePart(
46 std::unique_ptr<IProfilePart> &&part)
47 {
48 outer_.parts_.emplace_back(std::move(part));
49 }
50
51 std::optional<std::reference_wrapper<Exportable::Exporter>>
52 ControlModeProfilePart::Factory::provideExporter(Item const &i)
53 {
54 return factory(i.ID());
55 }
56
57 class ControlModeProfilePart::Initializer final : public ControlMode::Exporter
58 {
59 public:
60 Initializer(ControlModeProfilePart &outer) noexcept
61 : outer_(outer)
62 {
63 }
64
65 std::optional<std::reference_wrapper<Exportable::Exporter>>
66 provideExporter(Item const &i) override;
67
68 void takeActive(bool) override;
69 void takeMode(std::string const &) override;
70 void takeModes(std::vector<std::string> const &) override
71 {
72 }
73
74 private:
75 ControlModeProfilePart &outer_;
76 std::unordered_map<std::string, std::unique_ptr<Exportable::Exporter>> initializers_;
77 };
78
79 std::optional<std::reference_wrapper<Exportable::Exporter>>
80 ControlModeProfilePart::Initializer::provideExporter(Item const &i)
81 {
82 for (auto &part : outer_.parts_) {
83 auto &id = part->ID();
84 if (id == i.ID()) {
85 if (initializers_.count(id) > 0)
86 return *initializers_.at(id);
87 else {
88 auto initializer = part->initializer();
89 if (initializer != nullptr) {
90 initializers_.emplace(id, std::move(initializer));
91 return *initializers_.at(id);
92 }
93 }
94
95 break;
96 }
97 }
98
99 return {};
100 }
101
102 void ControlModeProfilePart::Initializer::takeActive(bool active)
103 {
104 outer_.activate(active);
105 }
106
107 void ControlModeProfilePart::Initializer::takeMode(std::string const &mode)
108 {
109 outer_.mode_ = mode;
110 }
111
112 ControlModeProfilePart::ControlModeProfilePart(std::string_view id) noexcept
113 : id_(id)
114 {
115 }
116
117 std::unique_ptr<Exportable::Exporter>
118 ControlModeProfilePart::factory(IProfilePartProvider const &profilePartProvider)
119 {
120 return std::make_unique<ControlModeProfilePart::Factory>(profilePartProvider,
121 *this);
122 }
123
124 std::unique_ptr<Exportable::Exporter> ControlModeProfilePart::initializer()
125 {
126 return std::make_unique<ControlModeProfilePart::Initializer>(*this);
127 }
128
129 std::string const &ControlModeProfilePart::ID() const
130 {
131 return id_;
132 }
133
134 std::optional<std::reference_wrapper<Importable::Importer>>
135 ControlModeProfilePart::provideImporter(Item const &i)
136 {
137 auto &id = i.ID();
138 auto partIter = std::find_if(
139 parts_.cbegin(), parts_.cend(),
140 [&](auto const &part) { return part->ID() == id; });
141
142 if (partIter != parts_.cend()) {
143 auto importer = dynamic_cast<Importable::Importer *>(partIter->get());
144 if (importer != nullptr)
145 return *importer;
146 }
147
148 return {};
149 }
150
151 bool ControlModeProfilePart::provideActive() const
152 {
153 return active();
154 }
155
156 std::string const &ControlModeProfilePart::provideMode() const
157 {
158 return mode_;
159 }
160
161 void ControlModeProfilePart::importProfilePart(IProfilePart::Importer &i)
162 {
163 auto &pmImporter = dynamic_cast<ControlModeProfilePart::Importer &>(i);
164 mode(pmImporter.provideMode());
165
166 for (auto &part : parts_) {
167 part->importWith(i);
168
169 // only the selected mode profile part can be active
170 part->activate(part->ID() == mode_);
171 }
172 }
173
174 void ControlModeProfilePart::exportProfilePart(IProfilePart::Exporter &e) const
175 {
176 auto &pmExporter = dynamic_cast<ControlModeProfilePart::Exporter &>(e);
177 pmExporter.takeMode(mode_);
178
179 for (auto const &part : parts_)
180 part->exportWith(e);
181 }
182
183 std::unique_ptr<IProfilePart> ControlModeProfilePart::cloneProfilePart() const
184 {
185 auto clone = instance();
186
187 clone->parts_.reserve(parts_.size());
188 std::transform(
189 parts_.cbegin(), parts_.cend(), std::back_inserter(clone->parts_),
190 [](std::unique_ptr<IProfilePart> const &part) { return part->clone(); });
191
192 clone->mode_ = mode_;
193
194 return std::move(clone);
195 }
196
197 void ControlModeProfilePart::mode(std::string const &mode)
198 {
199 auto iter = std::find_if(parts_.cbegin(), parts_.cend(),
200 [&](auto const &part) { return mode == part->ID(); });
201 if (iter != parts_.cend())
202 mode_ = mode;
203 }