-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportable_library_example.cpp
More file actions
170 lines (143 loc) · 8 KB
/
portable_library_example.cpp
File metadata and controls
170 lines (143 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @file portable_library_demo.cpp
* @brief Comprehensive demonstration of TRLC Platform Library usage
*/
#include "trlc/platform/core.hpp"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace trlc::platform;
int main() {
std::cout << "🚀 TRLC Platform Demo - Using API instead of traditional macros" << std::endl;
std::cout << "=================================================================" << std::endl;
// Initialize platform
initializePlatform();
// Demo 1: Platform detection using TRLC APIs
std::cout << "\n=== Platform Detection Demo ===" << std::endl;
std::cout << "Using TRLC Platform API instead of traditional macros:" << std::endl;
std::cout << std::string(60, '-') << std::endl;
// Traditional way: #ifdef _WIN32, #ifdef __linux__, etc.
// TRLC way: Use API functions
constexpr auto os = getOperatingSystem();
constexpr auto compiler = getCompilerType();
constexpr auto arch = getCpuArchitecture();
std::cout << "Operating System: ";
if constexpr (os == OperatingSystem::windows) {
std::cout << "Windows" << std::endl;
} else if constexpr (os == OperatingSystem::linux_generic) {
std::cout << "Linux" << std::endl;
} else if constexpr (os == OperatingSystem::macos) {
std::cout << "macOS" << std::endl;
} else {
std::cout << "Other Unix-like" << std::endl;
}
std::cout << "Compiler: ";
if constexpr (compiler == CompilerType::gcc) {
std::cout << "GCC" << std::endl;
} else if constexpr (compiler == CompilerType::clang) {
std::cout << "Clang" << std::endl;
} else if constexpr (compiler == CompilerType::msvc) {
std::cout << "MSVC" << std::endl;
} else {
std::cout << "Other" << std::endl;
}
std::cout << "Architecture: ";
if constexpr (arch == CpuArchitecture::x86_64) {
std::cout << "x86-64" << std::endl;
} else if constexpr (arch == CpuArchitecture::arm_v8_64) {
std::cout << "ARM64" << std::endl;
} else {
std::cout << "Other" << std::endl;
}
std::cout << "Pointer size: " << getPointerSize() << " bits" << std::endl;
std::cout << "Endianness: " << (isLittleEndian() ? "Little" : "Big") << " Endian" << std::endl;
// Demo 2: Feature detection using TRLC APIs
std::cout << "\n=== Feature Detection Demo ===" << std::endl;
std::cout << "Using TRLC Platform feature detection instead of compiler macros:" << std::endl;
std::cout << std::string(60, '-') << std::endl;
// Traditional way: #ifdef __cpp_exceptions, #ifdef _OPENMP, etc.
// TRLC way: Use template-based feature detection
std::cout << "Language Features:" << std::endl;
std::cout << " Exceptions: " << (hasFeature<LanguageFeature::exceptions>() ? "✓" : "✗") << std::endl;
std::cout << " RTTI: " << (hasFeature<LanguageFeature::rtti>() ? "✓" : "✗") << std::endl;
std::cout << " Threads: " << (hasFeature<LanguageFeature::threads>() ? "✓" : "✗") << std::endl;
std::cout << " Atomic: " << (hasFeature<LanguageFeature::atomic_operations>() ? "✓" : "✗") << std::endl;
std::cout << "\nSIMD Features:" << std::endl;
std::cout << " SSE: " << (hasRuntimeFeature(RuntimeFeature::sse) ? "✓" : "✗") << std::endl;
std::cout << " AVX: " << (hasRuntimeFeature(RuntimeFeature::avx) ? "✓" : "✗") << std::endl;
std::cout << " NEON: " << (hasRuntimeFeature(RuntimeFeature::neon) ? "✓" : "✗") << std::endl;
std::cout << "\nCompiler Features:" << std::endl;
std::cout << " Builtin Functions: " << ((getCompilerType() == CompilerType::gcc || getCompilerType() == CompilerType::clang) ? "✓" : "✗") << std::endl;
std::cout << " Inline Assembly: " << ((getCompilerType() == CompilerType::gcc || getCompilerType() == CompilerType::clang) ? "✓" : "✗") << std::endl;
std::cout << " Attributes: " << ((getCompilerType() == CompilerType::gcc || getCompilerType() == CompilerType::clang) ? "✓" : "✗") << std::endl;
// Demo 3: Compiler-specific optimizations using TRLC APIs
std::cout << "\n=== Compiler Optimizations Demo ===" << std::endl;
std::cout << "Using TRLC Platform API for compiler-specific optimizations:" << std::endl;
std::cout << std::string(60, '-') << std::endl;
// Traditional way: #if defined(__GNUC__) || defined(__clang__)
// TRLC way: Use compiler detection
unsigned int test_value = 0b11010110101011010110101101011010;
std::cout << "Test value: 0x" << std::hex << test_value << std::dec << std::endl;
// Population count using TRLC Platform API
unsigned int popcount = 0;
// Use TRLC Platform API for compiler detection instead of preprocessor
if constexpr (getCompilerType() == CompilerType::gcc || getCompilerType() == CompilerType::clang) {
// Use software fallback for better compatibility
unsigned int x = test_value;
while (x) {
popcount += x & 1;
x >>= 1;
}
std::cout << "Population count (GCC/Clang - software implementation): " << popcount << std::endl;
} else if constexpr (getCompilerType() == CompilerType::msvc) {
// Use software fallback for better compatibility
unsigned int x = test_value;
while (x) {
popcount += x & 1;
x >>= 1;
}
std::cout << "Population count (MSVC - software implementation): " << popcount << std::endl;
} else {
// Fallback implementation for other compilers
unsigned int x = test_value;
while (x) {
popcount += x & 1;
x >>= 1;
}
std::cout << "Population count (fallback): " << popcount << std::endl;
}
// Demo 4: Debug utilities
std::cout << "\n=== Debug Utilities Demo ===" << std::endl;
std::cout << "Using TRLC Platform debug API instead of traditional macros:" << std::endl;
std::cout << std::string(60, '-') << std::endl;
#ifdef TRLC_PLATFORM_ENABLE_DEBUG_UTILS
std::cout << "Debug build: " << (isDebugBuild() ? "Yes" : "No") << std::endl;
std::cout << "Release build: " << (isReleaseBuild() ? "Yes" : "No") << std::endl;
std::cout << "Debug info available: " << (hasDebugInfo() ? "Yes" : "No") << std::endl;
#else
std::cout << "Debug utilities not enabled in this build" << std::endl;
std::cout << "Enable with -DTRLC_PLATFORM_ENABLE_DEBUG_UTILS=1 or Debug CMake build" << std::endl;
#endif
// Generate comprehensive platform report
std::cout << "\n=== Complete Platform Report ===" << std::endl;
std::cout << "Generated using TRLC Platform comprehensive reporting:" << std::endl;
std::cout << std::string(60, '-') << std::endl;
auto report = getPlatformReport();
std::cout << report.getBriefSummary() << std::endl;
std::cout << "\n✅ Demo completed!" << std::endl;
std::cout << "\nTraditional approach vs TRLC Platform approach:" << std::endl;
std::cout << "❌ Traditional: #if defined(__GNUC__) || defined(__clang__)" << std::endl;
std::cout << "✅ TRLC: if constexpr (getCompilerType() == CompilerType::gcc)" << std::endl;
std::cout << "\n❌ Traditional: #ifdef _WIN32 / #ifdef __linux__" << std::endl;
std::cout << "✅ TRLC: if constexpr (getOperatingSystem() == OperatingSystem::windows)" << std::endl;
std::cout << "\n❌ Traditional: #ifdef __cpp_exceptions" << std::endl;
std::cout << "✅ TRLC: if constexpr (hasFeature<LanguageFeature::exceptions>())" << std::endl;
std::cout << "\nAdvantages of TRLC Platform approach:" << std::endl;
std::cout << " ✓ Type-safe compile-time decisions" << std::endl;
std::cout << " ✓ IDE-friendly with auto-completion" << std::endl;
std::cout << " ✓ Template-based feature detection" << std::endl;
std::cout << " ✓ Consistent API across all platforms" << std::endl;
std::cout << " ✓ Zero runtime overhead (constexpr evaluation)" << std::endl;
std::cout << " ✓ Compile-time verification of platform assumptions" << std::endl;
return 0;
}