FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tuple_utils.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (1 Mar. 2011)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 #include <tuple>
19 
20 namespace cpp {
21 
23 template<typename TUPLE, typename FUNC>
24 void tuple_for_each(const TUPLE& t, FUNC f);
25 
27 template<typename TUPLE, typename FUNC>
28 void tuple_reversed_for_each(const TUPLE& t, FUNC f);
29 
30 // --
31 // -- Implementation
32 // --
33 
34 namespace internal {
35 
36 template<std::size_t N, std::size_t I, typename TUPLE, typename FUNC>
37 struct impl_tuple_for_each
38 {
39  static void call(const TUPLE& t, FUNC f)
40  {
41  f(std::get<N - I>(t));
42  impl_tuple_for_each<N, I - 1, TUPLE, FUNC>::call(t, f);
43  }
44 };
45 
46 template<std::size_t N, typename TUPLE, typename FUNC>
47 struct impl_tuple_for_each<N, 1, TUPLE, FUNC>
48 {
49  static void call(const TUPLE& t, FUNC f)
50  {
51  f(std::get<N - 1>(t));
52  }
53 };
54 
55 // Empty tuple
56 template<typename TUPLE, typename FUNC>
57 struct impl_tuple_for_each<0, 0, TUPLE, FUNC>
58 {
59  static void call(const TUPLE&, FUNC)
60  { }
61 };
62 
63 template<std::size_t N, std::size_t I, typename TUPLE, typename FUNC>
64 struct impl_tuple_reversed_for_each
65 {
66  static void call(const TUPLE& t, FUNC f)
67  {
68  f(std::get<N - I - 1>(t));
69  impl_tuple_reversed_for_each<N, I + 1, TUPLE, FUNC>::call(t, f);
70  }
71 };
72 
73 template<std::size_t N, typename TUPLE, typename FUNC>
74 struct impl_tuple_reversed_for_each<N, N, TUPLE, FUNC>
75 {
76  static void call(const TUPLE&, FUNC)
77  { }
78 };
79 
80 // Empty tuple
81 template<typename TUPLE, typename FUNC>
82 struct impl_tuple_reversed_for_each<0, 0, TUPLE, FUNC>
83 {
84  static void call(const TUPLE&, FUNC)
85  { }
86 };
87 
88 } // namespace internal
89 
90 template<typename TUPLE, typename FUNC>
91 void tuple_for_each(const TUPLE& t, FUNC f)
92 {
93  internal::impl_tuple_for_each<
94  std::tuple_size<TUPLE>::value,
95  std::tuple_size<TUPLE>::value,
96  TUPLE,
97  FUNC>
98  ::call(t, f);
99 }
100 
101 template<typename TUPLE, typename FUNC>
102 void tuple_reversed_for_each(const TUPLE& t, FUNC f)
103 {
104  internal::impl_tuple_reversed_for_each<
105  std::tuple_size<TUPLE>::value, 0, TUPLE, FUNC>
106  ::call(t, f);
107 }
108 
109 } // namespace cpp
void tuple_reversed_for_each(const TUPLE &t, FUNC f)
Definition: tuple_utils.h:102
Definition: basic_shared_pointer.h:20
void tuple_for_each(const TUPLE &t, FUNC f)
Definition: tuple_utils.h:91