| // Copyright 2019 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef BASE_PARAMETER_PACK_H_ |
| #define BASE_PARAMETER_PACK_H_ |
| |
| #include<stddef.h> |
| |
| #include<initializer_list> |
| #include<tuple> |
| #include<type_traits> |
| |
| #include"base/containers/contains.h" |
| |
| namespacebase{ |
| |
| // Checks if any of the elements in |ilist| is true. |
| inlineconstexprbool any_of(std::initializer_list<bool> ilist){ |
| returnbase::Contains(ilist,true); |
| } |
| |
| // Checks if all of the elements in |ilist| are true. |
| inlineconstexprbool all_of(std::initializer_list<bool> ilist){ |
| return!base::Contains(ilist,false); |
| } |
| |
| // Counts the elements in |ilist| that are equal to |value|. |
| // Similar to std::count for the case of constexpr initializer_list. |
| template<class T> |
| inlineconstexprsize_t count(std::initializer_list<T> ilist, T value){ |
| size_t c=0; |
| for(constauto& v: ilist){ |
| c+=(v== value); |
| } |
| return c; |
| } |
| |
| constexprsize_t pack_npos=static_cast<size_t>(-1); |
| |
| template<typename...Ts> |
| structParameterPack{ |
| // Checks if |Type| occurs in the parameter pack. |
| template<typenameType> |
| usingHasType= std::bool_constant<any_of({std::is_same_v<Type,Ts>...})>; |
| |
| // Checks if the parameter pack only contains |Type|. |
| template<typenameType> |
| usingOnlyHasType= std::bool_constant<all_of({std::is_same_v<Type,Ts>...})>; |
| |
| // Checks if |Type| occurs only once in the parameter pack. |
| template<typenameType> |
| usingIsUniqueInPack= |
| std::bool_constant<count({std::is_same_v<Type,Ts>...},true)==1>; |
| |
| // Returns the zero-based index of |Type| within |Pack...| or |pack_npos| if |
| // it's not within the pack. |
| template<typenameType> |
| staticconstexprsize_tIndexInPack(){ |
| size_t index=0; |
| for(bool value:{std::is_same_v<Type,Ts>...}){ |
| if(value){ |
| return index; |
| } |
| index++; |
| } |
| return pack_npos; |
| } |
| |
| // Helper for extracting the Nth type from a parameter pack. |
| template<size_t N> |
| usingNthType= std::tuple_element_t<N, std::tuple<Ts...>>; |
| |
| // Checks if every type in the parameter pack is the same. |
| usingIsAllSameType= |
| std::bool_constant<all_of({std::is_same_v<NthType<0>,Ts>...})>; |
| }; |
| |
| }// namespace base |
| |
| #endif// BASE_PARAMETER_PACK_H_ |