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::PolicyCommentTypes> = 2;
67
68 template<>
69 inline constexpr auto
70 default_json_option_value<options::PolicyCommentTypes> =
71 options::PolicyCommentTypes::none;
72
73 template<>
74 inline constexpr unsigned
75 json_option_bits_width<options::CheckedParseMode> = 1;
76
77 template<>
78 inline constexpr auto
79 default_json_option_value<options::CheckedParseMode> =
80 options::CheckedParseMode::yes;
81
82 template<>
83 inline constexpr unsigned
84 json_option_bits_width<options::MinifiedDocument> = 1;
85
86 template<>
87 inline constexpr auto
88 default_json_option_value<options::MinifiedDocument> =
89 options::MinifiedDocument::no;
90
91 template<>
92 inline constexpr unsigned
93 json_option_bits_width<options::AllowEscapedNames> = 1;
94
95 template<>
96 inline constexpr auto
97 default_json_option_value<options::AllowEscapedNames> =
98 options::AllowEscapedNames::no;
99
100 template<>
101 inline constexpr unsigned
102 json_option_bits_width<options::IEEE754Precise> = 1;
103
104 template<>
105 inline constexpr auto default_json_option_value<options::IEEE754Precise> =
106 options::IEEE754Precise::no;
107
108 template<>
109 inline constexpr unsigned
110 json_option_bits_width<options::ForceFullNameCheck> = 1;
111
112 template<>
113 inline constexpr auto
114 default_json_option_value<options::ForceFullNameCheck> =
115 options::ForceFullNameCheck::no;
116
117 template<>
118 inline constexpr unsigned
119 json_option_bits_width<options::UseExactMappingsByDefault> = 1;
120
121 template<>
122 inline constexpr auto
123 default_json_option_value<options::UseExactMappingsByDefault> =
124 options::UseExactMappingsByDefault::no;
125
126 template<>
127 inline constexpr unsigned
128 json_option_bits_width<options::MustVerifyEndOfDataIsValid> = 1;
129
130 template<>
131 inline constexpr auto
132 default_json_option_value<options::MustVerifyEndOfDataIsValid> =
133 options::MustVerifyEndOfDataIsValid::no;
134
135 template<>
136 inline constexpr unsigned
137 json_option_bits_width<options::ExpectLongNames> = 1;
138
139 template<>
140 inline constexpr auto
141 default_json_option_value<options::ExpectLongNames> =
142 options::ExpectLongNames::no;
143
144 /*
145 template<>
146 inline constexpr unsigned
147 json_option_bits_width<options::TemporarilyMutateBuffer> = 1;
148
149 template<>
150 inline constexpr auto
151 default_json_option_value<options::TemporarilyMutateBuffer> =
152 options::TemporarilyMutateBuffer::no;
153 */
154
155 template<>
156 inline constexpr unsigned
157 json_option_bits_width<options::ExcludeSpecialEscapes> = 1;
158
159 template<>
160 inline constexpr auto
161 default_json_option_value<options::ExcludeSpecialEscapes> =
162 options::ExcludeSpecialEscapes::no;
163
164 using policy_list = typename option_list_impl<
165 options::ExecModeTypes, options::ZeroTerminatedString,
166 options::PolicyCommentTypes, options::CheckedParseMode,
167 options::AllowEscapedNames, options::IEEE754Precise,
168 options::ForceFullNameCheck, options::MinifiedDocument,
169 options::UseExactMappingsByDefault, options::MustVerifyEndOfDataIsValid,
170 options::ExcludeSpecialEscapes, options::ExpectLongNames>::type;
171
172 template<typename Policy, typename Policies>
173 inline constexpr unsigned basic_policy_bits_start =
174 option_bits_start_impl<Policy, Policies>::template calc<>(
175 std::make_index_sequence<pack_size_v<Policies>>{ } );
176
177 template<typename Policy>
178 inline constexpr unsigned policy_bits_start =
179 basic_policy_bits_start<Policy, policy_list>;
180
181 DAW_CONSTEVAL json_options_t set_bits( json_options_t value ) {
182 return value;
183 }
184
185 template<typename PolicyFlag,
186 typename... PolicyFlags DAW_ENABLEIF(
187 are_option_flags<PolicyFlag, PolicyFlags...> )>
188 DAW_REQUIRES( are_option_flags<PolicyFlag, PolicyFlags...> )
189 DAW_CONSTEVAL json_options_t
190 set_bits( json_options_t value, PolicyFlag pol, PolicyFlags... pols ) {
191 static_assert( are_option_flags<PolicyFlags...>,
192 "Only registered policy types are allowed" );
193
194 auto new_bits = static_cast<unsigned>( pol );
195 using mask =
196 daw::constant<( (1U << json_option_bits_width<PolicyFlag>)-1U )>;
197 new_bits &= mask::value;
198 new_bits <<= policy_bits_start<PolicyFlag>;
199 value &= ~( mask::value << policy_bits_start<PolicyFlag> );
200 value |= new_bits;
201 if constexpr( sizeof...( PolicyFlags ) > 0 ) {
202 if constexpr( sizeof...( pols ) > 0 ) {
203 return set_bits( value, pols... );
204 } else {
205 return value;
206 }
207 } else {
208 return value;
209 }
210 }
211
212 template<typename Policy>
213 DAW_CONSTEVAL json_options_t set_bits_for( Policy e ) {
214 static_assert( is_option_flag<Policy>,
215 "Only registered policy types are allowed" );
216 auto new_bits = static_cast<json_options_t>( e );
217 new_bits <<= policy_bits_start<Policy>;
218 return new_bits;
219 }
220
221 template<typename>
222 struct default_policy_flag_t;
223
224 template<typename... Policies>
225 struct default_policy_flag_t<pack_list<Policies...>> {
226 static constexpr json_options_t value =
227 ( set_bits_for<Policies>( default_json_option_value<Policies> ) |
228 ... );
229 };
230
231 /***
232 * The defaults for all known policies encoded as a json_options_t
233 */
234 inline constexpr json_options_t default_policy_flag =
235 default_policy_flag_t<policy_list>::value;
236
237 template<typename Policy, typename Result = Policy>
238 DAW_CONSTEVAL Result get_bits_for( json_options_t value ) {
239 static_assert( is_option_flag<Policy>,
240 "Only registered policy types are allowed" );
241 using mask = daw::constant<( 1U << (policy_bits_start<Policy> +
242 json_option_bits_width<Policy>)) -
243 1U>;
244 value &= mask::value;
245 value >>= policy_bits_start<Policy>;
246 return static_cast<Result>( Policy{ value } );
247 }
248 } // namespace json_details
249 // ***********************************************
250
251 /***
252 * Create the parser options flag for BasicParsePolicy
253 * @tparam Policies Policy types that satisfy the `is_policy_flag` trait.
254 * @param policies A list of parser options to change from the defaults.
255 * @return A json_options_t that encodes the options for the parser
256 */
257 template<typename... Policies>
258 DAW_CONSTEVAL json_options_t parse_options( Policies... policies ) {
259 static_assert( json_details::are_option_flags<Policies...>,
260 "Only registered policy types are allowed" );
261 auto result = json_details::default_policy_flag;
262 if constexpr( sizeof...( Policies ) > 0 ) {
263 result |= ( json_details::set_bits_for( policies ) | ... );
264 }
265 return result;
266 }
267 } // namespace DAW_JSON_VER
268} // 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)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20