| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ut0tuple.h |
| 29 |
std::tuple helper utilities. */ |
| 30 |
|
| 31 |
#ifndef ut0tuple_h |
| 32 |
#define ut0tuple_h |
| 33 |
|
| 34 |
#include <tuple> |
| 35 |
#include <utility> |
| 36 |
|
| 37 |
// std::index_sequence can only generate a sequence of indices starting from 0. |
| 38 |
// This utility, index_sequence_with_offset, enables us to generate a |
| 39 |
// sequence of indices with the offset. |
| 40 |
// |
| 41 |
// E.g. |
| 42 |
// index_sequence_with_offset_t<10, std::make_index_sequence<5>{}> |
| 43 |
// generates a sequence of {10, 11, 12, 13, 14} integers. |
| 44 |
template <std::size_t N, typename Seq> |
| 45 |
struct index_sequence_with_offset; |
| 46 |
|
| 47 |
template <std::size_t N, std::size_t... Ints> |
| 48 |
struct index_sequence_with_offset<N, std::index_sequence<Ints...>> { |
| 49 |
using type = std::index_sequence<Ints + N...>; |
| 50 |
}; |
| 51 |
|
| 52 |
// Helper alias which saves us from typing ::type |
| 53 |
template <std::size_t N, typename Seq> |
| 54 |
using index_sequence_with_offset_t = |
| 55 |
typename index_sequence_with_offset<N, Seq>::type; |
| 56 |
|
| 57 |
namespace detail { |
| 58 |
template <typename Tuple, size_t... Is> |
| 59 |
constexpr auto select_from_tuple_impl(Tuple &&t, std::index_sequence<Is...>) { |
| 60 |
return std::make_tuple(std::get<Is>(t)...); |
| 61 |
} |
| 62 |
} // namespace detail |
| 63 |
|
| 64 |
// Utility function which returns a new tuple which is a [Begin, End> subset of |
| 65 |
// given tuple. |
| 66 |
// E.g. |
| 67 |
// auto t = select_from_tuple<1, 3>(make_tuple(1, 2, 3, 4, 5)); |
| 68 |
// assert(t == make_tuple(2, 3)); |
| 69 |
template <size_t Begin, size_t End, typename Tuple> |
| 70 |
constexpr auto select_from_tuple(Tuple &&t) { |
| 71 |
return detail::select_from_tuple_impl( |
| 72 |
std::forward<Tuple>(t), |
| 73 |
index_sequence_with_offset_t<Begin, |
| 74 |
std::make_index_sequence<End - Begin>>{}); |
| 75 |
} |
| 76 |
|
| 77 |
#endif /* ut0tuple_h */ |