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

Side by Side Diff: base/containers/adapters.h

Issue 1360423002: Extend base::Reversed to support iterating arrays (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 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 | « no previous file | base/containers/adapters_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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_CONTAINERS_ADAPTERS_H_ 5 #ifndef BASE_CONTAINERS_ADAPTERS_H_
6 #define BASE_CONTAINERS_ADAPTERS_H_ 6 #define BASE_CONTAINERS_ADAPTERS_H_
7 7
8 #include <stddef.h>
9
10 #include <iterator>
11
8 #include "base/macros.h" 12 #include "base/macros.h"
9 13
10 namespace base { 14 namespace base {
11 15
12 namespace internal { 16 namespace internal {
13 17
14 // Internal adapter class for implementing base::Reversed. 18 // Internal adapter class for implementing base::Reversed.
15 template <typename T> 19 template <typename T>
16 class ReversedAdapter { 20 class ReversedAdapter {
17 public: 21 public:
18 typedef decltype(static_cast<T*>(nullptr)->rbegin()) Iterator; 22 using Iterator = decltype(static_cast<T*>(nullptr)->rbegin());
19 23
20 explicit ReversedAdapter(T& t) : t_(t) {} 24 explicit ReversedAdapter(T& t) : t_(t) {}
21 ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {} 25 ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {}
22 26
27 // TODO(mdempsky): Once we can use C++14 library features, use std::rbegin
28 // and std::rend instead, so we can remove the specialization below.
23 Iterator begin() const { return t_.rbegin(); } 29 Iterator begin() const { return t_.rbegin(); }
24 Iterator end() const { return t_.rend(); } 30 Iterator end() const { return t_.rend(); }
25 31
26 private: 32 private:
27 T& t_; 33 T& t_;
28 34
29 DISALLOW_ASSIGN(ReversedAdapter); 35 DISALLOW_ASSIGN(ReversedAdapter);
30 }; 36 };
31 37
38 template <typename T, size_t N>
39 class ReversedAdapter<T[N]> {
40 public:
41 using Iterator = std::reverse_iterator<T*>;
42
43 // TODO(mdempsky): clang-format rewrites this as "T(&t)[N]" (b/24351059).
danakj 2015/10/14 23:21:05 Thanks for filing. Has the fix rolled to chrome ye
mdempsky 2015/10/14 23:39:11 Not yet, as far as I can tell. Still, I removed t
44 // clang-format off
45 explicit ReversedAdapter(T (&t)[N]) : t_(t) {}
46 // clang-format on
47 ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {}
48
49 Iterator begin() const { return Iterator(&t_[N]); }
50 Iterator end() const { return Iterator(&t_[0]); }
51
52 private:
53 // See above.
54 // clang-format off
55 T (&t_)[N];
56 // clang-format on
57
58 DISALLOW_ASSIGN(ReversedAdapter);
59 };
60
32 } // namespace internal 61 } // namespace internal
33 62
34 // Reversed returns a container adapter usable in a range-based "for" statement 63 // Reversed returns a container adapter usable in a range-based "for" statement
35 // for iterating a reversible container in reverse order. 64 // for iterating a reversible container in reverse order.
36 // 65 //
37 // Example: 66 // Example:
38 // 67 //
39 // std::vector<int> v = ...; 68 // std::vector<int> v = ...;
40 // for (int i : base::Reversed(v)) { 69 // for (int i : base::Reversed(v)) {
41 // // iterates through v from back to front 70 // // iterates through v from back to front
42 // } 71 // }
43 template <typename T> 72 template <typename T>
44 internal::ReversedAdapter<T> Reversed(T& t) { 73 internal::ReversedAdapter<T> Reversed(T& t) {
45 return internal::ReversedAdapter<T>(t); 74 return internal::ReversedAdapter<T>(t);
46 } 75 }
47 76
48 } // namespace base 77 } // namespace base
49 78
50 #endif // BASE_CONTAINERS_ADAPTERS_H_ 79 #endif // BASE_CONTAINERS_ADAPTERS_H_
OLDNEW
« no previous file with comments | « no previous file | base/containers/adapters_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698