OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_ | 5 #ifndef PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_ |
6 #define PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_ | 6 #define PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_ |
7 | 7 |
8 #include <cstddef> // For size_t. | 8 #include <cstddef> // For size_t. |
9 | 9 |
10 namespace pdfium { | 10 namespace pdfium { |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 template<class T, T v> | 13 template<class T, T v> |
14 struct integral_constant { | 14 struct integral_constant { |
Nico
2015/12/22 23:29:42
this is in std too
| |
15 static const T value = v; | 15 static const T value = v; |
16 typedef T value_type; | 16 typedef T value_type; |
17 typedef integral_constant<T, v> type; | 17 typedef integral_constant<T, v> type; |
18 }; | 18 }; |
19 | 19 |
20 typedef integral_constant<bool, true> true_type; | 20 typedef integral_constant<bool, true> true_type; |
21 typedef integral_constant<bool, false> false_type; | 21 typedef integral_constant<bool, false> false_type; |
22 | 22 |
23 template <class T, class U> struct is_same : public false_type {}; | 23 template <class T, class U> struct is_same : public false_type {}; |
24 template <class T> | 24 template <class T> |
25 struct is_same<T, T> : true_type {}; | 25 struct is_same<T, T> : true_type {}; |
Nico
2015/12/22 23:29:42
this is in std too
| |
26 | 26 |
27 template<bool B, class T = void> | 27 template<bool B, class T = void> |
28 struct enable_if {}; | 28 struct enable_if {}; |
29 | 29 |
30 template<class T> | 30 template<class T> |
31 struct enable_if<true, T> { typedef T type; }; | 31 struct enable_if<true, T> { typedef T type; }; |
Nico
2015/12/22 23:29:42
this is in std too
| |
32 | 32 |
33 namespace internal { | |
34 | |
35 // Types YesType and NoType are guaranteed such that sizeof(YesType) < | |
36 // sizeof(NoType). | |
37 typedef char YesType; | |
38 | |
39 struct NoType { | |
40 YesType dummy[2]; | |
41 }; | |
42 | |
43 // This class is an implementation detail for is_convertible, and you | |
44 // don't need to know how it works to use is_convertible. For those | |
45 // who care: we declare two different functions, one whose argument is | |
46 // of type To and one with a variadic argument list. We give them | |
47 // return types of different size, so we can use sizeof to trick the | |
48 // compiler into telling us which function it would have chosen if we | |
49 // had called it with an argument of type From. See Alexandrescu's | |
50 // _Modern C++ Design_ for more details on this sort of trick. | |
51 | |
52 struct ConvertHelper { | |
53 template <typename To> | |
54 static YesType Test(To); | |
55 | |
56 template <typename To> | |
57 static NoType Test(...); | |
58 | |
59 template <typename From> | |
60 static From& Create(); | |
61 }; | |
62 | |
63 } // namespace internal | |
64 | |
65 // Inherits from true_type if From is convertible to To, false_type otherwise. | |
66 // | |
67 // Note that if the type is convertible, this will be a true_type REGARDLESS | |
68 // of whether or not the conversion would emit a warning. | |
69 template <typename From, typename To> | |
70 struct is_convertible | |
71 : integral_constant<bool, | |
72 sizeof(internal::ConvertHelper::Test<To>( | |
73 internal::ConvertHelper::Create<From>())) == | |
74 sizeof(internal::YesType)> {}; | |
75 | |
76 } // namespace base | 33 } // namespace base |
77 } // namespace pdfium | 34 } // namespace pdfium |
78 | 35 |
79 #endif // PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_ | 36 #endif // PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_ |
OLD | NEW |