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

Unified 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: Remove clang-format directives Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | 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
index cc151fc2468bfd630f80e959437fe95fb2d3724b..fa671b46d991ae1bafab2632fd60c4ebb8fa42bf 100644
--- a/base/containers/adapters.h
+++ b/base/containers/adapters.h
@@ -5,6 +5,10 @@
#ifndef BASE_CONTAINERS_ADAPTERS_H_
#define BASE_CONTAINERS_ADAPTERS_H_
+#include <stddef.h>
+
+#include <iterator>
+
#include "base/macros.h"
namespace base {
@@ -15,11 +19,13 @@ namespace internal {
template <typename T>
class ReversedAdapter {
public:
- typedef decltype(static_cast<T*>(nullptr)->rbegin()) Iterator;
+ using Iterator = decltype(static_cast<T*>(nullptr)->rbegin());
explicit ReversedAdapter(T& t) : t_(t) {}
ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {}
+ // TODO(mdempsky): Once we can use C++14 library features, use std::rbegin
+ // and std::rend instead, so we can remove the specialization below.
Iterator begin() const { return t_.rbegin(); }
Iterator end() const { return t_.rend(); }
@@ -29,6 +35,23 @@ class ReversedAdapter {
DISALLOW_ASSIGN(ReversedAdapter);
};
+template <typename T, size_t N>
+class ReversedAdapter<T[N]> {
+ public:
+ using Iterator = std::reverse_iterator<T*>;
+
+ explicit ReversedAdapter(T (&t)[N]) : t_(t) {}
+ ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {}
+
+ Iterator begin() const { return Iterator(&t_[N]); }
+ Iterator end() const { return Iterator(&t_[0]); }
+
+ private:
+ T (&t_)[N];
+
+ DISALLOW_ASSIGN(ReversedAdapter);
+};
+
} // namespace internal
// Reversed returns a container adapter usable in a range-based "for" statement
« 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