iterator.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include "yaml-cpp/dll.h"
  9. #include "yaml-cpp/node/node.h"
  10. #include "yaml-cpp/node/ptr.h"
  11. #include "yaml-cpp/node/detail/node_iterator.h"
  12. #include <cstddef>
  13. #include <iterator>
  14. namespace YAML {
  15. namespace detail {
  16. struct iterator_value;
  17. template <typename V>
  18. class iterator_base : public std::iterator<std::forward_iterator_tag, V,
  19. std::ptrdiff_t, V*, V> {
  20. private:
  21. template <typename>
  22. friend class iterator_base;
  23. struct enabler {};
  24. typedef node_iterator base_type;
  25. struct proxy {
  26. explicit proxy(const V& x) : m_ref(x) {}
  27. V* operator->() { return std::addressof(m_ref); }
  28. operator V*() { return std::addressof(m_ref); }
  29. V m_ref;
  30. };
  31. public:
  32. typedef typename iterator_base::value_type value_type;
  33. public:
  34. iterator_base() : m_iterator(), m_pMemory() {}
  35. explicit iterator_base(base_type rhs, shared_memory_holder pMemory)
  36. : m_iterator(rhs), m_pMemory(pMemory) {}
  37. template <class W>
  38. iterator_base(const iterator_base<W>& rhs,
  39. typename std::enable_if<std::is_convertible<W*, V*>::value,
  40. enabler>::type = enabler())
  41. : m_iterator(rhs.m_iterator), m_pMemory(rhs.m_pMemory) {}
  42. iterator_base<V>& operator++() {
  43. ++m_iterator;
  44. return *this;
  45. }
  46. iterator_base<V> operator++(int) {
  47. iterator_base<V> iterator_pre(*this);
  48. ++(*this);
  49. return iterator_pre;
  50. }
  51. template <typename W>
  52. bool operator==(const iterator_base<W>& rhs) const {
  53. return m_iterator == rhs.m_iterator;
  54. }
  55. template <typename W>
  56. bool operator!=(const iterator_base<W>& rhs) const {
  57. return m_iterator != rhs.m_iterator;
  58. }
  59. value_type operator*() const {
  60. const typename base_type::value_type& v = *m_iterator;
  61. if (v.pNode)
  62. return value_type(Node(*v, m_pMemory));
  63. if (v.first && v.second)
  64. return value_type(Node(*v.first, m_pMemory), Node(*v.second, m_pMemory));
  65. return value_type();
  66. }
  67. proxy operator->() const { return proxy(**this); }
  68. private:
  69. base_type m_iterator;
  70. shared_memory_holder m_pMemory;
  71. };
  72. }
  73. }
  74. #endif // VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66