Merge pull request #8162 from lioncash/label

DSP/LabelMap: C++17 transitional changes/cleanup
This commit is contained in:
Léo Lam 2019-06-08 18:16:20 +02:00 committed by GitHub
commit d927cd2b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 42 deletions

View File

@ -200,9 +200,9 @@ s32 DSPAssembler::ParseValue(const char* str)
else // Everything else is a label. else // Everything else is a label.
{ {
// Lookup label // Lookup label
u16 value; if (const std::optional<u16> value = m_labels.GetLabelValue(ptr))
if (m_labels.GetLabelValue(ptr, &value)) return *value;
return value;
if (m_cur_pass == 2) if (m_cur_pass == 2)
ShowError(AssemblerError::UnknownLabel, str); ShowError(AssemblerError::UnknownLabel, str);
} }

View File

@ -4,6 +4,7 @@
#include "Core/DSP/LabelMap.h" #include "Core/DSP/LabelMap.h"
#include <algorithm>
#include <string> #include <string>
#include <vector> #include <vector>
@ -11,9 +12,20 @@
namespace DSP namespace DSP
{ {
LabelMap::LabelMap() struct LabelMap::Label
{
Label(std::string lbl, s32 address, LabelType ltype)
: name(std::move(lbl)), addr(address), type(ltype)
{ {
} }
std::string name;
s32 addr;
LabelType type;
};
LabelMap::LabelMap() = default;
LabelMap::~LabelMap() = default;
void LabelMap::RegisterDefaults() void LabelMap::RegisterDefaults()
{ {
@ -30,40 +42,38 @@ void LabelMap::RegisterDefaults()
} }
} }
void LabelMap::RegisterLabel(const std::string& label, u16 lval, LabelType type) void LabelMap::RegisterLabel(std::string label, u16 lval, LabelType type)
{ {
u16 old_value; const std::optional<u16> old_value = GetLabelValue(label);
if (GetLabelValue(label, &old_value) && old_value != lval) if (old_value && old_value != lval)
{ {
printf("WARNING: Redefined label %s to %04x - old value %04x\n", label.c_str(), lval, printf("WARNING: Redefined label %s to %04x - old value %04x\n", label.c_str(), lval,
old_value); *old_value);
DeleteLabel(label); DeleteLabel(label);
} }
labels.emplace_back(label, lval, type); labels.emplace_back(std::move(label), lval, type);
} }
void LabelMap::DeleteLabel(const std::string& label) void LabelMap::DeleteLabel(std::string_view label)
{ {
for (std::vector<label_t>::iterator iter = labels.begin(); iter != labels.end(); ++iter) const auto iter = std::find_if(labels.cbegin(), labels.cend(),
{ [&label](const auto& entry) { return entry.name == label; });
if (!label.compare(iter->name))
{ if (iter == labels.cend())
labels.erase(iter);
return; return;
}
} 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 label.addr;
return true;
} }
else else
{ {
@ -71,7 +81,8 @@ bool LabelMap::GetLabelValue(const std::string& name, u16* value, LabelType type
} }
} }
} }
return false;
return std::nullopt;
} }
void LabelMap::Clear() void LabelMap::Clear()

View File

@ -4,7 +4,9 @@
#pragma once #pragma once
#include <optional>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -21,25 +23,18 @@ enum LabelType
class LabelMap class LabelMap
{ {
struct label_t
{
label_t(const std::string& lbl, s32 address, LabelType ltype)
: name(lbl), addr(address), type(ltype)
{
}
std::string name;
s32 addr;
LabelType type;
};
std::vector<label_t> labels;
public: public:
LabelMap(); LabelMap();
~LabelMap() {} ~LabelMap();
void RegisterDefaults(); void RegisterDefaults();
void RegisterLabel(const std::string& label, u16 lval, LabelType type = LABEL_VALUE); void RegisterLabel(std::string label, u16 lval, LabelType type = LABEL_VALUE);
void DeleteLabel(const std::string& label); void DeleteLabel(std::string_view 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(); void Clear();
private:
struct Label;
std::vector<Label> labels;
}; };
} // namespace DSP } // namespace DSP