iter_impl.hpp 18 KB

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