Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /base /parameter_pack.h
blob: c0ffb58bc22e327df69c2205597455c35bd52acf [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:06[diff] [blame]1// Copyright 2019 The Chromium Authors
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_PARAMETER_PACK_H_
6#define BASE_PARAMETER_PACK_H_
7
Jan Wilken Dörriea33bc992020-03-24 17:45:31[diff] [blame]8#include<stddef.h>
9
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]10#include<initializer_list>
Jan Wilken Dörriea33bc992020-03-24 17:45:31[diff] [blame]11#include<tuple>
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]12#include<type_traits>
13
Peter Kasting0909bd192022-09-01 21:39:04[diff] [blame]14#include"base/containers/contains.h"
15
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]16namespacebase{
17
18// Checks if any of the elements in |ilist| is true.
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]19inlineconstexprbool any_of(std::initializer_list<bool> ilist){
Peter Kasting0909bd192022-09-01 21:39:04[diff] [blame]20returnbase::Contains(ilist,true);
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]21}
22
23// Checks if all of the elements in |ilist| are true.
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]24inlineconstexprbool all_of(std::initializer_list<bool> ilist){
Peter Kasting0909bd192022-09-01 21:39:04[diff] [blame]25return!base::Contains(ilist,false);
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]26}
27
28// Counts the elements in |ilist| that are equal to |value|.
29// Similar to std::count for the case of constexpr initializer_list.
30template<class T>
31inlineconstexprsize_t count(std::initializer_list<T> ilist, T value){
32size_t c=0;
33for(constauto& v: ilist){
34 c+=(v== value);
35}
36return c;
37}
38
Peter Kastingde85e742022-06-01 17:41:54[diff] [blame]39constexprsize_t pack_npos=static_cast<size_t>(-1);
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]40
41template<typename...Ts>
42structParameterPack{
43// Checks if |Type| occurs in the parameter pack.
44template<typenameType>
Andrew Rayskiy629912382023-10-18 22:58:42[diff] [blame]45usingHasType= std::bool_constant<any_of({std::is_same_v<Type,Ts>...})>;
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]46
47// Checks if the parameter pack only contains |Type|.
48template<typenameType>
Andrew Rayskiy629912382023-10-18 22:58:42[diff] [blame]49usingOnlyHasType= std::bool_constant<all_of({std::is_same_v<Type,Ts>...})>;
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]50
51// Checks if |Type| occurs only once in the parameter pack.
52template<typenameType>
Jan Wilken Dörriea33bc992020-03-24 17:45:31[diff] [blame]53usingIsUniqueInPack=
Andrew Rayskiy629912382023-10-18 22:58:42[diff] [blame]54 std::bool_constant<count({std::is_same_v<Type,Ts>...},true)==1>;
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]55
56// Returns the zero-based index of |Type| within |Pack...| or |pack_npos| if
57// it's not within the pack.
58template<typenameType>
59staticconstexprsize_tIndexInPack(){
60size_t index=0;
Andrew Rayskiy629912382023-10-18 22:58:42[diff] [blame]61for(bool value:{std::is_same_v<Type,Ts>...}){
Peter Kasting134ef9af2024-12-28 02:30:09[diff] [blame]62if(value){
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]63return index;
Peter Kasting134ef9af2024-12-28 02:30:09[diff] [blame]64}
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]65 index++;
66}
67return pack_npos;
68}
69
70// Helper for extracting the Nth type from a parameter pack.
71template<size_t N>
Jan Wilken Dörriea33bc992020-03-24 17:45:31[diff] [blame]72usingNthType= std::tuple_element_t<N, std::tuple<Ts...>>;
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]73
74// Checks if every type in the parameter pack is the same.
Jan Wilken Dörriea33bc992020-03-24 17:45:31[diff] [blame]75usingIsAllSameType=
Andrew Rayskiy629912382023-10-18 22:58:42[diff] [blame]76 std::bool_constant<all_of({std::is_same_v<NthType<0>,Ts>...})>;
Alex Clarke8906d052019-03-13 14:58:04[diff] [blame]77};
78
79}// namespace base
80
81#endif// BASE_PARAMETER_PACK_H_

[8]ページ先頭

©2009-2025 Movatter.jp