Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
tuple_utils.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2017 ScyllaDB.
20 */
21
22#pragma once
23
24#include <tuple>
25#include <utility>
26#include <stddef.h>
27
28namespace seastar {
29
31namespace internal {
32
33template<typename Tuple>
34Tuple untuple(Tuple t) {
35 return t;
36}
37
38template<typename T>
39T untuple(std::tuple<T> t) {
40 return std::get<0>(std::move(t));
41}
42
43template<typename Tuple, typename Function, size_t... I>
44void tuple_for_each_helper(Tuple&& t, Function&& f, std::index_sequence<I...>&&) {
45 auto ignore_me = { (f(std::get<I>(std::forward<Tuple>(t))), 1)... };
46 (void)ignore_me;
47}
48
49template<typename Tuple, typename MapFunction, size_t... I>
50auto tuple_map_helper(Tuple&& t, MapFunction&& f, std::index_sequence<I...>&&) {
51 return std::make_tuple(f(std::get<I>(std::forward<Tuple>(t)))...);
52}
53
54template<size_t I, typename IndexSequence>
55struct prepend;
56
57template<size_t I, size_t... Is>
58struct prepend<I, std::index_sequence<Is...>> {
59 using type = std::index_sequence<I, Is...>;
60};
61
62template<template<typename> class Filter, typename Tuple, typename IndexSequence>
63struct tuple_filter;
64
65template<template<typename> class Filter, typename T, typename... Ts, size_t I, size_t... Is>
66struct tuple_filter<Filter, std::tuple<T, Ts...>, std::index_sequence<I, Is...>> {
67 using tail = typename tuple_filter<Filter, std::tuple<Ts...>, std::index_sequence<Is...>>::type;
68 using type = std::conditional_t<Filter<T>::value, typename prepend<I, tail>::type, tail>;
69};
70
71template<template<typename> class Filter>
72struct tuple_filter<Filter, std::tuple<>, std::index_sequence<>> {
73 using type = std::index_sequence<>;
74};
75
76template<typename Tuple, size_t... I>
77auto tuple_filter_helper(Tuple&& t, std::index_sequence<I...>&&) {
78 return std::make_tuple(std::get<I>(std::forward<Tuple>(t))...);
79}
80
81}
83
86
95template<template<typename> class MapClass, typename Tuple>
97
99
100template<template<typename> class MapClass, typename... Elements>
101struct tuple_map_types<MapClass, std::tuple<Elements...>> {
102 using type = std::tuple<typename MapClass<Elements>::type...>;
103};
104
107
117template<template<typename> class FilterClass, typename... Elements>
118auto tuple_filter_by_type(const std::tuple<Elements...>& t) {
119 using sequence = typename internal::tuple_filter<FilterClass, std::tuple<Elements...>,
120 std::index_sequence_for<Elements...>>::type;
121 return internal::tuple_filter_helper(t, sequence());
122}
123template<template<typename> class FilterClass, typename... Elements>
124auto tuple_filter_by_type(std::tuple<Elements...>&& t) {
125 using sequence = typename internal::tuple_filter<FilterClass, std::tuple<Elements...>,
126 std::index_sequence_for<Elements...>>::type;
127 return internal::tuple_filter_helper(std::move(t), sequence());
128}
129
138template<typename Function, typename... Elements>
139auto tuple_map(const std::tuple<Elements...>& t, Function&& f) {
140 return internal::tuple_map_helper(t, std::forward<Function>(f),
141 std::index_sequence_for<Elements...>());
142}
143template<typename Function, typename... Elements>
144auto tuple_map(std::tuple<Elements...>&& t, Function&& f) {
145 return internal::tuple_map_helper(std::move(t), std::forward<Function>(f),
146 std::index_sequence_for<Elements...>());
147}
148
156template<typename Function, typename... Elements>
157void tuple_for_each(const std::tuple<Elements...>& t, Function&& f) {
158 return internal::tuple_for_each_helper(t, std::forward<Function>(f),
159 std::index_sequence_for<Elements...>());
160}
161template<typename Function, typename... Elements>
162void tuple_for_each(std::tuple<Elements...>& t, Function&& f) {
163 return internal::tuple_for_each_helper(t, std::forward<Function>(f),
164 std::index_sequence_for<Elements...>());
165}
166template<typename Function, typename... Elements>
167void tuple_for_each(std::tuple<Elements...>&& t, Function&& f) {
168 return internal::tuple_for_each_helper(std::move(t), std::forward<Function>(f),
169 std::index_sequence_for<Elements...>());
170}
171
173
174}
auto tuple_map(const std::tuple< Elements... > &t, Function &&f)
Definition: tuple_utils.hh:139
void tuple_for_each(const std::tuple< Elements... > &t, Function &&f)
Definition: tuple_utils.hh:157
auto tuple_filter_by_type(const std::tuple< Elements... > &t)
Definition: tuple_utils.hh:118
Definition: tuple_utils.hh:96
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
STL namespace.