mirror of https://github.com/stella-emu/stella.git
Improve Bankswitch class efficiency by using string_view.
This commit is contained in:
parent
b382878d0e
commit
7c3200500e
|
@ -20,13 +20,12 @@
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
string Bankswitch::typeToName(Bankswitch::Type type)
|
string Bankswitch::typeToName(Bankswitch::Type type)
|
||||||
{
|
{
|
||||||
return BSList[static_cast<int>(type)].name;
|
return string{BSList[static_cast<int>(type)].name};
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
Bankswitch::Type Bankswitch::nameToType(string_view n) // FIXME: name
|
Bankswitch::Type Bankswitch::nameToType(string_view name)
|
||||||
{
|
{
|
||||||
string name{n};
|
|
||||||
const auto it = ourNameToTypes.find(name);
|
const auto it = ourNameToTypes.find(name);
|
||||||
if(it != ourNameToTypes.end())
|
if(it != ourNameToTypes.end())
|
||||||
return it->second;
|
return it->second;
|
||||||
|
@ -37,17 +36,17 @@ Bankswitch::Type Bankswitch::nameToType(string_view n) // FIXME: name
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
string Bankswitch::typeToDesc(Bankswitch::Type type)
|
string Bankswitch::typeToDesc(Bankswitch::Type type)
|
||||||
{
|
{
|
||||||
return BSList[static_cast<int>(type)].desc;
|
return string{BSList[static_cast<int>(type)].desc};
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
Bankswitch::Type Bankswitch::typeFromExtension(const FSNode& file)
|
Bankswitch::Type Bankswitch::typeFromExtension(const FSNode& file)
|
||||||
{
|
{
|
||||||
const string& name = file.getPath();
|
const string_view name = file.getPath();
|
||||||
const string::size_type idx = name.find_last_of('.');
|
const auto idx = name.find_last_of('.');
|
||||||
if(idx != string::npos)
|
if(idx != string_view::npos)
|
||||||
{
|
{
|
||||||
const auto it = ourExtensions.find(name.c_str() + idx + 1);
|
const auto it = ourExtensions.find(name.substr(idx + 1));
|
||||||
if(it != ourExtensions.end())
|
if(it != ourExtensions.end())
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
@ -56,12 +55,12 @@ Bankswitch::Type Bankswitch::typeFromExtension(const FSNode& file)
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
bool Bankswitch::isValidRomName(const string& name, string& ext)
|
bool Bankswitch::isValidRomName(string_view name, string& ext)
|
||||||
{
|
{
|
||||||
const string::size_type idx = name.find_last_of('.');
|
const auto idx = name.find_last_of('.');
|
||||||
if(idx != string::npos)
|
if(idx != string_view::npos)
|
||||||
{
|
{
|
||||||
const char* const e = name.c_str() + idx + 1;
|
const auto e = name.substr(idx + 1);
|
||||||
const auto it = ourExtensions.find(e);
|
const auto it = ourExtensions.find(e);
|
||||||
if(it != ourExtensions.end())
|
if(it != ourExtensions.end())
|
||||||
{
|
{
|
||||||
|
@ -73,27 +72,7 @@ bool Bankswitch::isValidRomName(const string& name, string& ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
bool Bankswitch::isValidRomName(const FSNode& name, string& ext)
|
constexpr std::array<Bankswitch::Description, static_cast<uInt32>(Bankswitch::Type::NumSchemes)>
|
||||||
{
|
|
||||||
return isValidRomName(name.getPath(), ext);
|
|
||||||
}
|
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
||||||
bool Bankswitch::isValidRomName(const FSNode& name)
|
|
||||||
{
|
|
||||||
string ext; // extension not used
|
|
||||||
return isValidRomName(name.getPath(), ext);
|
|
||||||
}
|
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
||||||
bool Bankswitch::isValidRomName(const string& name)
|
|
||||||
{
|
|
||||||
string ext; // extension not used
|
|
||||||
return isValidRomName(name, ext);
|
|
||||||
}
|
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
||||||
constexpr std::array<Bankswitch::Description, static_cast<int>(Bankswitch::Type::NumSchemes)>
|
|
||||||
Bankswitch::BSList = {{
|
Bankswitch::BSList = {{
|
||||||
{ "AUTO" , "Auto-detect" },
|
{ "AUTO" , "Auto-detect" },
|
||||||
{ "0840" , "0840 (8K EconoBanking)" },
|
{ "0840" , "0840 (8K EconoBanking)" },
|
||||||
|
@ -154,8 +133,8 @@ Bankswitch::BSList = {{
|
||||||
#endif
|
#endif
|
||||||
}};
|
}};
|
||||||
|
|
||||||
#if defined(GUI_SUPPORT)
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
const std::array<Bankswitch::SizesType, static_cast<int>(Bankswitch::Type::NumSchemes)>
|
const std::array<Bankswitch::SizesType, static_cast<uInt32>(Bankswitch::Type::NumSchemes)>
|
||||||
Bankswitch::Sizes = {{
|
Bankswitch::Sizes = {{
|
||||||
{ Bankswitch::any_KB, Bankswitch::any_KB }, // _AUTO
|
{ Bankswitch::any_KB, Bankswitch::any_KB }, // _AUTO
|
||||||
{ 8_KB, 8_KB }, // _0840
|
{ 8_KB, 8_KB }, // _0840
|
||||||
|
@ -215,7 +194,6 @@ Bankswitch::Sizes = {{
|
||||||
{ Bankswitch::any_KB, Bankswitch::any_KB }
|
{ Bankswitch::any_KB, Bankswitch::any_KB }
|
||||||
#endif
|
#endif
|
||||||
}};
|
}};
|
||||||
#endif // GUI_SUPPORT
|
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
Bankswitch::ExtensionMap Bankswitch::ourExtensions = {
|
Bankswitch::ExtensionMap Bankswitch::ourExtensions = {
|
||||||
|
|
|
@ -52,23 +52,23 @@ class Bankswitch
|
||||||
NumMulti = _128IN1 - _2IN1 + 1,
|
NumMulti = _128IN1 - _2IN1 + 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GUI_SUPPORT
|
|
||||||
struct SizesType {
|
struct SizesType {
|
||||||
size_t minSize{0};
|
size_t minSize{0};
|
||||||
size_t maxSize{0};
|
size_t maxSize{0};
|
||||||
};
|
};
|
||||||
static constexpr size_t any_KB = 0;
|
static constexpr size_t any_KB = 0;
|
||||||
|
|
||||||
static const std::array<SizesType, static_cast<uInt32>(Type::NumSchemes)> Sizes;
|
static const std::array<SizesType,
|
||||||
#endif
|
static_cast<uInt32>(Type::NumSchemes)> Sizes;
|
||||||
|
|
||||||
// Info about the various bankswitch schemes, useful for displaying
|
// Info about the various bankswitch schemes, useful for displaying
|
||||||
// in GUI dropdown boxes, etc
|
// in GUI dropdown boxes, etc
|
||||||
struct Description {
|
struct Description {
|
||||||
const char* const name{nullptr};
|
string_view name;
|
||||||
const char* const desc{nullptr};
|
string_view desc;
|
||||||
};
|
};
|
||||||
static const std::array<Description, static_cast<int>(Type::NumSchemes)> BSList;
|
static const std::array<Description,
|
||||||
|
static_cast<uInt32>(Type::NumSchemes)> BSList;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Convert BSType enum to string
|
// Convert BSType enum to string
|
||||||
|
@ -90,14 +90,22 @@ class Bankswitch
|
||||||
@param name Filename of potential ROM file
|
@param name Filename of potential ROM file
|
||||||
@param ext The extension extracted from the given file
|
@param ext The extension extracted from the given file
|
||||||
*/
|
*/
|
||||||
static bool isValidRomName(const string& name, string& ext);
|
static bool isValidRomName(string_view name, string& ext);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Convenience functions for different parameter types.
|
Convenience functions for different parameter types.
|
||||||
*/
|
*/
|
||||||
static bool isValidRomName(const FSNode& name, string& ext);
|
static inline bool isValidRomName(const FSNode& name, string& ext) {
|
||||||
static bool isValidRomName(const FSNode& name);
|
return isValidRomName(name.getPath(), ext);
|
||||||
static bool isValidRomName(const string& name);
|
}
|
||||||
|
static inline bool isValidRomName(const FSNode& name) {
|
||||||
|
string ext; // extension not used
|
||||||
|
return isValidRomName(name.getPath(), ext);
|
||||||
|
}
|
||||||
|
static inline bool isValidRomName(string_view name) {
|
||||||
|
string ext; // extension not used
|
||||||
|
return isValidRomName(name, ext);
|
||||||
|
}
|
||||||
|
|
||||||
// Output operator
|
// Output operator
|
||||||
friend ostream& operator<<(ostream& os, const Bankswitch::Type& t) {
|
friend ostream& operator<<(ostream& os, const Bankswitch::Type& t) {
|
||||||
|
@ -106,14 +114,16 @@ class Bankswitch
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct TypeComparator {
|
struct TypeComparator {
|
||||||
bool operator() (const string& a, const string& b) const {
|
bool operator() (string_view a, string_view b) const {
|
||||||
return BSPF::compareIgnoreCase(a, b) < 0;
|
return BSPF::compareIgnoreCase(a, b) < 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
using ExtensionMap = const std::map<string, Bankswitch::Type, TypeComparator>;
|
using ExtensionMap = const std::map<string_view, Bankswitch::Type,
|
||||||
|
TypeComparator>;
|
||||||
static ExtensionMap ourExtensions;
|
static ExtensionMap ourExtensions;
|
||||||
|
|
||||||
using NameToTypeMap = const std::map<string, Bankswitch::Type, TypeComparator>;
|
using NameToTypeMap = const std::map<string_view, Bankswitch::Type,
|
||||||
|
TypeComparator>;
|
||||||
static NameToTypeMap ourNameToTypes;
|
static NameToTypeMap ourNameToTypes;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue