iter_impl.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #pragma once
  9. #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
  10. #include <type_traits> // conditional, is_const, remove_const
  11. #include <nlohmann/detail/exceptions.hpp>
  12. #include <nlohmann/detail/iterators/internal_iterator.hpp>
  13. #include <nlohmann/detail/iterators/primitive_iterator.hpp>
  14. #include <nlohmann/detail/macro_scope.hpp>
  15. #include <nlohmann/detail/meta/cpp_future.hpp>
  16. #include <nlohmann/detail/meta/type_traits.hpp>
  17. #include <nlohmann/detail/value_t.hpp>
  18. NLOHMANN_JSON_NAMESPACE_BEGIN
  19. namespace detail
  20. {
  21. // forward declare, to be able to friend it later on
  22. template<typename IteratorType> class iteration_proxy;
  23. template<typename IteratorType> class iteration_proxy_value;
  24. /*!
  25. @brief a template for a bidirectional iterator for the @ref basic_json class
  26. This class implements a both iterators (iterator and const_iterator) for the
  27. @ref basic_json class.
  28. @note An iterator is called *initialized* when a pointer to a JSON value has
  29. been set (e.g., by a constructor or a copy assignment). If the iterator is
  30. default-constructed, it is *uninitialized* and most methods are undefined.
  31. **The library uses assertions to detect calls on uninitialized iterators.**
  32. @requirement The class satisfies the following concept requirements:
  33. -
  34. [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
  35. The iterator that can be moved can be moved in both directions (i.e.
  36. incremented and decremented).
  37. @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
  38. iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
  39. */
  40. template<typename BasicJsonType>
  41. class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
  42. {
  43. /// the iterator with BasicJsonType of different const-ness
  44. using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
  45. /// allow basic_json to access private members
  46. friend other_iter_impl;
  47. friend BasicJsonType;
  48. friend iteration_proxy<iter_impl>;
  49. friend iteration_proxy_value<iter_impl>;
  50. using object_t = typename BasicJsonType::object_t;
  51. using array_t = typename BasicJsonType::array_t;
  52. // make sure BasicJsonType is basic_json or const basic_json
  53. static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
  54. "iter_impl only accepts (const) basic_json");
  55. // superficial check for the LegacyBidirectionalIterator named requirement
  56. static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
  57. && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
  58. "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
  59. public:
  60. /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
  61. /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
  62. /// A user-defined iterator should provide publicly accessible typedefs named
  63. /// iterator_category, value_type, difference_type, pointer, and reference.
  64. /// Note that value_type is required to be non-const, even for constant iterators.
  65. using iterator_category = std::bidirectional_iterator_tag;
  66. /// the type of the values when the iterator is dereferenced
  67. using value_type = typename BasicJsonType::value_type;
  68. /// a type to represent differences between iterators
  69. using difference_type = typename BasicJsonType::difference_type;
  70. /// defines a pointer to the type iterated over (value_type)
  71. using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
  72. typename BasicJsonType::const_pointer,
  73. typename BasicJsonType::pointer>::type;
  74. /// defines a reference to the type iterated over (value_type)
  75. using reference =
  76. typename std::conditional<std::is_const<BasicJsonType>::value,
  77. typename BasicJsonType::const_reference,
  78. typename BasicJsonType::reference>::type;
  79. iter_impl() = default;
  80. ~iter_impl() = default;
  81. iter_impl(iter_impl&&) noexcept = default;
  82. iter_impl& operator=(iter_impl&&) noexcept = default;
  83. /*!
  84. @brief constructor for a given JSON instance
  85. @param[in] object pointer to a JSON object for this iterator
  86. @pre object != nullptr
  87. @post The iterator is initialized; i.e. `m_object != nullptr`.
  88. */
  89. explicit iter_impl(pointer object) noexcept : m_object(object)
  90. {
  91. JSON_ASSERT(m_object != nullptr);
  92. switch (m_object->m_data.m_type)
  93. {
  94. case value_t::object:
  95. {
  96. m_it.object_iterator = typename object_t::iterator();
  97. break;
  98. }
  99. case value_t::array:
  100. {
  101. m_it.array_iterator = typename array_t::iterator();
  102. break;
  103. }
  104. case value_t::null:
  105. case value_t::string:
  106. case value_t::boolean:
  107. case value_t::number_integer:
  108. case value_t::number_unsigned:
  109. case value_t::number_float:
  110. case value_t::binary:
  111. case value_t::discarded:
  112. default:
  113. {
  114. m_it.primitive_iterator = primitive_iterator_t();
  115. break;
  116. }
  117. }
  118. }
  119. /*!
  120. @note The conventional copy constructor and copy assignment are implicitly
  121. defined. Combined with the following converting constructor and
  122. assignment, they support: (1) copy from iterator to iterator, (2)
  123. copy from const iterator to const iterator, and (3) conversion from
  124. iterator to const iterator. However conversion from const iterator
  125. to iterator is not defined.
  126. */
  127. /*!
  128. @brief const copy constructor
  129. @param[in] other const iterator to copy from
  130. @note This copy constructor had to be defined explicitly to circumvent a bug
  131. occurring on msvc v19.0 compiler (VS 2015) debug build. For more
  132. information refer to: https://github.com/nlohmann/json/issues/1608
  133. */
  134. iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
  135. : m_object(other.m_object), m_it(other.m_it)
  136. {}
  137. /*!
  138. @brief converting assignment
  139. @param[in] other const iterator to copy from
  140. @return const/non-const iterator
  141. @note It is not checked whether @a other is initialized.
  142. */
  143. iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
  144. {
  145. if (&other != this)
  146. {
  147. m_object = other.m_object;
  148. m_it = other.m_it;
  149. }
  150. return *this;
  151. }
  152. /*!
  153. @brief converting constructor
  154. @param[in] other non-const iterator to copy from
  155. @note It is not checked whether @a other is initialized.
  156. */
  157. iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
  158. : m_object(other.m_object), m_it(other.m_it)
  159. {}
  160. /*!
  161. @brief converting assignment
  162. @param[in] other non-const iterator to copy from
  163. @return const/non-const iterator
  164. @note It is not checked whether @a other is initialized.
  165. */
  166. iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
  167. {
  168. m_object = other.m_object;
  169. m_it = other.m_it;
  170. return *this;
  171. }
  172. JSON_PRIVATE_UNLESS_TESTED:
  173. /*!
  174. @brief set the iterator to the first value
  175. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  176. */
  177. void set_begin() noexcept
  178. {
  179. JSON_ASSERT(m_object != nullptr);
  180. switch (m_object->m_data.m_type)
  181. {
  182. case value_t::object:
  183. {
  184. m_it.object_iterator = m_object->m_data.m_value.object->begin();
  185. break;
  186. }
  187. case value_t::array:
  188. {
  189. m_it.array_iterator = m_object->m_data.m_value.array->begin();
  190. break;
  191. }
  192. case value_t::null:
  193. {
  194. // set to end so begin()==end() is true: null is empty
  195. m_it.primitive_iterator.set_end();
  196. break;
  197. }
  198. case value_t::string:
  199. case value_t::boolean:
  200. case value_t::number_integer:
  201. case value_t::number_unsigned:
  202. case value_t::number_float:
  203. case value_t::binary:
  204. case value_t::discarded:
  205. default:
  206. {
  207. m_it.primitive_iterator.set_begin();
  208. break;
  209. }
  210. }
  211. }
  212. /*!
  213. @brief set the iterator past the last value
  214. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  215. */
  216. void set_end() noexcept
  217. {
  218. JSON_ASSERT(m_object != nullptr);
  219. switch (m_object->m_data.m_type)
  220. {
  221. case value_t::object:
  222. {
  223. m_it.object_iterator = m_object->m_data.m_value.object->end();
  224. break;
  225. }
  226. case value_t::array:
  227. {
  228. m_it.array_iterator = m_object->m_data.m_value.array->end();
  229. break;
  230. }
  231. case value_t::null:
  232. case value_t::string:
  233. case value_t::boolean:
  234. case value_t::number_integer:
  235. case value_t::number_unsigned:
  236. case value_t::number_float:
  237. case value_t::binary:
  238. case value_t::discarded:
  239. default:
  240. {
  241. m_it.primitive_iterator.set_end();
  242. break;
  243. }
  244. }
  245. }
  246. public:
  247. /*!
  248. @brief return a reference to the value pointed to by the iterator
  249. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  250. */
  251. reference operator*() const
  252. {
  253. JSON_ASSERT(m_object != nullptr);
  254. switch (m_object->m_data.m_type)
  255. {
  256. case value_t::object:
  257. {
  258. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  259. return m_it.object_iterator->second;
  260. }
  261. case value_t::array:
  262. {
  263. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  264. return *m_it.array_iterator;
  265. }
  266. case value_t::null:
  267. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  268. case value_t::string:
  269. case value_t::boolean:
  270. case value_t::number_integer:
  271. case value_t::number_unsigned:
  272. case value_t::number_float:
  273. case value_t::binary:
  274. case value_t::discarded:
  275. default:
  276. {
  277. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  278. {
  279. return *m_object;
  280. }
  281. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  282. }
  283. }
  284. }
  285. /*!
  286. @brief dereference the iterator
  287. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  288. */
  289. pointer operator->() const
  290. {
  291. JSON_ASSERT(m_object != nullptr);
  292. switch (m_object->m_data.m_type)
  293. {
  294. case value_t::object:
  295. {
  296. JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
  297. return &(m_it.object_iterator->second);
  298. }
  299. case value_t::array:
  300. {
  301. JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
  302. return &*m_it.array_iterator;
  303. }
  304. case value_t::null:
  305. case value_t::string:
  306. case value_t::boolean:
  307. case value_t::number_integer:
  308. case value_t::number_unsigned:
  309. case value_t::number_float:
  310. case value_t::binary:
  311. case value_t::discarded:
  312. default:
  313. {
  314. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
  315. {
  316. return m_object;
  317. }
  318. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  319. }
  320. }
  321. }
  322. /*!
  323. @brief post-increment (it++)
  324. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  325. */
  326. iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
  327. {
  328. auto result = *this;
  329. ++(*this);
  330. return result;
  331. }
  332. /*!
  333. @brief pre-increment (++it)
  334. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  335. */
  336. iter_impl& operator++()
  337. {
  338. JSON_ASSERT(m_object != nullptr);
  339. switch (m_object->m_data.m_type)
  340. {
  341. case value_t::object:
  342. {
  343. std::advance(m_it.object_iterator, 1);
  344. break;
  345. }
  346. case value_t::array:
  347. {
  348. std::advance(m_it.array_iterator, 1);
  349. break;
  350. }
  351. case value_t::null:
  352. case value_t::string:
  353. case value_t::boolean:
  354. case value_t::number_integer:
  355. case value_t::number_unsigned:
  356. case value_t::number_float:
  357. case value_t::binary:
  358. case value_t::discarded:
  359. default:
  360. {
  361. ++m_it.primitive_iterator;
  362. break;
  363. }
  364. }
  365. return *this;
  366. }
  367. /*!
  368. @brief post-decrement (it--)
  369. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  370. */
  371. iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
  372. {
  373. auto result = *this;
  374. --(*this);
  375. return result;
  376. }
  377. /*!
  378. @brief pre-decrement (--it)
  379. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  380. */
  381. iter_impl& operator--()
  382. {
  383. JSON_ASSERT(m_object != nullptr);
  384. switch (m_object->m_data.m_type)
  385. {
  386. case value_t::object:
  387. {
  388. std::advance(m_it.object_iterator, -1);
  389. break;
  390. }
  391. case value_t::array:
  392. {
  393. std::advance(m_it.array_iterator, -1);
  394. break;
  395. }
  396. case value_t::null:
  397. case value_t::string:
  398. case value_t::boolean:
  399. case value_t::number_integer:
  400. case value_t::number_unsigned:
  401. case value_t::number_float:
  402. case value_t::binary:
  403. case value_t::discarded:
  404. default:
  405. {
  406. --m_it.primitive_iterator;
  407. break;
  408. }
  409. }
  410. return *this;
  411. }
  412. /*!
  413. @brief comparison: equal
  414. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  415. */
  416. template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
  417. bool operator==(const IterImpl& other) const
  418. {
  419. // if objects are not the same, the comparison is undefined
  420. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  421. {
  422. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  423. }
  424. JSON_ASSERT(m_object != nullptr);
  425. switch (m_object->m_data.m_type)
  426. {
  427. case value_t::object:
  428. return (m_it.object_iterator == other.m_it.object_iterator);
  429. case value_t::array:
  430. return (m_it.array_iterator == other.m_it.array_iterator);
  431. case value_t::null:
  432. case value_t::string:
  433. case value_t::boolean:
  434. case value_t::number_integer:
  435. case value_t::number_unsigned:
  436. case value_t::number_float:
  437. case value_t::binary:
  438. case value_t::discarded:
  439. default:
  440. return (m_it.primitive_iterator == other.m_it.primitive_iterator);
  441. }
  442. }
  443. /*!
  444. @brief comparison: not equal
  445. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  446. */
  447. template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
  448. bool operator!=(const IterImpl& other) const
  449. {
  450. return !operator==(other);
  451. }
  452. /*!
  453. @brief comparison: smaller
  454. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  455. */
  456. bool operator<(const iter_impl& other) const
  457. {
  458. // if objects are not the same, the comparison is undefined
  459. if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
  460. {
  461. JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
  462. }
  463. JSON_ASSERT(m_object != nullptr);
  464. switch (m_object->m_data.m_type)
  465. {
  466. case value_t::object:
  467. JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
  468. case value_t::array:
  469. return (m_it.array_iterator < other.m_it.array_iterator);
  470. case value_t::null:
  471. case value_t::string:
  472. case value_t::boolean:
  473. case value_t::number_integer:
  474. case value_t::number_unsigned:
  475. case value_t::number_float:
  476. case value_t::binary:
  477. case value_t::discarded:
  478. default:
  479. return (m_it.primitive_iterator < other.m_it.primitive_iterator);
  480. }
  481. }
  482. /*!
  483. @brief comparison: less than or equal
  484. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  485. */
  486. bool operator<=(const iter_impl& other) const
  487. {
  488. return !other.operator < (*this);
  489. }
  490. /*!
  491. @brief comparison: greater than
  492. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  493. */
  494. bool operator>(const iter_impl& other) const
  495. {
  496. return !operator<=(other);
  497. }
  498. /*!
  499. @brief comparison: greater than or equal
  500. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  501. */
  502. bool operator>=(const iter_impl& other) const
  503. {
  504. return !operator<(other);
  505. }
  506. /*!
  507. @brief add to iterator
  508. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  509. */
  510. iter_impl& operator+=(difference_type i)
  511. {
  512. JSON_ASSERT(m_object != nullptr);
  513. switch (m_object->m_data.m_type)
  514. {
  515. case value_t::object:
  516. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  517. case value_t::array:
  518. {
  519. std::advance(m_it.array_iterator, i);
  520. break;
  521. }
  522. case value_t::null:
  523. case value_t::string:
  524. case value_t::boolean:
  525. case value_t::number_integer:
  526. case value_t::number_unsigned:
  527. case value_t::number_float:
  528. case value_t::binary:
  529. case value_t::discarded:
  530. default:
  531. {
  532. m_it.primitive_iterator += i;
  533. break;
  534. }
  535. }
  536. return *this;
  537. }
  538. /*!
  539. @brief subtract from iterator
  540. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  541. */
  542. iter_impl& operator-=(difference_type i)
  543. {
  544. return operator+=(-i);
  545. }
  546. /*!
  547. @brief add to iterator
  548. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  549. */
  550. iter_impl operator+(difference_type i) const
  551. {
  552. auto result = *this;
  553. result += i;
  554. return result;
  555. }
  556. /*!
  557. @brief addition of distance and iterator
  558. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  559. */
  560. friend iter_impl operator+(difference_type i, const iter_impl& it)
  561. {
  562. auto result = it;
  563. result += i;
  564. return result;
  565. }
  566. /*!
  567. @brief subtract from iterator
  568. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  569. */
  570. iter_impl operator-(difference_type i) const
  571. {
  572. auto result = *this;
  573. result -= i;
  574. return result;
  575. }
  576. /*!
  577. @brief return difference
  578. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  579. */
  580. difference_type operator-(const iter_impl& other) const
  581. {
  582. JSON_ASSERT(m_object != nullptr);
  583. switch (m_object->m_data.m_type)
  584. {
  585. case value_t::object:
  586. JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
  587. case value_t::array:
  588. return m_it.array_iterator - other.m_it.array_iterator;
  589. case value_t::null:
  590. case value_t::string:
  591. case value_t::boolean:
  592. case value_t::number_integer:
  593. case value_t::number_unsigned:
  594. case value_t::number_float:
  595. case value_t::binary:
  596. case value_t::discarded:
  597. default:
  598. return m_it.primitive_iterator - other.m_it.primitive_iterator;
  599. }
  600. }
  601. /*!
  602. @brief access to successor
  603. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  604. */
  605. reference operator[](difference_type n) const
  606. {
  607. JSON_ASSERT(m_object != nullptr);
  608. switch (m_object->m_data.m_type)
  609. {
  610. case value_t::object:
  611. JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
  612. case value_t::array:
  613. return *std::next(m_it.array_iterator, n);
  614. case value_t::null:
  615. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  616. case value_t::string:
  617. case value_t::boolean:
  618. case value_t::number_integer:
  619. case value_t::number_unsigned:
  620. case value_t::number_float:
  621. case value_t::binary:
  622. case value_t::discarded:
  623. default:
  624. {
  625. if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
  626. {
  627. return *m_object;
  628. }
  629. JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
  630. }
  631. }
  632. }
  633. /*!
  634. @brief return the key of an object iterator
  635. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  636. */
  637. const typename object_t::key_type& key() const
  638. {
  639. JSON_ASSERT(m_object != nullptr);
  640. if (JSON_HEDLEY_LIKELY(m_object->is_object()))
  641. {
  642. return m_it.object_iterator->first;
  643. }
  644. JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
  645. }
  646. /*!
  647. @brief return the value of an iterator
  648. @pre The iterator is initialized; i.e. `m_object != nullptr`.
  649. */
  650. reference value() const
  651. {
  652. return operator*();
  653. }
  654. JSON_PRIVATE_UNLESS_TESTED:
  655. /// associated JSON instance
  656. pointer m_object = nullptr;
  657. /// the actual iterator of the associated instance
  658. internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
  659. };
  660. } // namespace detail
  661. NLOHMANN_JSON_NAMESPACE_END