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>
46 std::bool_constant<any_of({std::is_same<Type, Ts>::value...})>;
48 // Checks if the parameter pack only contains |Type|.
49 template <typename Type>
51 std::bool_constant<all_of({std::is_same<Type, Ts>::value...})>;
53 // Checks if |Type| occurs only once in the parameter pack.
54 template <typename Type>
55 using IsUniqueInPack =
56 std::bool_constant<count({std::is_same<Type, Ts>::value...}, true) == 1>;
58 // Returns the zero-based index of |Type| within |Pack...| or |pack_npos| if
59 // it's not within the pack.
60 template <typename Type>
61 static constexpr size_t IndexInPack() {
63 for (bool value : {std::is_same<Type, Ts>::value...}) {
71 // Helper for extracting the Nth type from a parameter pack.
73 using NthType = std::tuple_element_t<N, std::tuple<Ts...>>;
75 // Checks if every type in the parameter pack is the same.
77 std::bool_constant<all_of({std::is_same<NthType<0>, Ts>::value...})>;
82 #endif // BASE_PARAMETER_PACK_H_