OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TEMPLATE_UTIL_H_ | 5 #ifndef BASE_TEMPLATE_UTIL_H_ |
6 #define BASE_TEMPLATE_UTIL_H_ | 6 #define BASE_TEMPLATE_UTIL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include <cstddef> // For size_t. |
| 10 |
9 #include "build/build_config.h" | 11 #include "build/build_config.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
12 | 14 |
13 // template definitions from tr1 | 15 // template definitions from tr1 |
14 | 16 |
15 template<class T, T v> | 17 template<class T, T v> |
16 struct integral_constant { | 18 struct integral_constant { |
17 static const T value = v; | 19 static const T value = v; |
18 typedef T value_type; | 20 typedef T value_type; |
19 typedef integral_constant<T, v> type; | 21 typedef integral_constant<T, v> type; |
20 }; | 22 }; |
21 | 23 |
22 template <class T, T v> const T integral_constant<T, v>::value; | 24 template <class T, T v> const T integral_constant<T, v>::value; |
23 | 25 |
24 typedef integral_constant<bool, true> true_type; | 26 typedef integral_constant<bool, true> true_type; |
25 typedef integral_constant<bool, false> false_type; | 27 typedef integral_constant<bool, false> false_type; |
26 | 28 |
27 template <class T> struct is_pointer : false_type {}; | 29 template <class T> struct is_pointer : false_type {}; |
28 template <class T> struct is_pointer<T*> : true_type {}; | 30 template <class T> struct is_pointer<T*> : true_type {}; |
29 | 31 |
| 32 template<class> struct is_array : public false_type {}; |
| 33 template<class T, size_t n> struct is_array<T[n]> : public true_type {}; |
| 34 template<class T> struct is_array<T[]> : public true_type {}; |
| 35 |
| 36 template <class T> struct is_non_const_reference : false_type {}; |
| 37 template <class T> struct is_non_const_reference<T&> : true_type {}; |
| 38 template <class T> struct is_non_const_reference<const T&> : false_type {}; |
| 39 |
30 namespace internal { | 40 namespace internal { |
31 | 41 |
32 // Types small_ and big_ are guaranteed such that sizeof(small_) < | 42 // Types YesType and NoType are guaranteed such that sizeof(YesType) < |
33 // sizeof(big_) | 43 // sizeof(NoType). |
34 typedef char small_; | 44 typedef char YesType; |
35 | 45 |
36 struct big_ { | 46 struct NoType { |
37 small_ dummy[2]; | 47 YesType dummy[2]; |
38 }; | 48 }; |
39 | 49 |
40 #if !defined(OS_WIN) | 50 #if !defined(OS_WIN) |
41 | 51 |
42 // This class is an implementation detail for is_convertible, and you | 52 // This class is an implementation detail for is_convertible, and you |
43 // don't need to know how it works to use is_convertible. For those | 53 // don't need to know how it works to use is_convertible. For those |
44 // who care: we declare two different functions, one whose argument is | 54 // who care: we declare two different functions, one whose argument is |
45 // of type To and one with a variadic argument list. We give them | 55 // of type To and one with a variadic argument list. We give them |
46 // return types of different size, so we can use sizeof to trick the | 56 // return types of different size, so we can use sizeof to trick the |
47 // compiler into telling us which function it would have chosen if we | 57 // compiler into telling us which function it would have chosen if we |
48 // had called it with an argument of type From. See Alexandrescu's | 58 // had called it with an argument of type From. See Alexandrescu's |
49 // _Modern C++ Design_ for more details on this sort of trick. | 59 // _Modern C++ Design_ for more details on this sort of trick. |
50 | 60 |
51 template <typename From, typename To> | 61 template <typename From, typename To> |
52 struct ConvertHelper { | 62 struct ConvertHelper { |
53 static small_ Test(To); | 63 static YesType Test(To); |
54 static big_ Test(...); | 64 static NoType Test(...); |
55 static From Create(); | 65 static From Create(); |
56 }; | 66 }; |
57 | 67 |
58 #endif // !defined(OS_WIN) | 68 #endif // !defined(OS_WIN) |
59 | 69 |
| 70 // Used to determine if a type is a struct/union/class. Inspired by Boost's |
| 71 // is_class type_trait implementation. |
| 72 struct IsClassHelper { |
| 73 template <typename C> |
| 74 static YesType Test(void(C::*)(void)); |
| 75 |
| 76 template <typename C> |
| 77 static NoType Test(...); |
| 78 }; |
| 79 |
60 } // namespace internal | 80 } // namespace internal |
61 | 81 |
62 #if !defined(OS_WIN) | 82 #if !defined(OS_WIN) |
63 | 83 |
64 // Inherits from true_type if From is convertible to To, false_type otherwise. | 84 // Inherits from true_type if From is convertible to To, false_type otherwise. |
65 template <typename From, typename To> | 85 template <typename From, typename To> |
66 struct is_convertible | 86 struct is_convertible |
67 : integral_constant<bool, | 87 : integral_constant<bool, |
68 sizeof(internal::ConvertHelper<From, To>::Test( | 88 sizeof(internal::ConvertHelper<From, To>::Test( |
69 internal::ConvertHelper<From, To>::Create())) | 89 internal::ConvertHelper<From, To>::Create())) |
70 == sizeof(internal::small_)> { | 90 == sizeof(internal::YesType)> { |
71 }; | 91 }; |
72 | 92 |
73 #endif // !defined(OS_WIN) | 93 #endif // !defined(OS_WIN) |
74 | 94 |
| 95 template <typename T> |
| 96 struct is_class |
| 97 : integral_constant<bool, |
| 98 sizeof(internal::IsClassHelper::Test<T>(0)) == |
| 99 sizeof(internal::YesType)> { |
| 100 }; |
| 101 |
75 } // namespace base | 102 } // namespace base |
76 | 103 |
77 #endif // BASE_TEMPLATE_UTIL_H_ | 104 #endif // BASE_TEMPLATE_UTIL_H_ |
OLD | NEW |