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