WG14 result
Loading...
Searching...
No Matches
config.h
Go to the documentation of this file.
1/* Proposed WG14 Result
2(C) 2024 - 2026 Niall Douglas <http://www.nedproductions.biz/>
3File Created: Oct 2025
4
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License in the accompanying file
9Licence.txt or at
10
11http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18*/
19
20
21#ifndef WG14_RESULT_CONFIG_H
22#define WG14_RESULT_CONFIG_H
23
24#if WG14_RESULT_ENABLE_HEADER_ONLY || WG14_RESULT_SOURCE
25#if defined(__cplusplus)
26#if __cplusplus < 202000L && !_HAS_CXX20
27#include <version> // some toolchains need this to set the version macros correctly
28#endif
29#if __cplusplus < 202000L && !_HAS_CXX20
30#error \
31"If being compiled as C++, the source code requires a minimum of a C++ 20 compiler for decent C11 support."
32#endif
33#elif __STDC_VERSION__ < 201100L
34#error \
35"The source code requires a minimum of a C11 compiler for atomics support."
36#endif
37#endif
38
39#ifndef WG14_RESULT_PREFIX
40#define WG14_RESULT_PREFIX(x) x
41#endif
42
43#ifndef WG14_RESULT_NULLPTR
44#if __STDC_VERSION__ >= 202300L
45#define WG14_RESULT_NULLPTR nullptr
46#else
47#define WG14_RESULT_NULLPTR NULL
48#endif
49#endif
50
51#ifndef WG14_RESULT_CONSTEXPR
52#if __STDC_VERSION__ >= 202300L && (!defined(__clang__) || __clang_major >= 19)
53#define WG14_RESULT_CONSTEXPR constexpr
54#else
55#define WG14_RESULT_CONSTEXPR
56#endif
57#endif
58
59#ifndef WG14_RESULT_INLINE
60#if __STDC_VERSION__ >= 201100L
61#define WG14_RESULT_INLINE static inline
62#else
63#define WG14_RESULT_INLINE static
64#endif
65#endif
66
67#ifndef WG14_RESULT_TYPEOF
68#if __STDC_VERSION__ >= 202300L
69#define WG14_RESULT_TYPEOF(...) typeof(__VA_ARGS__)
70#else
71#define WG14_RESULT_TYPEOF(...) __typeof__(__VA_ARGS__)
72#endif
73#endif
74
75#ifndef WG14_RESULT_TYPEOF_UNQUAL
76#if __STDC_VERSION__ >= 202300L
77#define WG14_RESULT_TYPEOF_UNQUAL(...) typeof_unqual(__VA_ARGS__)
78#elif(defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 14) || \
79(defined(__clang__) && __clang_major__ < 19)
80// Not much else we can do here
81#define WG14_RESULT_TYPEOF_UNQUAL(...) __typeof__(__VA_ARGS__)
82#else
83#define WG14_RESULT_TYPEOF_UNQUAL(...) __typeof_unqual__(__VA_ARGS__)
84#endif
85#endif
86
87#ifndef WG14_RESULT_CONSTEXPR_OR_CONST
88#if __STDC_VERSION__ >= 202300L && (!defined(__clang__) || __clang_major >= 19)
89#define WG14_RESULT_CONSTEXPR_OR_CONST constexpr
90#else
91#define WG14_RESULT_CONSTEXPR_OR_CONST const
92#endif
93#endif
94
95#ifndef WG14_RESULT_DEFAULT_VISIBILITY
96#ifdef _WIN32
97#define WG14_RESULT_DEFAULT_VISIBILITY
98#else
99#define WG14_RESULT_DEFAULT_VISIBILITY __attribute__((visibility("default")))
100#endif
101#endif
102
103#ifndef WG14_RESULT_EXTERN
104#if WG14_RESULT_SOURCE
105#ifdef _WIN32
106#define WG14_RESULT_EXTERN extern __declspec(dllexport)
107#else
108#define WG14_RESULT_EXTERN extern __attribute__((visibility("default")))
109#endif
110#elif WG14_RESULT_ENABLE_HEADER_ONLY
111#define WG14_RESULT_EXTERN extern inline
112#else
113#define WG14_RESULT_EXTERN extern
114#endif
115#endif
116
117#ifndef WG14_RESULT_SINGLETON
118#if defined(__GNUC__) || defined(__clang__)
119#define WG14_RESULT_SINGLETON __attribute__((const))
120#else
121#define WG14_RESULT_SINGLETON
122#endif
123#endif
124
125#ifndef WG14_RESULT_STATIC_ASSERT_WITHIN_EXPR
126#define WG14_RESULT_STATIC_ASSERT_WITHIN_EXPR(pred, msg, ...) \
127 ((__VA_ARGS__) + 0 * sizeof(struct { \
128 _Static_assert((pred), msg); \
129 int x; \
130 }))
131#endif
132
133
134#ifndef WG14_RESULT_STDERR_PRINTF
135#include <stdio.h>
136#define WG14_RESULT_STDERR_PRINTF(...) fprintf(stderr, __VA_ARGS__)
137#endif
138
139#ifndef WG14_RESULT_ABORTF
140#include <stdarg.h>
141#include <stdio.h>
142#include <stdlib.h>
143WG14_RESULT_INLINE unsigned
144#ifndef _MSC_VER
145__attribute__((noreturn))
146#endif
147WG14_RESULT_PREFIX(abortf_impl)(const char *msg, ...)
148{
149 va_list args;
150 va_start(args, msg);
151 vfprintf(stderr, msg, args);
153#ifdef _MSC_VER
154 return 0;
155#else
156 va_end(args);
157#endif
158}
159#if defined(_MSC_VER) && !defined(__clang__)
160#define WG14_RESULT_ABORTF(X, ...) \
161 WG14_RESULT_PREFIX(abortf_impl)("FATAL: " X "\n", ##__VA_ARGS__)
162#elif __STDC_VERSION__ >= 202300L && !defined(__clang__)
163#define WG14_RESULT_ABORTF(X, ...) \
164 WG14_RESULT_PREFIX(abortf_impl)("FATAL: " X "\n" __VA_OPT(, ) __VA_ARGS__)
165#else
166#define WG14_RESULT_ABORTF_HAS_COMMA(...) \
167 WG14_RESULT_ABORTF_HAS_COMMA_16__(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
168 1, 1, 1, 1, 0, 0)
169#define WG14_RESULT_ABORTF_HAS_COMMA_16__( \
170_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) \
171 _15
172#define WG14_RESULT_ABORTF_CAT_(X, Y) X##Y
173#define WG14_RESULT_ABORTF_CAT(X, Y) WG14_RESULT_ABORTF_CAT_(X, Y)
174#define WG14_RESULT_ABORTF_INVOKE0(X) \
175 WG14_RESULT_PREFIX(abortf_impl)("FATAL: " X "\n")
176#define WG14_RESULT_ABORTF_INVOKE1(X, ...) \
177 WG14_RESULT_PREFIX(abortf_impl)("FATAL: " X "\n", __VA_ARGS__)
178
179#define WG14_RESULT_ABORTF(...) \
180 WG14_RESULT_ABORTF_CAT(WG14_RESULT_ABORTF_INVOKE, \
181 WG14_RESULT_ABORTF_HAS_COMMA(__VA_ARGS__)) \
182 (__VA_ARGS__)
183#endif
184#endif
185
186#ifdef __cplusplus
187extern "C"
188{
189#endif
190
191
192#ifdef __cplusplus
193}
194#endif
195
196#endif
abort()
va_end(args)
vfprintf(stderr, msg, args)
#define WG14_RESULT_INLINE
Definition config.h:63
static unsigned va_start(args, msg)