TRLC Platform Library  1.0.0
Header-only C++ library for compile-time platform detection and abstraction
traits.hpp
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <cstddef>
17 #include <type_traits>
18 
20 
21 namespace trlc {
22 namespace platform {
23 
24 // =============================================================================
25 // Template Specializations for hasFeature
26 // =============================================================================
27 
34 template <>
35 constexpr bool hasFeature<LanguageFeature::exceptions>() noexcept {
36  return hasExceptions();
37 }
38 
40 template <>
41 constexpr bool hasFeature<LanguageFeature::rtti>() noexcept {
42  return hasRtti();
43 }
44 
46 template <>
47 constexpr bool hasFeature<LanguageFeature::threads>() noexcept {
48  return hasThreads();
49 }
50 
52 template <>
53 constexpr bool hasFeature<LanguageFeature::atomic_operations>() noexcept {
54  return hasAtomicOperations();
55 }
56 
58 template <>
59 constexpr bool hasFeature<LanguageFeature::inline_assembly>() noexcept {
60  return hasInlineAssembly();
61 }
62 
64 template <>
65 constexpr bool hasFeature<LanguageFeature::vector_intrinsics>() noexcept {
66  return hasVectorIntrinsics();
67 }
68 
70 template <>
71 constexpr bool hasFeature<LanguageFeature::stack_protection>() noexcept {
72  return hasStackProtection();
73 }
74 
76 template <>
77 constexpr bool hasFeature<LanguageFeature::address_sanitizer>() noexcept {
78  return hasAddressSanitizer();
79 }
80 
82 template <>
83 constexpr bool hasFeature<LanguageFeature::thread_sanitizer>() noexcept {
84  return hasThreadSanitizer();
85 }
86 
88 template <>
89 constexpr bool hasFeature<LanguageFeature::memory_sanitizer>() noexcept {
90  return hasMemorySanitizer();
91 }
92 
93 // =============================================================================
94 // Runtime Feature Template Functions
95 // =============================================================================
96 
102 template <RuntimeFeature TFeature>
103 bool hasRuntimeFeature() noexcept {
104  return getFeatureSet().hasRuntimeFeature(TFeature);
105 }
106 
107 // Specializations for common runtime features (for better optimization)
108 template <>
109 inline bool hasRuntimeFeature<RuntimeFeature::sse>() noexcept {
110  return hasSseSupport();
111 }
112 
113 template <>
114 inline bool hasRuntimeFeature<RuntimeFeature::sse2>() noexcept {
115  return hasSse2Support();
116 }
117 
118 template <>
119 inline bool hasRuntimeFeature<RuntimeFeature::sse3>() noexcept {
120  return hasSse3Support();
121 }
122 
123 template <>
124 inline bool hasRuntimeFeature<RuntimeFeature::sse4_1>() noexcept {
125  return hasSse41Support();
126 }
127 
128 template <>
129 inline bool hasRuntimeFeature<RuntimeFeature::sse4_2>() noexcept {
130  return hasSse42Support();
131 }
132 
133 template <>
134 inline bool hasRuntimeFeature<RuntimeFeature::avx>() noexcept {
135  return hasAvxSupport();
136 }
137 
138 template <>
139 inline bool hasRuntimeFeature<RuntimeFeature::avx2>() noexcept {
140  return hasAvx2Support();
141 }
142 
143 template <>
144 inline bool hasRuntimeFeature<RuntimeFeature::avx512f>() noexcept {
145  return hasAvx512fSupport();
146 }
147 
148 template <>
149 inline bool hasRuntimeFeature<RuntimeFeature::neon>() noexcept {
150  return hasNeonSupport();
151 }
152 
153 namespace traits {
154 
155 // =============================================================================
156 // C++17 Compatibility Helpers
157 // =============================================================================
158 
159 #if __cplusplus >= 201703L
160 // C++17 has these in std
161 template <bool B, class T = void>
162 using enable_if_t = typename std::enable_if<B, T>::type;
163 
164 template <bool B>
165 using bool_constant = std::integral_constant<bool, B>;
166 #else
167 // C++14 compatibility
168 template <bool B, class T = void>
169 using enable_if_t = typename std::enable_if<B, T>::type;
170 
171 template <bool B>
172 using bool_constant = std::integral_constant<bool, B>;
173 #endif
174 
175 // =============================================================================
176 // SFINAE Helpers for Template Constraints
177 // =============================================================================
178 
183 template <LanguageFeature TFeature>
185 
190 template <LanguageFeature TFeature>
192 
197 template <LanguageFeature... Features>
199 
204 template <LanguageFeature... Features>
206 
207 // =============================================================================
208 // Variadic Template Functions for Multi-Feature Requirements
209 // =============================================================================
210 
216 template <LanguageFeature... Features>
217 constexpr bool hasAllFeatures() noexcept {
218  return (hasFeature<Features>() && ...);
219 }
220 
226 template <LanguageFeature... Features>
227 constexpr bool hasAnyFeature() noexcept {
228  return (hasFeature<Features>() || ...);
229 }
230 
236 template <LanguageFeature... Features>
237 constexpr std::size_t countAvailableFeatures() noexcept {
238  return (static_cast<std::size_t>(hasFeature<Features>()) + ...);
239 }
240 
241 // =============================================================================
242 // Type Traits for Feature Detection
243 // =============================================================================
244 
249 template <LanguageFeature TFeature>
250 struct FeatureAvailable : bool_constant<hasFeature<TFeature>()> {};
251 
256 template <LanguageFeature TFeature>
258 
263 template <LanguageFeature... Features>
264 struct AllFeaturesAvailable : bool_constant<hasAllFeatures<Features...>()> {};
265 
270 template <LanguageFeature... Features>
271 constexpr bool all_features_available_v = AllFeaturesAvailable<Features...>::value;
272 
277 template <LanguageFeature... Features>
278 struct AnyFeatureAvailable : bool_constant<hasAnyFeature<Features...>()> {};
279 
284 template <LanguageFeature... Features>
285 constexpr bool any_feature_available_v = AnyFeatureAvailable<Features...>::value;
286 
287 // =============================================================================
288 // Runtime Feature Type Traits
289 // =============================================================================
290 
295 template <RuntimeFeature TFeature>
297  static bool value() noexcept { return hasRuntimeFeature<TFeature>(); }
298 };
299 
300 // =============================================================================
301 // Compile-Time Feature Constants
302 // =============================================================================
303 
309 template <LanguageFeature TFeature>
310 constexpr bool getFeatureConstant() noexcept {
311  return hasFeature<TFeature>();
312 }
313 
318 template <LanguageFeature TFeature>
319 constexpr bool feature_constant_v = getFeatureConstant<TFeature>();
320 
321 } // namespace traits
322 } // namespace platform
323 } // namespace trlc
324 
325 // =============================================================================
326 // Convenience Macros for Advanced Feature Testing
327 // =============================================================================
328 
333 #define TRLC_HAS_LANGUAGE_FEATURE(feature) \
334  (trlc::platform::hasFeature<trlc::platform::LanguageFeature::feature>())
335 
340 #define TRLC_HAS_RUNTIME_FEATURE(feature) \
341  (trlc::platform::hasRuntimeFeature<trlc::platform::RuntimeFeature::feature>())
342 
347 #define TRLC_IF_LANGUAGE_FEATURE(feature) \
348  if constexpr (trlc::platform::hasFeature<trlc::platform::LanguageFeature::feature>())
349 
354 #define TRLC_IF_NO_LANGUAGE_FEATURE(feature) \
355  if constexpr (!trlc::platform::hasFeature<trlc::platform::LanguageFeature::feature>())
356 
361 #define TRLC_REQUIRE_FEATURE(feature) \
362  template <typename = trlc::platform::traits::enable_if_feature_t< \
363  trlc::platform::LanguageFeature::feature>>
364 
369 #define TRLC_REQUIRE_NO_FEATURE(feature) \
370  template <typename = trlc::platform::traits::enable_if_no_feature_t< \
371  trlc::platform::LanguageFeature::feature>>
372 
377 #define TRLC_HAS_ALL_FEATURES(...) (trlc::platform::traits::hasAllFeatures<__VA_ARGS__>())
378 
383 #define TRLC_HAS_ANY_FEATURES(...) (trlc::platform::traits::hasAnyFeature<__VA_ARGS__>())
Language and runtime feature detection utilities.
enable_if_t<!hasFeature< TFeature >()> enable_if_no_feature_t
SFINAE helper to enable template only if feature is NOT available.
Definition: traits.hpp:191
static bool all_features_available_v
Variable template for AllFeaturesAvailable.
Definition: traits.hpp:271
static bool feature_constant_v
Template variable for feature constants (C++14+)
Definition: traits.hpp:319
static std::size_t countAvailableFeatures()
Count how many of the specified features are available.
Definition: traits.hpp:237
std::integral_constant< bool, B > bool_constant
Definition: traits.hpp:165
static bool hasAnyFeature()
Check if ANY of the specified features are available.
Definition: traits.hpp:227
typename std::enable_if< B, T >::type enable_if_t
Definition: traits.hpp:162
enable_if_t<(hasFeature< Features >() &&...)> enable_if_all_features_t
SFINAE helper to enable template only if ALL features are available.
Definition: traits.hpp:198
static bool any_feature_available_v
Variable template for AnyFeatureAvailable.
Definition: traits.hpp:285
static bool hasAllFeatures()
Check if ALL specified features are available.
Definition: traits.hpp:217
static bool feature_available_v
Variable template for FeatureAvailable (C++14+)
Definition: traits.hpp:257
enable_if_t< hasFeature< TFeature >()> enable_if_feature_t
SFINAE helper to enable template only if feature is available.
Definition: traits.hpp:184
enable_if_t<(hasFeature< Features >()||...)> enable_if_any_feature_t
SFINAE helper to enable template only if ANY feature is available.
Definition: traits.hpp:205
static bool getFeatureConstant()
Get compile-time constant for a language feature.
Definition: traits.hpp:310
bool hasSseSupport()
Detects SSE support at runtime.
Definition: features.hpp:408
bool hasAvx512fSupport()
Detects AVX-512F support at runtime.
Definition: features.hpp:492
static bool hasVectorIntrinsics()
Detects if vector intrinsics are available.
Definition: features.hpp:274
static bool hasAddressSanitizer()
Detects if AddressSanitizer is enabled.
Definition: features.hpp:298
static bool hasRtti()
Detects if RTTI (Runtime Type Information) is enabled.
Definition: features.hpp:210
static bool hasThreads()
Detects if threading support is available.
Definition: features.hpp:230
static bool hasInlineAssembly()
Detects if inline assembly is supported.
Definition: features.hpp:260
bool hasSse2Support()
Detects SSE2 support at runtime.
Definition: features.hpp:420
static bool hasExceptions()
Detects if C++ exceptions are enabled.
Definition: features.hpp:196
bool hasAvx2Support()
Detects AVX2 support at runtime.
Definition: features.hpp:480
bool hasSse3Support()
Detects SSE3 support at runtime.
Definition: features.hpp:432
LanguageFeature
Language feature enumeration.
Definition: features.hpp:49
static bool hasStackProtection()
Detects if stack protection is enabled.
Definition: features.hpp:282
static FeatureSet getFeatureSet()
Gets a complete feature set with all detected features.
Definition: features.hpp:553
bool hasSse41Support()
Detects SSE4.1 support at runtime.
Definition: features.hpp:444
bool hasSse42Support()
Detects SSE4.2 support at runtime.
Definition: features.hpp:456
bool hasNeonSupport()
Detects ARM NEON support.
Definition: features.hpp:504
bool hasAvxSupport()
Detects AVX support at runtime.
Definition: features.hpp:468
static bool hasMemorySanitizer()
Detects if MemorySanitizer is enabled.
Definition: features.hpp:332
static bool hasThreadSanitizer()
Detects if ThreadSanitizer is enabled.
Definition: features.hpp:315
bool hasRuntimeFeature(RuntimeFeature feature)
Checks if a specific runtime feature is available.
Definition: features.hpp:623
static bool hasAtomicOperations()
Detects if atomic operations are supported.
Definition: features.hpp:244
bool hasRuntimeFeature(RuntimeFeature feature) const
Checks if a specific runtime feature is available.
Definition: features.hpp:158
Type trait to check if ALL features are available.
Definition: traits.hpp:264
Type trait to check if ANY feature is available.
Definition: traits.hpp:278
Type trait to check if a language feature is available.
Definition: traits.hpp:250
Type trait for runtime feature availability (non-constexpr)
Definition: traits.hpp:296