Removed unnecessary QDomDocument instance from TDF class

This commit is contained in:
Nav
2023-03-05 23:30:42 +00:00
parent 755d24da5c
commit 5833aeeb26
3 changed files with 66 additions and 49 deletions

View File

@@ -150,9 +150,7 @@ namespace Bloom::Targets::Microchip::Avr::Avr8Bit::TargetDescription
Family TargetDescriptionFile::getFamily() const { Family TargetDescriptionFile::getFamily() const {
static const auto targetFamiliesByName = TargetDescriptionFile::getFamilyNameToEnumMapping(); static const auto targetFamiliesByName = TargetDescriptionFile::getFamilyNameToEnumMapping();
const auto familyName = this->deviceElement.attributes().namedItem( const auto& familyName = this->getFamilyName();
"family"
).nodeValue().toLower().toStdString();
if (familyName.empty()) { if (familyName.empty()) {
throw Exception("Could not find target family name in target description file."); throw Exception("Could not find target family name in target description file.");

View File

@@ -11,8 +11,12 @@ namespace Bloom::Targets::TargetDescription
{ {
using namespace Bloom::Exceptions; using namespace Bloom::Exceptions;
std::string TargetDescriptionFile::getTargetName() const { const std::string& TargetDescriptionFile::getTargetName() const {
return this->deviceElement.attributes().namedItem("name").nodeValue().toStdString(); return this->targetName;
}
const std::string& TargetDescriptionFile::getFamilyName() const {
return this->familyName;
} }
void TargetDescriptionFile::init(const QString& xmlFilePath) { void TargetDescriptionFile::init(const QString& xmlFilePath) {
@@ -23,32 +27,31 @@ namespace Bloom::Targets::TargetDescription
} }
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
auto xml = QDomDocument(); auto document = QDomDocument();
if (!xml.setContent(file.readAll())) { if (!document.setContent(file.readAll())) {
throw Exception("Failed to parse target description file - please report this error " throw Exception("Failed to parse target description file - please report this error "
"to Bloom developers via " + Services::PathService::homeDomainName() + "/report-issue"); "to Bloom developers via " + Services::PathService::homeDomainName() + "/report-issue");
} }
this->init(xml); this->init(document);
} }
void TargetDescriptionFile::init(const QDomDocument& xml) { void TargetDescriptionFile::init(const QDomDocument& document) {
this->xml = xml; const auto device = document.elementsByTagName("device").item(0).toElement();
auto device = xml.elementsByTagName("device").item(0).toElement();
if (!device.isElement()) { if (!device.isElement()) {
throw TargetDescriptionParsingFailureException("Device element not found."); throw TargetDescriptionParsingFailureException("Device element not found.");
} }
this->deviceElement = device; this->targetName = device.attributes().namedItem("name").nodeValue().toStdString();
this->familyName = device.attributes().namedItem("family").nodeValue().toLower().toStdString();
this->loadAddressSpaces(); this->loadAddressSpaces(document);
this->loadPropertyGroups(); this->loadPropertyGroups(document);
this->loadModules(); this->loadModules(document);
this->loadPeripheralModules(); this->loadPeripheralModules(document);
this->loadVariants(); this->loadVariants(document);
this->loadPinouts(); this->loadPinouts(document);
this->loadInterfaces(); this->loadInterfaces(document);
} }
AddressSpace TargetDescriptionFile::generateAddressSpaceFromXml(const QDomElement& xmlElement) { AddressSpace TargetDescriptionFile::generateAddressSpaceFromXml(const QDomElement& xmlElement) {
@@ -280,9 +283,10 @@ namespace Bloom::Targets::TargetDescription
return bitField; return bitField;
} }
void TargetDescriptionFile::loadAddressSpaces() { void TargetDescriptionFile::loadAddressSpaces(const QDomDocument& document) {
const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
auto addressSpaceNodes = this->deviceElement.elementsByTagName("address-spaces").item(0).toElement() auto addressSpaceNodes = deviceElement.elementsByTagName("address-spaces").item(0).toElement()
.elementsByTagName("address-space"); .elementsByTagName("address-space");
for (int addressSpaceIndex = 0; addressSpaceIndex < addressSpaceNodes.count(); addressSpaceIndex++) { for (int addressSpaceIndex = 0; addressSpaceIndex < addressSpaceNodes.count(); addressSpaceIndex++) {
@@ -301,12 +305,10 @@ namespace Bloom::Targets::TargetDescription
} }
} }
void TargetDescriptionFile::loadPropertyGroups() { void TargetDescriptionFile::loadPropertyGroups(const QDomDocument& document) {
if (!this->deviceElement.isElement()) { const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
throw TargetDescriptionParsingFailureException("Device element not found.");
}
auto propertyGroupNodes = this->deviceElement.elementsByTagName("property-groups").item(0).toElement() auto propertyGroupNodes = deviceElement.elementsByTagName("property-groups").item(0).toElement()
.elementsByTagName("property-group"); .elementsByTagName("property-group");
for (int propertyGroupIndex = 0; propertyGroupIndex < propertyGroupNodes.count(); propertyGroupIndex++) { for (int propertyGroupIndex = 0; propertyGroupIndex < propertyGroupNodes.count(); propertyGroupIndex++) {
@@ -335,8 +337,10 @@ namespace Bloom::Targets::TargetDescription
} }
} }
void TargetDescriptionFile::loadModules() { void TargetDescriptionFile::loadModules(const QDomDocument& document) {
auto moduleNodes = this->xml.elementsByTagName("modules").item(0).toElement() const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
auto moduleNodes = document.elementsByTagName("modules").item(0).toElement()
.elementsByTagName("module"); .elementsByTagName("module");
for (int moduleIndex = 0; moduleIndex < moduleNodes.count(); moduleIndex++) { for (int moduleIndex = 0; moduleIndex < moduleNodes.count(); moduleIndex++) {
@@ -358,8 +362,10 @@ namespace Bloom::Targets::TargetDescription
} }
} }
void TargetDescriptionFile::loadPeripheralModules() { void TargetDescriptionFile::loadPeripheralModules(const QDomDocument& document) {
auto moduleNodes = this->deviceElement.elementsByTagName("peripherals").item(0).toElement() const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
auto moduleNodes = deviceElement.elementsByTagName("peripherals").item(0).toElement()
.elementsByTagName("module"); .elementsByTagName("module");
for (int moduleIndex = 0; moduleIndex < moduleNodes.count(); moduleIndex++) { for (int moduleIndex = 0; moduleIndex < moduleNodes.count(); moduleIndex++) {
@@ -432,8 +438,10 @@ namespace Bloom::Targets::TargetDescription
} }
} }
void TargetDescriptionFile::loadVariants() { void TargetDescriptionFile::loadVariants(const QDomDocument& document) {
auto variantNodes = this->xml.elementsByTagName("variants").item(0).toElement() const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
auto variantNodes = document.elementsByTagName("variants").item(0).toElement()
.elementsByTagName("variant"); .elementsByTagName("variant");
for (int variantIndex = 0; variantIndex < variantNodes.count(); variantIndex++) { for (int variantIndex = 0; variantIndex < variantNodes.count(); variantIndex++) {
@@ -471,8 +479,10 @@ namespace Bloom::Targets::TargetDescription
} }
} }
void TargetDescriptionFile::loadPinouts() { void TargetDescriptionFile::loadPinouts(const QDomDocument& document) {
auto pinoutNodes = this->xml.elementsByTagName("pinouts").item(0).toElement() const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
auto pinoutNodes = document.elementsByTagName("pinouts").item(0).toElement()
.elementsByTagName("pinout"); .elementsByTagName("pinout");
for (int pinoutIndex = 0; pinoutIndex < pinoutNodes.count(); pinoutIndex++) { for (int pinoutIndex = 0; pinoutIndex < pinoutNodes.count(); pinoutIndex++) {
@@ -524,8 +534,10 @@ namespace Bloom::Targets::TargetDescription
} }
} }
void TargetDescriptionFile::loadInterfaces() { void TargetDescriptionFile::loadInterfaces(const QDomDocument& document) {
auto interfaceNodes = this->deviceElement.elementsByTagName("interfaces").item(0).toElement() const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
auto interfaceNodes = deviceElement.elementsByTagName("interfaces").item(0).toElement()
.elementsByTagName("interface"); .elementsByTagName("interface");
for (int interfaceIndex = 0; interfaceIndex < interfaceNodes.count(); interfaceIndex++) { for (int interfaceIndex = 0; interfaceIndex < interfaceNodes.count(); interfaceIndex++) {

View File

@@ -56,11 +56,18 @@ namespace Bloom::Targets::TargetDescription
} }
/** /**
* Extracts target name. * Returns the target name extracted from the TDF.
* *
* @return * @return
*/ */
[[nodiscard]] std::string getTargetName() const; [[nodiscard]] const std::string& getTargetName() const;
/**
* Returns the target family name extracted from the TDF.
*
* @return
*/
[[nodiscard]] const std::string& getFamilyName() const;
/** /**
* Returns a mapping of all property groups, with the property group name being the key. * Returns a mapping of all property groups, with the property group name being the key.
@@ -108,8 +115,8 @@ namespace Bloom::Targets::TargetDescription
} }
protected: protected:
QDomDocument xml; std::string targetName;
QDomElement deviceElement; std::string familyName;
std::map<std::string, AddressSpace> addressSpacesMappedById; std::map<std::string, AddressSpace> addressSpacesMappedById;
std::map<std::string, PropertyGroup> propertyGroupsMappedByName; std::map<std::string, PropertyGroup> propertyGroupsMappedByName;
@@ -129,7 +136,7 @@ namespace Bloom::Targets::TargetDescription
TargetDescriptionFile& operator = (const TargetDescriptionFile& other) = default; TargetDescriptionFile& operator = (const TargetDescriptionFile& other) = default;
TargetDescriptionFile& operator = (TargetDescriptionFile&& other) = default; TargetDescriptionFile& operator = (TargetDescriptionFile&& other) = default;
virtual void init(const QDomDocument& xml); virtual void init(const QDomDocument& document);
void init(const QString& xmlFilePath); void init(const QString& xmlFilePath);
/** /**
@@ -175,36 +182,36 @@ namespace Bloom::Targets::TargetDescription
/** /**
* Extracts all address spaces and loads them into this->addressSpacesMappedById. * Extracts all address spaces and loads them into this->addressSpacesMappedById.
*/ */
void loadAddressSpaces(); void loadAddressSpaces(const QDomDocument& document);
/** /**
* Extracts all property groups and loads them into this->propertyGroupsMappedByName. * Extracts all property groups and loads them into this->propertyGroupsMappedByName.
*/ */
void loadPropertyGroups(); void loadPropertyGroups(const QDomDocument& document);
/** /**
* Extracts all modules and loads them into this->modulesMappedByName. * Extracts all modules and loads them into this->modulesMappedByName.
*/ */
void loadModules(); void loadModules(const QDomDocument& document);
/** /**
* Extracts all peripheral modules and loads them into this->peripheralModulesMappedByName. * Extracts all peripheral modules and loads them into this->peripheralModulesMappedByName.
*/ */
void loadPeripheralModules(); void loadPeripheralModules(const QDomDocument& document);
/** /**
* Extracts all variants and loads them into this->variants. * Extracts all variants and loads them into this->variants.
*/ */
void loadVariants(); void loadVariants(const QDomDocument& document);
/** /**
* Extracts all pinouts and loads them into this->pinoutsMappedByName. * Extracts all pinouts and loads them into this->pinoutsMappedByName.
*/ */
void loadPinouts(); void loadPinouts(const QDomDocument& document);
/** /**
* Extracts all interfaces and loads them into this->interfacesByName * Extracts all interfaces and loads them into this->interfacesByName
*/ */
void loadInterfaces(); void loadInterfaces(const QDomDocument& document);
}; };
} }