Simple Cache: Enable by default on Android developer builds
[chromium-blink-merge.git] / base / template_util.h
blob30572f1635857bc9a9c21da19500ca3ef90283ae
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
3 // found in the LICENSE file.
5 #ifndef BASE_TEMPLATE_UTIL_H_
6 #define BASE_TEMPLATE_UTIL_H_
8 #include <cstddef> // For size_t.
10 #include "build/build_config.h"
12 namespace base {
14 // template definitions from tr1
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;
23 template <class T, T v> const T integral_constant<T, v>::value;
25 typedef integral_constant<bool, true> true_type;
26 typedef integral_constant<bool, false> false_type;
28 template <class T> struct is_pointer : false_type {};
29 template <class T> struct is_pointer<T*> : true_type {};
31 template <class T, class U> struct is_same : public false_type {};
32 template <class T> struct is_same<T,T> : true_type {};
34 template<class> struct is_array : public false_type {};
35 template<class T, size_t n> struct is_array<T[n]> : public true_type {};
36 template<class T> struct is_array<T[]> : public true_type {};
38 template <class T> struct is_non_const_reference : false_type {};
39 template <class T> struct is_non_const_reference<T&> : true_type {};
40 template <class T> struct is_non_const_reference<const T&> : false_type {};
42 template <class T> struct is_const : false_type {};
43 template <class T> struct is_const<const T> : true_type {};
45 template <class T> struct is_void : false_type {};
46 template <> struct is_void<void> : true_type {};
48 namespace internal {
50 // Types YesType and NoType are guaranteed such that sizeof(YesType) <
51 // sizeof(NoType).
52 typedef char YesType;
54 struct NoType {
55 YesType dummy[2];
58 // This class is an implementation detail for is_convertible, and you
59 // don't need to know how it works to use is_convertible. For those
60 // who care: we declare two different functions, one whose argument is
61 // of type To and one with a variadic argument list. We give them
62 // return types of different size, so we can use sizeof to trick the
63 // compiler into telling us which function it would have chosen if we
64 // had called it with an argument of type From. See Alexandrescu's
65 // _Modern C++ Design_ for more details on this sort of trick.
67 struct ConvertHelper {
68 template <typename To>
69 static YesType Test(To);
71 template <typename To>
72 static NoType Test(...);
74 template <typename From>
75 static From& Create();
78 // Used to determine if a type is a struct/union/class. Inspired by Boost's
79 // is_class type_trait implementation.
80 struct IsClassHelper {
81 template <typename C>
82 static YesType Test(void(C::*)(void));
84 template <typename C>
85 static NoType Test(...);
88 } // namespace internal
90 // Inherits from true_type if From is convertible to To, false_type otherwise.
92 // Note that if the type is convertible, this will be a true_type REGARDLESS
93 // of whether or not the conversion would emit a warning.
94 template <typename From, typename To>
95 struct is_convertible
96 : integral_constant<bool,
97 sizeof(internal::ConvertHelper::Test<To>(
98 internal::ConvertHelper::Create<From>())) ==
99 sizeof(internal::YesType)> {
102 template <typename T>
103 struct is_class
104 : integral_constant<bool,
105 sizeof(internal::IsClassHelper::Test<T>(0)) ==
106 sizeof(internal::YesType)> {
109 template<bool B, class T = void>
110 struct enable_if {};
112 template<class T>
113 struct enable_if<true, T> { typedef T type; };
115 } // namespace base
117 #endif // BASE_TEMPLATE_UTIL_H_