| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "controlgroupprofilepart.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <iterator> |
| 8 | #include <unordered_map> |
| 9 | #include <utility> |
| 10 | |
| 11 | class ControlGroupProfilePart::Factory final |
| 12 | : public ProfilePart::Factory |
| 13 | , public ControlGroup::Exporter |
| 14 | { |
| 15 | public: |
| 16 | Factory(IProfilePartProvider const &profilePartProvider, |
| 17 | ControlGroupProfilePart &profilePart) noexcept |
| 18 | : ProfilePart::Factory(profilePartProvider) |
| 19 | , outer_(profilePart) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void takeProfilePart(std::unique_ptr<IProfilePart> &&part) override; |
| 24 | |
| 25 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 26 | provideExporter(Item const &i) override; |
| 27 | |
| 28 | void takeActive(bool) override |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | ControlGroupProfilePart &outer_; |
| 34 | }; |
| 35 | |
| 36 | void ControlGroupProfilePart::Factory::takeProfilePart( |
| 37 | std::unique_ptr<IProfilePart> &&part) |
| 38 | { |
| 39 | outer_.parts_.emplace_back(std::move(part)); |
| 40 | } |
| 41 | |
| 42 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 43 | ControlGroupProfilePart::Factory::provideExporter(Item const &i) |
| 44 | { |
| 45 | return factory(i.ID()); |
| 46 | } |
| 47 | |
| 48 | class ControlGroupProfilePart::Initializer final : public ControlGroup::Exporter |
| 49 | { |
| 50 | public: |
| 51 | Initializer(ControlGroupProfilePart &profilePart) noexcept |
| 52 | : outer_(profilePart) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 57 | provideExporter(Item const &i) override; |
| 58 | |
| 59 | void takeActive(bool active) override; |
| 60 | |
| 61 | private: |
| 62 | ControlGroupProfilePart &outer_; |
| 63 | std::unordered_map<std::string, std::unique_ptr<Exportable::Exporter>> initializers_; |
| 64 | }; |
| 65 | |
| 66 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 67 | ControlGroupProfilePart::Initializer::provideExporter(Item const &i) |
| 68 | { |
| 69 | for (auto &part : outer_.parts_) { |
| 70 | |
| 71 | if (part->ID() == i.ID() && |
| 72 | (part->instanceID() == i.instanceID() || part->instanceID().empty())) { |
| 73 | |
| 74 | // Each item instance must have its own initializer |
| 75 | auto const id = i.ID() != i.instanceID() ? i.ID() + i.instanceID() |
| 76 | : i.ID(); |
| 77 | if (initializers_.count(id) > 0) |
| 78 | return *initializers_.at(id); |
| 79 | else { |
| 80 | auto initializer = part->initializer(); |
| 81 | if (initializer != nullptr) { |
| 82 | initializers_.emplace(id, std::move(initializer)); |
| 83 | return *initializers_.at(id); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | void ControlGroupProfilePart::Initializer::takeActive(bool active) |
| 95 | { |
| 96 | outer_.activate(active); |
| 97 | } |
| 98 | |
| 99 | ControlGroupProfilePart::ControlGroupProfilePart(std::string_view id) noexcept |
| 100 | : id_(id) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | std::unique_ptr<Exportable::Exporter> |
| 105 | ControlGroupProfilePart::factory(IProfilePartProvider const &profilePartProvider) |
| 106 | { |
| 107 | return std::make_unique<ControlGroupProfilePart::Factory>(profilePartProvider, |
| 108 | *this); |
| 109 | } |
| 110 | |
| 111 | std::unique_ptr<Exportable::Exporter> ControlGroupProfilePart::initializer() |
| 112 | { |
| 113 | return std::make_unique<ControlGroupProfilePart::Initializer>(*this); |
| 114 | } |
| 115 | |
| 116 | std::string const &ControlGroupProfilePart::ID() const |
| 117 | { |
| 118 | return id_; |
| 119 | } |
| 120 | |
| 121 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 122 | ControlGroupProfilePart::provideImporter(Item const &i) |
| 123 | { |
| 124 | auto partIter = std::find_if( |
| 125 | parts_.cbegin(), parts_.cend(), [&](auto const &part) { |
| 126 | return part->ID() == i.ID() && part->instanceID() == i.instanceID(); |
| 127 | }); |
| 128 | |
| 129 | if (partIter != parts_.cend()) { |
| 130 | auto importer = dynamic_cast<Importable::Importer *>(partIter->get()); |
| 131 | if (importer != nullptr) |
| 132 | return *importer; |
| 133 | } |
| 134 | |
| 135 | return {}; |
| 136 | } |
| 137 | |
| 138 | bool ControlGroupProfilePart::provideActive() const |
| 139 | { |
| 140 | return active(); |
| 141 | } |
| 142 | |
| 143 | void ControlGroupProfilePart::importProfilePart(IProfilePart::Importer &i) |
| 144 | { |
| 145 | for (auto &part : parts_) { |
| 146 | part->importWith(i); |
| 147 | |
| 148 | // ensure that all parts are active |
| 149 | part->activate(true); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void ControlGroupProfilePart::exportProfilePart(IProfilePart::Exporter &e) const |
| 154 | { |
| 155 | for (auto const &part : parts_) |
| 156 | part->exportWith(e); |
| 157 | } |
| 158 | |
| 159 | std::unique_ptr<IProfilePart> ControlGroupProfilePart::cloneProfilePart() const |
| 160 | { |
| 161 | auto clone = instance(); |
| 162 | clone->parts_.reserve(parts_.size()); |
| 163 | std::transform( |
| 164 | parts_.cbegin(), parts_.cend(), std::back_inserter(clone->parts_), |
| 165 | [](std::unique_ptr<IProfilePart> const &part) { return part->clone(); }); |
| 166 | |
| 167 | return std::move(clone); |
| 168 | } |