The Gaudi Framework  master (37c0b60a)
reverse.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_REVERSE_H
12 #define GAUDIKERNEL_REVERSE_H
13 //
14 // provide a generic 'reverse' function for use in range-based for loops.
15 //
16 // example:
17 //
18 // #include "GaudiKernel/reverse.h"
19 // auto l = { 1,2,3,4 };
20 // for ( const auto& i : reverse( l ) ) std::cout << i << std::endl;
21 //
22 // Note that it is perfectly fine to reverse a temporary. The temporary
23 // will be moved into the reverse_wrapper, and thus kept alive for the
24 // duration of the loop. In case reverse is called on an lvalue, the wrapper
25 // will take a reference, so no copy will be performed. (if you wonder how
26 // the code below (very implicitly) distinguishes between the two cases,
27 // google for 'C++11 reference collapsing' -- short version: C++ does not
28 // allow references to references, so in cases where this would happen,
29 // one gets an lvalue reference)
30 //
31 // Also note that reverse_wrapper does not have a constructor, but this
32 // implies that it does meet the requirements for aggregate initializaton,
33 // which allows for {} initialization of its member.
34 //
35 
36 #include <iterator>
37 #include <utility>
38 
39 namespace details {
40 
41  template <typename Iterable>
42  struct reverse_wrapper {
43  Iterable iterable;
44  };
45 
46  template <typename T>
48  using std::rbegin;
49  return rbegin( w.iterable );
50  }
51  template <typename T>
52  auto end( reverse_wrapper<T>& w ) {
53  using std::rend;
54  return rend( w.iterable );
55  }
56 } // namespace details
57 
58 template <typename T>
60  return { std::forward<T>( iterable ) };
61 }
62 
63 #endif
reverse
::details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:59
details::reverse_wrapper
Definition: reverse.h:42
details
Definition: AnyDataWrapper.h:19
details::begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:47
details::reverse_wrapper::iterable
Iterable iterable
Definition: reverse.h:43
details::end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:52