From af815fba13e46ef82cea78eea1e542c484b53145 Mon Sep 17 00:00:00 2001 From: Nav Date: Wed, 20 Mar 2024 21:13:32 +0000 Subject: [PATCH] Added `tryGetDeviceAttribute()` member function to TDF class --- .../TargetDescriptionFile.cpp | 20 +++++++++++++++---- .../TargetDescriptionFile.hpp | 5 ++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Targets/TargetDescription/TargetDescriptionFile.cpp b/src/Targets/TargetDescription/TargetDescriptionFile.cpp index 8d056ea3..6fd04568 100644 --- a/src/Targets/TargetDescription/TargetDescriptionFile.cpp +++ b/src/Targets/TargetDescription/TargetDescriptionFile.cpp @@ -30,11 +30,11 @@ namespace Targets::TargetDescription } const std::string& TargetDescriptionFile::getTargetName() const { - return this->deviceAttribute("name"); + return this->getDeviceAttribute("name"); } TargetFamily TargetDescriptionFile::getFamily() const { - const auto& family = this->deviceAttribute("family"); + const auto& family = this->getDeviceAttribute("family"); if (family == "AVR8") { return TargetFamily::AVR_8; @@ -280,14 +280,26 @@ namespace Targets::TargetDescription } } - const std::string& TargetDescriptionFile::deviceAttribute(const std::string& attributeName) const { + std::optional> TargetDescriptionFile::tryGetDeviceAttribute( + const std::string& attributeName + ) const { const auto attributeIt = this->deviceAttributesByName.find(attributeName); 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 + "\")"); } - return attributeIt->second; + return attribute->get(); } std::optional TargetDescriptionFile::tryGetAttribute( diff --git a/src/Targets/TargetDescription/TargetDescriptionFile.hpp b/src/Targets/TargetDescription/TargetDescriptionFile.hpp index a8581434..93ff4ca1 100644 --- a/src/Targets/TargetDescription/TargetDescriptionFile.hpp +++ b/src/Targets/TargetDescription/TargetDescriptionFile.hpp @@ -129,7 +129,10 @@ namespace Targets::TargetDescription void init(const std::string& xmlFilePath); void init(const QDomDocument& document); - const std::string& deviceAttribute(const std::string& attributeName) const; + std::optional> tryGetDeviceAttribute( + const std::string& attributeName + ) const; + const std::string& getDeviceAttribute(const std::string& attributeName) const; static std::optional tryGetAttribute(const QDomElement& element, const QString& attributeName); static std::string getAttribute(const QDomElement& element, const QString& attributeName);