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