| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "pmpowerstateprofilepart.h" |
| 5 | |
| 6 | #include "core/profilepartprovider.h" |
| 7 | #include <algorithm> |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | |
| 11 | class AMD::PMPowerStateProfilePart::Initializer final |
| 12 | : public AMD::PMPowerState::Exporter |
| 13 | { |
| 14 | public: |
| 15 | Initializer(AMD::PMPowerStateProfilePart &outer) noexcept |
| 16 | : outer_(outer) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 21 | provideExporter(Item const &) override |
| 22 | { |
| 23 | return {}; |
| 24 | } |
| 25 | |
| 26 | void takeActive(bool active) override; |
| 27 | void takePMPowerStateMode(std::string const &mode) override; |
| 28 | void takePMPowerStateModes(std::vector<std::string> const &modes) override; |
| 29 | |
| 30 | private: |
| 31 | AMD::PMPowerStateProfilePart &outer_; |
| 32 | }; |
| 33 | |
| 34 | void AMD::PMPowerStateProfilePart::Initializer::takeActive(bool active) |
| 35 | { |
| 36 | outer_.activate(active); |
| 37 | } |
| 38 | |
| 39 | void AMD::PMPowerStateProfilePart::Initializer::takePMPowerStateMode( |
| 40 | std::string const &mode) |
| 41 | { |
| 42 | outer_.mode_ = mode; |
| 43 | } |
| 44 | |
| 45 | void AMD::PMPowerStateProfilePart::Initializer::takePMPowerStateModes( |
| 46 | std::vector<std::string> const &modes) |
| 47 | { |
| 48 | outer_.modes_ = modes; |
| 49 | } |
| 50 | |
| 51 | AMD::PMPowerStateProfilePart::PMPowerStateProfilePart() noexcept |
| 52 | : id_(AMD::PMPowerState::ItemID) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | std::unique_ptr<Exportable::Exporter> |
| 57 | AMD::PMPowerStateProfilePart::factory(IProfilePartProvider const &) |
| 58 | { |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | std::unique_ptr<Exportable::Exporter> AMD::PMPowerStateProfilePart::initializer() |
| 63 | { |
| 64 | return std::make_unique<AMD::PMPowerStateProfilePart::Initializer>(*this); |
| 65 | } |
| 66 | |
| 67 | std::string const &AMD::PMPowerStateProfilePart::ID() const |
| 68 | { |
| 69 | return id_; |
| 70 | } |
| 71 | |
| 72 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 73 | AMD::PMPowerStateProfilePart::provideImporter(Item const &) |
| 74 | { |
| 75 | return {}; |
| 76 | } |
| 77 | |
| 78 | bool AMD::PMPowerStateProfilePart::provideActive() const |
| 79 | { |
| 80 | return active(); |
| 81 | } |
| 82 | |
| 83 | std::string const &AMD::PMPowerStateProfilePart::providePMPowerStateMode() const |
| 84 | { |
| 85 | return mode_; |
| 86 | } |
| 87 | |
| 88 | void AMD::PMPowerStateProfilePart::importProfilePart(IProfilePart::Importer &i) |
| 89 | { |
| 90 | auto &pmfImporter = dynamic_cast<AMD::PMPowerStateProfilePart::Importer &>(i); |
| 91 | mode(pmfImporter.providePMPowerStateMode()); |
| 92 | } |
| 93 | |
| 94 | void AMD::PMPowerStateProfilePart::exportProfilePart(IProfilePart::Exporter &e) const |
| 95 | { |
| 96 | auto &pmfExporter = dynamic_cast<AMD::PMPowerStateProfilePart::Exporter &>(e); |
| 97 | pmfExporter.takePMPowerStateMode(mode_); |
| 98 | } |
| 99 | |
| 100 | std::unique_ptr<IProfilePart> AMD::PMPowerStateProfilePart::cloneProfilePart() const |
| 101 | { |
| 102 | auto clone = std::make_unique<AMD::PMPowerStateProfilePart>(); |
| 103 | clone->modes_ = modes_; |
| 104 | clone->mode_ = mode_; |
| 105 | |
| 106 | return std::move(clone); |
| 107 | } |
| 108 | |
| 109 | void AMD::PMPowerStateProfilePart::mode(std::string const &mode) |
| 110 | { |
| 111 | // import known modes |
| 112 | auto iter = std::find_if( |
| 113 | modes_.cbegin(), modes_.cend(), |
| 114 | [&](auto const &availableMode) { return mode == availableMode; }); |
| 115 | if (iter != modes_.cend()) |
| 116 | mode_ = mode; |
| 117 | } |
| 118 | |
| 119 | bool const AMD::PMPowerStateProfilePart::registered_ = |
| 120 | ProfilePartProvider::registerProvider(AMD::PMPowerState::ItemID, []() { |
| 121 | return std::make_unique<AMD::PMPowerStateProfilePart>(); |
| 122 | }); |