| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuxmlparser.h" |
| 5 | |
| 6 | #include "core/info/icpuinfo.h" |
| 7 | #include "core/profilepartxmlparserprovider.h" |
| 8 | #include "icpu.h" |
| 9 | #include <utility> |
| 10 | |
| 11 | class CPUXMLParser::Factory final |
| 12 | : public ProfilePartXMLParser::Factory |
| 13 | , public ICPUProfilePart::Exporter |
| 14 | { |
| 15 | public: |
| 16 | Factory(IProfilePartXMLParserProvider const &profilePartParserProvider, |
| 17 | CPUXMLParser &parser) noexcept |
| 18 | : ProfilePartXMLParser::Factory(profilePartParserProvider) |
| 19 | , outer_(parser) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void takePartParser(Item const &i, |
| 24 | std::unique_ptr<IProfilePartXMLParser> &&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 takePhysicalId(int) override |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | CPUXMLParser &outer_; |
| 39 | }; |
| 40 | |
| 41 | void CPUXMLParser::Factory::takePartParser( |
| 42 | Item const &i, std::unique_ptr<IProfilePartXMLParser> &&part) |
| 43 | { |
| 44 | outer_.parsers_.emplace(i.ID(), std::move(part)); |
| 45 | } |
| 46 | |
| 47 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 48 | CPUXMLParser::Factory::provideExporter(Item const &i) |
| 49 | { |
| 50 | if (i.ID() == ICPU::ItemID) |
| 51 | return *this; |
| 52 | else |
| 53 | return factory(i); |
| 54 | } |
| 55 | |
| 56 | class CPUXMLParser::Initializer final : public ICPUProfilePart::Exporter |
| 57 | { |
| 58 | public: |
| 59 | Initializer(CPUXMLParser &outer) noexcept |
| 60 | : outer_(outer) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 65 | provideExporter(Item const &i) override; |
| 66 | |
| 67 | void takeActive(bool active) override; |
| 68 | void takePhysicalId(int id) override; |
| 69 | |
| 70 | private: |
| 71 | CPUXMLParser &outer_; |
| 72 | std::unordered_map<std::string, std::unique_ptr<Exportable::Exporter>> initializers_; |
| 73 | }; |
| 74 | |
| 75 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 76 | CPUXMLParser::Initializer::provideExporter(Item const &i) |
| 77 | { |
| 78 | auto &id = i.ID(); |
| 79 | if (outer_.parsers_.count(id) > 0) { |
| 80 | if (initializers_.count(id) > 0) |
| 81 | return *initializers_.at(id); |
| 82 | else { |
| 83 | auto initializer = outer_.parsers_.at(id)->initializer(); |
| 84 | if (initializer != nullptr) { |
| 85 | initializers_.emplace(id, std::move(initializer)); |
| 86 | return *initializers_.at(id); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | void CPUXMLParser::Initializer::takeActive(bool active) |
| 95 | { |
| 96 | outer_.active_ = outer_.activeDefault_ = active; |
| 97 | } |
| 98 | |
| 99 | void CPUXMLParser::Initializer::takePhysicalId(int id) |
| 100 | { |
| 101 | outer_.physicalId_ = outer_.physicalIdDefault_ = id; |
| 102 | } |
| 103 | |
| 104 | CPUXMLParser::CPUXMLParser() noexcept |
| 105 | : ProfilePartXMLParser(ICPU::ItemID, *this, *this) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | std::unique_ptr<Exportable::Exporter> CPUXMLParser::factory( |
| 110 | IProfilePartXMLParserProvider const &profilePartParserProvider) |
| 111 | { |
| 112 | return std::make_unique<CPUXMLParser::Factory>(profilePartParserProvider, |
| 113 | *this); |
| 114 | } |
| 115 | |
| 116 | std::unique_ptr<Exportable::Exporter> CPUXMLParser::initializer() |
| 117 | { |
| 118 | return std::make_unique<CPUXMLParser::Initializer>(*this); |
| 119 | } |
| 120 | |
| 121 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 122 | CPUXMLParser::provideExporter(Item const &i) |
| 123 | { |
| 124 | auto parserIt = parsers_.find(i.ID()); |
| 125 | if (parserIt != parsers_.cend()) |
| 126 | return parserIt->second->profilePartExporter(); |
| 127 | |
| 128 | return {}; |
| 129 | } |
| 130 | |
| 131 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 132 | CPUXMLParser::provideImporter(Item const &i) |
| 133 | { |
| 134 | auto parserIt = parsers_.find(i.ID()); |
| 135 | if (parserIt != parsers_.cend()) |
| 136 | return parserIt->second->profilePartImporter(); |
| 137 | |
| 138 | return {}; |
| 139 | } |
| 140 | |
| 141 | void CPUXMLParser::takeActive(bool active) |
| 142 | { |
| 143 | active_ = active; |
| 144 | } |
| 145 | |
| 146 | bool CPUXMLParser::provideActive() const |
| 147 | { |
| 148 | return active_; |
| 149 | } |
| 150 | |
| 151 | void CPUXMLParser::takePhysicalId(int id) |
| 152 | { |
| 153 | physicalId_ = id; |
| 154 | } |
| 155 | |
| 156 | int CPUXMLParser::providePhysicalId() const |
| 157 | { |
| 158 | return physicalId_; |
| 159 | } |
| 160 | |
| 161 | void CPUXMLParser::appendTo(pugi::xml_node &parentNode) |
| 162 | { |
| 163 | auto cpuNode = parentNode.append_child(ID().c_str()); |
| 164 | cpuNode.append_attribute("active") = active_; |
| 165 | cpuNode.append_attribute("physicalId") = physicalId_; |
| 166 | |
| 167 | for (auto &[key, component] : parsers_) |
| 168 | component->appendTo(cpuNode); |
| 169 | } |
| 170 | |
| 171 | void CPUXMLParser::resetAttributes() |
| 172 | { |
| 173 | active_ = activeDefault_; |
| 174 | physicalId_ = physicalIdDefault_; |
| 175 | } |
| 176 | |
| 177 | void CPUXMLParser::loadPartFrom(pugi::xml_node const &parentNode) |
| 178 | { |
| 179 | auto cpuNode = parentNode.find_child([&](pugi::xml_node const &node) { |
| 180 | // match cpu node |
| 181 | if (node.name() != ID()) |
| 182 | return false; |
| 183 | |
| 184 | // match specific cpu |
| 185 | auto physicalIdAttr = node.attribute("physicalId"); |
| 186 | if (physicalIdAttr.empty()) |
| 187 | // try the legacy "socketId" attribute node |
| 188 | physicalIdAttr = node.attribute("socketId"); |
| 189 | |
| 190 | return physicalIdAttr.as_int(-1) == physicalId_; |
| 191 | }); |
| 192 | |
| 193 | active_ = cpuNode.attribute("active").as_bool(activeDefault_); |
| 194 | |
| 195 | for (auto &[key, component] : parsers_) |
| 196 | component->loadFrom(cpuNode); |
| 197 | } |
| 198 | |
| 199 | bool const CPUXMLParser::registered_ = |
| 200 | ProfilePartXMLParserProvider::registerProvider( |
| 201 | ICPU::ItemID, []() { return std::make_unique<CPUXMLParser>(); }); |