Added tryGetDeviceAttribute() member function to TDF class

This commit is contained in:
Nav
2024-03-20 21:13:32 +00:00
parent 0ee769c872
commit af815fba13
2 changed files with 20 additions and 5 deletions

View File

@@ -30,11 +30,11 @@ namespace Targets::TargetDescription
} }
const std::string& TargetDescriptionFile::getTargetName() const { const std::string& TargetDescriptionFile::getTargetName() const {
return this->deviceAttribute("name"); return this->getDeviceAttribute("name");
} }
TargetFamily TargetDescriptionFile::getFamily() const { TargetFamily TargetDescriptionFile::getFamily() const {
const auto& family = this->deviceAttribute("family"); const auto& family = this->getDeviceAttribute("family");
if (family == "AVR8") { if (family == "AVR8") {
return TargetFamily::AVR_8; return TargetFamily::AVR_8;
@@ -280,14 +280,26 @@ namespace Targets::TargetDescription
} }
} }
const std::string& TargetDescriptionFile::deviceAttribute(const std::string& attributeName) const { std::optional<std::reference_wrapper<const std::string>> TargetDescriptionFile::tryGetDeviceAttribute(
const std::string& attributeName
) const {
const auto attributeIt = this->deviceAttributesByName.find(attributeName); const auto attributeIt = this->deviceAttributesByName.find(attributeName);
if (attributeIt == this->deviceAttributesByName.end()) { if (attributeIt == this->deviceAttributesByName.end()) {
return std::nullopt;
}
return std::cref(attributeIt->second);
}
const std::string& TargetDescriptionFile::getDeviceAttribute(const std::string& attributeName) const {
const auto attribute = this->tryGetDeviceAttribute(attributeName);
if (!attribute.has_value()) {
throw InvalidTargetDescriptionDataException("Missing target device attribute (\"" + attributeName + "\")"); throw InvalidTargetDescriptionDataException("Missing target device attribute (\"" + attributeName + "\")");
} }
return attributeIt->second; return attribute->get();
} }
std::optional<std::string> TargetDescriptionFile::tryGetAttribute( std::optional<std::string> TargetDescriptionFile::tryGetAttribute(

View File

@@ -129,7 +129,10 @@ namespace Targets::TargetDescription
void init(const std::string& xmlFilePath); void init(const std::string& xmlFilePath);
void init(const QDomDocument& document); void init(const QDomDocument& document);
const std::string& deviceAttribute(const std::string& attributeName) const; std::optional<std::reference_wrapper<const std::string>> tryGetDeviceAttribute(
const std::string& attributeName
) const;
const std::string& getDeviceAttribute(const std::string& attributeName) const;
static std::optional<std::string> tryGetAttribute(const QDomElement& element, const QString& attributeName); static std::optional<std::string> tryGetAttribute(const QDomElement& element, const QString& attributeName);
static std::string getAttribute(const QDomElement& element, const QString& attributeName); static std::string getAttribute(const QDomElement& element, const QString& attributeName);