Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_VALUE_ITERATORS_H_ |
| 6 | #define BASE_VALUE_ITERATORS_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include "base/base_export.h" |
Tom Sepez | 04e98bf | 2024-10-25 18:19:31 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 14 | #include "base/containers/flat_map.h" |
Keishi Hattori | e175ac5 | 2022-06-07 06:24:57 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | class Value; |
| 20 | |
| 21 | namespace detail { |
| 22 | |
| 23 | using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>; |
| 24 | |
| 25 | // This iterator closely resembles DictStorage::iterator, with one |
| 26 | // important exception. It abstracts the underlying unique_ptr away, meaning its |
Peter Kasting | c573e2d | 2025-01-10 21:00:00 | [diff] [blame^] | 27 | // reference type is a std::pair<const std::string&, Value&>, so that callers |
| 28 | // have read-write access without incurring a copy. |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 29 | class BASE_EXPORT dict_iterator { |
| 30 | public: |
| 31 | using difference_type = DictStorage::iterator::difference_type; |
Peter Kasting | c573e2d | 2025-01-10 21:00:00 | [diff] [blame^] | 32 | using value_type = std::pair<const std::string&, Value&>; |
| 33 | using reference = value_type; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 34 | using iterator_category = std::bidirectional_iterator_tag; |
| 35 | |
| 36 | class pointer { |
| 37 | public: |
| 38 | explicit pointer(const reference& ref); |
| 39 | pointer(const pointer& ptr); |
| 40 | pointer& operator=(const pointer& ptr) = delete; |
| 41 | |
| 42 | reference* operator->() { return &ref_; } |
| 43 | |
| 44 | private: |
| 45 | reference ref_; |
| 46 | }; |
| 47 | |
Daniel Cheng | be3ac3a | 2024-05-15 17:38:23 | [diff] [blame] | 48 | constexpr dict_iterator() = default; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 49 | explicit dict_iterator(DictStorage::iterator dict_iter); |
| 50 | dict_iterator(const dict_iterator& dict_iter); |
| 51 | dict_iterator& operator=(const dict_iterator& dict_iter); |
| 52 | ~dict_iterator(); |
| 53 | |
Peter Kasting | c573e2d | 2025-01-10 21:00:00 | [diff] [blame^] | 54 | reference operator*() const; |
| 55 | pointer operator->() const; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 56 | |
| 57 | dict_iterator& operator++(); |
| 58 | dict_iterator operator++(int); |
| 59 | dict_iterator& operator--(); |
| 60 | dict_iterator operator--(int); |
| 61 | |
| 62 | BASE_EXPORT friend bool operator==(const dict_iterator& lhs, |
| 63 | const dict_iterator& rhs); |
| 64 | BASE_EXPORT friend bool operator!=(const dict_iterator& lhs, |
| 65 | const dict_iterator& rhs); |
| 66 | |
Daniel Cheng | a367fe5 | 2022-02-15 18:08:48 | [diff] [blame] | 67 | // Currently, there is no easy way to friend Value::Dict. Once dictionary |
| 68 | // storage is updated to not require a proxy iterator, the implementation can |
|
|