henrike@webrtc.org | f7795df | 2014-05-13 18:00:26 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 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 | |
| 11 | #ifndef WEBRTC_BASE_TEMPLATE_UTIL_H_ |
| 12 | #define WEBRTC_BASE_TEMPLATE_UTIL_H_ |
| 13 | |
| 14 | #include <stddef.h> // For size_t. |
| 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | // template definitions from tr1 |
| 19 | |
| 20 | template<class T, T v> |
| 21 | struct integral_constant { |
| 22 | static const T value = v; |
| 23 | typedef T value_type; |
| 24 | typedef integral_constant<T, v> type; |
| 25 | }; |
| 26 | |
| 27 | template <class T, T v> const T integral_constant<T, v>::value; |
| 28 | |
| 29 | typedef integral_constant<bool, true> true_type; |
| 30 | typedef integral_constant<bool, false> false_type; |
| 31 | |
| 32 | template <class T> struct is_pointer : false_type {}; |
| 33 | template <class T> struct is_pointer<T*> : true_type {}; |
| 34 | |
| 35 | template <class T, class U> struct is_same : public false_type {}; |
| 36 | template <class T> struct is_same<T,T> : true_type {}; |
| 37 | |
| 38 | template<class> struct is_array : public false_type {}; |
| 39 | template<class T, size_t n> struct is_array<T[n]> : public true_type {}; |
| 40 | template<class T> struct is_array<T[]> : public true_type {}; |
| 41 | |
| 42 | template <class T> struct is_non_const_reference : false_type {}; |
| 43 | template <class T> struct is_non_const_reference<T&> : true_type {}; |
| 44 | template <class T> struct is_non_const_reference<const T&> : false_type {}; |
| 45 | |
| 46 | template <class T> struct is_void : false_type {}; |
| 47 | template <> struct is_void<void> : true_type {}; |
| 48 | |
| 49 | namespace internal { |
| 50 | |
| 51 | // Types YesType and NoType are guaranteed such that sizeof(YesType) < |
| 52 | // sizeof(NoType). |
| 53 | typedef char YesType; |
| 54 | |
| 55 | struct NoType { |
| 56 | YesType dummy[2]; |
| 57 | }; |
| 58 | |
| 59 | // This class is an implementation detail for is_convertible, and you |
| 60 | // don't need to know how it works to use is_convertible. For those |
| 61 | // who care: we declare two different functions, one whose argument is |
| 62 | // of type To and one with a variadic argument list. We give them |
| 63 | // return types of different size, so we can use sizeof to trick the |
| 64 | // compiler into telling us which function it would have chosen if we |
| 65 | // had called it with an argument of type From. See Alexandrescu's |
| 66 | // _Modern C++ Design_ for more details on this sort of trick. |
| 67 | |
| 68 | struct ConvertHelper { |
| 69 | template <typename To> |
| 70 | static YesType Test(To); |
| 71 | |
| 72 | template <typename To> |
| 73 | static NoType Test(...); |
| 74 | |
| 75 | template <typename From> |
| 76 | static From& Create(); |
| 77 | }; |
| 78 | |
| 79 | // Used to determine if a type is a struct/union/class. Inspired by Boost's |
| 80 | // is_class type_trait implementation. |
| 81 | struct IsClassHelper { |
| 82 | template <typename C> |
| 83 | static YesType Test(void(C::*)(void)); |
| 84 | |
| 85 | template <typename C> |
| 86 | static NoType Test(...); |
| 87 | }; |
| 88 | |
| 89 | } // namespace internal |
| 90 | |
| 91 | // Inherits from true_type if From is convertible to To, false_type otherwise. |
| 92 | // |
| 93 | // Note that if the type is convertible, this will be a true_type REGARDLESS |
| 94 | // of whether or not the conversion would emit a warning. |
| 95 | template <typename From, typename To> |
| 96 | struct is_convertible |
| 97 | : integral_constant<bool, |
| 98 | sizeof(internal::ConvertHelper::Test<To>( |
| 99 | internal::ConvertHelper::Create<From>())) == |
| 100 | sizeof(internal::YesType)> { |
| 101 | }; |
| 102 | |
| 103 | template <typename T> |
| 104 | struct is_class |
| 105 | : integral_constant<bool, |
| 106 | sizeof(internal::IsClassHelper::Test<T>(0)) == |
| 107 | sizeof(internal::YesType)> { |
| 108 | }; |
| 109 | |
| 110 | } // namespace rtc |
| 111 | |
| 112 | #endif // WEBRTC_BASE_TEMPLATE_UTIL_H_ |