TRLC Platform Library  1.0.0
Header-only C++ library for compile-time platform detection and abstraction
core.hpp
Go to the documentation of this file.
1 #pragma once
2 
26 #include <atomic>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <thread> // For std::this_thread::yield
31 #include <type_traits>
32 
33 // Include all platform detection headers in dependency order
39 #include "trlc/platform/macros.hpp"
42 
43 // Conditionally include debug utilities
44 #ifdef TRLC_PLATFORM_ENABLE_DEBUG_UTILS
45  #include "trlc/platform/debug.hpp"
46 #endif
47 
48 namespace trlc {
49 namespace platform {
50 
51 //==============================================================================
52 // Version Information
53 //==============================================================================
54 
61 struct Version {
63  static constexpr int MAJOR = 1;
64 
66  static constexpr int MINOR = 0;
67 
69  static constexpr int PATCH = 0;
70 
72  static constexpr const char* STRING = "1.0.0";
73 
78  static constexpr uint32_t asInteger() noexcept {
79  return (static_cast<uint32_t>(MAJOR) << 16) | (static_cast<uint32_t>(MINOR) << 8) |
80  static_cast<uint32_t>(PATCH);
81  }
82 
90  static constexpr bool isAtLeast(int major, int minor = 0, int patch = 0) noexcept {
91  uint32_t current = asInteger();
92  uint32_t required = (static_cast<uint32_t>(major) << 16) |
93  (static_cast<uint32_t>(minor) << 8) | static_cast<uint32_t>(patch);
94  return current >= required;
95  }
96 };
97 
98 //==============================================================================
99 // Consolidated Platform Report
100 //==============================================================================
101 
114 
117 
120 
123 
126 
129 
145  std::string generateReport() const {
146  std::stringstream ss;
147 
148  ss << "TRLC Platform Detection Report v" << Version::STRING << "\n";
149  ss << std::string(60, '=') << "\n\n";
150 
151  // Compiler Information
152  ss << "COMPILER INFORMATION:\n";
153  ss << std::string(25, '-') << "\n";
154  ss << " Type: " << compiler.name << "\n";
155  ss << " Version: " << compiler.version.major << "." << compiler.version.minor
156  << "." << compiler.version.patch << "\n";
157  ss << " Builtin Attributes: " << (compiler.has_builtin_attribute ? "Yes" : "No") << "\n";
158  ss << " Inline Assembly: " << (compiler.has_inline_assembly ? "Yes" : "No") << "\n";
159  ss << " Color Diagnostics: " << (compiler.has_color_diagnostics ? "Yes" : "No") << "\n";
160  ss << " GCC Compatible: " << (compiler.isGccCompatible() ? "Yes" : "No") << "\n";
161  ss << " Clang Compatible: " << (compiler.isClangCompatible() ? "Yes" : "No") << "\n\n";
162 
163  // Platform Information
164  ss << "PLATFORM INFORMATION:\n";
165  ss << std::string(25, '-') << "\n";
166  ss << " Operating System: " << platform.os_name << "\n";
167  ss << " Kernel Family: " << platform.kernel_family << "\n";
168  ss << " Environment Type: ";
169  switch (platform.environment) {
171  ss << "Desktop";
172  break;
174  ss << "Server";
175  break;
177  ss << "Embedded";
178  break;
180  ss << "Mobile";
181  break;
182  default:
183  ss << "Unknown";
184  break;
185  }
186  ss << "\n";
187  ss << " POSIX API: " << (platform.isPosix() ? "Yes" : "No") << "\n";
188  ss << " Windows API: " << (platform.isWindows() ? "Yes" : "No") << "\n";
189  ss << " Case Sensitive FS: " << (supportsCaseSensitiveFilesystem() ? "Yes" : "No")
190  << "\n\n";
191 
192  // Architecture Information
193  ss << "ARCHITECTURE INFORMATION:\n";
194  ss << std::string(29, '-') << "\n";
195  ss << " CPU Architecture: " << architecture.arch_name << "\n";
196  ss << " Pointer Size: " << architecture.pointer_size_bits << " bits\n";
197  ss << " Byte Order: ";
198  switch (architecture.byte_order) {
200  ss << "Little Endian";
201  break;
203  ss << "Big Endian";
204  break;
206  ss << "Mixed Endian";
207  break;
208  default:
209  ss << "Unknown";
210  break;
211  }
212  ss << "\n";
213  ss << " Cache Line Size: " << architecture.cache_line_size << " bytes\n";
214  ss << " Unaligned Access: " << (architecture.supportsUnalignedAccess() ? "Yes" : "No")
215  << "\n";
216  ss << " SIMD Support: " << (hasSimdSupport() ? "Yes" : "No") << "\n\n";
217 
218  // C++ Standard Information
219  ss << "C++ STANDARD INFORMATION:\n";
220  ss << std::string(29, '-') << "\n";
221  ss << " Standard Version: " << cpp_standard.standard_name << "\n";
222  ss << " Version Macro: " << cpp_standard.version_macro << "\n";
223  ss << " Structured Bindings: " << (hasStructuredBindings() ? "Yes" : "No") << "\n";
224  ss << " If Constexpr: " << (hasIfConstexpr() ? "Yes" : "No") << "\n";
225  ss << " Concepts: " << (hasConcepts() ? "Yes" : "No") << "\n";
226  ss << " Coroutines: " << (hasCoroutines() ? "Yes" : "No") << "\n";
227  ss << " Modules: " << (hasModules() ? "Yes" : "No") << "\n";
228  ss << " Ranges: " << (hasRanges() ? "Yes" : "No") << "\n\n";
229 
230  // Feature Information
231  ss << "FEATURE AVAILABILITY:\n";
232  ss << std::string(25, '-') << "\n";
233  ss << " Exceptions: " << (features.has_exceptions ? "Yes" : "No") << "\n";
234  ss << " RTTI: " << (features.has_rtti ? "Yes" : "No") << "\n";
235  ss << " Threads: " << (features.has_threads ? "Yes" : "No") << "\n";
236  ss << " Atomic Operations: " << (features.has_atomic ? "Yes" : "No") << "\n";
237  ss << " Inline Assembly: " << (features.has_inline_asm ? "Yes" : "No") << "\n";
238  ss << " SSE Support: " << (features.has_sse ? "Yes" : "No") << "\n";
239  ss << " AVX Support: " << (features.has_avx ? "Yes" : "No") << "\n";
240  ss << " NEON Support: " << (features.has_neon ? "Yes" : "No") << "\n\n";
241 
242  // Endianness Information (using data from ArchitectureInfo)
243  ss << "ENDIANNESS INFORMATION:\n";
244  ss << std::string(27, '-') << "\n";
245  ss << " Byte Order: ";
246  switch (architecture.byte_order) {
248  ss << "Little Endian";
249  break;
251  ss << "Big Endian";
252  break;
254  ss << "Mixed Endian";
255  break;
256  default:
257  ss << "Unknown";
258  break;
259  }
260  ss << "\n";
261  ss << " Little Endian: " << (architecture.isLittleEndian() ? "Yes" : "No") << "\n";
262  ss << " Big Endian: "
264  ? "Yes"
265  : "No")
266  << "\n\n";
267 
268  // Debug Information (if available)
269 #ifdef TRLC_PLATFORM_ENABLE_DEBUG_UTILS
270  ss << "DEBUG INFORMATION:\n";
271  ss << std::string(22, '-') << "\n";
272  ss << " Debug Build: " << (isDebugBuild() ? "Yes" : "No") << "\n";
273  ss << " Release Build: " << (isReleaseBuild() ? "Yes" : "No") << "\n";
274  ss << " Debug Info: " << (hasDebugInfo() ? "Yes" : "No") << "\n";
275  ss << " Stack Trace: " << (DebugUtils::canCaptureStackTrace() ? "Yes" : "No")
276  << "\n\n";
277 #endif
278 
279  ss << std::string(60, '=') << "\n";
280  ss << "Report generated by TRLC Platform v" << Version::STRING << "\n";
281 
282  return ss.str();
283  }
284 
297  void printReport() const { std::cout << generateReport() << std::endl; }
298 
303  std::string getBriefSummary() const {
304  std::stringstream ss;
305  ss << compiler.name << " " << compiler.version.major << "." << compiler.version.minor
306  << " on " << platform.os_name << " " << architecture.arch_name << " ("
307  << architecture.pointer_size_bits << "-bit)";
308  return ss.str();
309  }
310 };
311 
312 //==============================================================================
313 // Core Platform Detection Function
314 //==============================================================================
315 
338  return PlatformReport{
339  getCompilerInfo(),
340  getPlatformInfo(),
343  getFeatureSet(),
344  getEndiannessInfo() // Now available from endianness.hpp
345  };
346 }
347 
348 //==============================================================================
349 // Runtime Feature Initialization
350 //==============================================================================
351 
352 namespace detail {
354 inline std::atomic<bool> g_platform_initialized{false};
355 inline std::atomic<bool> g_initialization_in_progress{false};
356 } // namespace detail
357 
383 inline void initializePlatform() noexcept {
384  // Check if already initialized
385  if (detail::g_platform_initialized.load(std::memory_order_acquire)) {
386  return;
387  }
388 
389  // Check if initialization is in progress (prevent recursive calls)
390  bool expected = false;
391  if (!detail::g_initialization_in_progress.compare_exchange_strong(
392  expected, true, std::memory_order_acq_rel)) {
393  // Another thread is initializing, wait for completion
394  while (!detail::g_platform_initialized.load(std::memory_order_acquire)) {
395  // Use standard thread yield instead of platform-specific intrinsics
396  std::this_thread::yield();
397  }
398  return;
399  }
400 
401  try {
402  // Perform runtime feature detection initialization
403  // Most features are compile-time, but some CPU features need runtime detection
404 
405  // Initialize any runtime feature detection here
406  // For now, most features are compile-time detected
407 
408  // Mark initialization complete
409  detail::g_platform_initialized.store(true, std::memory_order_release);
410 
411  } catch (...) {
412  // If initialization fails, reset the in-progress flag
413  detail::g_initialization_in_progress.store(false, std::memory_order_release);
414  // Don't propagate exceptions from initialization
415  return;
416  }
417 
418  // Clear the in-progress flag
419  detail::g_initialization_in_progress.store(false, std::memory_order_release);
420 }
421 
444 inline bool isPlatformInitialized() noexcept {
445  return detail::g_platform_initialized.load(std::memory_order_acquire);
446 }
447 
448 //==============================================================================
449 // Convenience Functions
450 //==============================================================================
451 
466 inline std::string getBriefPlatformSummary() {
467  auto report = getPlatformReport();
468  return report.getBriefSummary();
469 }
470 
482 inline void printPlatformReport() {
483  auto report = getPlatformReport();
484  report.printReport();
485 }
486 
487 } // namespace platform
488 } // namespace trlc
489 
490 //==============================================================================
491 // Master Include Guard and Compatibility Macros
492 //==============================================================================
493 
495 #define TRLC_PLATFORM_INCLUDED
496 
498 #define TRLC_PLATFORM_VERSION_MAJOR (trlc::platform::Version::MAJOR)
499 #define TRLC_PLATFORM_VERSION_MINOR (trlc::platform::Version::MINOR)
500 #define TRLC_PLATFORM_VERSION_PATCH (trlc::platform::Version::PATCH)
501 #define TRLC_PLATFORM_VERSION_STRING (trlc::platform::Version::STRING)
502 
504 #define TRLC_PLATFORM_VERSION_ATLEAST(major, minor, patch) \
505  (trlc::platform::Version::isAtLeast(major, minor, patch))
506 
507 //==============================================================================
508 // Compile-Time Validation
509 //==============================================================================
510 
511 // Verify that all required modules are available
512 static_assert(std::is_default_constructible_v<trlc::platform::CompilerInfo>,
513  "CompilerInfo must be default constructible");
514 static_assert(std::is_default_constructible_v<trlc::platform::PlatformInfo>,
515  "PlatformInfo must be default constructible");
516 static_assert(std::is_default_constructible_v<trlc::platform::ArchitectureInfo>,
517  "ArchitectureInfo must be default constructible");
518 static_assert(std::is_default_constructible_v<trlc::platform::CppStandardInfo>,
519  "CppStandardInfo must be default constructible");
520 static_assert(std::is_default_constructible_v<trlc::platform::FeatureSet>,
521  "FeatureSet must be default constructible");
522 
523 // Verify version information
524 static_assert(trlc::platform::Version::MAJOR >= 1, "Library major version must be at least 1");
525 static_assert(trlc::platform::Version::isAtLeast(1, 0, 0),
526  "Version comparison must work correctly");
527 
528 // Verify that the PlatformReport can be created
529 static_assert(std::is_default_constructible_v<trlc::platform::PlatformReport>,
530  "PlatformReport must be constructible");
531 
532 //==============================================================================
533 // Documentation Examples and Usage Notes
534 //==============================================================================
535 
536 /*
537 Example Usage:
538 
539 1. Basic platform detection:
540  #include "trlc/platform/core.hpp"
541 
542  constexpr auto platform = trlc::platform::getPlatformReport();
543  if constexpr (platform.compiler.type == trlc::platform::CompilerType::gcc) {
544  // GCC-specific code
545  }
546 
547 2. Runtime initialization for CPU features:
548  #include "trlc/platform/core.hpp"
549 
550  int main() {
551  trlc::platform::initializePlatform();
552 
553  if (trlc::platform::hasAvxSupport()) {
554  // Use AVX optimizations
555  }
556  }
557 
558 3. Platform reporting:
559  #include "trlc/platform/core.hpp"
560 
561  void logPlatformInfo() {
562  auto report = trlc::platform::getPlatformReport();
563  std::cout << report.generateReport() << std::endl;
564  }
565 
566 4. Brief platform summary:
567  #include "trlc/platform/core.hpp"
568 
569  std::string getPlatformSummary() {
570  return trlc::platform::getBriefPlatformSummary();
571  }
572 
573 5. Conditional compilation based on platform:
574  #include "trlc/platform/core.hpp"
575 
576  void optimizedFunction() {
577  constexpr auto platform = trlc::platform::getPlatformReport();
578 
579  if constexpr (platform.architecture.is64Bit() &&
580  platform.features.has_sse) {
581  // 64-bit SSE optimized path
582  } else {
583  // Generic fallback
584  }
585  }
586 
587 Thread Safety:
588 - All compile-time detection is inherently thread-safe
589 - Runtime initialization is thread-safe and idempotent
590 - Platform report generation is thread-safe
591 - Multiple threads can safely call all functions concurrently
592 
593 Performance:
594 - Compile-time detection has zero runtime overhead
595 - Runtime initialization has minimal one-time cost
596 - Report generation has moderate cost (string formatting)
597 - Brief summaries are lightweight for frequent use
598 
599 Integration:
600 - Header-only design requires no separate compilation
601 - All dependencies are standard library or self-contained
602 - Can be included in any C++17 or later project
603 - Works with all major compilers and platforms
604 */
CPU architecture and byte order detection for trlc-platform.
static bool canCaptureStackTrace()
Check if stack trace capture is supported.
Definition: debug.hpp:344
Compiler detection and capability utilities.
C++ standard version and feature detection for trlc-platform.
Portable debug and assertion utilities.
Byte order detection and utilities for cross-platform development.
Language and runtime feature detection utilities.
Portable cross-platform macros for common C++ idioms and optimizations.
std::atomic< bool > g_platform_initialized
Internal initialization state tracking (inline to avoid ODR violations)
Definition: core.hpp:354
std::atomic< bool > g_initialization_in_progress
Definition: core.hpp:355
@ mixed_endian
Mixed endianness (rare architectures)
@ little_endian
Little-endian (LSB first)
@ big_endian
Big-endian (MSB first)
static bool isReleaseBuild()
Check if the current build is a release build.
Definition: debug.hpp:105
static EndiannessInfo getEndiannessInfo()
Get comprehensive endianness information.
Definition: endianness.hpp:178
static bool hasSimdSupport()
static bool hasStructuredBindings()
Checks if structured bindings are supported (C++17)
static bool hasRanges()
Checks if ranges library is supported (C++20)
@ mobile
Mobile device environment.
@ embedded
Embedded system environment.
@ desktop
Desktop/workstation environment.
@ server
Server environment.
static bool hasDebugInfo()
Check if debug information is available.
Definition: debug.hpp:121
PlatformReport getPlatformReport()
Definition: core.hpp:337
static ArchitectureInfo getArchitectureInfo()
static bool hasConcepts()
Checks if concepts are supported (C++20)
std::string getBriefPlatformSummary()
Definition: core.hpp:466
static CppStandardInfo getCppStandardInfo()
Gets comprehensive C++ standard information.
static bool hasModules()
Checks if modules are supported (C++20)
static FeatureSet getFeatureSet()
Gets a complete feature set with all detected features.
Definition: features.hpp:553
static CompilerInfo getCompilerInfo()
Get consolidated compiler information.
Definition: compiler.hpp:517
static PlatformInfo getPlatformInfo()
Gets comprehensive platform information.
Definition: platform.hpp:172
static bool isDebugBuild()
Definition: debug.hpp:87
static bool hasIfConstexpr()
Checks if if constexpr is supported (C++17)
bool isPlatformInitialized()
Definition: core.hpp:444
static bool supportsCaseSensitiveFilesystem()
Checks if the platform supports case-sensitive filesystems.
Definition: platform.hpp:291
static bool hasCoroutines()
Checks if coroutines are supported (C++20)
void initializePlatform()
Definition: core.hpp:383
void printPlatformReport()
Definition: core.hpp:482
Operating system and environment detection utilities.
size_t cache_line_size
Typical cache line size in bytes.
ByteOrder byte_order
Detected byte order (endianness)
int pointer_size_bits
Pointer size in bits (32 or 64)
static bool supportsUnalignedAccess() const
Checks if the architecture supports unaligned memory access.
const char * arch_name
Human-readable architecture name.
static bool isLittleEndian() const
Checks if the architecture uses little-endian byte order.
Consolidated compiler information structure.
Definition: compiler.hpp:472
bool has_inline_assembly
Supports inline assembly.
Definition: compiler.hpp:477
const char * name
Human-readable compiler name.
Definition: compiler.hpp:475
static bool isGccCompatible() const
Check if this is a GCC-compatible compiler.
Definition: compiler.hpp:493
bool has_builtin_attribute
Supports __has_builtin attributes.
Definition: compiler.hpp:476
CompilerVersion version
Compiler version information.
Definition: compiler.hpp:474
bool has_color_diagnostics
Supports colored diagnostic output.
Definition: compiler.hpp:478
static bool isClangCompatible() const
Check if this is a Clang-compatible compiler.
Definition: compiler.hpp:502
int minor
Minor version number (e.g., 3 in GCC 9.3.0)
Definition: compiler.hpp:177
int patch
Patch version number (e.g., 0 in GCC 9.3.0)
Definition: compiler.hpp:178
int major
Major version number (e.g., 9 in GCC 9.3.0)
Definition: compiler.hpp:176
C++ standard information structure.
const char * standard_name
Human-readable standard name.
long version_macro
Value of __cplusplus macro.
Comprehensive endianness information structure.
Definition: endianness.hpp:46
Feature detection structure.
Definition: features.hpp:91
bool has_inline_asm
Inline assembly available.
Definition: features.hpp:97
bool has_rtti
RTTI available.
Definition: features.hpp:94
bool has_exceptions
Exception handling available.
Definition: features.hpp:93
bool has_sse
SSE support.
Definition: features.hpp:106
bool has_neon
ARM NEON support.
Definition: features.hpp:114
bool has_threads
Threading support available.
Definition: features.hpp:95
bool has_avx
AVX support.
Definition: features.hpp:111
bool has_atomic
Atomic operations available.
Definition: features.hpp:96
Platform information structure.
Definition: platform.hpp:55
static bool isPosix() const
Checks if the platform supports POSIX APIs.
Definition: platform.hpp:65
EnvironmentType environment
Detected environment type.
Definition: platform.hpp:57
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
Consolidated platform information structure.
Definition: core.hpp:111
FeatureSet features
Language and runtime feature availability.
Definition: core.hpp:125
EndiannessInfo endianness
Endianness information (byte order, utilities)
Definition: core.hpp:128
std::string getBriefSummary() const
Get a brief one-line summary of the platform.
Definition: core.hpp:303
PlatformInfo platform
Platform/OS information (type, environment, API support)
Definition: core.hpp:116
ArchitectureInfo architecture
CPU architecture information (type, endianness, capabilities)
Definition: core.hpp:119
std::string generateReport() const
Definition: core.hpp:145
CompilerInfo compiler
Compiler information (type, version, capabilities)
Definition: core.hpp:113
CppStandardInfo cpp_standard
C++ standard information (version, feature support)
Definition: core.hpp:122
Library version information.
Definition: core.hpp:61
static static bool isAtLeast(int major, int minor=0, int patch=0)
Check if this version is at least the specified version.
Definition: core.hpp:90
static static int MINOR
Minor version number (feature additions)
Definition: core.hpp:66
static static int MAJOR
Major version number (breaking changes)
Definition: core.hpp:63
static static int PATCH
Patch version number (bug fixes)
Definition: core.hpp:69
static static const char * STRING
Version string in semantic versioning format.
Definition: core.hpp:72
static static uint32_t asInteger()
Get the version as a 32-bit integer.
Definition: core.hpp:78
Compile-time type and alignment information utilities.