RESTinio
tuple_algorithms.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
15
16#include <utility>
17#include <tuple>
18
19namespace restinio
20{
21
22namespace utils
23{
24
25namespace tuple_algorithms
26{
27
28namespace impl
29{
30
31template< typename T >
33 std::make_index_sequence< std::tuple_size<T>::value >;
34
35template< typename Predicate >
37bool
38all_of_impl( Predicate && /*p*/ )
39{
40 return true;
41}
42
43template< typename Predicate, typename T, typename... Vs >
45bool
46all_of_impl( Predicate && p, T && current, Vs &&... rest )
47{
48 return p( std::forward<T>(current) ) &&
49 all_of_impl( std::forward<Predicate>(p), std::forward<Vs>(rest)... );
50}
51
52template< typename Predicate, typename Tuple, std::size_t... I >
54bool
56 Predicate && p,
57 Tuple && t,
58 std::index_sequence<I...> )
59{
60 return all_of_impl(
61 std::forward<Predicate>(p),
62 std::get<I>(std::forward<Tuple>(t))... );
63}
64
65template< typename Predicate >
67bool
68any_of_impl( Predicate && /*p*/ )
69{
70 return false;
71}
72
73template< typename Predicate, typename T, typename... Vs >
75bool
76any_of_impl( Predicate && p, T && current, Vs &&... rest )
77{
78 return p( std::forward<T>(current) ) ||
79 any_of_impl( std::forward<Predicate>(p), std::forward<Vs>(rest)... );
80}
81
82template< typename Predicate, typename Tuple, std::size_t... I >
84bool
86 Predicate && p,
87 Tuple && t,
88 std::index_sequence<I...> )
89{
90 return any_of_impl(
91 std::forward<Predicate>(p),
92 std::get<I>(std::forward<Tuple>(t))... );
93}
94
95} /* namespace impl */
96
97//
98// all_of
99//
100template< typename Tuple, typename Predicate >
102bool
103all_of( Tuple && tuple, Predicate && predicate )
104{
106 std::forward<Predicate>(predicate),
107 std::forward<Tuple>(tuple),
108 typename impl::index_sequence_for_tuple<std::decay_t<Tuple>>{} );
109}
110
111//
112// any_of
113//
114template< typename Tuple, typename Predicate >
116bool
117any_of( Tuple && tuple, Predicate && predicate )
118{
120 std::forward<Predicate>(predicate),
121 std::forward<Tuple>(tuple),
122 typename impl::index_sequence_for_tuple<std::decay_t<Tuple>>{} );
123}
124
125} /* namespace tuple_algorithms */
126
127} /* namespace utils */
128
129} /* namespace restinio */
130
Detection of compiler version and absence of various features.
#define RESTINIO_NODISCARD
std::make_index_sequence< std::tuple_size< T >::value > index_sequence_for_tuple
RESTINIO_NODISCARD bool perform_any_of(Predicate &&p, Tuple &&t, std::index_sequence< I... >)
RESTINIO_NODISCARD bool perform_all_of(Predicate &&p, Tuple &&t, std::index_sequence< I... >)
RESTINIO_NODISCARD bool all_of_impl(Predicate &&)
RESTINIO_NODISCARD bool any_of_impl(Predicate &&)
RESTINIO_NODISCARD bool all_of(Tuple &&tuple, Predicate &&predicate)
RESTINIO_NODISCARD bool any_of(Tuple &&tuple, Predicate &&predicate)