Permalink
Browse files
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.
- Loading branch information
|
@@ -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<u16> value = m_labels.GetLabelValue(ptr)) |
|
|
return *value; |
|
|
|
|
|
if (m_cur_pass == 2) |
|
|
ShowError(AssemblerError::UnknownLabel, str); |
|
|
} |
|
|
|
@@ -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<u16> 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,24 +54,24 @@ void LabelMap::DeleteLabel(const std::string& label) |
|
|
labels.erase(iter); |
|
|
} |
|
|
|
|
|
bool LabelMap::GetLabelValue(const std::string& name, u16* value, LabelType type) const |
|
|
std::optional<u16> 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 |
|
|
{ |
|
|
printf("WARNING: Wrong label type requested. %s\n", name.c_str()); |
|
|
} |
|
|
} |
|
|
} |
|
|
return false; |
|
|
|
|
|
return std::nullopt; |
|
|
} |
|
|
|
|
|
void LabelMap::Clear() |
|
|
|
@@ -4,6 +4,7 @@ |
|
|
|
|
|
#pragma once |
|
|
|
|
|
#include <optional> |
|
|
#include <string> |
|
|
#include <vector> |
|
|
|
|
@@ -28,7 +29,7 @@ class LabelMap |
|
|
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<u16> GetLabelValue(const std::string& label, LabelType type = LABEL_ANY) const; |
|
|
void Clear(); |
|
|
|
|
|
private: |
|
|
0 comments on commit
a3ed4ce