rapidxml_iterators.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. {
  10. //! Iterator of child nodes of xml_node
  11. template<class Ch>
  12. class node_iterator
  13. {
  14. public:
  15. typedef typename xml_node<Ch> value_type;
  16. typedef typename xml_node<Ch> &reference;
  17. typedef typename xml_node<Ch> *pointer;
  18. typedef std::ptrdiff_t difference_type;
  19. typedef std::bidirectional_iterator_tag iterator_category;
  20. node_iterator()
  21. : m_node(0)
  22. {
  23. }
  24. node_iterator(xml_node<Ch> *node)
  25. : m_node(node->first_node())
  26. {
  27. }
  28. reference operator *() const
  29. {
  30. assert(m_node);
  31. return *m_node;
  32. }
  33. pointer operator->() const
  34. {
  35. assert(m_node);
  36. return m_node;
  37. }
  38. node_iterator& operator++()
  39. {
  40. assert(m_node);
  41. m_node = m_node->next_sibling();
  42. return *this;
  43. }
  44. node_iterator operator++(int)
  45. {
  46. node_iterator tmp = *this;
  47. ++this;
  48. return tmp;
  49. }
  50. node_iterator& operator--()
  51. {
  52. assert(m_node && m_node->previous_sibling());
  53. m_node = m_node->previous_sibling();
  54. return *this;
  55. }
  56. node_iterator operator--(int)
  57. {
  58. node_iterator tmp = *this;
  59. ++this;
  60. return tmp;
  61. }
  62. bool operator ==(const node_iterator<Ch> &rhs)
  63. {
  64. return m_node == rhs.m_node;
  65. }
  66. bool operator !=(const node_iterator<Ch> &rhs)
  67. {
  68. return m_node != rhs.m_node;
  69. }
  70. private:
  71. xml_node<Ch> *m_node;
  72. };
  73. //! Iterator of child attributes of xml_node
  74. template<class Ch>
  75. class attribute_iterator
  76. {
  77. public:
  78. typedef typename xml_attribute<Ch> value_type;
  79. typedef typename xml_attribute<Ch> &reference;
  80. typedef typename xml_attribute<Ch> *pointer;
  81. typedef std::ptrdiff_t difference_type;
  82. typedef std::bidirectional_iterator_tag iterator_category;
  83. attribute_iterator()
  84. : m_attribute(0)
  85. {
  86. }
  87. attribute_iterator(xml_node<Ch> *node)
  88. : m_attribute(node->first_attribute())
  89. {
  90. }
  91. reference operator *() const
  92. {
  93. assert(m_attribute);
  94. return *m_attribute;
  95. }
  96. pointer operator->() const
  97. {
  98. assert(m_attribute);
  99. return m_attribute;
  100. }
  101. attribute_iterator& operator++()
  102. {
  103. assert(m_attribute);
  104. m_attribute = m_attribute->next_attribute();
  105. return *this;
  106. }
  107. attribute_iterator operator++(int)
  108. {
  109. attribute_iterator tmp = *this;
  110. ++this;
  111. return tmp;
  112. }
  113. attribute_iterator& operator--()
  114. {
  115. assert(m_attribute && m_attribute->previous_attribute());
  116. m_attribute = m_attribute->previous_attribute();
  117. return *this;
  118. }
  119. attribute_iterator operator--(int)
  120. {
  121. attribute_iterator tmp = *this;
  122. ++this;
  123. return tmp;
  124. }
  125. bool operator ==(const attribute_iterator<Ch> &rhs)
  126. {
  127. return m_attribute == rhs.m_attribute;
  128. }
  129. bool operator !=(const attribute_iterator<Ch> &rhs)
  130. {
  131. return m_attribute != rhs.m_attribute;
  132. }
  133. private:
  134. xml_attribute<Ch> *m_attribute;
  135. };
  136. }
  137. #endif