1 // Copyright 2014 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_CONTAINERS_ADAPTERS_H_ 6 #define BASE_CONTAINERS_ADAPTERS_H_ 7 8 #include <ranges> 9 #include <type_traits> 10 #include <utility> 11 12 #include "base/compiler_specific.h" 13 #include "base/containers/adapters_internal.h" 14 15 namespace base { 16 17 // Returns a range adapter that exposes its elements as rvalues. When used as an 18 // input range, this means the values in `range` will be moved from (and 19 // the values potentially in an unspecified but valid state). 20 template <typename Range> 21 requires(std::ranges::input_range<Range> && 22 // The elements in the input range will be consumed, so restrict this 23 // to non-borrowed ranges, as the elements from a borrowed range can 24 // outlive this call. 25 !std::ranges::borrowed_range<Range> && 26 // Disallow input ranges if the elements cannot be moved from (e.g. 27 // if they are const-qualified). 28 std::movable< 29 std::remove_reference_t<std::ranges::range_reference_t<Range>>>) RangeAsRvalues(Range && range LIFETIME_BOUND)30auto RangeAsRvalues(Range&& range LIFETIME_BOUND) { 31 return internal::RangeOfRvaluesAdapter<Range>(std::forward<Range>(range)); 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 Range> Reversed(Range && range LIFETIME_BOUND)44auto Reversed(Range&& range LIFETIME_BOUND) { 45 return internal::ReversedAdapter<Range>(std::forward<Range>(range)); 46 } 47 48 } // namespace base 49 50 #endif // BASE_CONTAINERS_ADAPTERS_H_ 51