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
121
122#ifdef UGMISC_DOCS
123#error "UGMISC_DOCS must be defined for documenting, not compiling."
124#endif
125
126#ifdef UGMISC_DOCS
137#define UGMISC_PREDEFS_HEADER "quoted_header_name"
138#endif
139#ifdef UGMISC_PREDEFS_HEADER
140# include UGMISC_PREDEFS_HEADER
141#endif
142
143
144
145
146/*
147 * I don't want to remember the proper version number everywhere.
148 */
149
150#if __cplusplus >= 202002L
151# define UGMISC_CXX_20 __cplusplus
152#else
153# undef UGMISC_CXX_20
154#endif
155
156#if __cplusplus < 201703L
157# error "features.hpp doesn't bother to work with less than C++17."
158#endif
159
160#ifdef UGMISC_DOCS
169#define UGMISC_HAVE_VERSION
170#endif
171#ifdef __has_include
172# if __has_include(<version>)
173# include <version>
174# endif
175#elif defined(UGMISC_HAVE_VERSION)
176# include <version>
177#endif
178
179#ifdef __has_include
180# define UGMISC_HAS_INCLUDE_OR_IGNORE(...) __has_include(__VA_ARGS__)
181#else
182# define UGMISC_HAS_INCLUDE_OR_IGNORE(...)
183#endif
184
185
186
187
188#ifndef UGMISC_DOCS
189/*
190 * These exist for documentation purposes.
191 * See doc/default_Doxyfile.in: PREDEFINED.
192 *
193 * They allow the feature flags to be defined as true or false at compile time,
194 * but as "compiler and library dependent" in the documentation.
195 */
196#define UGMISC_FEATURE_TRUE true
197#define UGMISC_FEATURE_FALSE false
198#endif
199
200
201
202namespace ugmisc {
203
204
205
206
239
240struct Features {
241 /*
242 * Language features.
243 */
244
245#ifdef UGMISC_DOCS
256# define UGMISC_HAVE_CONSTEXPR_DESTRUCTORS
263# define UGMISC_NO_CONSTEXPR_DESTRUCTORS
264#endif
279#if defined(UGMISC_HAVE_CONSTEXPR_DESTRUCTORS) && defined(UGMISC_NO_CONSTEXPR_DESTRUCTORS)
280# error "Predefines include HAVE_CONSTEXPR_DESTRUCTORS and NO_CONSTEXPR_DESCTRUCTORS."
281#endif
282#if !defined(UGMISC_HAVE_CONSTEXPR_DESTRUCTORS) && !defined(UGMISC_NO_CONSTEXPR_DESTRUCTORS)
283# ifdef UGMISC_CXX_20
284# define UGMISC_HAVE_CONSTEXPR_DESCTRUCTORS
285# else
286# undef UGMISC_HAVE_CONSTEXPR_DESTRUCTORS
287# endif
288#endif
289#ifdef UGMISC_HAVE_CONSTEXPR_DESTRUCTORS
290 static constexpr bool constexpr_destructors = UGMISC_FEATURE_TRUE;
291#else
292 static constexpr bool constexpr_destructors = UGMISC_FEATURE_FALSE;
293#endif
294
295
296
297
298#ifdef UGMISC_DOCS
303# define UGMISC_HAVE_CONSTEVAL
304#endif
305#if __cpp_consteval
306# define UGMISC_HAVE_CONSTEVAL
307 static constexpr bool consteval_functions = UGMISC_FEATURE_TRUE;
308#else
309# undef UGMISC_HAVE_CONSTEVAL
314 static constexpr bool consteval_functions = UGMISC_FEATURE_FALSE;
315#endif
316
317
318
319
320#if UGMISC_DOCS
323# define UGMISC_HAVE_CONSTEVAL_IF
324#endif
329#if __cpp_consteval >= 202211L
330# define UGMISC_HAVE_CONSTEVAL_IF
331 static constexpr bool consteval_if = UGMISC_FEATURE_TRUE;
332#else
333# undef UGMISC_HAVE_CONSTEVAL_IF
334 static constexpr bool consteval_if = UGMISC_FEATURE_FALSE;
335#endif
336
337
338
339
340 /*
341 * Library features.
342 */
343
344
345
346
347#if UGMISC_DOCS
352# define UGMISC_HAVE_BIT_CAST
353#endif
360#if __cpp_lib_bit_cast
361# define UGMISC_HAVE_BIT_CAST
362 static constexpr bool bit_cast = UGMISC_FEATURE_TRUE;
363#else
364# undef UGMISC_HAVE_BIT_CAST
365 static constexpr bool bit_cast = UGMISC_FEATURE_FALSE;
366#endif
367
368
369
370
371#if UGMISC_DOCS
376#define UGMISC_HAVE_BITOPS
377#endif
378#if __cpp_lib_bitops
379# define UGMISC_HAVE_BITOPS
380 static constexpr bool bitops = UGMISC_FEATURE_TRUE;
381#else
382# undef UGMISC_HAVE_BITOPS
387 static constexpr bool bitops = UGMISC_FEATURE_FALSE;
388#endif
389
390
391
392
393#ifdef UGMISC_DOCS
398# define UGMISC_HAVE_CONSTEXPR_SWAP_ALGORITHMS
399#endif
400#if __cpp_lib_constexpr_swap_algorithms
401# define UGMISC_HAVE_CONSTEXPR_SWAP_ALGORITHMS
402 static constexpr bool constexpr_swap_algorithms = UGMISC_FEATURE_TRUE;
403#else
404# undef UGMISC_HAVE_CONSTEXPR_SWAP_ALGORITHMS
409 static constexpr bool constexpr_swap_algorithms = UGMISC_FEATURE_FALSE;
410#endif
411
412
413
414
415#ifdef UGMISC_DOCS
420# define UGMISC_HAVE_UNREACHABLE
421#endif
422#if __cpp_lib_unreachable
423# define UGMISC_HAVE_UNREACHABLE
424# define UGMISC_UNREACHABLE std::unreachable();
425 static constexpr bool unreachable = UGMISC_FEATURE_TRUE;
426#else
431 static constexpr bool unreachable = UGMISC_FEATURE_FALSE;
432# undef UGMISC_HAVE_UNREACHABLE
433#endif
434}; // struct Features
435
436
437} /* ::ugmisc */
438
439
440
441
442#ifdef UGMISC_HAVE_UNREACHABLE
443# define UGMISC_unreachable std::unreachable
444# include <utility>
445#else
446# define UGMISC_unreachable ::ugmisc::unreachable
447[[noreturn]] inline void unreachable() { }
448#endif
Definition features.hpp:240
static constexpr bool bit_cast
Definition features.hpp:365
static constexpr bool consteval_if
Definition features.hpp:334
static constexpr bool constexpr_destructors
Definition features.hpp:290
static constexpr bool bitops
Definition features.hpp:387
static constexpr bool unreachable
Definition features.hpp:431
static constexpr bool consteval_functions
Definition features.hpp:314
static constexpr bool constexpr_swap_algorithms
Definition features.hpp:409