| The Gaudi Framework v32r1 (f65d50dc)

reverse.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_REVERSE_H
2 #define GAUDIKERNEL_REVERSE_H
3 //
4 // provide a generic 'reverse' function for use in range-based for loops.
5 //
6 // example:
7 //
8 // #include "GaudiKernel/reverse.h"
9 // auto l = { 1,2,3,4 };
10 // for ( const auto& i : reverse( l ) ) std::cout << i << std::endl;
11 //
12 // Note that it is perfectly fine to reverse a temporary. The temporary
13 // will be moved into the reverse_wrapper, and thus kept alive for the
14 // duration of the loop. In case reverse is called on an lvalue, the wrapper
15 // will take a reference, so no copy will be performed. (if you wonder how
16 // the code below (very implicitly) distinguishes between the two cases,
17 // google for 'C++11 reference collapsing' -- short version: C++ does not
18 // allow references to references, so in cases where this would happen,
19 // one gets an lvalue reference)
20 //
21 // Also note that reverse_wrapper does not have a constructor, but this
22 // implies that it does meet the requirements for aggregate initializaton,
23 // which allows for {} initialization of its member.
24 //
25
26 #include <iterator>
27 #include <utility>
28
29 namespace details {
30
31 template <typename Iterable>
32 struct reverse_wrapper {
33 Iterable iterable;
34 };
35
36 template <typename T>
37 auto begin( reverse_wrapper<T>& w ) {
38 using std::rbegin;
39 return rbegin( w.iterable );
40 }
41 template <typename T>
42 auto end( reverse_wrapper<T>& w ) {
43 using std::rend;
44 return rend( w.iterable );
45 }
46 } // namespace details
47
48 template <typename T>
49 ::details::reverse_wrapper<T> reverse( T&& iterable ) {
50 return {std::forward<T>( iterable )};
51 }
52
53 #endif
details::end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:42
reverse
::details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:49
details::reverse_wrapper
Definition: reverse.h:32
details
Definition: AnyDataWrapper.h:10
details::begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:37
details::reverse_wrapper::iterable
Iterable iterable
Definition: reverse.h:33