@@ -50,11 +50,14 @@ template<typename T> struct span
50
50
51
51
constexpr span () : begin_{ nullptr }, end_{ nullptr } {}
52
52
53
- constexpr const T *begin () const { return begin_; }
53
+ [[nodiscard]] constexpr const T *begin () const noexcept { return begin_; }
54
54
55
- constexpr const T *end () const { return end_; }
55
+ [[nodiscard]] constexpr const T *end () const noexcept { return end_; }
56
56
57
- constexpr std::size_t size () const { return static_cast <std::size_t >(std::distance (begin_, end_)); }
57
+ [[nodiscard]] constexpr std::size_t size () const noexcept
58
+ {
59
+ return static_cast <std::size_t >(std::distance (begin_, end_));
60
+ }
58
61
59
62
const T *begin_;
60
63
const T *end_;
@@ -188,10 +191,11 @@ template<typename CharType> struct basic_json
188
191
189
192
struct iterator
190
193
{
191
- constexpr explicit iterator (const basic_json &value, std::size_t index = 0 ) : parent_value_(&value), index_{ index }
194
+ constexpr explicit iterator (const basic_json &value, std::size_t index = 0 ) noexcept
195
+ : parent_value_(&value), index_{ index }
192
196
{}
193
197
194
- constexpr const basic_json &operator *() const
198
+ constexpr const basic_json &operator *() const noexcept
195
199
{
196
200
if (parent_value_->is_array ()) {
197
201
return (*parent_value_)[index_];
@@ -202,11 +206,11 @@ template<typename CharType> struct basic_json
202
206
}
203
207
}
204
208
205
- constexpr const basic_json *operator ->() const { return &(*(*this )); }
209
+ constexpr const basic_json *operator ->() const noexcept { return &(*(*this )); }
206
210
207
- constexpr std::size_t index () const { return index_; }
211
+ constexpr std::size_t index () const noexcept { return index_; }
208
212
209
- constexpr const basic_json &value () const { return *(*this ); }
213
+ constexpr const basic_json &value () const noexcept { return *(*this ); }
210
214
211
215
212
216
constexpr std::basic_string_view<CharType> key () const
@@ -218,53 +222,52 @@ template<typename CharType> struct basic_json
218
222
}
219
223
}
220
224
221
- constexpr bool operator ==(const iterator &other) const
225
+ constexpr bool operator ==(const iterator &other) const noexcept
222
226
{
223
227
return other.parent_value_ == parent_value_ && other.index_ == index_;
224
228
}
225
- constexpr bool operator !=(const iterator &other) const { return !(*this == other); }
229
+ constexpr bool operator !=(const iterator &other) const noexcept { return !(*this == other); }
226
230
227
231
228
- constexpr bool operator <(const iterator &other) const
232
+ constexpr bool operator <(const iterator &other) const noexcept
229
233
{
230
- assert (other.parent_value_ == parent_value_);
231
- return index_ < other.index_ ;
234
+ return other.parent_value_ == parent_value_ && index_ < other.index_ ;
232
235
}
233
236
234
- constexpr iterator &operator --()
237
+ constexpr iterator &operator --() noexcept
235
238
{
236
239
--index_;
237
240
return *this ;
238
241
}
239
242
240
- constexpr iterator operator --(int )
243
+ [[nodiscard]] constexpr iterator operator --(int ) noexcept
241
244
{
242
245
iterator result{ *this };
243
246
index_--;
244
247
return result;
245
248
}
246
249
247
- constexpr iterator &operator ++()
250
+ constexpr iterator &operator ++() noexcept
248
251
{
249
252
++index_;
250
253
return *this ;
251
254
}
252
255
253
- constexpr iterator operator ++(int )
256
+ [[nodiscard]] constexpr iterator operator ++(int ) noexcept
254
257
{
255
258
iterator result{ *this };
256
259
index_++;
257
260
return result;
258
261
}
259
262
260
- constexpr iterator &operator +=(const std::ptrdiff_t value)
263
+ constexpr iterator &operator +=(const std::ptrdiff_t value) noexcept
261
264
{
262
265
index_ = static_cast <std::size_t >(static_cast <std::ptrdiff_t >(index_) + value);
263
266
return *this ;
264
267
}
265
268
266
269
267
- constexpr iterator &operator +=(const std::size_t value)
270
+ constexpr iterator &operator +=(const std::size_t value) noexcept
268
271
{
269
272
index_ += value;
270
273
return *this ;
@@ -276,25 +279,24 @@ template<typename CharType> struct basic_json
276
279
277
280
using const_iterator = iterator;
278
281
279
- constexpr iterator begin () const { return iterator{ *this }; }
282
+ [[nodiscard]] constexpr iterator begin () const noexcept { return iterator{ *this }; }
280
283
281
- constexpr iterator end () const { return iterator{ *this , size () }; }
284
+ [[nodiscard]] constexpr iterator end () const noexcept { return iterator{ *this , size () }; }
282
285
283
- constexpr iterator cbegin () const { return begin (); }
286
+ [[nodiscard]] constexpr iterator cbegin () const noexcept { return begin (); }
284
287
285
- constexpr iterator cend () const { return end (); }
288
+ [[nodiscard]] constexpr iterator cend () const noexcept { return end (); }
286
289
287
- constexpr std::size_t size () const noexcept
290
+ [[nodiscard]] constexpr std::size_t size () const noexcept
288
291
{
289
292
if (is_null ()) { return 0 ; }
290
293
if (is_object ()) { return data.get_if_object ()->size (); }
291
-
292
294
if (is_array ()) { return data.get_if_array ()->size (); }
293
295
294
296
return 1 ;
295
297
}
296
298
297
- constexpr const basic_json &operator [](const std::size_t idx) const
299
+ [[nodiscard]] constexpr const basic_json &operator [](const std::size_t idx) const
298
300
{
299
301
if (const auto &children = array_data (); idx < children.size ()) {
300
302
return *std::next (children.begin (), static_cast <std::ptrdiff_t >(idx));
@@ -303,7 +305,7 @@ template<typename CharType> struct basic_json
303
305
}
304
306
}
305
307
306
- constexpr iterator find (const std::basic_string_view<CharType> key) const
308
+ [[nodiscard]] constexpr iterator find (const std::basic_string_view<CharType> key) const noexcept
307
309
{
308
310
for (auto itr = begin (); itr != end (); ++itr) {
309
311
if (itr.key () == key) { return itr; }
@@ -312,7 +314,7 @@ template<typename CharType> struct basic_json
312
314
return end ();
313
315
}
314
316
315
- constexpr const basic_json &operator [](const std::basic_string_view<CharType> key) const
317
+ [[nodiscard]] constexpr const basic_json &operator [](const std::basic_string_view<CharType> key) const
316
318
{
317
319
const auto &children = object_data ();
318
320
@@ -359,7 +361,7 @@ template<typename CharType> struct basic_json
359
361
constexpr static basic_json object () { return basic_json{ data_t { basic_object_t <CharType>{} } }; }
360
362
constexpr static basic_json array () { return basic_json{ data_t { basic_array_t <CharType>{} } }; }
361
363
362
- template <typename Type> constexpr Type get () const
364
+ template <typename Type> [[nodiscard]] constexpr Type get () const
363
365
{
364
366
if constexpr (std::is_same_v<Type, std::uint64_t > || std::is_same_v<Type, std::int64_t >) {
365
367
if (const auto *uint_value = data.get_if_uinteger (); uint_value != nullptr ) {
@@ -381,59 +383,32 @@ template<typename CharType> struct basic_json
381
383
throw std::runtime_error (" Incorrect type for get()" );
382
384
}
383
385
384
- constexpr bool is_object () const noexcept { return data.selected == data_t ::selected_type::object; }
385
-
386
- constexpr bool is_array () const noexcept { return data.selected == data_t ::selected_type::array; }
387
-
388
- constexpr bool is_string () const noexcept { return data.selected == data_t ::selected_type::string; }
389
-
390
- constexpr bool is_boolean () const noexcept { return data.selected == data_t ::selected_type::boolean; }
391
-
392
- constexpr bool is_structured () const noexcept { return is_object () || is_array (); }
393
-
394
- constexpr bool is_number () const noexcept { return is_number_integer () || is_number_float (); }
395
-
396
- constexpr double as_number_float () const
386
+ [[nodiscard]] constexpr bool is_object () const noexcept { return data.selected == data_t ::selected_type::object; }
387
+ [[nodiscard]] constexpr bool is_array () const noexcept { return data.selected == data_t ::selected_type::array; }
388
+ [[nodiscard]] constexpr bool is_string () const noexcept { return data.selected == data_t ::selected_type::string; }
389
+ [[nodiscard]] constexpr bool is_boolean () const noexcept { return data.selected == data_t ::selected_type::boolean; }
390
+ [[nodiscard]] constexpr bool is_structured () const noexcept { return is_object () || is_array (); }
391
+ [[nodiscard]] constexpr bool is_number () const noexcept { return is_number_integer () || is_number_float (); }
392
+ [[nodiscard]] constexpr bool is_number_integer () const noexcept { return is_number_signed () || is_number_unsigned (); }
393
+ [[nodiscard]] constexpr bool is_null () const noexcept { return data.selected == data_t ::selected_type::empty; }
394
+ [[nodiscard]] constexpr bool is_binary () const noexcept { return data.selected == data_t ::selected_type::binary; }
395
+
396
+ [[nodiscard]] constexpr bool is_number_signed () const noexcept
397
397
{
398
- if (const double *value = data.get_if_floating_point (); value != nullptr ) {
399
- return *value;
400
- } else {
401
- throw std::runtime_error (" Not a float type" );
402
- }
398
+ return data.selected == data_t ::selected_type::integer;
403
399
}
404
- constexpr double as_boolean () const
400
+
401
+ [[nodiscard]] constexpr bool is_number_unsigned () const noexcept
405
402
{
406
- if (const bool *value = data.get_if_boolean (); value != nullptr ) {
407
- return *value;
408
- } else {
409
- throw std::runtime_error (" Not a boolean type" );
410
- }
403
+ return data.selected == data_t ::selected_type::uinteger;
411
404
}
412
405
413
- constexpr auto as_string () const
406
+ [[nodiscard]] constexpr bool is_number_float () const noexcept
414
407
{
415
- if (const auto *value = data.get_if_string (); value != nullptr ) {
416
- return *value;
417
- } else {
418
- throw std::runtime_error (" Not a string type" );
419
- }
408
+ return data.selected == data_t ::selected_type::floating_point;
420
409
}
421
410
422
- constexpr operator double () const { return as_number_float (); }
423
-
424
- constexpr bool is_number_integer () const noexcept { return is_number_signed () || is_number_unsigned (); }
425
-
426
- constexpr bool is_number_signed () const noexcept { return data.selected == data_t ::selected_type::integer; }
427
-
428
- constexpr bool is_number_unsigned () const noexcept { return data.selected == data_t ::selected_type::uinteger; }
429
-
430
- constexpr bool is_number_float () const noexcept { return data.selected == data_t ::selected_type::floating_point; }
431
-
432
- constexpr bool is_null () const noexcept { return data.selected == data_t ::selected_type::empty; }
433
-
434
- constexpr bool is_binary () const noexcept { return data.selected == data_t ::selected_type::binary; }
435
-
436
- constexpr bool is_primitive () const noexcept
411
+ [[nodiscard]] constexpr bool is_primitive () const noexcept
437
412
{
438
413
return is_null () || is_string () || is_boolean () || is_number () || is_binary ();
439
414
}
0 commit comments