ugmisc 0.2
Miscellaneous C++ header library
Loading...
Searching...
No Matches
features.hpp
Go to the documentation of this file.
1/*
2 * SPDX-Licence-Identifier: MIT
3 *
4 * Copyright 2025 Larry Chips
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the “Software”), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
125
126#ifdef UGMISC_DOCS
127#error "UGMISC_DOCS must be defined for documenting, not compiling."
128#endif
129
130#ifdef UGMISC_DOCS
141#define UGMISC_PREDEFS_HEADER "quoted_header_name"
142#endif
143#ifdef UGMISC_PREDEFS_HEADER
144# include UGMISC_PREDEFS_HEADER
145#endif
146
147
148
149
150/*
151 * I don't want to remember the proper version number everywhere.
152 */
153
154#if __cplusplus >= 202002L
155# define UGMISC_CXX_20 __cplusplus
156#else
157# undef UGMISC_CXX_20
158#endif
159
160#if __cplusplus < 201703L
161# error "features.hpp doesn't bother to work with less than C++17."
162#endif
163
164#ifdef UGMISC_DOCS
173#define UGMISC_HAVE_VERSION
174#endif
175#ifdef __has_include
176# if __has_include(<version>)
177# include <version>
178# endif
179#elif defined(UGMISC_HAVE_VERSION)
180# include <version>
181#endif
182
183#ifdef __has_include
184# define UGMISC_HAS_INCLUDE_OR_IGNORE(...) __has_include(__VA_ARGS__)
185#else
186# define UGMISC_HAS_INCLUDE_OR_IGNORE(...)
187#endif
188
189
190
191
192#ifndef UGMISC_DOCS
193/*
194 * These exist for documentation purposes.
195 * See doc/default_Doxyfile.in: PREDEFINED.
196 *
197 * They allow the feature flags to be defined as true or false at compile time,
198 * but as "compiler and library dependent" in the documentation.
199 */
200#define UGMISC_FEATURE_TRUE true
201#define UGMISC_FEATURE_FALSE false
202#endif
203
204
205
206namespace ugmisc {
207
208
209
210
243
244struct Features {
245 /*
246 * Language features.
247 */
248
249#ifdef UGMISC_DOCS
260# define UGMISC_HAVE_CONSTEXPR_DESTRUCTORS
267# define UGMISC_NO_CONSTEXPR_DESTRUCTORS
268#endif
283#if defined(UGMISC_HAVE_CONSTEXPR_DESTRUCTORS) && defined(UGMISC_NO_CONSTEXPR_DESTRUCTORS)
284# error "Predefines include HAVE_CONSTEXPR_DESTRUCTORS and NO_CONSTEXPR_DESCTRUCTORS."
285#endif
286#if !defined(UGMISC_HAVE_CONSTEXPR_DESTRUCTORS) && !defined(UGMISC_NO_CONSTEXPR_DESTRUCTORS)
287# ifdef UGMISC_CXX_20
288# define UGMISC_HAVE_CONSTEXPR_DESCTRUCTORS
289# else
290# undef UGMISC_HAVE_CONSTEXPR_DESTRUCTORS
291# endif
292#endif
293#ifdef UGMISC_HAVE_CONSTEXPR_DESTRUCTORS
294 static constexpr bool constexpr_destructors = UGMISC_FEATURE_TRUE;
295#else
296 static constexpr bool constexpr_destructors = UGMISC_FEATURE_FALSE;
297#endif
298
299
300
301
302#ifdef UGMISC_DOCS
307# define UGMISC_HAVE_CONSTEVAL
308#endif
309#if __cpp_consteval
310# define UGMISC_HAVE_CONSTEVAL
311 static constexpr bool consteval_functions = UGMISC_FEATURE_TRUE;
312#else
313# undef UGMISC_HAVE_CONSTEVAL
318 static constexpr bool consteval_functions = UGMISC_FEATURE_FALSE;
319#endif
320
321
322
323
324#if UGMISC_DOCS
327# define UGMISC_HAVE_CONSTEVAL_IF
328#endif
333#if __cpp_consteval >= 202211L
334# define UGMISC_HAVE_CONSTEVAL_IF
335 static constexpr bool consteval_if = UGMISC_FEATURE_TRUE;
336#else
337# undef UGMISC_HAVE_CONSTEVAL_IF
338 static constexpr bool consteval_if = UGMISC_FEATURE_FALSE;
339#endif
340
341
342
343
344 /*
345 * Library features.
346 */
347
348
349
350
351#if UGMISC_DOCS
356# define UGMISC_HAVE_BIT_CAST
357#endif
364#if __cpp_lib_bit_cast
365# define UGMISC_HAVE_BIT_CAST
366 static constexpr bool bit_cast = UGMISC_FEATURE_TRUE;
367#else
368# undef UGMISC_HAVE_BIT_CAST
369 static constexpr bool bit_cast = UGMISC_FEATURE_FALSE;
370#endif
371
372
373
374
375#if UGMISC_DOCS
380#define UGMISC_HAVE_BITOPS
381#endif
382#if __cpp_lib_bitops
383# define UGMISC_HAVE_BITOPS
384 static constexpr bool bitops = UGMISC_FEATURE_TRUE;
385#else
386# undef UGMISC_HAVE_BITOPS
391 static constexpr bool bitops = UGMISC_FEATURE_FALSE;
392#endif
393
394
395
396
397#ifdef UGMISC_DOCS
402# define UGMISC_HAVE_CONSTEXPR_SWAP_ALGORITHMS
403#endif
404#if __cpp_lib_constexpr_swap_algorithms
405# define UGMISC_HAVE_CONSTEXPR_SWAP_ALGORITHMS
406 static constexpr bool constexpr_swap_algorithms = UGMISC_FEATURE_TRUE;
407#else
408# undef UGMISC_HAVE_CONSTEXPR_SWAP_ALGORITHMS
413 static constexpr bool constexpr_swap_algorithms = UGMISC_FEATURE_FALSE;
414#endif
415
416
417
418
419#ifdef UGMISC_DOCS
424# define UGMISC_HAVE_UNREACHABLE
425#endif
426#if __cpp_lib_unreachable
427# define UGMISC_HAVE_UNREACHABLE
428# define UGMISC_UNREACHABLE std::unreachable();
429 static constexpr bool unreachable = UGMISC_FEATURE_TRUE;
430#else
435 static constexpr bool unreachable = UGMISC_FEATURE_FALSE;
436# undef UGMISC_HAVE_UNREACHABLE
437#endif
438}; // struct Features
439
440
441} /* ::ugmisc */
442
443
444
445
446#ifdef UGMISC_HAVE_UNREACHABLE
447# define UGMISC_unreachable std::unreachable
448# include <utility>
449#else
450# define UGMISC_unreachable ::ugmisc::unreachable
451[[noreturn]] inline void unreachable() { }
452#endif
Definition features.hpp:244
static constexpr bool bit_cast
Definition features.hpp:369
static constexpr bool consteval_if
Definition features.hpp:338
static constexpr bool constexpr_destructors
Definition features.hpp:294
static constexpr bool bitops
Definition features.hpp:391
static constexpr bool unreachable
Definition features.hpp:435
static constexpr bool consteval_functions
Definition features.hpp:318
static constexpr bool constexpr_swap_algorithms
Definition features.hpp:413