DAW JSON Link
Loading...
Searching...
No Matches
daw_json_option_bits.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
13#include <daw/cpp_17.h>
14#include <daw/daw_attributes.h>
15#include <daw/daw_bit_count.h>
16#include <daw/daw_constant.h>
17#include <daw/daw_cpp20_concept.h>
18#include <daw/daw_traits.h>
19
20#include <climits>
21#include <cstddef>
22#include <cstdint>
23#include <utility>
24
25namespace daw::json {
26 inline namespace DAW_JSON_VER {
27 using json_options_t = std::uint32_t;
28 namespace json_details {
29
30 template<typename>
31 inline constexpr unsigned json_option_bits_width = 0;
32
33 template<typename>
34 inline constexpr auto default_json_option_value =
35 [] DAW_CPP23_STATIC_CALL_OP {
36 struct unknown_policy {};
37 return unknown_policy{ };
38 }( );
39
40 template<typename Policy, typename Options>
41 struct option_bits_start_impl;
42
43 template<typename Policy, typename... Options>
44 struct option_bits_start_impl<Policy, pack_list<Options...>> {
45 static constexpr auto idx =
46 daw::traits::pack_index_of_v<Policy, Options...>;
47 static_assert( idx >= 0, "Policy is not registered" );
48 using tp_policies = pack_list<Options...>;
49
50 template<std::size_t Pos, int End>
51 static DAW_CONSTEVAL unsigned do_step( ) {
52 if constexpr( Pos >= static_cast<std::size_t>( End ) ) {
53 return 0U;
54 }
55 return json_option_bits_width<pack_element_t<Pos, tp_policies>>;
56 }
57
58 template<std::size_t... Is>
59 DAW_ATTRIB_INLINE static DAW_CONSTEVAL unsigned
60 calc( std::index_sequence<Is...> ) {
61 return ( do_step<Is, idx>( ) + ... );
62 }
63 };
64
65 template<typename... Options>
66 struct option_list_impl {
67 using type = pack_list<Options...>;
68
69 static_assert(
70 ( json_option_bits_width<Options> + ... + 0 ) <=
71 daw::bit_count_v<json_options_t>,
72 "The size of json_options_t is not large enough "
73 "to safely hold all the bits of state. Use a larger size." );
74 };
75
76 template<typename Option>
77 DAW_CPP20_CONCEPT is_option_flag = json_option_bits_width<Option> > 0;
78
79 template<typename... Options>
80 DAW_CPP20_CONCEPT are_option_flags = ( is_option_flag<Options> and ... );
81
82 template<typename Option, typename Options>
83 inline constexpr unsigned basic_option_bits_start =
84 option_bits_start_impl<Option, Options>::template calc<>(
85 std::make_index_sequence<pack_size_v<Options>>{ } );
86
87 template<typename... JsonOptions>
88 struct JsonOptionList;
89
90 template<typename... OptionList, typename Option>
91 static constexpr json_options_t
92 set_bits_for( JsonOptionList<OptionList...>, Option e );
93
94 template<typename... JsonOptions>
95 struct JsonOptionList {
96 using OptionList = typename option_list_impl<JsonOptions...>::type;
97
98 template<typename Option>
99 static constexpr unsigned option_bits_start =
100 basic_option_bits_start<Option, OptionList>;
101
102 template<typename... /*OptionList*/, typename Option>
103 static constexpr json_options_t set_bits_for( Option e ) {
104 static_assert( is_option_flag<Option>,
105 "Only registered policy types are allowed" );
106 auto new_bits = static_cast<json_options_t>( e );
107 new_bits <<= option_bits_start<Option>;
108 return new_bits;
109 }
110
111 static constexpr json_options_t default_option_flag =
112 ( set_bits_for<JsonOptions>(
113 default_json_option_value<JsonOptions> ) |
114 ... | 0 );
115 /***
116 * Create the parser options flag for BasicParseOption
117 * @tparam Options Option types that satisfy the `is_option_flag`
118 * trait.
119 * @param policies A list of parser options to change from the defaults.
120 * @return A json_options_t that encodes the options for the parser
121 */
122 template<typename... Options>
123 static constexpr json_options_t options( Options... options ) {
124 static_assert( json_details::are_option_flags<Options...>,
125 "Only registered option types are allowed" );
126 auto result = default_option_flag;
127 if constexpr( sizeof...( Options ) > 0 ) {
128 result |= ( set_bits_for( options ) | ... );
129 }
130 return result;
131 }
132 };
133
134 template<typename... OptionList, typename Option>
135 constexpr void set_bits_in( JsonOptionList<OptionList...>,
136 json_options_t &value, Option e ) {
137 static_assert( is_option_flag<Option>,
138 "Only registered policy types are allowed" );
139 auto new_bits = static_cast<unsigned>( e );
140 using mask = daw::constant<(1U << json_option_bits_width<Option>)-1U>;
141 new_bits &= mask::value;
142 new_bits <<=
143 JsonOptionList<OptionList...>::template option_bits_start<Option>;
144 value &= ~mask::value;
145 value |= new_bits;
146 }
147
148 template<typename... OptionList, typename Option, typename... Options>
149 constexpr json_options_t set_bits( JsonOptionList<OptionList...>,
150 json_options_t value, Option pol,
151 Options... pols ) {
152 static_assert( are_option_flags<Options...>,
153 "Only registered policy types are allowed" );
154
155 auto new_bits = static_cast<unsigned>( pol );
156 using mask =
157 daw::constant<( (1U << json_option_bits_width<Option>)-1U )>;
158 new_bits &= mask::value;
159 new_bits <<=
160 JsonOptionList<OptionList...>::template option_bits_start<Option>;
161 value &= ~( mask::value << JsonOptionList<
162 OptionList...>::template option_bits_start<Option> );
163 value |= new_bits;
164 if constexpr( sizeof...( Options ) > 0 ) {
165 return set_bits( JsonOptionList<OptionList...>{ }, value, pols... );
166 } else {
167 return value;
168 }
169 }
170
171 template<typename... OptionList, typename Option>
172 static constexpr json_options_t
173 set_bits_for( JsonOptionList<OptionList...>, Option e ) {
174 return JsonOptionList<OptionList...>::set_bits_for( e );
175 }
176
177 template<typename Option, typename Result = Option,
178 typename... OptionList>
179 constexpr Result get_bits_for( JsonOptionList<OptionList...>,
180 json_options_t value ) {
181 static_assert( std::is_enum_v<Option>,
182 "Only enum options are allowed" );
183 static_assert( std::is_same_v<unsigned, std::underlying_type_t<Option>>,
184 "Looks like option was no specified correctly. "
185 "Underlying type should be unsigned" );
186 static_assert( is_option_flag<Option>,
187 "Only registered option types are allowed" );
188 using mask =
189 daw::constant<( 1U << (JsonOptionList<OptionList...>::
190 template option_bits_start<Option> +
191 json_option_bits_width<Option>)) -
192 1U>;
193 value &= mask::value;
194 value >>=
195 JsonOptionList<OptionList...>::template option_bits_start<Option>;
196 return static_cast<Result>( Option{ value } );
197 }
198
199 template<typename... OptionList, typename... Options>
200 constexpr json_options_t json_option( JsonOptionList<OptionList...>,
201 Options... options ) {
202 return JsonOptionList<OptionList...>::options( options... );
203 }
204
205 template<typename>
206 struct default_option_flag_t;
207
208 template<template<class...> class OptionList, typename... Options>
209 struct default_option_flag_t<OptionList<Options...>> {
210 static constexpr json_options_t value =
211 ( OptionList<Options...>::template set_bits_for<Options>(
212 default_json_option_value<Options> ) |
213 ... );
214 };
215
216 /***
217 * The defaults for all known policies encoded as a json_options_t
218 */
219 template<typename OptionList>
220 static inline constexpr json_options_t default_option_flag =
221 default_option_flag_t<OptionList>::value;
222
223 } // namespace json_details
224 } // namespace DAW_JSON_VER
225} // namespace daw::json
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20