geode/loader/include/Geode/platform/platform.hpp

265 lines
8 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#pragma once
#include "cplatform.h"
#include <string>
2022-10-30 14:59:20 -04:00
#include <functional>
2024-09-05 05:35:12 -04:00
#include <memory>
2022-10-30 14:59:20 -04:00
#if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__)
2022-10-30 14:59:20 -04:00
#define GEODE_PRETTY_FUNCTION std::string(__FUNCSIG__)
#else
2022-10-30 14:59:20 -04:00
#define GEODE_PRETTY_FUNCTION std::string(__PRETTY_FUNCTION__)
#endif
2022-07-30 12:24:03 -04:00
// Windows
#ifdef GEODE_IS_WINDOWS
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_HIDDEN
#define GEODE_INLINE __forceinline
#define GEODE_VIRTUAL_CONSTEXPR
#define GEODE_NOINLINE __declspec(noinline)
#ifdef GEODE_EXPORTING
#define GEODE_DLL __declspec(dllexport)
#else
#define GEODE_DLL __declspec(dllimport)
#endif
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_API extern "C" __declspec(dllexport)
#define GEODE_EXPORT __declspec(dllexport)
2022-07-30 12:24:03 -04:00
#if defined(GEODE_IS_WINDOWS64)
#define GEODE_IS_X64
#define GEODE_CDECL_CALL
#else
#define GEODE_IS_X86
#define GEODE_CDECL_CALL __cdecl
#endif
2022-10-30 14:59:20 -04:00
#include "windows.hpp"
2022-07-30 12:24:03 -04:00
#elif defined(GEODE_IS_MACOS)
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_HIDDEN __attribute__((visibility("hidden")))
#define GEODE_INLINE inline __attribute__((always_inline))
#define GEODE_VIRTUAL_CONSTEXPR constexpr
#define GEODE_NOINLINE __attribute__((noinline))
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#ifdef GEODE_EXPORTING
#define GEODE_DLL __attribute__((visibility("default")))
#else
#define GEODE_DLL
#endif
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_API extern "C" __attribute__((visibility("default")))
#define GEODE_EXPORT __attribute__((visibility("default")))
2022-07-30 12:24:03 -04:00
#define GEODE_IS_X64
#define GEODE_CDECL_CALL
2022-10-30 14:59:20 -04:00
#include "macos.hpp"
2022-07-30 12:24:03 -04:00
#elif defined(GEODE_IS_IOS)
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_HIDDEN __attribute__((visibility("hidden")))
#define GEODE_INLINE inline __attribute__((always_inline))
#define GEODE_VIRTUAL_CONSTEXPR constexpr
#define GEODE_NOINLINE __attribute__((noinline))
#ifdef GEODE_EXPORTING
#define GEODE_DLL __attribute__((visibility("default")))
#else
#define GEODE_DLL
#endif
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_API extern "C" __attribute__((visibility("default")))
#define GEODE_EXPORT __attribute__((visibility("default")))
2022-07-30 12:24:03 -04:00
#define GEODE_IS_X64
#define GEODE_CDECL_CALL
2022-10-30 14:59:20 -04:00
#include "ios.hpp"
2022-07-30 12:24:03 -04:00
#elif defined(GEODE_IS_ANDROID)
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_HIDDEN __attribute__((visibility("hidden")))
#define GEODE_INLINE inline __attribute__((always_inline))
#define GEODE_VIRTUAL_CONSTEXPR constexpr
#define GEODE_NOINLINE __attribute__((noinline))
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#ifdef GEODE_EXPORTING
#define GEODE_DLL __attribute__((visibility("default")))
#else
#define GEODE_DLL
#endif
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
#define GEODE_API extern "C" __attribute__((visibility("default")))
#define GEODE_EXPORT __attribute__((visibility("default")))
2022-07-30 12:24:03 -04:00
#if defined(GEODE_IS_ANDROID64)
#define GEODE_IS_X64
#else
#define GEODE_IS_X86
#endif
#define GEODE_CDECL_CALL
2022-10-30 14:59:20 -04:00
#include "android.hpp"
2022-07-30 12:24:03 -04:00
#else
2022-10-30 14:59:20 -04:00
#error "Unsupported Platform!"
2022-07-30 12:24:03 -04:00
#endif
namespace geode {
class PlatformID {
public:
2024-09-09 15:29:47 -04:00
// todo in v4: make these flags and add archless Mac and Android as well as Desktop and Mobile and remove Linux
enum {
2024-09-09 15:29:47 -04:00
Unknown = -1,
Windows = 0,
MacIntel = 1,
MacArm = 2,
iOS = 3,
Android32 = 4,
Android64 = 5,
Linux = 6,
};
using Type = decltype(Unknown);
Type m_value;
constexpr PlatformID(Type t) {
m_value = t;
}
constexpr PlatformID& operator=(Type t) {
m_value = t;
return *this;
}
constexpr bool operator==(int other) const {
return m_value == other;
}
constexpr bool operator==(PlatformID const& other) const {
return m_value == other.m_value;
}
constexpr bool operator<(PlatformID const& other) const {
return m_value < other.m_value;
}
constexpr bool operator>(PlatformID const& other) const {
return m_value > other.m_value;
}
constexpr operator int() const {
return m_value;
}
/**
* Parse string into PlatformID. String should be all-lowercase, for
* example "windows" or "linux"
*/
static GEODE_DLL PlatformID from(const char* str);
static GEODE_DLL PlatformID from(std::string const& str);
2024-06-08 02:42:54 -04:00
/**
* Determines if a given platform string "covers" the given platform.
* For example, "android" is covered by Platform::Android32 and Platform::Android64.
* Input string must follow the format in PlatformID::toShortString.
*/
static GEODE_DLL bool coveredBy(const char* str, PlatformID t);
static GEODE_DLL bool coveredBy(std::string const& str, PlatformID t);
2024-09-09 15:29:47 -04:00
/**
* 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);
2024-06-08 02:42:54 -04:00
2024-09-09 15:29:47 -04:00
// 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";
case Windows: return "Windows";
2024-06-07 08:56:35 -04:00
case MacIntel: return "MacIntel";
case MacArm: return "MacArm";
case iOS: return "iOS";
2023-12-23 08:10:23 -05:00
case Android32: return "Android32";
case Android64: return "Android64";
case Linux: return "Linux";
default: break;
}
return "Undefined";
}
2024-09-09 15:29:47 -04:00
// 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";
case Windows: return "win";
2024-06-07 08:56:35 -04:00
case MacIntel: return ignoreArch ? "mac" : "mac-intel";
case MacArm: return ignoreArch ? "mac" : "mac-arm";
case iOS: return "ios";
case Android32: return ignoreArch ? "android" : "android32";
case Android64: return ignoreArch ? "android" : "android64";
case Linux: return "linux";
default: break;
}
return "undefined";
}
template <class T>
requires requires(T t) {
static_cast<Type>(t);
}
constexpr static PlatformID from(T t) {
return static_cast<Type>(t);
}
template <class T>
requires requires(Type t) {
static_cast<T>(t);
}
constexpr T to() const {
return static_cast<T>(m_value);
}
};
}
namespace std {
template <>
struct hash<geode::PlatformID> {
inline std::size_t operator()(geode::PlatformID const& id) const {
return std::hash<geode::PlatformID::Type>()(id.m_value);
}
};
}
#ifdef GEODE_IS_WINDOWS
#define GEODE_PLATFORM_TARGET PlatformID::Windows
2024-06-07 08:56:35 -04:00
#elif defined(GEODE_IS_ARM_MAC)
#define GEODE_PLATFORM_TARGET PlatformID::MacArm
#elif defined(GEODE_IS_INTEL_MAC)
#define GEODE_PLATFORM_TARGET PlatformID::MacIntel
#elif defined(GEODE_IS_IOS)
#define GEODE_PLATFORM_TARGET PlatformID::iOS
2023-12-23 08:10:23 -05:00
#elif defined(GEODE_IS_ANDROID32)
#define GEODE_PLATFORM_TARGET PlatformID::Android32
#elif defined(GEODE_IS_ANDROID64)
#define GEODE_PLATFORM_TARGET PlatformID::Android64
#endif
// this is cross-platform so not duplicating it across the typeinfo_cast definitions
namespace geode::cast {
template<class T, class U>
std::shared_ptr<T> typeinfo_pointer_cast(std::shared_ptr<U> const& r) noexcept {
// https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast
auto p = typeinfo_cast<typename std::shared_ptr<T>::element_type*>(r.get());
return std::shared_ptr<T>(r, p);
}
}