DAW JSON Link
Loading...
Searching...
No Matches
daw_json_parse_options_impl.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#pragma once
10
12
15
16#include <daw/cpp_17.h>
17#include <daw/daw_attributes.h>
18#include <daw/daw_enable_requires.h>
19#include <daw/daw_string_view.h>
20#include <daw/daw_traits.h>
21#include <daw/daw_unreachable.h>
22
23#include <climits>
24#include <cstddef>
25#include <cstdint>
26#include <utility>
27
28namespace daw::json {
29 inline namespace DAW_JSON_VER {
30 namespace options {
31 constexpr daw::string_view to_string( ExecModeTypes mode ) {
32 switch( mode ) {
33 case ExecModeTypes::default_mode:
34 return "default_mode";
35 case ExecModeTypes::compile_time:
36 return "compile_time";
37 case ExecModeTypes::runtime:
38 return "runtime";
39 case ExecModeTypes::simd:
40 return "simd";
41 }
42 DAW_UNREACHABLE( );
43 }
44 } // namespace options
45
46 namespace json_details {
47 template<>
48 inline constexpr unsigned json_option_bits_width<options::ExecModeTypes> =
49 2;
50
51 template<>
52 inline constexpr auto default_json_option_value<options::ExecModeTypes> =
53 options::ExecModeTypes::default_mode;
54
55 template<>
56 inline constexpr unsigned
57 json_option_bits_width<options::ZeroTerminatedString> = 1;
58
59 template<>
60 inline constexpr auto
61 default_json_option_value<options::ZeroTerminatedString> =
62 options::ZeroTerminatedString::no;
63
64 template<>
65 inline constexpr unsigned
66 json_option_bits_width<options::AllowStringMutation> = 1;
67
68 template<>
69 inline constexpr auto
70 default_json_option_value<options::AllowStringMutation> =
71 options::AllowStringMutation::no;
72
73 template<>
74 inline constexpr unsigned
75 json_option_bits_width<options::PolicyCommentTypes> = 2;
76
77 template<>
78 inline constexpr auto
79 default_json_option_value<options::PolicyCommentTypes> =
80 options::PolicyCommentTypes::none;
81
82 template<>
83 inline constexpr unsigned
84 json_option_bits_width<options::CheckedParseMode> = 1;
85
86 template<>
87 inline constexpr auto
88 default_json_option_value<options::CheckedParseMode> =
89 options::CheckedParseMode::yes;
90
91 template<>
92 inline constexpr unsigned
93 json_option_bits_width<options::MinifiedDocument> = 1;
94
95 template<>
96 inline constexpr auto
97 default_json_option_value<options::MinifiedDocument> =
98 options::MinifiedDocument::no;
99
100 template<>
101 inline constexpr unsigned
102 json_option_bits_width<options::AllowEscapedNames> = 1;
103
104 template<>
105 inline constexpr auto
106 default_json_option_value<options::AllowEscapedNames> =
107 options::AllowEscapedNames::no;
108
109 template<>
110 inline constexpr unsigned
111 json_option_bits_width<options::IEEE754Precise> = 1;
112
113 template<>
114 inline constexpr auto default_json_option_value<options::IEEE754Precise> =
115 options::IEEE754Precise::yes;
116
117 template<>
118 inline constexpr unsigned
119 json_option_bits_width<options::ForceFullNameCheck> = 1;
120
121 template<>
122 inline constexpr auto
123 default_json_option_value<options::ForceFullNameCheck> =
124 options::ForceFullNameCheck::no;
125
126 template<>
127 inline constexpr unsigned
128 json_option_bits_width<options::UseExactMappingsByDefault> = 1;
129
130 template<>
131 inline constexpr auto
132 default_json_option_value<options::UseExactMappingsByDefault> =
133 options::UseExactMappingsByDefault::no;
134
135 template<>
136 inline constexpr unsigned
137 json_option_bits_width<options::MustVerifyEndOfDataIsValid> = 1;
138
139 template<>
140 inline constexpr auto
141 default_json_option_value<options::MustVerifyEndOfDataIsValid> =
142 options::MustVerifyEndOfDataIsValid::no;
143
144 template<>
145 inline constexpr unsigned
146 json_option_bits_width<options::ExpectLongNames> = 1;
147
148 template<>
149 inline constexpr auto
150 default_json_option_value<options::ExpectLongNames> =
151 options::ExpectLongNames::no;
152
153 template<>
154 inline constexpr unsigned json_option_bits_width<options::ArrayReserve> =
155 4;
156
157 template<>
158 inline constexpr auto default_json_option_value<options::ArrayReserve> =
159 options::ArrayReserve::none;
160
161 /*
162 template<>
163 inline constexpr unsigned
164 json_option_bits_width<options::TemporarilyMutateBuffer> = 1;
165
166 template<>
167 inline constexpr auto
168 default_json_option_value<options::TemporarilyMutateBuffer> =
169 options::TemporarilyMutateBuffer::no;
170 */
171
172 template<>
173 inline constexpr unsigned
174 json_option_bits_width<options::ExcludeSpecialEscapes> = 1;
175
176 template<>
177 inline constexpr auto
178 default_json_option_value<options::ExcludeSpecialEscapes> =
179 options::ExcludeSpecialEscapes::no;
180
181 using policy_list = typename option_list_impl<
182 options::ExecModeTypes, options::ZeroTerminatedString,
183 options::AllowStringMutation, options::PolicyCommentTypes,
184 options::CheckedParseMode, options::AllowEscapedNames,
185 options::IEEE754Precise, options::ForceFullNameCheck,
186 options::MinifiedDocument, options::UseExactMappingsByDefault,
187 options::MustVerifyEndOfDataIsValid, options::ExcludeSpecialEscapes,
188 options::ExpectLongNames, options::ArrayReserve>::type;
189
190 template<typename Policy, typename Policies>
191 inline constexpr unsigned basic_policy_bits_start =
192 option_bits_start_impl<Policy, Policies>::template calc<>(
193 std::make_index_sequence<pack_size_v<Policies>>{ } );
194
195 template<typename Policy>
196 inline constexpr unsigned policy_bits_start =
197 basic_policy_bits_start<Policy, policy_list>;
198
199 DAW_CONSTEVAL json_options_t set_bits( json_options_t value ) {
200 return value;
201 }
202
203 template<typename PolicyFlag,
204 typename... PolicyFlags DAW_ENABLEIF(
205 are_option_flags<PolicyFlag, PolicyFlags...> )>
206 DAW_REQUIRES( are_option_flags<PolicyFlag, PolicyFlags...> )
207 DAW_CONSTEVAL json_options_t
208 set_bits( json_options_t value, PolicyFlag pol, PolicyFlags... pols ) {
209 static_assert( are_option_flags<PolicyFlags...>,
210 "Only registered policy types are allowed" );
211
212 auto new_bits = static_cast<unsigned>( pol );
213 using mask =
214 daw::constant<( (1U << json_option_bits_width<PolicyFlag>)-1U )>;
215 new_bits &= mask::value;
216 new_bits <<= policy_bits_start<PolicyFlag>;
217 value &= ~( mask::value << policy_bits_start<PolicyFlag> );
218 value |= new_bits;
219 if constexpr( sizeof...( PolicyFlags ) > 0 ) {
220 if constexpr( sizeof...( pols ) > 0 ) {
221 return set_bits( value, pols... );
222 } else {
223 return value;
224 }
225 } else {
226 return value;
227 }
228 }
229
230 template<typename Policy>
231 DAW_CONSTEVAL json_options_t set_bits_for( Policy e ) {
232 static_assert( is_option_flag<Policy>,
233 "Only registered policy types are allowed" );
234 auto new_bits = static_cast<json_options_t>( e );
235 new_bits <<= policy_bits_start<Policy>;
236 return new_bits;
237 }
238
239 template<typename>
240 struct default_policy_flag_t;
241
242 template<typename... Policies>
243 struct default_policy_flag_t<pack_list<Policies...>> {
244 static constexpr json_options_t value =
245 ( set_bits_for<Policies>( default_json_option_value<Policies> ) |
246 ... );
247 };
248
249 /***
250 * The defaults for all known policies encoded as a json_options_t
251 */
252 inline constexpr json_options_t default_policy_flag =
253 default_policy_flag_t<policy_list>::value;
254
255 template<typename Policy, typename Result = Policy>
256 DAW_CONSTEVAL Result get_bits_for( json_options_t value ) {
257 static_assert( is_option_flag<Policy>,
258 "Only registered policy types are allowed" );
259 using mask = daw::constant<( 1U << (policy_bits_start<Policy> +
260 json_option_bits_width<Policy>)) -
261 1U>;
262 value &= mask::value;
263 value >>= policy_bits_start<Policy>;
264 return static_cast<Result>( Policy{ value } );
265 }
266 } // namespace json_details
267 // ***********************************************
268
269 /***
270 * Create the parser options flag for BasicParsePolicy
271 * @tparam Policies Policy types that satisfy the `is_policy_flag` trait.
272 * @param policies A list of parser options to change from the defaults.
273 * @return A json_options_t that encodes the options for the parser
274 */
275 template<typename... Policies>
276 DAW_CONSTEVAL json_options_t parse_options( Policies... policies ) {
277 static_assert( json_details::are_option_flags<Policies...>,
278 "Only registered policy types are allowed" );
279 return json_details::set_bits( json_details::default_policy_flag,
280 policies... );
281 }
282 } // namespace DAW_JSON_VER
283} // namespace daw::json
DAW_REQUIRES(daw::json::json_details::is_container_opted_into_json_iostreams_v< Container >) std
An opt in ostream interface for containers of types that have JSON mappings.
constexpr std::string_view to_string(JsonBaseParseTypes pt)
string_writer grows its string with resize_and_overwrite, leaving the new bytes uninitialized,...
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20