TRLC Platform Library  1.0.0
Header-only C++ library for compile-time platform detection and abstraction
platform.hpp
Go to the documentation of this file.
1 #pragma once
2 
13 namespace trlc {
14 namespace platform {
15 
21 enum class OperatingSystem : int {
22  unknown = 0,
23  windows,
25  ubuntu,
26  debian,
27  redhat,
28  macos,
29  freebsd,
30  openbsd,
31  netbsd,
32  android,
33  ios
34 };
35 
41 enum class EnvironmentType : int {
42  unknown = 0,
43  desktop,
44  server,
45  embedded,
46  mobile
47 };
48 
55 struct PlatformInfo {
58  const char* os_name;
59  const char* kernel_family;
60 
65  constexpr bool isPosix() const noexcept {
66  return (kernel_family[0] == 'p' && kernel_family[1] == 'o') || // "posix"
67  (kernel_family[0] == 'u' && kernel_family[1] == 'n'); // "unix"
68  }
69 
74  constexpr bool isUnix() const noexcept {
80  }
81 
86  constexpr bool isWindows() const noexcept { return os == OperatingSystem::windows; }
87 };
88 
98 constexpr OperatingSystem getOperatingSystem() noexcept {
99 // Check for Android first (it defines __linux__ but is not generic Linux)
100 #if defined(__ANDROID__)
102 // Check for iOS
103 #elif defined(__APPLE__) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
104  return OperatingSystem::ios;
105 // Check for macOS
106 #elif defined(__APPLE__) && defined(__MACH__)
107  return OperatingSystem::macos;
108 // Check for Windows
109 #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
111 // Check for specific BSD variants
112 #elif defined(__FreeBSD__)
114 #elif defined(__OpenBSD__)
116 #elif defined(__NetBSD__)
118 // Check for Linux distributions
119 #elif defined(__linux__) || defined(__gnu_linux__)
120  // For now, return generic Linux
121  // In a real implementation, we might read /etc/os-release at runtime
122  // or use additional compile-time detection methods
124 #else
126 #endif
127 }
128 
138 constexpr EnvironmentType getEnvironmentType() noexcept {
139  constexpr auto os = getOperatingSystem();
140 
141  // Mobile platforms
142  if (os == OperatingSystem::android || os == OperatingSystem::ios) {
144  }
145 
146  // Desktop platforms
149  }
150 
151  // Unix-like systems - typically server environments
155  os == OperatingSystem::netbsd) {
156  // Default to server for Unix-like systems
157  // In practice, this might be refined based on additional detection
159  }
160 
162 }
163 
172 constexpr PlatformInfo getPlatformInfo() noexcept {
173  constexpr auto os = getOperatingSystem();
174  constexpr auto env = getEnvironmentType();
175 
176  // Determine OS name and kernel family based on detected OS
177  const char* os_name = nullptr;
178  const char* kernel_family = nullptr;
179 
180  switch (os) {
182  os_name = "Windows";
183  kernel_family = "nt";
184  break;
186  os_name = "Linux";
187  kernel_family = "posix";
188  break;
190  os_name = "Ubuntu";
191  kernel_family = "posix";
192  break;
194  os_name = "Debian";
195  kernel_family = "posix";
196  break;
198  os_name = "Red Hat";
199  kernel_family = "posix";
200  break;
202  os_name = "macOS";
203  kernel_family = "unix";
204  break;
206  os_name = "FreeBSD";
207  kernel_family = "unix";
208  break;
210  os_name = "OpenBSD";
211  kernel_family = "unix";
212  break;
214  os_name = "NetBSD";
215  kernel_family = "unix";
216  break;
218  os_name = "Android";
219  kernel_family = "posix";
220  break;
222  os_name = "iOS";
223  kernel_family = "unix";
224  break;
226  default:
227  os_name = "Unknown";
228  kernel_family = "unknown";
229  break;
230  }
231 
232  return PlatformInfo{os, env, os_name, kernel_family};
233 }
234 
243 constexpr bool hasPosixApi() noexcept {
244 // Check for POSIX version macro
245 #if defined(_POSIX_VERSION)
246  return true;
247 // Check for common Unix/POSIX indicators
248 #elif defined(__unix__) || defined(__unix) || defined(unix)
249  return true;
250 // Platform-specific checks
251 #elif defined(__linux__) || defined(__gnu_linux__)
252  return true;
253 #elif defined(__APPLE__) && defined(__MACH__)
254  return true;
255 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
256  return true;
257 #elif defined(__ANDROID__)
258  return true;
259 #else
260  return false;
261 #endif
262 }
263 
272 constexpr bool hasWin32Api() noexcept {
273 #if defined(_WIN32) || defined(_WIN64)
274  return true;
275 #elif defined(__CYGWIN__)
276  return true; // Cygwin provides Win32 API access
277 #else
278  return false;
279 #endif
280 }
281 
291 constexpr bool supportsCaseSensitiveFilesystem() noexcept {
292  constexpr auto os = getOperatingSystem();
293 
294  // Unix-like systems typically have case-sensitive filesystems
299  return true;
300  }
301 
302  // Windows and macOS typically have case-insensitive filesystems by default
303  // (though they can be configured to be case-sensitive)
305  os == OperatingSystem::ios) {
306  return false;
307  }
308 
309  // Default to case-sensitive for unknown platforms
310  return true;
311 }
312 
313 } // namespace platform
314 } // namespace trlc
315 
316 // Convenience macros for conditional compilation
317 
321 #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
322  #define TRLC_PLATFORM_WINDOWS 1
323 #else
324  #define TRLC_PLATFORM_WINDOWS 0
325 #endif
326 
330 #if defined(__linux__) || defined(__gnu_linux__)
331  #define TRLC_PLATFORM_LINUX 1
332 #else
333  #define TRLC_PLATFORM_LINUX 0
334 #endif
335 
339 #if defined(__APPLE__) && defined(__MACH__) && !defined(TARGET_OS_IPHONE)
340  #define TRLC_PLATFORM_MACOS 1
341 #else
342  #define TRLC_PLATFORM_MACOS 0
343 #endif
344 
348 #if defined(_POSIX_VERSION) || defined(__unix__) || defined(__unix) || defined(unix) || \
349  defined(__linux__) || defined(__gnu_linux__) || (defined(__APPLE__) && defined(__MACH__)) || \
350  defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__ANDROID__)
351  #define TRLC_PLATFORM_POSIX 1
352 #else
353  #define TRLC_PLATFORM_POSIX 0
354 #endif
355 
359 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
360  (defined(__APPLE__) && defined(__MACH__))
361  #define TRLC_PLATFORM_BSD 1
362 #else
363  #define TRLC_PLATFORM_BSD 0
364 #endif
365 
369 #if TRLC_PLATFORM_LINUX || TRLC_PLATFORM_MACOS || TRLC_PLATFORM_BSD || defined(__ANDROID__)
370  #define TRLC_PLATFORM_UNIX 1
371 #else
372  #define TRLC_PLATFORM_UNIX 0
373 #endif
374 
378 #if defined(__ANDROID__) || (defined(__APPLE__) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
379  #define TRLC_PLATFORM_MOBILE 1
380 #else
381  #define TRLC_PLATFORM_MOBILE 0
382 #endif
383 
387 #if TRLC_PLATFORM_UNIX && !TRLC_PLATFORM_MACOS
388  #define TRLC_PLATFORM_CASE_SENSITIVE_FS 1
389 #else
390  #define TRLC_PLATFORM_CASE_SENSITIVE_FS 0
391 #endif
static EnvironmentType getEnvironmentType()
Detects the current environment type at compile time.
Definition: platform.hpp:138
EnvironmentType
Environment type identification enumeration.
Definition: platform.hpp:41
@ mobile
Mobile device environment.
@ embedded
Embedded system environment.
@ unknown
Unknown environment type.
@ desktop
Desktop/workstation environment.
@ server
Server environment.
static bool hasPosixApi()
Checks if the platform supports POSIX APIs.
Definition: platform.hpp:243
static PlatformInfo getPlatformInfo()
Gets comprehensive platform information.
Definition: platform.hpp:172
static bool supportsCaseSensitiveFilesystem()
Checks if the platform supports case-sensitive filesystems.
Definition: platform.hpp:291
OperatingSystem
Operating system identification enumeration.
Definition: platform.hpp:21
@ windows
Microsoft Windows (all versions)
@ freebsd
FreeBSD operating system.
@ ubuntu
Ubuntu Linux distribution.
@ linux_generic
Generic Linux distribution.
@ netbsd
NetBSD operating system.
@ macos
Apple macOS / Mac OS X.
@ debian
Debian Linux distribution.
@ openbsd
OpenBSD operating system.
@ ios
Apple iOS operating system.
@ unknown
Unknown or unsupported operating system.
@ android
Android operating system.
@ redhat
Red Hat Enterprise Linux / CentOS / Fedora.
static OperatingSystem getOperatingSystem()
Detects the current operating system at compile time.
Definition: platform.hpp:98
static bool hasWin32Api()
Checks if the platform supports Win32 APIs.
Definition: platform.hpp:272
Platform information structure.
Definition: platform.hpp:55
static bool isPosix() const
Checks if the platform supports POSIX APIs.
Definition: platform.hpp:65
static bool isUnix() const
Checks if the platform is Unix-like.
Definition: platform.hpp:74
EnvironmentType environment
Detected environment type.
Definition: platform.hpp:57
OperatingSystem os
Detected operating system.
Definition: platform.hpp:56
const char * kernel_family
Kernel family: "nt", "posix", "unix".
Definition: platform.hpp:59
const char * os_name
Human-readable OS name.
Definition: platform.hpp:58
static bool isWindows() const
Checks if the platform is Windows-based.
Definition: platform.hpp:86