@@ -126,6 +126,8 @@ 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 { return selected == selected_type::boolean; }
130
+
129
131
[[nodiscard]] constexpr const bool *get_if_boolean () const noexcept
130
132
{
131
133
if (selected == selected_type::boolean) {
@@ -135,51 +137,68 @@ template<typename CharType> struct data_variant
135
137
}
136
138
}
137
139
140
+ [[nodiscard]] constexpr bool is_array () const noexcept { return selected == selected_type::array; }
141
+
138
142
[[nodiscard]] constexpr const basic_array_t <CharType> *get_if_array () const noexcept
139
143
{
140
- if (selected == selected_type::array ) {
144
+ if (is_array () ) {
141
145
return &value.array_ ;
142
146
} else {
143
147
return nullptr ;
144
148
}
145
149
}
150
+
151
+ [[nodiscard]] constexpr bool is_object () const noexcept { return selected == selected_type::object; }
152
+
146
153
[[nodiscard]] constexpr const basic_object_t <CharType> *get_if_object () const noexcept
147
154
{
148
- if (selected == selected_type::object ) {
155
+ if (is_object () ) {
149
156
return &value.object_ ;
150
157
} else {
151
158
return nullptr ;
152
159
}
153
160
}
161
+
162
+ [[nodiscard]] constexpr bool is_integer () const noexcept { return selected == selected_type::integer; }
163
+
154
164
[[nodiscard]] constexpr const std::int64_t *get_if_integer () const noexcept
155
165
{
156
- if (selected == selected_type::integer ) {
166
+ if (is_integer () ) {
157
167
return &value.int64_t_ ;
158
168
} else {
159
169
return nullptr ;
160
170
}
161
171
}
172
+
173
+ [[nodiscard]] constexpr bool is_uinteger () const noexcept { return selected == selected_type::uinteger; }
174
+
162
175
[[nodiscard]] constexpr const std::uint64_t *get_if_uinteger () const noexcept
163
176
{
164
- if (selected == selected_type::uinteger ) {
177
+ if (is_uinteger () ) {
165
178
return &value.uint64_t_ ;
166
179
} else {
167
180
return nullptr ;
168
181
}
169
182
}
170
183
184
+
185
+ [[nodiscard]] constexpr bool is_floating_point () const noexcept { return selected == selected_type::floating_point; }
186
+
187
+
171
188
[[nodiscard]] constexpr const double *get_if_floating_point () const noexcept
172
189
{
173
- if (selected == selected_type::floating_point ) {
190
+ if (is_floating_point () ) {
174
191
return &value.double_ ;
175
192
} else {
176
193
return nullptr ;
177
194
}
178
195
}
179
196
197
+ [[nodiscard]] constexpr bool is_string () const noexcept { return selected == selected_type::string; }
198
+
180
199
[[nodiscard]] constexpr const std::basic_string_view<CharType> *get_if_string () const noexcept
181
200
{
182
- if (selected == selected_type::string ) {
201
+ if (is_string () ) {
183
202
return &value.string_view_ ;
184
203
} else {
185
204
return nullptr ;
@@ -199,7 +218,7 @@ template<typename CharType> struct basic_json
199
218
: parent_value_(&value), index_{ index }
200
219
{}
201
220
202
- constexpr const basic_json &operator *() const noexcept
221
+ constexpr const basic_json &operator *() const
203
222
{
204
223
if (parent_value_->is_array ()) {
205
224
return (*parent_value_)[index_];
@@ -340,7 +359,7 @@ template<typename CharType> struct basic_json
340
359
}
341
360
}
342
361
343
- template <typename Key>[[nodiscard]] constexpr std::size_t count (const Key &key) const noexcept
362
+ template <typename Key> [[nodiscard]] constexpr std::size_t count (const Key &key) const
344
363
{
345
364
if (is_object ()) {
346
365
const auto found = find (key);
@@ -353,7 +372,7 @@ template<typename CharType> struct basic_json
353
372
return 0 ;
354
373
}
355
374
356
- [[nodiscard]] constexpr iterator find (const std::basic_string_view<CharType> key) const noexcept
375
+ [[nodiscard]] constexpr iterator find (const std::basic_string_view<CharType> key) const
357
376
{
358
377
for (auto itr = begin (); itr != end (); ++itr) {
359
378
if (itr.key () == key) { return itr; }
@@ -369,17 +388,17 @@ template<typename CharType> struct basic_json
369
388
370
389
constexpr const auto &array_data () const
371
390
{
372
- if (const auto *result = data.get_if_array (); result != nullptr ) {
373
- return *result ;
391
+ if (data.is_array () ) {
392
+ return *data. get_if_array () ;
374
393
} else {
375
394
throw std::runtime_error (" value is not an array type" );
376
395
}
377
396
}
378
397
379
398
constexpr const auto &object_data () const
380
399
{
381
- if (const auto *result = data.get_if_object (); result != nullptr ) {
382
- return *result ;
400
+ if (data.is_object () ) {
401
+ return *data. get_if_object () ;
383
402
} else {
384
403
throw std::runtime_error (" value is not an object type" );
385
404
}
@@ -388,35 +407,37 @@ template<typename CharType> struct basic_json
388
407
constexpr static basic_json object () { return basic_json{ data_t { basic_object_t <CharType>{} } }; }
389
408
constexpr static basic_json array () { return basic_json{ data_t { basic_array_t <CharType>{} } }; }
390
409
391
- template <typename Type>[[nodiscard]] constexpr auto get () const
410
+ template <typename Type> [[nodiscard]] constexpr auto get () const
392
411
{
393
412
// I don't like this level of implicit conversions in the `get()` function,
394
413
// but it's necessary for API compatibility with nlohmann::json
395
- if constexpr (std::is_same_v<Type, std::uint64_t > || std::is_same_v<Type, std::int64_t > || std::is_same_v<Type, double >) {
396
- if (const auto *uint_value = data.get_if_uinteger (); uint_value != nullptr ) {
397
- return Type (*uint_value);
398
- } else if (const auto *value = data.get_if_integer (); value != nullptr ) {
399
- return Type (*value);
400
- } else if (const auto *fpvalue = data.get_if_floating_point (); fpvalue != nullptr ) {
401
- return Type (*fpvalue);
414
+ if constexpr (std::is_same_v<Type,
415
+ std::uint64_t > || std::is_same_v<Type, std::int64_t > || std::is_same_v<Type, double >) {
416
+ if (data.is_uinteger ()) {
417
+ return Type (*data.get_if_uinteger ());
418
+ } else if (data.is_integer ()) {
419
+ return Type (*data.get_if_integer ());
420
+ } else if (data.is_floating_point ()) {
421
+ return Type (*data.get_if_floating_point ());
402
422
} else {
403
423
throw std::runtime_error (" Unexpected type: number requested" );// + ss.str() );
404
424
}
405
425
} else if constexpr (std::is_same_v<Type,
406
426
std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
407
- if (const auto *value = data.get_if_string (); value != nullptr ) { return *value; }
408
- else {
427
+ if (data.is_string ()) {
428
+ return *data.get_if_string ();
429
+ } else {
409
430
throw std::runtime_error (" Unexpected type: string-like requested" );
410
431
}
411
432
} else if constexpr (std::is_same_v<Type, bool >) {
412
- if (const auto *value = data.get_if_boolean (); value != nullptr ) { return *value; }
413
- else {
433
+ if (data.is_boolean ()) {
434
+ return *data.get_if_boolean ();
435
+ } else {
414
436
throw std::runtime_error (" Unexpected type: bool requested" );
415
437
}
416
438
} else {
417
439
throw std::runtime_error (" Unexpected type for get()" );
418
440
}
419
-
420
441
}
421
442
422
443
[[nodiscard]] constexpr bool is_object () const noexcept { return data.selected == data_t ::selected_type::object; }
0 commit comments