DAW JSON Link
Loading...
Searching...
No Matches
daw_json_switches.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9// This file will have all the preprocessor based switches that users can
10// switch. Many are controllable from cmake via commandline options too.
11
12#pragma once
13
14#include <daw/daw_consteval.h>
15#include <daw/daw_cpp_feature_check.h>
16#include <daw/daw_is_detected.h>
17#include <daw/stdinc/enable_if.h>
18
21#if defined( DAW_HAS_GCC ) and not defined( DAW_JSON_FLATTEN )
22#if not defined( DAW_NO_FLATTEN )
23#define DAW_NO_FLATTEN
24#endif
25#endif
26
27// Does json_link throw itself. Other things like constructors for the user
28// types may.
29#if defined( DAW_USE_EXCEPTIONS )
30#if defined( DAW_JSON_DONT_USE_EXCEPTIONS )
31#error Conflicting defines DAW_USE_EXCEPTIONS and DAW_JSON_DONT_USE_EXCEPTIONS
32#endif
33#if defined( DAW_DONT_USE_EXCEPTIONS )
34#error Conflicting defines DAW_USE_EXCEPTIONS and DAW_DONT_USE_EXCEPTIONS
35#endif
36#else
37#if not defined( DAW_JSON_DONT_USE_EXCEPTIONS )
38#define DAW_JSON_DONT_USE_EXCEPTIONS
39#endif
40#if not defined( DAW_DONT_USE_EXCEPTIONS )
41#define DAW_JSON_DONT_USE_EXCEPTIONS
42#endif
43#endif
44
45// Disable using guaranteed RVO to clean up after return value construction.
46// Use DAW_NO_CONSTEXPR_SCOPE_GUARD to disable constexpr scope guard.
47// DAW_HAS_CONSTEXPR_SCOPE_GUARD is defined when it is enabled and in
48// <daw/daw_scope_guard.h>
49// This has performance impacts on MSVC as on_exit_success calls
50// std::uncaught_exceptions( ) which is very slow there
51#if not defined( DAW_NO_CONSTEXPR_SCOPE_GUARD ) and \
52 not defined( DAW_JSON_ENABLE_FULL_RVO )
53#define DAW_NO_CONSTEXPR_SCOPE_GUARD
54#endif
55
56// DAW_IS_CONSTANT_EVALUATED is defined when a builtin or
57// std::is_constant_evaluated( ) is available in
58// <daw/daw_is_constant_evaluated.h>
59
60// Show extra diagnostic information like unmapped members when parsing
61// by defining DAW_JSON_PARSER_DIAGNOSTICS
62
63// DAW_CAN_CONSTANT_EVAL is used to test if we are in a constant expression
64#if defined( DAW_HAS_GCC_LIKE )
65#define DAW_CAN_CONSTANT_EVAL( ... ) \
66 ( __builtin_constant_p( __VA_ARGS__ ) == 1 )
67#else
68#define DAW_CAN_CONSTANT_EVAL( ... ) true
69#endif
70
71// If the compiler supports CNTTP types allow for strings in json data
72// contracts. Both support passing local char const[], but the type is
73// different. To keep old behaviour when using C++20, define
74// DAW_USE_CPP17_ABI
75#if not defined( DAW_USE_CPP17_ABI )
76#if defined( __cpp_nontype_template_parameter_class )
77#if not defined( DAW_JSON_CNTTP_JSON_NAME )
78#define DAW_JSON_CNTTP_JSON_NAME
79#endif
80#endif
81#if defined( __cpp_nontype_template_args )
82#if __cpp_nontype_template_args >= 201911L
83#if not defined( DAW_JSON_CNTTP_JSON_NAME )
84#define DAW_JSON_CNTTP_JSON_NAME
85#endif
86#endif
87#endif
88#if DAW_HAS_CLANG_VER_GTE( 12, 0 ) and DAW_CPP_VERSION >= 202002L
89// Clang 12 supports enough of CNTTP string literals and compiles the tests
90// successfully, but does not define the feature macro
91#if not defined( DAW_JSON_CNTTP_JSON_NAME )
92#define DAW_JSON_CNTTP_JSON_NAME
93#endif
94#endif
95#endif
96
97#if defined( __cpp_constexpr_dynamic_alloc )
98#define CPP20CONSTEXPR constexpr
99#else
100// TODO: rename in v4
101#define CPP20CONSTEXPR
102#endif
103
104// Fix bug in MSVC
105#if defined( DAW_HAS_MSVC )
106#define DAW_JSON_MAKE_LOC_INFO_CONSTEVAL constexpr
107#else
108#define DAW_JSON_MAKE_LOC_INFO_CONSTEVAL DAW_CONSTEVAL
109#endif
110
111// Allow experimental SIMD paths, if available
112// by defining DAW_ALLOW_SSE42 and using the parser policy ExecModeType simd
113#if not defined( DAW_JSON_NO_SSE42 )
114#if defined( __SSE4_2__ ) or defined( __AVX__ ) or defined( __AVX2__ )
115#if not defined( DAW_ALLOW_SSE42 )
116#define DAW_ALLOW_SSE42 1
117#endif
118#endif
119#endif
120
121// Use strtod instead of from_chars when avialable by defining
122// DAW_JSON_USE_STRTOD
123#if defined( __cpp_lib_to_chars )
124#if __cpp_lib_to_chars >= 201611L
125#define DAW_HAS_CPP17_FP_FROM_CHARS 1
126#endif
127#endif
128#if not defined( DAW_JSON_USE_STRTOD ) and \
129 not defined( DAW_HAS_CPP17_FP_FROM_CHARS )
130#define DAW_JSON_USE_STRTOD
131#endif
132
133// define DAW_JSON_DISABLE_RANDOM to disable creating random iterators when the
134// size is known up front. This is playing to the implementations prior to
135// C++23 when range constructors are added to containers. It is disabled on
136// MSVC as that impl does not work with it
137#if defined( DAW_HAS_MSVC )
138#if not defined( DAW_JSON_DISABLE_RANDOM )
139#define DAW_JSON_DISABLE_RANDOM
140#endif
141#endif
142
143// DAW_JSON_HAS_BUILTIN_UADD is used to switch to a constexpr method of overflow
144// addition when available
145#if DAW_HAS_GCC_VER_GTE( 8, 0 ) or defined( DAW_HAS_CLANG ) or \
146 ( DAW_HAS_BUILTIN( __builtin_uadd_overflow ) and \
147 DAW_HAS_BUILTIN( __builtin_uaddl_overflow ) and \
148 DAW_HAS_BUILTIN( __builtin_uaddll_overflow ) )
149#define DAW_JSON_HAS_BUILTIN_UADD
150#endif
151
152// DAW_JSON_HAS_FLOAT128 enables a binary128 power of ten table used when
153// scaling real numbers of types wider than double, e.g. user defined types
154// convertible to and from std::float128_t/__float128. It is detected when one
155// of those types is available, and can be defined by the user. Define
156// DAW_JSON_NO_FLOAT128 to disable it
157#if not defined( DAW_JSON_HAS_FLOAT128 ) and not defined( DAW_JSON_NO_FLOAT128 )
158#if defined( __STDCPP_FLOAT128_T__ ) or defined( __SIZEOF_FLOAT128__ )
159#define DAW_JSON_HAS_FLOAT128
160#endif
161#endif
162
163// DAW_JSON_BUGFIX_FROM_JSON_001
164// Defined for MSVC as it has been ICE'ing on daw_json_ensure in from_json
165#if defined( DAW_HAS_MSVC )
166#define DAW_JSON_BUGFIX_FROM_JSON_001
167#endif
168
169// DAW_JSON_BUGFIX_MSVC_EVAL_ORDER_002
170// MSVC cannot always evaluate in the correct order as defined by the standard.
171// JSON Link uses the left->right guarantees in places like constructors so that
172// less state is needed. In MSVC one must keep more state in places like
173// parse_tuple_value
174#if defined( DAW_HAS_MSVC )
175#define DAW_JSON_BUGFIX_MSVC_EVAL_ORDER_002
176#endif
177
178// DAW_JSON_BUGFIX_MSVC_KNOWN_LOC_ICE_003
179// MSVC in C++20 mode will ICE when known locations is evaluated at compile time
180#if defined( DAW_HAS_MSVC ) and __cpp_constexpr > 201700L
181#define DAW_JSON_BUGFIX_MSVC_KNOWN_LOC_ICE_003
182#endif
183
184// Allows pack expansions with get<Is> without separate methods. This should
185// improve symbol sizes
186#if defined( __cpp_generic_lambdas )
187#if __cpp_generic_lambdas >= 201707L
188#define DAW_JSON_USE_GENERIC_LAMBDAS
189#endif
190#endif
191
192// Use static operator( ) when supported
193#if defined( __cpp_static_call_operator )
194#if __cpp_static_call_operator >= 202207L
195#define DAW_JSON_HAS_STATIC_CALL_OP
196#endif
197#endif
198#if defined( DAW_JSON_HAS_STATIC_CALL_OP )
199#define DAW_JSON_CPP23_STATIC_CALL_OP static
200#define DAW_JSON_CPP23_STATIC_CALL_OP_CONST
201
202#if DAW_HAS_CLANG_VER_GTE( 17, 0 )
203#define DAW_JSON_CPP23_STATIC_CALL_OP_DISABLE_WARNING \
204 _Pragma( "clang diagnostic push" ) \
205 _Pragma( "clang diagnostic ignored \"-Wc++23-extensions\"" )
206
207#define DAW_JSON_CPP23_STATIC_CALL_OP_ENABLE_WARNING \
208 _Pragma( "clang diagnostic pop" )
209#else
210#define DAW_JSON_CPP23_STATIC_CALL_OP_DISABLE_WARNING
211#define DAW_JSON_CPP23_STATIC_CALL_OP_ENABLE_WARNING
212#endif
213#else
214#define DAW_JSON_CPP23_STATIC_CALL_OP
215#define DAW_JSON_CPP23_STATIC_CALL_OP_CONST const
216#define DAW_JSON_CPP23_STATIC_CALL_OP_DISABLE_WARNING
217#define DAW_JSON_CPP23_STATIC_CALL_OP_ENABLE_WARNING
218#endif
219
220#if defined( __cpp_constexpr_dynamic_alloc )
221#if __cpp_constexpr_dynamic_alloc >= 201907L
222#define DAW_JSON_HAS_CPP20_CX_ALLOC
223#endif
224#endif
225
226#if defined( DAW_JSON_HAS_CPP20_CX_ALLOC )
227#define DAW_JSON_HAS_CPP20_CX_DTOR
228#define DAW_JSON_CPP20_CX_DTOR constexpr
229#else
230#define DAW_JSON_CPP20_CX_DTOR
231#endif
232
233#if defined( DAW_JSON_HAS_CPP20_CX_ALLOC ) and \
234 defined( __cpp_lib_constexpr_vector )
235#if __cpp_lib_constexpr_vector >= 201907L
236#define DAW_JSON_HAS_CPP20_CX_VECTOR
237#endif
238#endif
239
240#if defined( DAW_JSON_HAS_CPP20_CX_VECTOR )
241#define DAW_JSON_CX_VECTOR constexpr
242#else
243#define DAW_JSON_CX_VECTOR
244#endif
245
246#if defined( DAW_JSON_HAS_CPP20_CX_ALLOC ) and \
247 defined( __cpp_lib_constexpr_string )
248#if __cpp_lib_constexpr_string >= 201907L
249#define DAW_JSON_HAS_CPP20_CX_STRING
250#endif
251#endif
252
253#if defined( DAW_JSON_HAS_CPP20_CX_STRING )
254#define DAW_JSON_CX_STRING constexpr
255#else
256#define DAW_JSON_CX_STRING inline
257#endif
258
259#if defined( DAW_JSON_HAS_CPP20_CX_STRING ) and \
260 defined( DAW_JSON_HAS_CPP20_CX_VECTOR )
261#define DAW_JSON_HAS_CPP20_CX_STRVEC
262#define DAW_JSON_CX_STRVEC constexpr
263#else
264#define DAW_JSON_CX_STRVEC
265#endif
266
267// Many of the iterators used offer some extra checking when this is set. By
268// default in debug mode they are enabled. Prior to C++20 affects the triviality
269// of their destructors and can prevent their use in constexpr code. Setting
270// DAW_JSON_NO_FULL_DEBUG_ITERATORS disables the destructor checks when compiler
271// support for constexpr destructors is unavailable
272#if defined( DAW_JSON_HAS_CPP20_CX_DTOR )
273#if not defined( NDEBUG ) and not defined( DAW_JSON_USE_FULL_DEBUG_ITERATORS )
274#define DAW_JSON_USE_FULL_DEBUG_ITERATORS
275#endif
276#endif
277
278#if defined( __cpp_lib_containers_ranges )
279#if __cpp_lib_containers_ranges >= 202202L
280#define DAW_JSON_HAS_CPP23_RANGE_CTOR
281#endif
282#endif
283
284#if defined( __cpp_lib_constexpr_exceptions )
285#if __cpp_lib_constexpr_exceptions >= 202411L
286#define DAW_JSON_HAS_CONSTEXPR_EXCEPTIONS
287#endif
288#endif
289
290#if defined( DAW_JSON_HAS_CONSTEXPR_EXCEPTIONS )
291#define DAW_JSON_CPP26_CX_EXCEPT constexpr
292#else
293#define DAW_JSON_CPP26_CX_EXCEPT inline
294#endif
295
296#if defined( __cpp_lib_reflection ) and not defined( DAW_JSON_NO_RELFECTION )
297#if __cpp_lib_reflection >= 202506L
298#define DAW_JSON_HAS_REFLECTION 1
299#endif
300#endif
301
302#if defined( __cpp_generic_lambdas )
303#if __cpp_generic_lambdas >= 201707L
304#define DAW_JSON_CPP20_TEMPLATE_LAMBDAS
305#endif
306#endif