Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: mojo/public/cpp/bindings/lib/iterator_util.h

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ITERATOR_UTIL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ITERATOR_UTIL_H_
7
8 #include <algorithm>
9
10 #include "mojo/public/cpp/bindings/array.h"
11 #include "mojo/public/cpp/bindings/map.h"
12 #include "mojo/public/cpp/environment/logging.h"
13
14 namespace mojo {
15 namespace internal {
16
17 // |MapKeyIterator| and |MapValueIterator| are templated interfaces for
18 // iterating over a Map<>'s keys and its values, respectively. They provide a
19 // begin() and end() for generating an STL-iterator. The generated Iterator
20 // follows the BidirectionalIterator concept
21 // (http://en.cppreference.com/w/cpp/concept/BidirectionalIterator).
22 //
23 // Example usage:
24 //
25 // Map<int,int> my_map;
26 // my_map[1] = 2;
27 // my_map[3] = 4;
28 // my_map[5] = 6;
29 // for (int key : MapKeyIterator<int,int>(&my_map)) {
30 // std::cout << key << std::endl;
31 // }
32 // for (int val : MapValueIterator<int,int>(&my_map)) {
33 // std::cout << val << std::endl;
34 // }
35
36 // Interface for iterating over a Map<K, V>'s keys.
37 // To construct a |MapKeyIterator|, pass in a non-null pointer to a Map<K, V>;
38 template <typename K, typename V>
39 class MapKeyIterator {
40 public:
41 class Iterator {
42 public:
43 Iterator() : it_() {}
44 explicit Iterator(typename Map<K, V>::MapIterator it) : it_(it) {}
45 Iterator& operator++() {
46 ++it_;
47 return *this;
48 }
49 Iterator operator++(int) {
50 Iterator original = *this;
51 ++it_;
52 return original;
53 }
54 Iterator& operator--() {
55 --it_;
56 return *this;
57 }
58 Iterator operator--(int) {
59 Iterator original = *this;
60 --it_;
61 return original;
62 }
63 bool operator==(const Iterator& o) const { return o.it_ == it_; }
64 bool operator!=(const Iterator& o) const { return o.it_ != it_; }
65 const K& operator*() { return it_.GetKey(); }
66 const K* operator->() { return &it_.GetKey(); }
67
68 private:
69 typename Map<K, V>::MapIterator it_;
70 };
71
72 explicit MapKeyIterator(Map<K, V>* map) : map_(map) { MOJO_DCHECK(map); }
73
74 size_t size() const { return map_->size(); }
75 Iterator begin() const { return Iterator{map_->begin()}; }
76 Iterator end() const { return Iterator{map_->end()}; }
77
78 private:
79 Map<K, V>* const map_;
80 };
81
82 // Interface for iterating over a Map<K, V>'s values.
83 template <typename K, typename V>
84 class MapValueIterator {
85 public:
86 class Iterator {
87 public:
88 Iterator() : it_(typename Map<K, V>::MapIterator()) {}
89 explicit Iterator(typename Map<K, V>::MapIterator it) : it_(it) {}
90 Iterator& operator++() {
91 ++it_;
92 return *this;
93 }
94 Iterator operator++(int) {
95 Iterator original = *this;
96 ++it_;
97 return original;
98 }
99 Iterator& operator--() {
100 --it_;
101 return *this;
102 }
103 Iterator operator--(int) {
104 Iterator original = *this;
105 --it_;
106 return original;
107 }
108 bool operator==(const Iterator& o) const { return o.it_ == it_; }
109 bool operator!=(const Iterator& o) const { return o.it_ != it_; }
110 V& operator*() { return it_.GetValue(); }
111 V* operator->() { return &it_.GetValue(); }
112
113 private:
114 typename Map<K, V>::MapIterator it_;
115 };
116
117 explicit MapValueIterator(Map<K, V>* map) : map_(map) { MOJO_DCHECK(map); }
118 size_t size() const { return map_->size(); }
119 Iterator begin() const { return Iterator{map_->begin()}; }
120 Iterator end() const { return Iterator{map_->end()}; }
121
122 private:
123 Map<K, V>* const map_;
124 };
125
126 } // namespace internal
127 } // namespace mojo
128
129 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ITERATOR_UTIL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/interface_ptr_internal.h ('k') | mojo/public/cpp/bindings/lib/map_data_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698