Redcraft/Redcraft.Utility/Source/Public/Miscellaneous/Platform.h

197 lines
4.6 KiB
C
Raw Normal View History

2021-12-02 15:20:40 +00:00
#pragma once
#include "Miscellaneous/CoreDefines.h"
2021-12-02 15:20:40 +00:00
#include <cstdint>
#include <cstdlib>
#include <cstddef>
2021-12-02 15:20:40 +00:00
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
2021-12-02 15:20:40 +00:00
NAMESPACE_MODULE_BEGIN(Utility)
// Build information macro
2021-12-02 15:20:40 +00:00
#ifndef PLATFORM_NAME
# error "PLATFORM_NAME must be defined."
2021-12-02 15:20:40 +00:00
#endif
#ifndef PLATFORM_WINDOWS
# define PLATFORM_WINDOWS 0
#endif
#ifndef PLATFORM_LINUX
# define PLATFORM_LINUX 0
#endif
#ifndef PLATFORM_UNKNOWN
# define PLATFORM_UNKNOWN 0
#endif
#ifndef BUILD_TYPE
# error "BUILD_TYPE must be defined."
2021-12-02 15:20:40 +00:00
#endif
#ifndef BUILD_DEBUG
# define BUILD_DEBUG 0
#endif
#ifndef BUILD_DEVELOPMENT
# define BUILD_DEVELOPMENT 0
#endif
2021-12-02 15:20:40 +00:00
#ifndef BUILD_RELEASE
# define BUILD_RELEASE 0
#endif
#ifndef BUILD_UNKNOWN
# define BUILD_UNKNOWN 0
#endif
// Whether the CPU is x86/x64 (i.e. both 32 and 64-bit variants)
#ifndef PLATFORM_CPU_X86_FAMILY
# if (defined(_M_IX86) || defined(__i386__) || defined(_M_X64) || defined(__amd64__) || defined(__x86_64__))
# define PLATFORM_CPU_X86_FAMILY 1
# else
# define PLATFORM_CPU_X86_FAMILY 0
# endif
#endif
// Whether the CPU is AArch32/AArch64 (i.e. both 32 and 64-bit variants)
#ifndef PLATFORM_CPU_ARM_FAMILY
# if (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || defined(_M_ARM64))
# define PLATFORM_CPU_ARM_FAMILY 1
# else
# define PLATFORM_CPU_ARM_FAMILY 0
# endif
#endif
// Function type macros
2021-12-02 15:20:40 +00:00
#if PLATFORM_WINDOWS
# define VARARGS __cdecl
# define CDECL __cdecl
# define STDCALL __stdcall
# define FORCEINLINE __forceinline
# define FORCENOINLINE __declspec(noinline)
# define RESTRICT __restrict
2021-12-02 15:20:40 +00:00
#elif PLATFORM_LINUX
# define VARARGS
# define CDECL
# define STDCALL
# define FORCENOINLINE __attribute__((noinline))
# define RESTRICT __restrict
2021-12-02 15:20:40 +00:00
# if BUILD_DEBUG
# define FORCEINLINE inline
2021-12-02 15:20:40 +00:00
# else
# define FORCEINLINE inline __attribute__((always_inline))
2021-12-02 15:20:40 +00:00
# endif
#else
# define VARARGS
# define CDECL
# define STDCALL
2021-12-02 15:20:40 +00:00
# define FORCEINLINE
# define FORCENOINLINE
# define RESTRICT __restrict
2021-12-02 15:20:40 +00:00
#endif
// DLL export and import macros
2021-12-02 15:20:40 +00:00
#if PLATFORM_WINDOWS
# define DLLEXPORT __declspec(dllexport)
# define DLLIMPORT __declspec(dllimport)
2021-12-02 15:20:40 +00:00
#elif PLATFORM_LINUX
# define DLLEXPORT __attribute__((visibility("default")))
# define DLLIMPORT __attribute__((visibility("default")))
2021-12-02 15:20:40 +00:00
#else
# error "DLL export and import macros must be defined."
#endif
2021-12-02 15:20:40 +00:00
// Optimization macros
#if !defined(__clang__)
# define PRAGMA_DISABLE_OPTIMIZATION_ACTUAL _Pragma("optimize(\"\", off)")
# define PRAGMA_ENABLE_OPTIMIZATION_ACTUAL _Pragma("optimize(\"\", on)")
#elif defined(_MSC_VER)
# define PRAGMA_DISABLE_OPTIMIZATION_ACTUAL _Pragma("clang optimize off")
# define PRAGMA_ENABLE_OPTIMIZATION_ACTUAL _Pragma("clang optimize on")
#elif defined(__GNUC__ )
# define PRAGMA_DISABLE_OPTIMIZATION_ACTUAL _Pragma("GCC push_options") _Pragma("GCC optimize (\"O0\")")
# define PRAGMA_ENABLE_OPTIMIZATION_ACTUAL _Pragma("GCC pop_options")
#else
# define PRAGMA_DISABLE_OPTIMIZATION_ACTUAL
# define PRAGMA_ENABLE_OPTIMIZATION_ACTUAL
#endif
2021-12-02 15:20:40 +00:00
#if BUILD_DEBUG
# define PRAGMA_DISABLE_OPTIMIZATION
# define PRAGMA_ENABLE_OPTIMIZATION
#else
# define PRAGMA_DISABLE_OPTIMIZATION PRAGMA_DISABLE_OPTIMIZATION_ACTUAL
# define PRAGMA_ENABLE_OPTIMIZATION PRAGMA_ENABLE_OPTIMIZATION_ACTUAL
2021-12-02 15:20:40 +00:00
#endif
// Unsigned base types
2021-12-02 15:20:40 +00:00
typedef NAMESPACE_STD::uint8_t uint8;
typedef NAMESPACE_STD::uint16_t uint16;
typedef NAMESPACE_STD::uint32_t uint32;
typedef NAMESPACE_STD::uint64_t uint64;
2021-12-02 15:20:40 +00:00
// Signed base types
2021-12-02 15:20:40 +00:00
typedef NAMESPACE_STD::int8_t int8;
typedef NAMESPACE_STD::int16_t int16;
typedef NAMESPACE_STD::int32_t int32;
typedef NAMESPACE_STD::int64_t int64;
2021-12-02 15:20:40 +00:00
// Character types
2021-12-02 15:20:40 +00:00
typedef char chara;
typedef wchar_t charw;
typedef charw chart;
typedef char8_t char8;
typedef char16_t char16;
typedef char32_t char32;
2021-12-02 15:20:40 +00:00
// Pointer types
2021-12-02 15:20:40 +00:00
typedef NAMESPACE_STD::uintptr_t uintptr;
typedef NAMESPACE_STD::intptr_t intptr;
typedef NAMESPACE_STD::ptrdiff_t ptrdiff;
typedef NAMESPACE_STD::size_t size_t;
typedef intptr_t ssize_t;
2021-12-02 15:20:40 +00:00
// Null types
2021-12-02 15:20:40 +00:00
typedef decltype(NULL) null_t;
typedef NAMESPACE_STD::nullptr_t nullptr_t;
#if PLATFORM_LINUX
# define PLATFORM_TCHAR_IS_CHAR16 1
#else
# define PLATFORM_TCHAR_IS_CHAR16 0
#endif
// Define the TEXT macro
2021-12-02 15:20:40 +00:00
#if PLATFORM_TCHAR_IS_CHAR16
# define TEXT_PASTE(x) u ## x
#else
# define TEXT_PASTE(x) L ## x
#endif
#define TEXT(x) TEXT_PASTE(x)
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
2021-12-02 15:20:40 +00:00
NAMESPACE_REDCRAFT_END