1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_PARAMETER_PACK_H_
6 #define BASE_PARAMETER_PACK_H_
10 #include <initializer_list>
12 #include <type_traits>
14 #include "base/containers/contains.h"
18 // Checks if any of the elements in |ilist| is true.
19 inline constexpr bool any_of(std::initializer_list<bool> ilist) {
20 return base::Contains(ilist, true);
23 // Checks if all of the elements in |ilist| are true.
24 inline constexpr bool all_of(std::initializer_list<bool> ilist) {
25 return !base::Contains(ilist, false);
28 // Counts the elements in |ilist| that are equal to |value|.
29 // Similar to std::count for the case of constexpr initializer_list.
31 inline constexpr size_t count(std::initializer_list<T> ilist, T value) {
33 for (const auto& v : ilist) {
39 constexpr size_t pack_npos = static_cast<size_t>(-1);
41 template <typename... Ts>
42 struct ParameterPack {
43 // Checks if |Type| occurs in the parameter pack.
44 template <typename Type>
45 using HasType = std::bool_constant<any_of({std::is_same_v<Type, Ts>...})>;
47 // Checks if the parameter pack only contains |Type|.
48 template <typename Type>
49 using OnlyHasType = std::bool_constant<all_of({std::is_same_v<Type, Ts>...})>;
51 // Checks if |Type| occurs only once in the parameter pack.
52 template <typename Type>
53 using IsUniqueInPack =
54 std::bool_constant<count({std::is_same_v<Type, Ts>...}, true) == 1>;
56 // Returns the zero-based index of |Type| within |Pack...| or |pack_npos| if
57 // it's not within the pack.
58 template <typename Type>
59 static constexpr size_t IndexInPack() {
61 for (bool value : {std::is_same_v<Type, Ts>...}) {
69 // Helper for extracting the Nth type from a parameter pack.
71 using NthType = std::tuple_element_t<N, std::tuple<Ts...>>;
73 // Checks if every type in the parameter pack is the same.
75 std::bool_constant<all_of({std::is_same_v<NthType<0>, Ts>...})>;
80 #endif // BASE_PARAMETER_PACK_H_