| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "profilepart.h" |
| 5 | |
| 6 | #include "iprofilepartprovider.h" |
| 7 | #include <utility> |
| 8 | |
| 9 | bool ProfilePart::active() const |
| 10 | { |
| 11 | return active_; |
| 12 | } |
| 13 | |
| 14 | void ProfilePart::activate(bool active) |
| 15 | { |
| 16 | active_ = active; |
| 17 | } |
| 18 | |
| 19 | std::unique_ptr<IProfilePart> ProfilePart::clone() const |
| 20 | { |
| 21 | auto clone = cloneProfilePart(); |
| 22 | clone->activate(active()); |
| 23 | |
| 24 | return clone; |
| 25 | } |
| 26 | |
| 27 | void ProfilePart::importWith(Importable::Importer &i) |
| 28 | { |
| 29 | auto importer = i.provideImporter(*this); |
| 30 | if (importer.has_value()) { |
| 31 | auto &profilePartImporter = |
| 32 | dynamic_cast<ProfilePart::Importer &>(importer->get()); |
| 33 | |
| 34 | activate(profilePartImporter.provideActive()); |
| 35 | importProfilePart(profilePartImporter); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void ProfilePart::exportWith(Exportable::Exporter &e) const |
| 40 | { |
| 41 | auto exporter = e.provideExporter(*this); |
| 42 | if (exporter.has_value()) { |
| 43 | auto &profilePartExporter = |
| 44 | dynamic_cast<ProfilePart::Exporter &>(exporter->get()); |
| 45 | |
| 46 | profilePartExporter.takeActive(active()); |
| 47 | exportProfilePart(profilePartExporter); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | ProfilePart::Factory::Factory(IProfilePartProvider const &profilePartProvider) noexcept |
| 52 | : profilePartProvider_(profilePartProvider) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 57 | ProfilePart::Factory::factory(std::string const &componentID) |
| 58 | { |
| 59 | auto profilePart = createPart(componentID); |
| 60 | if (profilePart != nullptr) { |
| 61 | auto factory = profilePart->factory(profilePartProvider_); |
| 62 | takeProfilePart(std::move(profilePart)); |
| 63 | |
| 64 | if (factory != nullptr) { |
| 65 | factories_.emplace_back(std::move(factory)); |
| 66 | return *factories_.back(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return {}; |
| 71 | } |
| 72 | |
| 73 | std::unique_ptr<IProfilePart> |
| 74 | ProfilePart::Factory::createPart(std::string const &componentID) const |
| 75 | { |
| 76 | auto &partProviders = profilePartProvider_.profilePartProviders(); |
| 77 | auto const providerIt = partProviders.find(componentID); |
| 78 | if (providerIt != partProviders.cend()) |
| 79 | return providerIt->second(); |
| 80 | |
| 81 | return nullptr; |
| 82 | } |