HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
os_detect.hpp
1// Copyright 2019 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include <exception>
7#include <cstddef>
8#include <type_traits>
9
10namespace tt {
11
12#define TT_BT_DEBUG 'D'
13#define TT_BT_RELEASE 'R'
14
15#if defined(NDEBUG)
16#define TT_BUILD_TYPE TT_BT_RELEASE
17#else
18#define TT_BUILD_TYPE TT_BT_DEBUG
19#endif
20
21enum class BuildType {
22 Debug = TT_BT_DEBUG,
23 Release = TT_BT_RELEASE,
24
25 current = TT_BUILD_TYPE
26};
27
28#define TT_OS_WINDOWS 'W'
29#define TT_OS_MACOS 'M'
30#define TT_OS_IOS 'i'
31#define TT_OS_LINUX 'L'
32#define TT_OS_POSIX 'P'
33#define TT_OS_UNIX 'U'
34#define TT_OS_ANDROID 'A'
35
36/* Create specific macros to detect the operating system.
37 */
38#if defined(_WIN64)
39#define TT_OPERATING_SYSTEM TT_OS_WINDOWS
40#elif defined(_WIN32)
41#define TT_OPERATING_SYSTEM TT_OS_WINDOWS
42#elif defined(__APPLE__)
43 #include "TargetConditionals.h"
44 #if TARGET_OS_IPHONE == 1
45 #define TT_OPERATING_SYSTEM TT_OS_IOS
46 #else
47 #define TT_OPERATING_SYSTEM TT_OS_MACOS
48 #endif
49#elif defined(__ANDROID__)
50#define TT_OPERATING_SYSTEM TT_OS_ANDROID
51#elif defined(__linux)
52#define TT_OPERATING_SYSTEM TT_OS_LINUX
53#elif defined(__unix)
54#define TT_OPERATING_SYSTEM TT_OS_UNIX
55#elif defined(__posix)
56#define TT_OPERATING_SYSTEM TT_OS_POSIX
57#else
58#error "Could not detect the operating system."
59#endif
60
61enum class OperatingSystem {
62 Windows = TT_OS_WINDOWS,
63 MacOS = TT_OS_MACOS,
64 iOS = TT_OS_IOS,
65 Linux = TT_OS_LINUX,
66 Android = TT_OS_ANDROID,
67 UNIX = TT_OS_UNIX,
68 Posix = TT_OS_POSIX,
69
70 current = TT_OPERATING_SYSTEM
71};
72
73#define TT_CC_MSVC 'm'
74#define TT_CC_GCC 'g'
75#define TT_CC_CLANG 'c'
76
77#if defined(__clang__)
78#define TT_COMPILER TT_CC_CLANG
79#elif defined(_MSC_BUILD)
80#define TT_COMPILER TT_CC_MSVC
81#elif defined(__GNUC__)
82#define TT_COMPILER TT_CC_GCC
83#else
84#error "Could not detect the compiler."
85#endif
86
87enum class Compiler {
88 MSVC = TT_CC_MSVC,
89 gcc = TT_CC_GCC,
90 clang = TT_CC_CLANG,
91
92 current = TT_COMPILER
93};
94
95#define TT_CPU_X64 'i'
96#define TT_CPU_ARM 'a'
97
98#if defined(__amd64__) || defined(__x86_64__) || defined(_M_AMD64)
99#define TT_PROCESSOR TT_CPU_X64
100#elif defined(__arm__) || defined(_M_ARM)
101#define TT_PROCESSOR TT_CPU_ARM
102#else
103#error "Could not detect processor."
104#endif
105
106enum class Processor {
107 X64 = TT_CPU_X64,
108 ARM = TT_CPU_ARM,
109
110 current = TT_PROCESSOR
111};
112
113#define tt_stringify(a) #a
114
115#if TT_COMPILER == TT_CC_MSVC
116#define tt_likely(condition) condition
117#define tt_unlikely(condition) condition
118#define tt_unreachable() __assume(0)
119#define tt_assume(condition) __assume(condition)
120#define tt_force_inline __forceinline
121#define tt_no_inline inline __declspec(noinline)
122#define clang_suppress(a)
123#define msvc_suppress(a) _Pragma(tt_stringify(warning(disable:a)))
124#define gsl_suppress(a) [[gsl::suppress(a)]]
125#define gsl_suppress2(a,b) [[gsl::suppress(a)]] [[gsl::suppress(b)]]
126#define gsl_suppress3(a,b,c) [[gsl::suppress(a)]] [[gsl::suppress(b)]] [[gsl::suppress(c)]]
127#define gsl_suppress4(a,b,c,d) [[gsl::suppress(a)]] [[gsl::suppress(b)]] [[gsl::suppress(c)]] [[gsl::suppress(d)]]
128#define gsl_suppress5(a,b,c,d,e) [[gsl::suppress(a)]] [[gsl::suppress(b)]] [[gsl::suppress(c)]] [[gsl::suppress(d)]] [[gsl::suppress(e)]]
129
130#elif TT_COMPILER == TT_CC_CLANG
131#define tt_likely(condition) __builtin_expect(static_cast<bool>(condition), 1)
132#define tt_unlikely(condition) __builtin_expect(static_cast<bool>(condition), 0)
133#define tt_unreachable() __builtin_unreachable()
134#define tt_assume(condition) __builtin_assume(static_cast<bool>(condition))
135#define tt_force_inline inline __attribute__((always_inline))
136#define tt_no_inline inline __attribute__((noinline))
137#define clang_suppress(a) _Pragma(tt_stringify(clang diagnostic ignored a))
138#define msvc_suppress(a)
139#define gsl_suppress(a) [[gsl::suppress(#a)]]
140#define gsl_suppress2(a,b) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]]
141#define gsl_suppress3(a,b,c) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]] [[gsl::suppress(#c)]]
142#define gsl_suppress4(a,b,c,d) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]] [[gsl::suppress(#c)]] [[gsl::suppress(#d)]]
143#define gsl_suppress5(a,b,c,d,e) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]] [[gsl::suppress(#c)]] [[gsl::suppress(#d)]] [[gsl::suppress(#e)]]
144
145#elif TT_COMPILER == TT_CC_GCC
146#define tt_likely(condition) __builtin_expect(static_cast<bool>(condition), 1)
147#define tt_unlikely(condition) __builtin_expect(static_cast<bool>(condition), 0)
148#define tt_unreachable() __builtin_unreachable()
149#define tt_assume(condition) do { if (!(condition)) tt_unreachable(); } while (false)
150#define tt_force_inline inline __attribute__((always_inline))
151#define tt_no_inline inline __attribute__((noinline))
152#define clang_suppress(a)
153#define msvc_suppress(a)
154#define gsl_suppress(a) [[gsl::suppress(#a)]]
155#define gsl_suppress2(a,b) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]]
156#define gsl_suppress3(a,b,c) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]] [[gsl::suppress(#c)]]
157#define gsl_suppress4(a,b,c,d) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]] [[gsl::suppress(#c)]] [[gsl::suppress(#d)]]
158#define gsl_suppress5(a,b,c,d,e) [[gsl::suppress(#a)]] [[gsl::suppress(#b)]] [[gsl::suppress(#c)]] [[gsl::suppress(#d)]] [[gsl::suppress(#e)]]
159
160#else
161#define tt_likely(condition) condition
162#define tt_unlikely(condition) condition
163#define tt_unreachable() std::terminate()
164#define tt_assume(condition) static_assert(sizeof(condition) == 1)
165#define tt_force_inline inline
166#define tt_no_inline
167#define clang_suppress(a)
168#define msvc_suppress(a)
169#define gsl_suppress(a)
170#define gsl_suppress2(a,b)
171#define gsl_suppress3(a,b,c)
172#define gsl_suppress4(a,b,c,d)
173#define gsl_suppress5(a,b,c,d,e)
174
175#endif
176
177#if TT_BUILD_TYPE == TT_BT_DEBUG
178#undef tt_assume
181#define tt_assume(expression) tt_assert(expression)
182#endif
183
184constexpr size_t cache_line_size =
185 Processor::current == Processor::X64 ? 128 :
186 Processor::current == Processor::ARM ? 64 :
187 0;
188
191using FileHandle =
192 std::conditional_t<OperatingSystem::current == OperatingSystem::Windows,void *,
193 std::conditional_t<OperatingSystem::current == OperatingSystem::MacOS,int,
194 void>>;
195
196}