22 namespace json_details {
23 template<
typename OutputIterator>
24 struct iterator_wrapper : OutputIterator {
25 inline constexpr OutputIterator
get( )
const {
29 static constexpr bool is_pointer =
false;
31 inline constexpr void set( OutputIterator it ) {
32 *
static_cast<OutputIterator *
>( this ) = it;
36 inline constexpr OutputIterator &raw_it( ) {
41 template<
typename CharT>
42 struct iterator_wrapper<CharT *> {
43 using difference_type = std::ptrdiff_t;
44 using size_type = std::size_t;
45 using value_type = CharT;
46 using pointer = CharT *;
47 using reference = CharT &;
48 using iterator_category = std::random_access_iterator_tag;
52 static constexpr bool is_pointer =
true;
55 inline constexpr CharT *raw_it( ) {
60 inline constexpr CharT *
get( )
const {
64 inline constexpr void set( CharT *p ) {
68 inline constexpr reference operator*( ) {
72 inline constexpr pointer operator->( ) {
76 inline constexpr iterator_wrapper &operator++( ) {
81 inline constexpr iterator_wrapper operator++(
int ) & {
87 inline constexpr iterator_wrapper &operator--( ) {
92 inline constexpr iterator_wrapper operator--(
int ) & {
98 inline constexpr iterator_wrapper &operator+=( difference_type n ) {
103 inline constexpr iterator_wrapper &operator-=( difference_type n ) {
108 inline constexpr iterator_wrapper
109 operator+( difference_type n )
const noexcept {
110 iterator_wrapper result = *
this;
115 inline constexpr iterator_wrapper
116 operator-( difference_type n )
const noexcept {
117 iterator_wrapper result = *
this;
122 inline constexpr reference operator[]( size_type n )
noexcept {
123 return *( ptr +
static_cast<difference_type
>( n ) );
126 explicit inline constexpr operator bool( )
const {
127 return static_cast<bool>( ptr );
130 friend inline constexpr bool operator==( iterator_wrapper
const &lhs,
131 iterator_wrapper
const &rhs ) {
132 return lhs.ptr == rhs.ptr;
135 friend inline constexpr bool operator!=( iterator_wrapper
const &lhs,
136 iterator_wrapper
const &rhs ) {
137 return lhs.ptr != rhs.ptr;
140 friend inline constexpr bool operator<( iterator_wrapper
const &lhs,
141 iterator_wrapper
const &rhs ) {
142 return lhs.ptr < rhs.ptr;
145 friend inline constexpr bool operator<=( iterator_wrapper
const &lhs,
146 iterator_wrapper
const &rhs ) {
147 return lhs.ptr <= rhs.ptr;
150 friend inline constexpr bool operator>( iterator_wrapper
const &lhs,
151 iterator_wrapper
const &rhs ) {
152 return lhs.ptr > rhs.ptr;
155 friend inline constexpr bool operator>=( iterator_wrapper
const &lhs,
156 iterator_wrapper
const &rhs ) {
157 return lhs.ptr >= rhs.ptr;
163 namespace json_details::serialization {
164 using policy_list =
typename option_list_impl<
165 options::SerializationFormat, options::IndentationType,
166 options::RestrictedStringOutput, options::NewLineDelimiter,
167 options::OutputTrailingComma>::type;
169 template<
typename Policy,
typename Policies>
170 inline constexpr unsigned basic_policy_bits_start =
171 option_bits_start_impl<Policy, Policies>::template calc<>(
172 std::make_index_sequence<pack_size_v<Policies>>{ } );
174 template<
typename Policy>
175 inline constexpr unsigned policy_bits_start =
176 basic_policy_bits_start<Policy, policy_list>;
178 template<
typename Policy>
179 static constexpr void set_bits_in(
json_options_t &value, Policy e ) {
180 static_assert( is_option_flag<Policy>,
181 "Only registered policy types are allowed" );
182 auto new_bits =
static_cast<unsigned>( e );
183 constexpr unsigned mask = (1U << json_option_bits_width<Policy>)-1U;
185 new_bits <<= policy_bits_start<Policy>;
190 template<
typename Policy,
typename... Policies>
192 Policy pol, Policies... pols ) {
193 static_assert( are_option_flags<Policies...>,
194 "Only registered policy types are allowed" );
196 auto new_bits =
static_cast<unsigned>( pol );
197 constexpr unsigned mask = ( (1U << json_option_bits_width<Policy>)-1U );
199 new_bits <<= policy_bits_start<Policy>;
200 value &= ~( mask << policy_bits_start<Policy> );
202 if constexpr(
sizeof...( Policies ) > 0 ) {
203 if constexpr(
sizeof...( pols ) > 0 ) {
204 return set_bits( value, pols... );
213 template<
typename Policy>
215 static_assert( is_option_flag<Policy>,
216 "Only registered policy types are allowed" );
218 new_bits <<= policy_bits_start<Policy>;
223 struct default_policy_flag_t;
225 template<
typename... Policies>
226 struct default_policy_flag_t<pack_list<Policies...>> {
228 ( set_bits_for<Policies>( default_json_option_value<Policies> ) |
236 default_policy_flag_t<policy_list>::value;
238 template<
typename Policy,
typename Result = Policy>
240 static_assert( is_option_flag<Policy>,
241 "Only registered policy types are allowed" );
242 constexpr unsigned mask = ( 1U << (policy_bits_start<Policy> +
243 json_option_bits_width<Policy>)) -
246 value >>= policy_bits_start<Policy>;
247 return static_cast<Result
>( Policy{ value } );
250 template<options::SerializationFormat, options::IndentationType>
251 inline constexpr std::string_view generate_indent{ };
254 inline constexpr std::string_view generate_indent<
255 options::SerializationFormat::Pretty, options::IndentationType::Tab> =
259 inline constexpr std::string_view
260 generate_indent<options::SerializationFormat::Pretty,
261 options::IndentationType::Space1> =
" ";
264 inline constexpr std::string_view
265 generate_indent<options::SerializationFormat::Pretty,
266 options::IndentationType::Space2> =
" ";
269 inline constexpr std::string_view
270 generate_indent<options::SerializationFormat::Pretty,
271 options::IndentationType::Space3> =
" ";
274 inline constexpr std::string_view
275 generate_indent<options::SerializationFormat::Pretty,
276 options::IndentationType::Space4> =
" ";
279 inline constexpr std::string_view
280 generate_indent<options::SerializationFormat::Pretty,
281 options::IndentationType::Space5> =
" ";
284 inline constexpr std::string_view
285 generate_indent<options::SerializationFormat::Pretty,
286 options::IndentationType::Space8> =
" ";
289 inline constexpr std::string_view
290 generate_indent<options::SerializationFormat::Pretty,
291 options::IndentationType::Space10> =
" ";