value_iterators.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_VALUE_ITERATORS_H_
  5. #define BASE_VALUE_ITERATORS_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/base_export.h"
  10. #include "base/containers/flat_map.h"
  11. #include "base/macros.h"
  12. namespace base {
  13. class Value;
  14. namespace detail {
  15. using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>;
  16. // This iterator closely resembles DictStorage::iterator, with one
  17. // important exception. It abstracts the underlying unique_ptr away, meaning its
  18. // value_type is std::pair<const std::string, Value>. It's reference type is a
  19. // std::pair<const std::string&, Value&>, so that callers have read-write
  20. // access without incurring a copy.
  21. class BASE_EXPORT dict_iterator {
  22. public:
  23. using difference_type = DictStorage::iterator::difference_type;
  24. using value_type = std::pair<const std::string, Value>;
  25. using reference = std::pair<const std::string&, Value&>;
  26. using iterator_category = std::bidirectional_iterator_tag;
  27. class pointer {
  28. public:
  29. explicit pointer(const reference& ref);
  30. pointer(const pointer& ptr);
  31. pointer& operator=(const pointer& ptr) = delete;
  32. reference* operator->() { return &ref_; }
  33. private:
  34. reference ref_;
  35. };
  36. explicit dict_iterator(DictStorage::iterator dict_iter);
  37. dict_iterator(const dict_iterator& dict_iter);
  38. dict_iterator& operator=(const dict_iterator& dict_iter);
  39. ~dict_iterator();
  40. reference operator*();
  41. pointer operator->();
  42. dict_iterator& operator++();
  43. dict_iterator operator++(int);
  44. dict_iterator& operator--();
  45. dict_iterator operator--(int);
  46. BASE_EXPORT friend bool operator==(const dict_iterator& lhs,
  47. const dict_iterator& rhs);
  48. BASE_EXPORT friend bool operator!=(const dict_iterator& lhs,
  49. const dict_iterator& rhs);
  50. private:
  51. DictStorage::iterator dict_iter_;
  52. };
  53. // This iterator closely resembles DictStorage::const_iterator, with one
  54. // important exception. It abstracts the underlying unique_ptr away, meaning its
  55. // value_type is std::pair<const std::string, Value>. It's reference type is a
  56. // std::pair<const std::string&, const Value&>, so that callers have read-only
  57. // access without incurring a copy.
  58. class BASE_EXPORT const_dict_iterator {
  59. public:
  60. using difference_type = DictStorage::const_iterator::difference_type;
  61. using value_type = std::pair<const std::string, Value>;
  62. using reference = std::pair<const std::string&, const Value&>;
  63. using iterator_category = std::bidirectional_iterator_tag;
  64. class pointer {
  65. public:
  66. explicit pointer(const reference& ref);
  67. pointer(const pointer& ptr);
  68. pointer& operator=(const pointer& ptr) = delete;
  69. const reference* operator->() const { return &ref_; }
  70. private:
  71. const reference ref_;
  72. };
  73. explicit const_dict_iterator(DictStorage::const_iterator dict_iter);
  74. const_dict_iterator(const const_dict_iterator& dict_iter);
  75. const_dict_iterator& operator=(const const_dict_iterator& dict_iter);
  76. ~const_dict_iterator();
  77. reference operator*() const;
  78. pointer operator->() const;
  79. const_dict_iterator& operator++();
  80. const_dict_iterator operator++(int);
  81. const_dict_iterator& operator--();
  82. const_dict_iterator operator--(int);
  83. BASE_EXPORT friend bool operator==(const const_dict_iterator& lhs,
  84. const const_dict_iterator& rhs);
  85. BASE_EXPORT friend bool operator!=(const const_dict_iterator& lhs,
  86. const const_dict_iterator& rhs);
  87. private:
  88. DictStorage::const_iterator dict_iter_;
  89. };
  90. // This class wraps the various |begin| and |end| methods of the underlying
  91. // DictStorage in dict_iterators and const_dict_iterators. This allows callers
  92. // to use this class for easy iteration over the underlying values, granting
  93. // them either read-only or read-write access, depending on the
  94. // const-qualification.
  95. class BASE_EXPORT dict_iterator_proxy {
  96. public:
  97. using key_type = DictStorage::key_type;
  98. using mapped_type = DictStorage::mapped_type::element_type;
  99. using value_type = std::pair<key_type, mapped_type>;
  100. using key_compare = DictStorage::key_compare;
  101. using size_type = DictStorage::size_type;
  102. using difference_type = DictStorage::difference_type;
  103. using iterator = dict_iterator;
  104. using const_iterator = const_dict_iterator;
  105. using reverse_iterator = std::reverse_iterator<iterator>;
  106. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  107. explicit dict_iterator_proxy(DictStorage* storage);
  108. iterator begin();
  109. const_iterator begin() const;
  110. iterator end();
  111. const_iterator end() const;
  112. reverse_iterator rbegin();
  113. const_reverse_iterator rbegin() const;
  114. reverse_iterator rend();
  115. const_reverse_iterator rend() const;
  116. const_dict_iterator cbegin() const;
  117. const_dict_iterator cend() const;
  118. const_reverse_iterator crbegin() const;
  119. const_reverse_iterator crend() const;
  120. private:
  121. DictStorage* storage_;
  122. };
  123. // This class wraps the various const |begin| and |end| methods of the
  124. // underlying DictStorage in const_dict_iterators. This allows callers to use
  125. // this class for easy iteration over the underlying values, granting them
  126. // either read-only access.
  127. class BASE_EXPORT const_dict_iterator_proxy {
  128. public:
  129. using key_type = const DictStorage::key_type;
  130. using mapped_type = const DictStorage::mapped_type::element_type;
  131. using value_type = std::pair<key_type, mapped_type>;
  132. using key_compare = DictStorage::key_compare;
  133. using size_type = DictStorage::size_type;
  134. using difference_type = DictStorage::difference_type;
  135. using iterator = const_dict_iterator;
  136. using const_iterator = const_dict_iterator;
  137. using reverse_iterator = std::reverse_iterator<iterator>;
  138. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  139. explicit const_dict_iterator_proxy(const DictStorage* storage);
  140. const_iterator begin() const;
  141. const_iterator end() const;
  142. const_reverse_iterator rbegin() const;
  143. const_reverse_iterator rend() const;
  144. const_iterator cbegin() const;
  145. const_iterator cend() const;
  146. const_reverse_iterator crbegin() const;
  147. const_reverse_iterator crend() const;
  148. private:
  149. const DictStorage* storage_;
  150. };
  151. } // namespace detail
  152. } // namespace base
  153. #endif // BASE_VALUE_ITERATORS_H_