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" |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | class Value; |
| 19 | |
| 20 | namespace detail { |
| 21 | |
| 22 | using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>; |
| 23 | |
| 24 | // This iterator closely resembles DictStorage::iterator, with one |
| 25 | // important exception. It abstracts the underlying unique_ptr away, meaning its |
Peter Kasting | c573e2d | 2025-01-10 21:00:00 | [diff] [blame] | 26 | // reference type is a std::pair<const std::string&, Value&>, so that callers |
| 27 | // have read-write access without incurring a copy. |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 28 | class BASE_EXPORT dict_iterator { |
| 29 | public: |
| 30 | using difference_type = DictStorage::iterator::difference_type; |
Peter Kasting | c573e2d | 2025-01-10 21:00:00 | [diff] [blame] | 31 | using value_type = std::pair<const std::string&, Value&>; |
| 32 | using reference = value_type; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 33 | using iterator_category = std::bidirectional_iterator_tag; |
| 34 | |
| 35 | class pointer { |
| 36 | public: |
| 37 | explicit pointer(const reference& ref); |
| 38 | pointer(const pointer& ptr); |
| 39 | pointer& operator=(const pointer& ptr) = delete; |
| 40 | |
| 41 | reference* operator->() { return &ref_; } |
| 42 | |
| 43 | private: |
| 44 | reference ref_; |
| 45 | }; |
| 46 | |
Daniel Cheng | be3ac3a | 2024-05-15 17:38:23 | [diff] [blame] |
|