[M108 Migration][VD] Support set time and time zone offset
[platform/framework/web/chromium-efl.git] / base / template_util.h
1 // Copyright 2011 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_TEMPLATE_UTIL_H_
6 #define BASE_TEMPLATE_UTIL_H_
7
8 #include <stddef.h>
9
10 #include <iosfwd>
11 #include <iterator>
12 #include <type_traits>
13 #include <utility>
14
15 #include "base/compiler_specific.h"
16
17 namespace base {
18
19 namespace internal {
20
21 // Uses expression SFINAE to detect whether using operator<< would work.
22 template <typename T, typename = void>
23 struct SupportsOstreamOperator : std::false_type {};
24 template <typename T>
25 struct SupportsOstreamOperator<T,
26                                decltype(void(std::declval<std::ostream&>()
27                                              << std::declval<T>()))>
28     : std::true_type {};
29
30 template <typename T, typename = void>
31 struct SupportsToString : std::false_type {};
32 template <typename T>
33 struct SupportsToString<T, decltype(void(std::declval<T>().ToString()))>
34     : std::true_type {};
35
36 // Used to detech whether the given type is an iterator.  This is normally used
37 // with std::enable_if to provide disambiguation for functions that take
38 // templatzed iterators as input.
39 template <typename T, typename = void>
40 struct is_iterator : std::false_type {};
41
42 template <typename T>
43 struct is_iterator<
44     T,
45     std::void_t<typename std::iterator_traits<T>::iterator_category>>
46     : std::true_type {};
47
48 // Helper to express preferences in an overload set. If more than one overload
49 // are available for a given set of parameters the overload with the higher
50 // priority will be chosen.
51 template <size_t I>
52 struct priority_tag : priority_tag<I - 1> {};
53
54 template <>
55 struct priority_tag<0> {};
56
57 }  // namespace internal
58
59 namespace internal {
60
61 // The indirection with std::is_enum<T> is required, because instantiating
62 // std::underlying_type_t<T> when T is not an enum is UB prior to C++20.
63 template <typename T, bool = std::is_enum<T>::value>
64 struct IsScopedEnumImpl : std::false_type {};
65
66 template <typename T>
67 struct IsScopedEnumImpl<T, /*std::is_enum<T>::value=*/true>
68     : std::negation<std::is_convertible<T, std::underlying_type_t<T>>> {};
69
70 }  // namespace internal
71
72 // Implementation of C++23's std::is_scoped_enum
73 //
74 // Reference: https://en.cppreference.com/w/cpp/types/is_scoped_enum
75 template <typename T>
76 struct is_scoped_enum : internal::IsScopedEnumImpl<T> {};
77
78 // Implementation of C++20's std::remove_cvref.
79 //
80 // References:
81 // - https://en.cppreference.com/w/cpp/types/remove_cvref
82 // - https://wg21.link/meta.trans.other#lib:remove_cvref
83 template <typename T>
84 struct remove_cvref {
85   using type = std::remove_cv_t<std::remove_reference_t<T>>;
86 };
87
88 // Implementation of C++20's std::remove_cvref_t.
89 //
90 // References:
91 // - https://en.cppreference.com/w/cpp/types/remove_cvref
92 // - https://wg21.link/meta.type.synop#lib:remove_cvref_t
93 template <typename T>
94 using remove_cvref_t = typename remove_cvref<T>::type;
95
96 // Simplified implementation of C++20's std::iter_value_t.
97 // As opposed to std::iter_value_t, this implementation does not restrict
98 // the type of `Iter` and does not consider specializations of
99 // `indirectly_readable_traits`.
100 //
101 // Reference: https://wg21.link/readable.traits#2
102 template <typename Iter>
103 struct IterValueImpl {
104   using value_type = typename std::iterator_traits<Iter>::value_type;
105 };
106
107 template <typename T, bool Cond = false>
108 struct IterValuePointerImpl {
109   // The `iterator_traits<T*>::value_type` member is not defined if T is not an
110   // object in C++20.
111 };
112 template <typename T>
113 struct IterValuePointerImpl<T*, true> {
114   using value_type = typename std::iterator_traits<T*>::value_type;
115 };
116
117 template <typename T>
118 struct IterValueImpl<T*> {
119   using value_type =
120       typename IterValuePointerImpl<T*, std::is_object_v<T>>::value_type;
121 };
122
123 template <typename Iter>
124 using iter_value_t = typename IterValueImpl<remove_cvref_t<Iter>>::value_type;
125
126 // Simplified implementation of C++20's std::iter_reference_t.
127 // As opposed to std::iter_reference_t, this implementation does not restrict
128 // the type of `Iter`.
129 //
130 // Reference: https://wg21.link/iterator.synopsis#:~:text=iter_reference_t
131 template <typename Iter>
132 using iter_reference_t = decltype(*std::declval<Iter&>());
133
134 // Simplified implementation of C++20's std::indirect_result_t. As opposed to
135 // std::indirect_result_t, this implementation does not restrict the type of
136 // `Func` and `Iters`.
137 //
138 // Reference: https://wg21.link/iterator.synopsis#:~:text=indirect_result_t
139 template <typename Func, typename... Iters>
140 using indirect_result_t =
141     std::invoke_result_t<Func, iter_reference_t<Iters>...>;
142
143 // Simplified implementation of C++20's std::projected. As opposed to
144 // std::projected, this implementation does not explicitly restrict the type of
145 // `Iter` and `Proj`, but rather does so implicitly by requiring
146 // `indirect_result_t<Proj, Iter>` is a valid type. This is required for SFINAE
147 // friendliness.
148 //
149 // Reference: https://wg21.link/projected
150 template <typename Iter,
151           typename Proj,
152           typename IndirectResultT = indirect_result_t<Proj, Iter>>
153 struct projected {
154   using value_type = remove_cvref_t<IndirectResultT>;
155
156   IndirectResultT operator*() const;  // not defined
157 };
158
159 }  // namespace base
160
161 #endif  // BASE_TEMPLATE_UTIL_H_