[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 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_ |
[email protected] | 951269b | 2010-06-15 19:39:24 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 9 | |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 10 | #include "build/build_config.h" |
| 11 | |
[email protected] | 951269b | 2010-06-15 19:39:24 | [diff] [blame] | 12 | namespace base { |
| 13 | |
| 14 | // template definitions from tr1 |
| 15 | |
| 16 | template<class T, T v> |
| 17 | struct integral_constant { |
| 18 | static const T value = v; |
| 19 | typedef T value_type; |
| 20 | typedef integral_constant<T, v> type; |
| 21 | }; |
| 22 | |
| 23 | template <class T, T v> const T integral_constant<T, v>::value; |
| 24 | |
| 25 | typedef integral_constant<bool, true> true_type; |
| 26 | typedef integral_constant<bool, false> false_type; |
| 27 | |
| 28 | template <class T> struct is_pointer : false_type {}; |
| 29 | template <class T> struct is_pointer<T*> : true_type {}; |
| 30 | |
aa | 489d671 | 2014-09-25 23:06:29 | [diff] [blame] | 31 | // Member function pointer detection. This is built-in to C++ 11's stdlib, and |
| 32 | // we can remove this when we switch to it. |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 33 | template<typename T> |
| 34 | struct is_member_function_pointer : false_type {}; |
| 35 | |
aa | 489d671 | 2014-09-25 23:06:29 | [diff] [blame] | 36 | template <typename R, typename Z, typename... A> |
| 37 | struct is_member_function_pointer<R(Z::*)(A...)> : true_type {}; |
| 38 | template <typename R, typename Z, typename... A> |
| 39 | struct is_member_function_pointer<R(Z::*)(A...) const> : true_type {}; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 40 | |
| 41 | |
[email protected] | 6a341fb | 2011-06-26 16:22:50 | [diff] [blame] | 42 | template <class T, class U> struct is_same : public false_type {}; |
| 43 | template <class T> struct is_same<T,T> : true_type {}; |
| 44 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 45 | template<class> struct is_array : public false_type {}; |
| 46 | template<class T, size_t n> struct is_array<T[n]> : public true_type {}; |
| 47 | template<class T> struct is_array<T[]> : public true_type {}; |
| 48 | |
| 49 | template <class T> struct is_non_const_reference : false_type {}; |
| 50 | template <class T> struct is_non_const_reference<T&> : true_type {}; |
| 51 | template <class T> struct is_non_const_reference<const T&> : false_type {}; |
| 52 | |
[email protected] | dbea388 | 2013-10-22 09:18:20 | [diff] [blame] | 53 | template <class T> struct is_const : false_type {}; |
| 54 | template <class T> struct is_const<const T> : true_type {}; |
| 55 | |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 56 | template <class T> struct is_void : false_type {}; |
| 57 | template <> struct is_void<void> : true_type {}; |
| 58 | |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 59 | namespace internal { |
| 60 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 61 | // Types YesType and NoType are guaranteed such that sizeof(YesType) < |
| 62 | // sizeof(NoType). |
| 63 | typedef char YesType; |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 64 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 65 | struct NoType { |
| 66 | YesType dummy[2]; |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 67 | }; |
| 68 | |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 69 | // This class is an implementation detail for is_convertible, and you |
| 70 | // don't need to know how it works to use is_convertible. For those |
| 71 | // who care: we declare two different functions, one whose argument is |
| 72 | // of type To and one with a variadic argument list. We give them |
| 73 | // return types of different size, so we can use sizeof to trick the |
| 74 | // compiler into telling us which function it would have chosen if we |
| 75 | // had called it with an argument of type From. See Alexandrescu's |
| 76 | // _Modern C++ Design_ for more details on this sort of trick. |
| 77 | |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 78 | struct ConvertHelper { |
[email protected] | ba019ac | 2011-02-16 22:37:58 | [diff] [blame] | 79 | template <typename To> |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 80 | static YesType Test(To); |
[email protected] | ba019ac | 2011-02-16 22:37:58 | [diff] [blame] | 81 | |
| 82 | template <typename To> |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 83 | static NoType Test(...); |
[email protected] | ba019ac | 2011-02-16 22:37:58 | [diff] [blame] | 84 | |
| 85 | template <typename From> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 86 | static From& Create(); |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 87 | }; |
| 88 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 89 | // Used to determine if a type is a struct/union/class. Inspired by Boost's |
| 90 | // is_class type_trait implementation. |
| 91 | struct IsClassHelper { |
| 92 | template <typename C> |
| 93 | static YesType Test(void(C::*)(void)); |
| 94 | |
| 95 | template <typename C> |
| 96 | static NoType Test(...); |
| 97 | }; |
| 98 | |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 99 | } // namespace internal |
| 100 | |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 101 | // Inherits from true_type if From is convertible to To, false_type otherwise. |
[email protected] | ba019ac | 2011-02-16 22:37:58 | [diff] [blame] | 102 | // |
| 103 | // Note that if the type is convertible, this will be a true_type REGARDLESS |
| 104 | // of whether or not the conversion would emit a warning. |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 105 | template <typename From, typename To> |
| 106 | struct is_convertible |
| 107 | : integral_constant<bool, |
[email protected] | ba019ac | 2011-02-16 22:37:58 | [diff] [blame] | 108 | sizeof(internal::ConvertHelper::Test<To>( |
| 109 | internal::ConvertHelper::Create<From>())) == |
| 110 | sizeof(internal::YesType)> { |
[email protected] | 792cdcf | 2010-12-15 09:55:35 | [diff] [blame] | 111 | }; |
| 112 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 113 | template <typename T> |
| 114 | struct is_class |
| 115 | : integral_constant<bool, |
| 116 | sizeof(internal::IsClassHelper::Test<T>(0)) == |
| 117 | sizeof(internal::YesType)> { |
| 118 | }; |
| 119 | |
[email protected] | 951269b | 2010-06-15 19:39:24 | [diff] [blame] | 120 | } // namespace base |
| 121 | |
[email protected] | 81e8ebf | 2010-09-16 07:59:27 | [diff] [blame] | 122 | #endif // BASE_TEMPLATE_UTIL_H_ |