summaryrefslogtreecommitdiff
path: root/sigc++/tuple-utils/tuple_for_each.h
blob: 5296b70349deb6a44559b1139101d657acfee5ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Copyright (C) 2016 Murray Cumming
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef SIGC_TUPLE_UTILS_TUPLE_FOR_EACH_H
#define SIGC_TUPLE_UTILS_TUPLE_FOR_EACH_H

#include <tuple>

namespace sigc
{

namespace internal
{

namespace detail
{

template <template <typename> class T_visitor, std::size_t size_from_index, typename... T_extras>
struct tuple_for_each_impl
{
  template <typename T>
  constexpr static void tuple_for_each(T&& t, T_extras&&... extras)
  {
    // We use std::decay_t<> because tuple_size is not defined for references.
    constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
    static_assert(size > 1, "size must be more than 0.");

    constexpr auto index = size - size_from_index;
    static_assert(index >= 0, "unexpected index.");

    using element_type = typename std::tuple_element<index, std::decay_t<T>>::type;
    T_visitor<element_type>::visit(std::get<index>(t), std::forward<T_extras>(extras)...);

    tuple_for_each_impl<T_visitor, size_from_index - 1, T_extras...>::tuple_for_each(
      std::forward<T>(t), std::forward<T_extras>(extras)...);
  }
};

template <template <typename> class T_visitor, typename... T_extras>
struct tuple_for_each_impl<T_visitor, 1, T_extras...>
{
  template <typename T>
  constexpr static void tuple_for_each(T&& t, T_extras&&... extras)
  {
    // We use std::decay_t<> because tuple_size is not defined for references.
    constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
    static_assert(size > 0, "size must be more than 0.");

    constexpr auto index = size - 1;
    static_assert(index >= 0, "unexpected index.");

    using element_type = typename std::tuple_element<index, std::decay_t<T>>::type;
    T_visitor<element_type>::visit(
      std::get<index>(std::forward<T>(t)), std::forward<T_extras>(extras)...);
  }
};

template <template <typename> class T_visitor, typename... T_extras>
struct tuple_for_each_impl<T_visitor, 0, T_extras...>
{
  template <typename T>
  constexpr static void tuple_for_each(T&& /* t */, T_extras&&... /* extras */)
  {
    // Do nothing because the tuple has no elements.
  }
};

} // detail namespace

/**
 * Call the @e T_Visitor functors visit() method for each element,
 * from the first to the last.
 *
 * @tparam T_visitor should be a template that has a static visit() method.
 * @tparam T the tuple type.
 * @tparam T_extras the types of any extra arguments to pass to @e T_Visitor's
 * visit() method.
 * @param t The tuple whose elements should be visited.
 * @param extras Any extra arguments to pass to @e T_Visitor's visit() method.
 */
template <template <typename> class T_visitor, typename T, typename... T_extras>
constexpr void
tuple_for_each(T&& t, T_extras&&... extras)
{
  // We use std::decay_t<> because tuple_size is not defined for references.
  constexpr auto size = std::tuple_size<std::decay_t<T>>::value;

  if constexpr (size != 0)
  {
    detail::tuple_for_each_impl<T_visitor, size, T_extras...>::tuple_for_each(
      std::forward<T>(t), std::forward<T_extras>(extras)...);
  } else {
    // Prevent 'unreferenced formal parameter' warning from MSVC by 'using' t
    static_cast<void>(t);
  }
}

} // namespace internal

} // namespace sigc

#endif // SIGC_TUPLE_UTILS_TUPLE_FOR_EACH_H