Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
willchan@chromium.org | 951269b | 2010-06-15 19:39:24 | [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_TEMPLATE_UTIL_H_ |
| 6 | #define BASE_TEMPLATE_UTIL_H_ |
willchan@chromium.org | 951269b | 2010-06-15 19:39:24 | [diff] [blame] | 7 | |
danakj | a26bdfc | 2016-04-13 22:08:03 | [diff] [blame] | 8 | #include <stddef.h> |
Avi Drissman | 02aadae1 | 2022-05-04 02:50:48 | [diff] [blame] | 9 | |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame] | 10 | #include <iosfwd> |
Brett Wilson | 322a546 | 2017-08-03 23:59:12 | [diff] [blame] | 11 | #include <iterator> |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 12 | #include <type_traits> |
danakj | a26bdfc | 2016-04-13 22:08:03 | [diff] [blame] | 13 | #include <utility> |
ajwong@chromium.org | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 14 | |
Jan Wilken Dörrie | 2d7bca0 | 2021-04-19 19:59:32 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
willchan@chromium.org | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 16 | |
willchan@chromium.org | 951269b | 2010-06-15 19:39:24 | [diff] [blame] | 17 | namespace base { |
| 18 | |
danakj | a26bdfc | 2016-04-13 22:08:03 | [diff] [blame] | 19 | namespace internal { |
| 20 | |
Peter Kasting | b2887c3 | 2024-01-08 21:41:04 | [diff] [blame] | 21 | // Used to detect whether the given type is an iterator. This is normally used |
Brett Wilson | 322a546 | 2017-08-03 23:59:12 | [diff] [blame] | 22 | // with std::enable_if to provide disambiguation for functions that take |
| 23 | // templatzed iterators as input. |
| 24 | template <typename T, typename = void> |
| 25 | struct is_iterator : std::false_type {}; |
| 26 | |
| 27 | template <typename T> |
Avi Drissman | 1089236e | 2022-04-28 13:44:41 | [diff] [blame] | 28 | struct is_iterator< |
| 29 | T, |
| 30 | std::void_t<typename std::iterator_traits<T>::iterator_category>> |
Brett Wilson | 322a546 | 2017-08-03 23:59:12 | [diff] [blame] | 31 | : std::true_type {}; |
| 32 | |
Jan Wilken Dörrie | 9f8955f | 2020-09-16 10:27:18 | [diff] [blame] | 33 | // Helper to express preferences in an overload set. If more than one overload |
| 34 | // are available for a given set of parameters the overload with the higher |
| 35 | // priority will be chosen. |
| 36 | template <size_t I> |
| 37 | struct priority_tag : priority_tag<I - 1> {}; |
| 38 | |
| 39 | template <> |
| 40 | struct priority_tag<0> {}; |
| 41 | |
danakj | a26bdfc | 2016-04-13 22:08:03 | [diff] [blame] | 42 | } // namespace internal |
| 43 | |
Jan Wilken Dörrie | f89fc2ac | 2021-03-05 06:51:22 | [diff] [blame] | 44 | namespace internal { |
| 45 | |
| 46 | // The indirection with std::is_enum<T> is required, because instantiating |
| 47 | // std::underlying_type_t<T> when T is not an enum is UB prior to C++20. |
Andrew Rayskiy | 62991238 | 2023-10-18 22:58:42 | [diff] [blame] | 48 | template <typename T, bool = std::is_enum_v<T>> |
Jan Wilken Dörrie | f89fc2ac | 2021-03-05 06:51:22 | [diff] [blame] | 49 | struct IsScopedEnumImpl : std::false_type {}; |
| 50 | |
| 51 | template <typename T> |
Andrew Rayskiy | 62991238 | 2023-10-18 22:58:42 | [diff] [blame] | 52 | struct IsScopedEnumImpl<T, /*std::is_enum_v<T>=*/true> |
Avi Drissman | c109efd | 2022-04-27 22:03:35 | [diff] [blame] | 53 | : std::negation<std::is_convertible<T, std::underlying_type_t<T>>> {}; |
Jan Wilken Dörrie | f89fc2ac | 2021-03-05 06:51:22 | [diff] [blame] | 54 | |
| 55 | } // namespace internal |
| 56 | |
| 57 | // Implementation of C++23's std::is_scoped_enum |
| 58 | // |
| 59 | // Reference: https://en.cppreference.com/w/cpp/types/is_scoped_enum |
| 60 | template <typename T> |
| 61 | struct is_scoped_enum : internal::IsScopedEnumImpl<T> {}; |
| 62 | |
willchan@chromium.org | 951269b | 2010-06-15 19:39:24 | [diff] [blame] | 63 | } // namespace base |
| 64 | |
dpolukhin@chromium.org | 81e8ebf | 2010-09-16 07:59:27 | [diff] [blame] | 65 | #endif // BASE_TEMPLATE_UTIL_H_ |