From a3ed4ceec5dda61f74a80fc1e7f4d9978a20cad2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 7 Jun 2019 17:26:22 -0400 Subject: [PATCH] DSP/LabelMap: Use std::optional with GetLabelValue() Rather than use a bool and out parameter, we can collapse them into one by using a std::optional. --- Source/Core/Core/DSP/DSPAssembler.cpp | 6 +++--- Source/Core/Core/DSP/LabelMap.cpp | 20 ++++++++++---------- Source/Core/Core/DSP/LabelMap.h | 3 ++- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Source/Core/Core/DSP/DSPAssembler.cpp b/Source/Core/Core/DSP/DSPAssembler.cpp index 5926bf9895..549e7df8f2 100644 --- a/Source/Core/Core/DSP/DSPAssembler.cpp +++ b/Source/Core/Core/DSP/DSPAssembler.cpp @@ -200,9 +200,9 @@ s32 DSPAssembler::ParseValue(const char* str) else // Everything else is a label. { // Lookup label - u16 value; - if (m_labels.GetLabelValue(ptr, &value)) - return value; + if (const std::optional value = m_labels.GetLabelValue(ptr)) + return *value; + if (m_cur_pass == 2) ShowError(AssemblerError::UnknownLabel, str); } diff --git a/Source/Core/Core/DSP/LabelMap.cpp b/Source/Core/Core/DSP/LabelMap.cpp index 79aa503f45..d2ac003f23 100644 --- a/Source/Core/Core/DSP/LabelMap.cpp +++ b/Source/Core/Core/DSP/LabelMap.cpp @@ -33,11 +33,11 @@ void LabelMap::RegisterDefaults() void LabelMap::RegisterLabel(const std::string& label, u16 lval, LabelType type) { - u16 old_value; - if (GetLabelValue(label, &old_value) && old_value != lval) + const std::optional old_value = GetLabelValue(label); + if (old_value && old_value != lval) { printf("WARNING: Redefined label %s to %04x - old value %04x\n", label.c_str(), lval, - old_value); + *old_value); DeleteLabel(label); } labels.emplace_back(label, lval, type); @@ -54,16 +54,15 @@ void LabelMap::DeleteLabel(const std::string& label) labels.erase(iter); } -bool LabelMap::GetLabelValue(const std::string& name, u16* value, LabelType type) const +std::optional LabelMap::GetLabelValue(const std::string& name, LabelType type) const { - for (auto& label : labels) + for (const auto& label : labels) { - if (!name.compare(label.name)) + if (name == label.name) { - if (type & label.type) + if ((type & label.type) != 0) { - *value = label.addr; - return true; + return label.addr; } else { @@ -71,7 +70,8 @@ bool LabelMap::GetLabelValue(const std::string& name, u16* value, LabelType type } } } - return false; + + return std::nullopt; } void LabelMap::Clear() diff --git a/Source/Core/Core/DSP/LabelMap.h b/Source/Core/Core/DSP/LabelMap.h index 4f541959a9..5458785610 100644 --- a/Source/Core/Core/DSP/LabelMap.h +++ b/Source/Core/Core/DSP/LabelMap.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include @@ -28,7 +29,7 @@ public: void RegisterDefaults(); void RegisterLabel(const std::string& label, u16 lval, LabelType type = LABEL_VALUE); void DeleteLabel(const std::string& label); - bool GetLabelValue(const std::string& label, u16* value, LabelType type = LABEL_ANY) const; + std::optional GetLabelValue(const std::string& label, LabelType type = LABEL_ANY) const; void Clear(); private: