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.
5 #ifndef BASE_CONTAINERS_ADAPTERS_H_
6 #define BASE_CONTAINERS_ADAPTERS_H_
8 #include "base/macros.h"
14 // Internal adapter class for implementing base::Reversed.
16 class ReversedAdapter
{
18 typedef decltype(static_cast<T
*>(nullptr)->rbegin()) Iterator
;
20 explicit ReversedAdapter(T
& t
) : t_(t
) {}
21 ReversedAdapter(const ReversedAdapter
& ra
) : t_(ra
.t_
) {}
23 Iterator
begin() const { return t_
.rbegin(); }
24 Iterator
end() const { return t_
.rend(); }
29 DISALLOW_ASSIGN(ReversedAdapter
);
32 } // namespace internal
34 // Reversed returns a container adapter usable in a range-based "for" statement
35 // for iterating a reversible container in reverse order.
39 // std::vector<int> v = ...;
40 // for (int i : base::Reversed(v)) {
41 // // iterates through v from back to front
44 internal::ReversedAdapter
<T
> Reversed(T
& t
) {
45 return internal::ReversedAdapter
<T
>(t
);
50 #endif // BASE_CONTAINERS_ADAPTERS_H_