fixup! [M120 Migration] Notify media device state to webbrowser
[platform/framework/web/chromium-efl.git] / base / parameter_pack.h
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.
4
5 #ifndef BASE_PARAMETER_PACK_H_
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/containers/contains.h"
15
16 namespace base {
17
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);
21 }
22
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);
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.
30 template <class T>
31 inline constexpr size_t count(std::initializer_list<T> ilist, T value) {
32   size_t c = 0;
33   for (const auto& v : ilist) {
34     c += (v == value);
35   }
36   return c;
37 }
38
39 constexpr size_t pack_npos = static_cast<size_t>(-1);
40
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>...})>;
46
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>...})>;
50
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>;
55
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() {
60     size_t index = 0;
61     for (bool value : {std::is_same_v<Type, Ts>...}) {
62       if (value)
63         return index;
64       index++;
65     }
66     return pack_npos;
67   }
68
69   // Helper for extracting the Nth type from a parameter pack.
70   template <size_t N>
71   using NthType = std::tuple_element_t<N, std::tuple<Ts...>>;
72
73   // Checks if every type in the parameter pack is the same.
74   using IsAllSameType =
75       std::bool_constant<all_of({std::is_same_v<NthType<0>, Ts>...})>;
76 };
77
78 }  // namespace base
79
80 #endif  // BASE_PARAMETER_PACK_H_