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

Unified 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, 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/base.gypi ('k') | base/containers/adapters_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/containers/adapters.h
diff --git a/base/containers/adapters.h b/base/containers/adapters.h
new file mode 100644
index 0000000000000000000000000000000000000000..01545a68dec74b4aee95da8c0e9b36000ff8bfd8
--- /dev/null
+++ b/base/containers/adapters.h
@@ -0,0 +1,49 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// 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.
+//
+// Example:
+//
+// std::vector<int> v = ...;
+// for (int i : base::reversed(v)) {
+// // iterates through v in reverse order
+// }
+
+#ifndef BASE_CONTAINERS_ADAPTERS_H_
+#define BASE_CONTAINERS_ADAPTERS_H_
+
+#include "base/macros.h"
+
+namespace base {
+
+namespace internal {
+
+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
+class ReversedAdapter {
+ public:
+ typedef decltype(static_cast<T*>(nullptr)->rbegin()) Iterator;
+
+ explicit ReversedAdapter(T& t) : t_(t) {}
+ ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {}
+
+ 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
+ friend Iterator end(const ReversedAdapter& ra) { return ra.t_.rend(); }
+
+ private:
+ T& t_;
+
+ DISALLOW_ASSIGN(ReversedAdapter);
+};
+
+} // namespace internal
+
+template <typename T>
+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.
+ return internal::ReversedAdapter<T>(t);
+}
+
+} // namespace base
+
+#endif // BASE_CONTAINERS_ADAPTERS_H_
« 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