rapidxml_iterators.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef RAPIDXML_ITERATORS_HPP_INCLUDED
  2. #define RAPIDXML_ITERATORS_HPP_INCLUDED
  3. // Copyright (C) 2006, 2009 Marcin Kalicinski
  4. // Version 1.13
  5. // Revision $DateTime: 2009/05/13 01:46:17 $
  6. //! \file rapidxml_iterators.hpp This file contains rapidxml iterators
  7. #include "rapidxml.hpp"
  8. namespace rapidxml {
  9. //! Iterator of child nodes of xml_node
  10. template <class Ch>
  11. class node_iterator {
  12. public:
  13. typedef typename xml_node<Ch> value_type;
  14. typedef typename xml_node<Ch> &reference;
  15. typedef typename xml_node<Ch> *pointer;
  16. typedef std::ptrdiff_t difference_type;
  17. typedef std::bidirectional_iterator_tag iterator_category;
  18. node_iterator() : m_node(0) {}
  19. node_iterator(xml_node<Ch> *node) : m_node(node->first_node()) {}
  20. reference operator*() const {
  21. assert(m_node);
  22. return *m_node;
  23. }
  24. pointer operator->() const {
  25. assert(m_node);
  26. return m_node;
  27. }
  28. node_iterator &operator++() {
  29. assert(m_node);
  30. m_node = m_node->next_sibling();
  31. return *this;
  32. }
  33. node_iterator operator++(int) {
  34. node_iterator tmp = *this;
  35. ++this;
  36. return tmp;
  37. }
  38. node_iterator &operator--() {
  39. assert(m_node && m_node->previous_sibling());
  40. m_node = m_node->previous_sibling();
  41. return *this;
  42. }
  43. node_iterator operator--(int) {
  44. node_iterator tmp = *this;
  45. ++this;
  46. return tmp;
  47. }
  48. bool operator==(const node_iterator<Ch> &rhs) { return m_node == rhs.m_node; }
  49. bool operator!=(const node_iterator<Ch> &rhs) { return m_node != rhs.m_node; }
  50. private:
  51. xml_node<Ch> *m_node;
  52. };
  53. //! Iterator of child attributes of xml_node
  54. template <class Ch>
  55. class attribute_iterator {
  56. public:
  57. typedef typename xml_attribute<Ch> value_type;
  58. typedef typename xml_attribute<Ch> &reference;
  59. typedef typename xml_attribute<Ch> *pointer;
  60. typedef std::ptrdiff_t difference_type;
  61. typedef std::bidirectional_iterator_tag iterator_category;
  62. attribute_iterator() : m_attribute(0) {}
  63. attribute_iterator(xml_node<Ch> *node)
  64. : m_attribute(node->first_attribute()) {}
  65. reference operator*() const {
  66. assert(m_attribute);
  67. return *m_attribute;
  68. }
  69. pointer operator->() const {
  70. assert(m_attribute);
  71. return m_attribute;
  72. }
  73. attribute_iterator &operator++() {
  74. assert(m_attribute);
  75. m_attribute = m_attribute->next_attribute();
  76. return *this;
  77. }
  78. attribute_iterator operator++(int) {
  79. attribute_iterator tmp = *this;
  80. ++this;
  81. return tmp;
  82. }
  83. attribute_iterator &operator--() {
  84. assert(m_attribute && m_attribute->previous_attribute());
  85. m_attribute = m_attribute->previous_attribute();
  86. return *this;
  87. }
  88. attribute_iterator operator--(int) {
  89. attribute_iterator tmp = *this;
  90. ++this;
  91. return tmp;
  92. }
  93. bool operator==(const attribute_iterator<Ch> &rhs) {
  94. return m_attribute == rhs.m_attribute;
  95. }
  96. bool operator!=(const attribute_iterator<Ch> &rhs) {
  97. return m_attribute != rhs.m_attribute;
  98. }
  99. private:
  100. xml_attribute<Ch> *m_attribute;
  101. };
  102. } // namespace rapidxml
  103. #endif