henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
kwiberg@webrtc.org | af9d56f | 2015-01-13 20:32:04 | [diff] [blame] | 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
kwiberg@webrtc.org | af9d56f | 2015-01-13 20:32:04 | [diff] [blame] | 11 | // Borrowed from Chromium's src/base/template_util.h. |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #ifndef RTC_BASE_TEMPLATE_UTIL_H_ |
| 14 | #define RTC_BASE_TEMPLATE_UTIL_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #include <stddef.h> // For size_t. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 18 | namespace rtc { |
| 19 | |
| 20 | // Template definitions from tr1. |
| 21 | |
| 22 | template<class T, T v> |
| 23 | struct integral_constant { |
| 24 | static const T value = v; |
| 25 | typedef T value_type; |
| 26 | typedef integral_constant<T, v> type; |
| 27 | }; |
| 28 | |
| 29 | template <class T, T v> const T integral_constant<T, v>::value; |
| 30 | |
| 31 | typedef integral_constant<bool, true> true_type; |
| 32 | typedef integral_constant<bool, false> false_type; |
| 33 | |
| 34 | template <class T> struct is_pointer : false_type {}; |
| 35 | template <class T> struct is_pointer<T*> : true_type {}; |
| 36 | |
| 37 | template <class T, class U> struct is_same : public false_type {}; |
| 38 | template <class T> struct is_same<T, T> : true_type {}; |
| 39 | |
| 40 | template<class> struct is_array : public false_type {}; |
| 41 | template<class T, size_t n> struct is_array<T[n]> : public true_type {}; |
| 42 | template<class T> struct is_array<T[]> : public true_type {}; |
| 43 | |
| 44 | template <class T> struct is_non_const_reference : false_type {}; |
| 45 | template <class T> struct is_non_const_reference<T&> : true_type {}; |
| 46 | template <class T> struct is_non_const_reference<const T&> : false_type {}; |
| 47 | |
| 48 | template <class T> struct is_void : false_type {}; |
| 49 | template <> struct is_void<void> : true_type {}; |
| 50 | |
| 51 | // Helper useful for converting a tuple to variadic template function |
| 52 | // arguments. |
| 53 | // |
| 54 | // sequence_generator<3>::type will be sequence<0, 1, 2>. |
| 55 | template <int...> |
| 56 | struct sequence {}; |
| 57 | template <int N, int... S> |
| 58 | struct sequence_generator : sequence_generator<N - 1, N - 1, S...> {}; |
| 59 | template <int... S> |
| 60 | struct sequence_generator<0, S...> { |
| 61 | typedef sequence<S...> type; |
| 62 | }; |
| 63 | |
| 64 | namespace internal { |
| 65 | |
| 66 | // Types YesType and NoType are guaranteed such that sizeof(YesType) < |
| 67 | // sizeof(NoType). |
| 68 | typedef char YesType; |
| 69 | |
| 70 | struct NoType { |
| 71 | YesType dummy[2]; |
| 72 | }; |
| 73 | |
| 74 | // This class is an implementation detail for is_convertible, and you |
| 75 | // don't need to know how it works to use is_convertible. For those |
| 76 | // who care: we declare two different functions, one whose argument is |
| 77 | // of type To and one with a variadic argument list. We give them |
| 78 | // return types of different size, so we can use sizeof to trick the |
| 79 | // compiler into telling us which function it would have chosen if we |
| 80 | // had called it with an argument of type From. See Alexandrescu's |
| 81 | // _Modern C++ Design_ for more details on this sort of trick. |
| 82 | |
| 83 | struct ConvertHelper { |
| 84 | template <typename To> |
| 85 | static YesType Test(To); |
| 86 | |
| 87 | template <typename To> |
| 88 | static NoType Test(...); |
| 89 | |
| 90 | template <typename From> |
| 91 | static From& Create(); |
| 92 | }; |
| 93 | |
| 94 | // Used to determine if a type is a struct/union/class. Inspired by Boost's |
| 95 | // is_class type_trait implementation. |
| 96 | struct IsClassHelper { |
| 97 | template <typename C> |
| 98 | static YesType Test(void(C::*)(void)); |
| 99 | |
| 100 | template <typename C> |
| 101 | static NoType Test(...); |
| 102 | }; |
| 103 | |
| 104 | } // namespace internal |
| 105 | |
| 106 | // Inherits from true_type if From is convertible to To, false_type otherwise. |
| 107 | // |
| 108 | // Note that if the type is convertible, this will be a true_type REGARDLESS |
| 109 | // of whether or not the conversion would emit a warning. |
| 110 | template <typename From, typename To> |
| 111 | struct is_convertible |
| 112 | : integral_constant<bool, |
| 113 | sizeof(internal::ConvertHelper::Test<To>( |
| 114 | internal::ConvertHelper::Create<From>())) == |
| 115 | sizeof(internal::YesType)> { |
| 116 | }; |
| 117 | |
| 118 | template <typename T> |
| 119 | struct is_class |
| 120 | : integral_constant<bool, |
| 121 | sizeof(internal::IsClassHelper::Test<T>(0)) == |
| 122 | sizeof(internal::YesType)> { |
| 123 | }; |
| 124 | |
| 125 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 126 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 127 | #endif // RTC_BASE_TEMPLATE_UTIL_H_ |