Home | History | Annotate | Download | only in base
      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 // Slightly adapted for inclusion in V8.
      6 // Copyright 2014 the V8 project authors. All rights reserved.
      7 
      8 #ifndef V8_BASE_ADAPTERS_H_
      9 #define V8_BASE_ADAPTERS_H_
     10 
     11 #include "src/base/macros.h"
     12 
     13 namespace v8 {
     14 namespace base {
     15 
     16 // Internal adapter class for implementing base::Reversed.
     17 template <typename T>
     18 class ReversedAdapter {
     19  public:
     20   typedef decltype(static_cast<T*>(nullptr)->rbegin()) Iterator;
     21 
     22   explicit ReversedAdapter(T& t) : t_(t) {}
     23   ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {}
     24 
     25   Iterator begin() const { return t_.rbegin(); }
     26   Iterator end() const { return t_.rend(); }
     27 
     28  private:
     29   T& t_;
     30 
     31   DISALLOW_ASSIGN(ReversedAdapter);
     32 };
     33 
     34 // Reversed returns a container adapter usable in a range-based "for" statement
     35 // for iterating a reversible container in reverse order.
     36 //
     37 // Example:
     38 //
     39 //   std::vector<int> v = ...;
     40 //   for (int i : base::Reversed(v)) {
     41 //     // iterates through v from back to front
     42 //   }
     43 template <typename T>
     44 ReversedAdapter<T> Reversed(T& t) {
     45   return ReversedAdapter<T>(t);
     46 }
     47 
     48 }  // namespace base
     49 }  // namespace v8
     50 
     51 #endif  // V8_BASE_ADAPTERS_H_
     52