|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_TUPLE_UTILS_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_TUPLE_UTILS_H |
| 17 | + |
| 18 | +#include "google/cloud/bigtable/version.h" |
| 19 | +#include <tuple> |
| 20 | +#include <type_traits> |
| 21 | + |
| 22 | +namespace google { |
| 23 | +namespace cloud { |
| 24 | +namespace bigtable_internal { |
| 25 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 26 | + |
| 27 | +// The implementation for `IsTuple<T>` (below). |
| 28 | +template <typename T> |
| 29 | +struct IsTupleImpl : std::false_type {}; |
| 30 | +template <typename... Ts> |
| 31 | +struct IsTupleImpl<std::tuple<Ts...>> : std::true_type {}; |
| 32 | + |
| 33 | +// Decays the given type `T` and determines whether it is a `std::tuple<...>`. |
| 34 | +// |
| 35 | +// Example: |
| 36 | +// |
| 37 | +// using Type = std::tuple<...>; |
| 38 | +// static_assert(IsTuple<Type>::value, ""); |
| 39 | +// |
| 40 | +template <typename T> |
| 41 | +using IsTuple = IsTupleImpl<std::decay_t<T>>; |
| 42 | + |
| 43 | +// Decays the tuple `T` and returns its size as in the ::value member. |
| 44 | +template <typename T> |
| 45 | +using TupleSize = std::tuple_size<std::decay_t<T>>; |
| 46 | + |
| 47 | +// Base case of `ForEach` that is called at the end of iterating a tuple. |
| 48 | +// See the docs for the next overload to see how to use `ForEach`. |
| 49 | +template <std::size_t I = 0, typename T, typename F, typename... Args> |
| 50 | +std::enable_if_t<I == TupleSize<T>::value, void> ForEach(T&&, F&&, Args&&...) {} |
| 51 | + |
| 52 | +// This function template iterates the elements of a tuple, calling the given |
| 53 | +// functor with each of the tuple's elements as well as any additional |
| 54 | +// (optional) caller-provided arguments. The given functor should be able to |
| 55 | +// accept each type in the container. All arguments are perfect-forwarded to |
| 56 | +// the functor, so the functor may choose to accept the tuple arguments by |
| 57 | +// value, const-ref, or even non-const reference, in which case the elements |
| 58 | +// inside the tuple may be modified. |
| 59 | +// |
| 60 | +// Example: |
| 61 | +// |
| 62 | +// struct Stringify { |
| 63 | +// template <typename T> |
| 64 | +// void operator()(T& t, std::vector<std::string>& out) const { |
| 65 | +// out.push_back(std::to_string(t)); |
| 66 | +// } |
| 67 | +// }; |
| 68 | +// auto tup = std::make_tuple(true, 42); |
| 69 | +// std::vector<std::string> v; |
| 70 | +// internal::ForEach(tup, Stringify{}, v); |
| 71 | +// EXPECT_THAT(v, testing::ElementsAre("1", "42")); |
| 72 | +// |
| 73 | +template <std::size_t I = 0, typename T, typename F, typename... Args> |
| 74 | +std::enable_if_t<(I < TupleSize<T>::value), void> ForEach(T&& t, F&& f, |
| 75 | + Args&&... args) { |
| 76 | + auto&& e = std::get<I>(std::forward<T>(t)); |
| 77 | + std::forward<F>(f)(std::forward<decltype(e)>(e), std::forward<Args>(args)...); |
| 78 | + ForEach<I + 1>(std::forward<T>(t), std::forward<F>(f), |
| 79 | + std::forward<Args>(args)...); |
| 80 | +} |
| 81 | + |
| 82 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 83 | +} // namespace bigtable_internal |
| 84 | +} // namespace cloud |
| 85 | +} // namespace google |
| 86 | + |
| 87 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_TUPLE_UTILS_H |
0 commit comments