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

Side by Side Diff: components/ntp_snippets/inner_iterator.h

Issue 1958163002: [NTP Snippets] Refactor home-grown container API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Avoided auto as suggested. Created 4 years, 7 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
« no previous file with comments | « components/ntp_snippets/BUILD.gn ('k') | components/ntp_snippets/inner_iterator_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 COMPONENTS_NTP_SNIPPETS_INNER_ITERATOR_H_
6 #define COMPONENTS_NTP_SNIPPETS_INNER_ITERATOR_H_
7
8 #include <iterator>
9
10 namespace ntp_snippets {
11
12 // Build an iterator returning a dereference of an original iterator. For
13 // example an object with an instance variable of type
14 // std::vector<std::unique_ptr<SomeClass>> instance_;
15 // could serve a const_iterator (of const SomeClass&) by simply doing the
16 // following:
17 //
18 // using const_iterator = InnerIterator<
19 // std::vector<std::unique_ptr<SomeClass>>::const_iterator,
20 // const Someclass>;
21 // [...]
22 // const_iterator begin() { return const_iterator(instance_.begin()); }
23 //
24 template <typename I, // type of original iterator
25 typename R // type returned by this iterator
26 >
27 class InnerIterator {
28 public:
29 using difference_type = typename std::iterator_traits<I>::difference_type;
30 using value_type = R;
31 using pointer = R*;
32 using reference = R&;
33 using iterator_category = std::random_access_iterator_tag;
34
35 // Construction from an iterator.
36 explicit InnerIterator(I from) : it_(from) {}
37
38 // Operators *, ->, and [] are first forwarded to the contained
39 // iterator, then extract the inner member.
40 reference operator*() const { return **it_; }
41 pointer operator->() const { return &(**it_); }
42 reference operator[](difference_type n) const { return *(it_[n]); }
43
44 // All operators that have to do with position are forwarded
45 // to the contained iterator.
46 InnerIterator& operator++() {
47 ++it_;
48 return *this;
49 }
50 InnerIterator operator++(int) {
51 I tmp(it_);
52 it_++;
53 return InnerIterator(tmp);
54 }
55 InnerIterator& operator--() {
56 --it_;
57 return *this;
58 }
59 InnerIterator operator--(int) {
60 I tmp(it_);
61 it_--;
62 return InnerIterator(tmp);
63 }
64 InnerIterator& operator+=(difference_type n) {
65 it_ += n;
66 return *this;
67 }
68 InnerIterator operator+(difference_type n) const {
69 I tmp(it_);
70 tmp += n;
71 return InnerIterator(tmp);
72 }
73 InnerIterator& operator-=(difference_type n) {
74 it_ -= n;
75 return *this;
76 }
77 InnerIterator operator-(difference_type n) const {
78 I tmp(it_);
79 tmp -= n;
80 return InnerIterator(tmp);
81 }
82
83 bool operator==(const InnerIterator<I, R>& rhs) const {
84 return it_ == rhs.it_;
85 }
86 bool operator!=(const InnerIterator<I, R>& rhs) const {
87 return it_ != rhs.it_;
88 }
89
90 protected:
91 I it_;
92 };
93
94 } // namespace ntp_snippets
95
96 #endif // COMPONENTS_NTP_SNIPPETS_INNER_ITERATOR_H_
OLDNEW
« no previous file with comments | « components/ntp_snippets/BUILD.gn ('k') | components/ntp_snippets/inner_iterator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698