| 6 | #define BASE_PARAMETER_PACK_H_ |
| 7 |
| 8 | #include <stddef.h> |
| 9 |
| 10 | #include <initializer_list> |
| 11 | #include <tuple> |
| 12 | #include <type_traits> |
| 13 |
| 14 | #include "base/template_util.h" |
| 15 | #include "build/build_config.h" |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | // Checks if any of the elements in |ilist| is true. |
| 20 | // Similar to std::any_of for the case of constexpr initializer_list. |
| 21 | inline constexpr bool any_of(std::initializer_list<bool> ilist) { |
| 22 | for (auto c : ilist) { |
| 23 | if (c) |
| 24 | return true; |
| 25 | } |
| 26 | return false; |
| 27 | } |
| 28 |
| 29 | // Checks if all of the elements in |ilist| are true. |
| 30 | // Similar to std::all_of for the case of constexpr initializer_list. |
| 31 | inline constexpr bool all_of(std::initializer_list<bool> ilist) { |
| 32 | for (auto c : ilist) { |
| 33 | if (!c) |
| 34 | return false; |
| 35 | } |
| 36 | return true; |
| 37 | } |
| 38 |
| 39 | // Counts the elements in |ilist| that are equal to |value|. |
| 40 | // Similar to std::count for the case of constexpr initializer_list. |
| 41 | template <class T> |
| 42 | inline constexpr size_t count(std::initializer_list<T> ilist, T value) { |
| 43 | size_t c = 0; |
| 44 | for (const auto& v : ilist) { |
| 45 | c += (v == value); |
| 46 | } |
| 47 | return c; |
| 48 | } |
| 49 |
| 50 | constexpr size_t pack_npos = -1; |
| 51 |
| 52 | template <typename... Ts> |
| 53 | struct ParameterPack { |
| 54 | // Checks if |Type| occurs in the parameter pack. |
| 55 | template <typename Type> |
| 56 | using HasType = bool_constant<any_of({std::is_same<Type, Ts>::value...})>; |
| 57 |
| 58 | // Checks if the parameter pack only contains |Type|. |
| 59 | template <typename Type> |
| 60 | using OnlyHasType = bool_constant<all_of({std::is_same<Type, Ts>::value...})>; |
| 61 |
| 62 | // Checks if |Type| occurs only once in the parameter pack. |
| 63 | template <typename Type> |
| 64 | using IsUniqueInPack = |
| 65 | bool_constant<count({std::is_same<Type, Ts>::value...}, true) == 1>; |
| 66 |
| 67 | // Returns the zero-based index of |Type| within |Pack...| or |pack_npos| if |
| 68 | // it's not within the pack. |
| 69 | template <typename Type> |
| 70 | static constexpr size_t IndexInPack() { |
| 71 | size_t index = 0; |
| 72 | for (bool value : {std::is_same<Type, Ts>::value...}) { |
| 73 | if (value) |
| 74 | return index; |
| 75 | index++; |
| 76 | } |
| 77 | return pack_npos; |
| 78 | } |
| 79 |
| 80 | // Helper for extracting the Nth type from a parameter pack. |
| 81 | template <size_t N> |
| 82 | using NthType = std::tuple_element_t<N, std::tuple<Ts...>>; |
| 83 |
| 84 | // Checks if every type in the parameter pack is the same. |
| 85 | using IsAllSameType = |
| 86 | bool_constant<all_of({std::is_same<NthType<0>, Ts>::value...})>; |
| 87 | }; |
| 88 |
| 89 | } // namespace base |
| 90 |
| 91 | #endif // BASE_PARAMETER_PACK_H_ |
| 92 |