@@ -126,6 +126,10 @@ template<typename CharType> struct data_variant
126
126
// cppcheck-suppress noExplicitConstructor
127
127
constexpr data_variant (std::basic_string_view<CharType> s) : value{ s }, selected{ selected_type::string } {}
128
128
129
+ [[nodiscard]] constexpr bool is_boolean () const noexcept {
130
+ return selected == selected_type::boolean;
131
+ }
132
+
129
133
[[nodiscard]] constexpr const bool *get_if_boolean () const noexcept
130
134
{
131
135
if (selected == selected_type::boolean) {
@@ -135,51 +139,80 @@ template<typename CharType> struct data_variant
135
139
}
136
140
}
137
141
142
+ [[nodiscard]] constexpr bool is_array () const noexcept {
143
+ return selected == selected_type::array;
144
+ }
145
+
138
146
[[nodiscard]] constexpr const basic_array_t <CharType> *get_if_array () const noexcept
139
147
{
140
- if (selected == selected_type::array ) {
148
+ if (is_array () ) {
141
149
return &value.array_ ;
142
150
} else {
143
151
return nullptr ;
144
152
}
145
153
}
154
+
155
+ [[nodiscard]] constexpr bool is_object () const noexcept {
156
+ return selected == selected_type::object;
157
+ }
158
+
146
159
[[nodiscard]] constexpr const basic_object_t <CharType> *get_if_object () const noexcept
147
160
{
148
- if (selected == selected_type::object ) {
161
+ if (is_object () ) {
149
162
return &value.object_ ;
150
163
} else {
151
164
return nullptr ;
152
165
}
153
166
}
167
+
168
+ [[nodiscard]] constexpr bool is_integer () const noexcept {
169
+ return selected == selected_type::integer;
170
+ }
171
+
154
172
[[nodiscard]] constexpr const std::int64_t *get_if_integer () const noexcept
155
173
{
156
- if (selected == selected_type::integer ) {
174
+ if (is_integer () ) {
157
175
return &value.int64_t_ ;
158
176
} else {
159
177
return nullptr ;
160
178
}
161
179
}
180
+
181
+ [[nodiscard]] constexpr bool is_uinteger () const noexcept {
182
+ return selected == selected_type::uinteger;
183
+ }
184
+
162
185
[[nodiscard]] constexpr const std::uint64_t *get_if_uinteger () const noexcept
163
186
{
164
- if (selected == selected_type::uinteger ) {
187
+ if (is_uinteger () ) {
165
188
return &value.uint64_t_ ;
166
189
} else {
167
190
return nullptr ;
168
191
}
169
192
}
170
193
194
+
195
+ [[nodiscard]] constexpr bool is_floating_point () const noexcept {
196
+ return selected == selected_type::floating_point;
197
+ }
198
+
199
+
171
200
[[nodiscard]] constexpr const double *get_if_floating_point () const noexcept
172
201
{
173
- if (selected == selected_type::floating_point ) {
202
+ if (is_floating_point () ) {
174
203
return &value.double_ ;
175
204
} else {
176
205
return nullptr ;
177
206
}
178
207
}
179
208
209
+ [[nodiscard]] constexpr bool is_string () const noexcept {
210
+ return selected == selected_type::string;
211
+ }
212
+
180
213
[[nodiscard]] constexpr const std::basic_string_view<CharType> *get_if_string () const noexcept
181
214
{
182
- if (selected == selected_type::string ) {
215
+ if (is_string () ) {
183
216
return &value.string_view_ ;
184
217
} else {
185
218
return nullptr ;
@@ -369,17 +402,17 @@ template<typename CharType> struct basic_json
369
402
370
403
constexpr const auto &array_data () const
371
404
{
372
- if (const auto *result = data.get_if_array (); result != nullptr ) {
373
- return *result ;
405
+ if (data.is_array () ) {
406
+ return *data. get_if_array () ;
374
407
} else {
375
408
throw std::runtime_error (" value is not an array type" );
376
409
}
377
410
}
378
411
379
412
constexpr const auto &object_data () const
380
413
{
381
- if (const auto *result = data.get_if_object (); result != nullptr ) {
382
- return *result ;
414
+ if (data.is_object () ) {
415
+ return *data. get_if_object () ;
383
416
} else {
384
417
throw std::runtime_error (" value is not an object type" );
385
418
}
@@ -394,25 +427,25 @@ template<typename CharType> struct basic_json
394
427
// but it's necessary for API compatibility with nlohmann::json
395
428
if constexpr (std::is_same_v<Type,
396
429
std::uint64_t > || std::is_same_v<Type, std::int64_t > || std::is_same_v<Type, double >) {
397
- if (const auto *uint_value = data.get_if_uinteger (); uint_value != nullptr ) {
398
- return Type (*uint_value );
399
- } else if (const auto *value = data.get_if_integer (); value != nullptr ) {
400
- return Type (*value );
401
- } else if (const auto *fpvalue = data.get_if_floating_point (); fpvalue != nullptr ) {
402
- return Type (*fpvalue );
430
+ if (data.is_uinteger () ) {
431
+ return Type (*data. get_if_uinteger () );
432
+ } else if (data.is_integer () ) {
433
+ return Type (*data. get_if_integer () );
434
+ } else if (data.is_floating_point () ) {
435
+ return Type (*data. get_if_floating_point () );
403
436
} else {
404
437
throw std::runtime_error (" Unexpected type: number requested" );// + ss.str() );
405
438
}
406
439
} else if constexpr (std::is_same_v<Type,
407
440
std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
408
- if (const auto *value = data.get_if_string (); value != nullptr ) {
409
- return *value ;
441
+ if (data.is_string () ) {
442
+ return *data. get_if_string () ;
410
443
} else {
411
444
throw std::runtime_error (" Unexpected type: string-like requested" );
412
445
}
413
446
} else if constexpr (std::is_same_v<Type, bool >) {
414
- if (const auto *value = data.get_if_boolean (); value != nullptr ) {
415
- return *value ;
447
+ if (data.is_boolean () ) {
448
+ return *data. get_if_boolean () ;
416
449
} else {
417
450
throw std::runtime_error (" Unexpected type: bool requested" );
418
451
}
0 commit comments