|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2019 ARM Limited |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES LEOR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#ifndef MSTD_TUPLE_ |
| 18 | +#define MSTD_TUPLE_ |
| 19 | + |
| 20 | +/* <mstd_tuple> |
| 21 | + * |
| 22 | + * - includes toolchain's <tuple> |
| 23 | + * - For all toolchains, C++17 backports: |
| 24 | + * - mstd::apply |
| 25 | + * - mstd::make_from_tuple |
| 26 | + */ |
| 27 | + |
| 28 | +#include <tuple> |
| 29 | + |
| 30 | +#if __cpp_lib_apply < 201603 || __cpp_lib_make_from_tuple < 201606 |
| 31 | +#include <mstd_utility> // integer_sequence |
| 32 | +#endif |
| 33 | +#if __cpp_lib_apply < 201603 |
| 34 | +#include <mstd_functional> // invoke |
| 35 | +#endif |
| 36 | + |
| 37 | +namespace mstd { |
| 38 | +using std::tuple; |
| 39 | +using std::ignore; |
| 40 | +using std::make_tuple; |
| 41 | +using std::forward_as_tuple; |
| 42 | +using std::tie; |
| 43 | +using std::tuple_cat; |
| 44 | +using std::tuple_size; |
| 45 | +using std::tuple_element; |
| 46 | +using std::tuple_element_t; |
| 47 | +using std::get; |
| 48 | + |
| 49 | +// [tuple.apply] |
| 50 | +#if __cpp_lib_apply >= 201603 |
| 51 | +using std::apply; |
| 52 | +#else |
| 53 | +namespace impl { |
| 54 | +template <class F, class Tuple, size_t... I> |
| 55 | +invoke_result_t<F, tuple_element_t<I, Tuple>...> apply(F&& f, Tuple&& t, std::index_sequence<I...>) |
| 56 | +{ |
| 57 | + return mstd::invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...); |
| 58 | +} |
| 59 | +} |
| 60 | + |
| 61 | +// apply<F, Tuple> - works also for tuple-like objects such as array or pair |
| 62 | +// user-defined types can specialize std::get and std::tuple_size to make this work |
| 63 | +template <class F, class Tuple> |
| 64 | +auto apply(F&& f, Tuple&& t) -> |
| 65 | +decltype(impl::apply(std::forward<F>(f), std::forward<Tuple>(t), std::make_index_sequence<tuple_size<remove_reference_t<Tuple>>::value>{})) |
| 66 | +{ |
| 67 | + return impl::apply(std::forward<F>(f), std::forward<Tuple>(t), |
| 68 | + std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{}); |
| 69 | +} |
| 70 | +#endif |
| 71 | + |
| 72 | +#if __cpp_lib_make_from_tuple >= 201606 |
| 73 | +using std::make_from_tuple; |
| 74 | +#else |
| 75 | +namespace impl { |
| 76 | +template <class T, class Tuple, size_t... I> |
| 77 | +T make_from_tuple(Tuple&& t, std::index_sequence<I...>) |
| 78 | +{ |
| 79 | + return T(std::get<I>(std::forward<Tuple>(t))...); |
| 80 | +} |
| 81 | +} |
| 82 | + |
| 83 | +template <class T, class Tuple> |
| 84 | +T make_from_tuple(Tuple&& t) |
| 85 | +{ |
| 86 | + return impl::make_from_tuple<T>(std::forward<Tuple>(t), |
| 87 | + std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{}); |
| 88 | +} |
| 89 | +#endif |
| 90 | + |
| 91 | +} // namespace mstd |
| 92 | + |
| 93 | +#endif // MSTD_TUPLE_ |
0 commit comments