diff --git a/common/EnumOps.h b/common/EnumOps.h index 0f670baecb..779a813743 100644 --- a/common/EnumOps.h +++ b/common/EnumOps.h @@ -5,6 +5,13 @@ #include +// Template function for casting enumerations to their underlying type +template +typename std::underlying_type::type enum_cast(Enumeration E) +{ + return static_cast::type>(E); +} + namespace detail { /// Marks an enum as supporting boolean operators diff --git a/common/Pcsx2Defs.h b/common/Pcsx2Defs.h index 059b41b5ba..557713c0c6 100644 --- a/common/Pcsx2Defs.h +++ b/common/Pcsx2Defs.h @@ -99,71 +99,6 @@ static constexpr unsigned int __pagemask = __pagesize - 1; #define safe_delete_array(ptr) (delete[] (ptr), (ptr) = nullptr) #define safe_free(ptr) (std::free(ptr), (ptr) = nullptr) -// -------------------------------------------------------------------------------------- -// ImplementEnumOperators (macro) -// -------------------------------------------------------------------------------------- -// This macro implements ++/-- operators for any conforming enumeration. In order for an -// enum to conform, it must have _FIRST and _COUNT members defined, and must have a full -// compliment of sequential members (no custom assignments) --- looking like so: -// -// enum Dummy { -// Dummy_FIRST, -// Dummy_Item = Dummy_FIRST, -// Dummy_Crap, -// Dummy_COUNT -// }; -// -// The macro also defines utility functions for bounds checking enumerations: -// EnumIsValid(value); // returns TRUE if the enum value is between FIRST and COUNT. -// EnumAssert(value); -// -// It also defines a *prototype* for converting the enumeration to a string. Note that this -// method is not implemented! You must implement it yourself if you want to use it: -// EnumToString(value); -// -#define ImplementEnumOperators(enumName) \ - static __fi enumName& operator++(enumName& src) \ - { \ - src = (enumName)((int)src + 1); \ - return src; \ - } \ -\ - static __fi enumName& operator--(enumName& src) \ - { \ - src = (enumName)((int)src - 1); \ - return src; \ - } \ -\ - static __fi enumName operator++(enumName& src, int) \ - { \ - enumName orig = src; \ - src = (enumName)((int)src + 1); \ - return orig; \ - } \ -\ - static __fi enumName operator--(enumName& src, int) \ - { \ - enumName orig = src; \ - src = (enumName)((int)src - 1); \ - return orig; \ - } \ -\ - static __fi bool operator<(const enumName& left, const pxEnumEnd_t&) { return (int)left < enumName##_COUNT; } \ - static __fi bool operator!=(const enumName& left, const pxEnumEnd_t&) { return (int)left != enumName##_COUNT; } \ - static __fi bool operator==(const enumName& left, const pxEnumEnd_t&) { return (int)left == enumName##_COUNT; } \ -\ - static __fi bool EnumIsValid(enumName id) \ - { \ - return ((int)id >= enumName##_FIRST) && ((int)id < enumName##_COUNT); \ - } \ -\ - extern const char* EnumToString(enumName id) - -class pxEnumEnd_t -{ -}; -static const pxEnumEnd_t pxEnumEnd = {}; - // -------------------------------------------------------------------------------------- // DeclareNoncopyableObject // -------------------------------------------------------------------------------------- diff --git a/common/SettingsWrapper.h b/common/SettingsWrapper.h index 0bc6f4d45f..23068744d1 100644 --- a/common/SettingsWrapper.h +++ b/common/SettingsWrapper.h @@ -5,6 +5,8 @@ #include "SettingsInterface.h" +#include "common/EnumOps.h" + // Helper class which loads or saves depending on the derived class. class SettingsWrapper { diff --git a/pcsx2/CDVD/CDVDcommon.cpp b/pcsx2/CDVD/CDVDcommon.cpp index 1390682881..1cb917589e 100644 --- a/pcsx2/CDVD/CDVDcommon.cpp +++ b/pcsx2/CDVD/CDVDcommon.cpp @@ -11,6 +11,7 @@ #include "common/Assertions.h" #include "common/Console.h" +#include "common/EnumOps.h" #include "common/FileSystem.h" #include "common/Path.h" #include "common/StringUtil.h" diff --git a/pcsx2/Config.h b/pcsx2/Config.h index fe9afaac36..c2e21c220b 100644 --- a/pcsx2/Config.h +++ b/pcsx2/Config.h @@ -376,15 +376,6 @@ enum class GSHalfPixelOffset : u8 MaxCount }; -// Template function for casting enumerations to their underlying type -template -typename std::underlying_type::type enum_cast(Enumeration E) -{ - return static_cast::type>(E); -} - -ImplementEnumOperators(GamefixId); - // -------------------------------------------------------------------------------------- // TraceFiltersEE // -------------------------------------------------------------------------------------- @@ -1043,6 +1034,8 @@ struct Pcsx2Config void LoadSave(SettingsWrapper& wrap); GamefixOptions& DisableAll(); + static const char* GetGameFixName(GamefixId id); + bool Get(GamefixId id) const; void Set(GamefixId id, bool enabled = true); void Clear(GamefixId id) { Set(id, false); } diff --git a/pcsx2/GameDatabase.cpp b/pcsx2/GameDatabase.cpp index 164a046c6e..26cab1e021 100644 --- a/pcsx2/GameDatabase.cpp +++ b/pcsx2/GameDatabase.cpp @@ -8,6 +8,7 @@ #include "vtlb.h" #include "common/Console.h" +#include "common/EnumOps.h" #include "common/Error.h" #include "common/FileSystem.h" #include "common/Path.h" @@ -193,9 +194,9 @@ void GameDatabase::parseAndInsert(const std::string_view& serial, const c4::yml: if (fix.ends_with("Hack")) { fix.erase(fix.size() - 4); - for (GamefixId id = GamefixId_FIRST; id < pxEnumEnd; ++id) + for (GamefixId id = GamefixId_FIRST; id < GamefixId_COUNT; id = static_cast(enum_cast(id) + 1)) { - if (fix.compare(EnumToString(id)) == 0 && + if (fix.compare(Pcsx2Config::GamefixOptions::GetGameFixName(id)) == 0 && std::find(gameEntry.gameFixes.begin(), gameEntry.gameFixes.end(), id) == gameEntry.gameFixes.end()) { gameEntry.gameFixes.push_back(id); @@ -535,12 +536,12 @@ void GameDatabaseSchema::GameEntry::applyGameFixes(Pcsx2Config& config, bool app { if (!applyAuto) { - Console.Warning("[GameDB] Skipping Gamefix: %s", EnumToString(id)); + Console.Warning("[GameDB] Skipping Gamefix: %s", Pcsx2Config::GamefixOptions::GetGameFixName(id)); continue; } // if the fix is present, it is said to be enabled config.Gamefixes.Set(id, true); - Console.WriteLn("(GameDB) Enabled Gamefix: %s", EnumToString(id)); + Console.WriteLn("(GameDB) Enabled Gamefix: %s", Pcsx2Config::GamefixOptions::GetGameFixName(id)); // The LUT is only used for 1 game so we allocate it only when the gamefix is enabled (save 4MB) if (id == Fix_GoemonTlbMiss && true) diff --git a/pcsx2/Pcsx2Config.cpp b/pcsx2/Pcsx2Config.cpp index 33c2523951..6c63107bb9 100644 --- a/pcsx2/Pcsx2Config.cpp +++ b/pcsx2/Pcsx2Config.cpp @@ -1135,7 +1135,7 @@ static const char* const tbl_GamefixNames[] = "FullVU0Sync", }; -const char* EnumToString(GamefixId id) +const char* Pcsx2Config::GamefixOptions::GetGameFixName(GamefixId id) { return tbl_GamefixNames[id]; } @@ -1154,7 +1154,6 @@ Pcsx2Config::GamefixOptions& Pcsx2Config::GamefixOptions::DisableAll() void Pcsx2Config::GamefixOptions::Set(GamefixId id, bool enabled) { - pxAssert(EnumIsValid(id)); switch (id) { // clang-format off @@ -1177,14 +1176,13 @@ void Pcsx2Config::GamefixOptions::Set(GamefixId id, bool enabled) case Fix_VUOverflow: VUOverflowHack = enabled; break; case Fix_BlitInternalFPS: BlitInternalFPSHack = enabled; break; case Fix_FullVU0Sync: FullVU0SyncHack = enabled; break; - jNO_DEFAULT; + default: break; // clang-format on } } bool Pcsx2Config::GamefixOptions::Get(GamefixId id) const { - pxAssert(EnumIsValid(id)); switch (id) { // clang-format off @@ -1207,7 +1205,7 @@ bool Pcsx2Config::GamefixOptions::Get(GamefixId id) const case Fix_VUOverflow: return VUOverflowHack; case Fix_BlitInternalFPS: return BlitInternalFPSHack; case Fix_FullVU0Sync: return FullVU0SyncHack; - jNO_DEFAULT; + default: return false; // clang-format on } return false; // unreachable, but we still need to suppress warnings >_<