add PlatformID::getCovered

This commit is contained in:
HJfod 2024-09-09 22:29:47 +03:00
parent 9f1c70ad09
commit d5718be422
2 changed files with 46 additions and 25 deletions

View file

@ -114,15 +114,16 @@
namespace geode {
class PlatformID {
public:
// todo in v4: make these flags and add archless Mac and Android as well as Desktop and Mobile and remove Linux
enum {
Unknown = -1,
Windows,
MacIntel,
MacArm,
iOS,
Android32,
Android64,
Linux,
Unknown = -1,
Windows = 0,
MacIntel = 1,
MacArm = 2,
iOS = 3,
Android32 = 4,
Android64 = 5,
Linux = 6,
};
using Type = decltype(Unknown);
@ -172,7 +173,14 @@ namespace geode {
*/
static GEODE_DLL bool coveredBy(const char* str, PlatformID t);
static GEODE_DLL bool coveredBy(std::string const& str, PlatformID t);
/**
* Returns the list of platforms covered by this string name. For
* example, "android" would return both Android32 and Android64
* todo in v4: deprecate this as the flagged version deals with this
*/
static GEODE_DLL std::vector<PlatformID> getCovered(std::string_view str);
// todo in v4: this does not need to be constexpr in the header. dllexport it
static constexpr char const* toString(Type lp) {
switch (lp) {
case Unknown: return "Unknown";
@ -188,6 +196,7 @@ namespace geode {
return "Undefined";
}
// todo in v4: this does not need to be constexpr in the header. dllexport it
static constexpr char const* toShortString(Type lp, bool ignoreArch = false) {
switch (lp) {
case Unknown: return "unknown";

View file

@ -5,6 +5,12 @@
using namespace geode::prelude;
PlatformID PlatformID::from(const char* str) {
// todo in v4: this should just be
// "win" -> Windows
// "mac", "mac-intel", "mac-arm" -> Mac
// "ios" -> iOS
// "android", "android32", "android64" -> Android
// no linux
switch (hash(str)) {
case hash("win"):
case hash("Windows"):
@ -33,29 +39,35 @@ PlatformID PlatformID::from(const char* str) {
}
bool PlatformID::coveredBy(const char* str, PlatformID t) {
switch (hash(str)) {
case hash("win"): return t == PlatformID::Windows;
case hash("mac"): return t == PlatformID::MacIntel || t == PlatformID::MacArm;
case hash("mac-intel"): return t == PlatformID::MacIntel;
case hash("mac-arm"): return t == PlatformID::MacArm;
case hash("ios"): return t == PlatformID::iOS;
case hash("android"): return t == PlatformID::Android32 || t == PlatformID::Android64;
case hash("android32"): return t == PlatformID::Android32;
case hash("android64"): return t == PlatformID::Android64;
case hash("linux"): return t == PlatformID::Linux;
default: return false;
}
// todo in v4: this is ridiculously inefficient currently - in v4 just use a flag check!
return ranges::contains(getCovered(str), t);
}
bool PlatformID::coveredBy(std::string const& str, PlatformID t) {
return PlatformID::coveredBy(str.c_str(), t);
}
std::vector<PlatformID> PlatformID::getCovered(std::string_view str) {
switch (hash(str)) {
case hash("win"): return { PlatformID::Windows };
case hash("mac"): return { PlatformID::MacIntel, PlatformID::MacArm };
case hash("mac-intel"): return { PlatformID::MacIntel };
case hash("mac-arm"): return { PlatformID::MacArm };
case hash("ios"): return { PlatformID::iOS };
case hash("android"): return { PlatformID::Android32, PlatformID::Android64 };
case hash("android32"): return { PlatformID::Android32 };
case hash("android64"): return { PlatformID::Android64 };
// todo in v4: no linux
case hash("linux"): return { PlatformID::Linux };
default: return {};
}
}
PlatformID PlatformID::from(std::string const& str) {
return PlatformID::from(str.c_str());
}