blob: 418390b431f8cf9ce69cd942d367a78069ab3f0d [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2017 The Chromium Authors
jdoerrie44efa9d2017-07-14 14:47:202// 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 Sepez04e98bf2024-10-25 18:19:3113#include "base/compiler_specific.h"
jdoerrie44efa9d2017-07-14 14:47:2014#include "base/containers/flat_map.h"
jdoerrie44efa9d2017-07-14 14:47:2015
16namespace base {
17
18class Value;
19
20namespace detail {
21
22using 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 Kastingc573e2d2025-01-10 21:00:0026// reference type is a std::pair<const std::string&, Value&>, so that callers
27// have read-write access without incurring a copy.
jdoerrie44efa9d2017-07-14 14:47:2028class BASE_EXPORT dict_iterator {
29 public:
30 using difference_type = DictStorage::iterator::difference_type;
Peter Kastingc573e2d2025-01-10 21:00:0031 using value_type = std::pair<const std::string&, Value&>;
32 using reference = value_type;
jdoerrie44efa9d2017-07-14 14:47:2033 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 Chengbe3ac3a2024-05-15 17:38:23