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

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

Issue 605243003: Add base::Reversed() as an adapter for range-based for loops in reverse (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Explicit copy constructor and explicitly disallow operator= Created 6 years, 2 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 | « base/base.gypi ('k') | 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
(Empty)
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
3 // found in the LICENSE file.
4
5 // This file contains adapters to iterate containers in alternative orders.
brettw 2014/09/29 16:55:14 I'd just remove this line. We could potentially ad
mdempsky 2014/09/29 17:47:40 Done.
6 //
7 // Example:
8 //
9 // std::vector<int> v = ...;
10 // for (int i : base::reversed(v)) {
11 // // iterates through v in reverse order
12 // }
13
14 #ifndef BASE_CONTAINERS_ADAPTERS_H_
15 #define BASE_CONTAINERS_ADAPTERS_H_
16
17 #include "base/macros.h"
18
19 namespace base {
20
21 namespace internal {
22
23 template <typename T>
brettw 2014/09/29 16:55:14 Can you include an example here?
mdempsky 2014/09/29 17:47:40 This class isn't intended for end users. I've adde
24 class ReversedAdapter {
25 public:
26 typedef decltype(static_cast<T*>(nullptr)->rbegin()) Iterator;
27
28 explicit ReversedAdapter(T& t) : t_(t) {}
29 ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {}
30
31 friend Iterator begin(const ReversedAdapter& ra) { return ra.t_.rbegin(); }
brettw 2014/09/29 16:55:14 I don't understand what "friend" does in this cont
mdempsky 2014/09/29 17:47:41 Turns out it's not necessary; I was looking at an
32 friend Iterator end(const ReversedAdapter& ra) { return ra.t_.rend(); }
33
34 private:
35 T& t_;
36
37 DISALLOW_ASSIGN(ReversedAdapter);
38 };
39
40 } // namespace internal
41
42 template <typename T>
43 internal::ReversedAdapter<T> reversed(T& t) {
brettw 2014/09/29 16:55:14 Google-style would say this should be capitalized.
mdempsky 2014/09/29 17:47:41 Done.
44 return internal::ReversedAdapter<T>(t);
45 }
46
47 } // namespace base
48
49 #endif // BASE_CONTAINERS_ADAPTERS_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/containers/adapters_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698