blob: f4bf7461327bcb076bbe4bbf30bdd405f9bf3234 [file] [log] [blame]
ajwong@chromium.orgc711b822011-05-17 07:35:14 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
willchan@chromium.orgb9b021a2010-06-16 04:39:24 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_TEMPLATE_UTIL_H_
6#define BASE_TEMPLATE_UTIL_H_
willchan@chromium.orgb9b021a2010-06-16 04:39:24 +09007
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09008#include <cstddef> // For size_t.
9
willchan@chromium.org97d688c2010-12-15 18:55:35 +090010#include "build/build_config.h"
11
willchan@chromium.orgb9b021a2010-06-16 04:39:24 +090012namespace base {
13
14// template definitions from tr1
15
16template<class T, T v>
17struct integral_constant {
18 static const T value = v;
19 typedef T value_type;
20 typedef integral_constant<T, v> type;
21};
22
23template <class T, T v> const T integral_constant<T, v>::value;
24
25typedef integral_constant<bool, true> true_type;
26typedef integral_constant<bool, false> false_type;
27
28template <class T> struct is_pointer : false_type {};
29template <class T> struct is_pointer<T*> : true_type {};
30
aa@chromium.orgd05701e2013-12-06 15:55:25 +090031// Member function pointer detection up to four params. Add more as needed
32// below. This is built-in to C++ 11, and we can remove this when we switch.
33template<typename T>
34struct is_member_function_pointer : false_type {};
35
36template <typename R, typename Z>
37struct is_member_function_pointer<R(Z::*)()> : true_type {};
38template <typename R, typename Z>
39struct is_member_function_pointer<R(Z::*)() const> : true_type {};
40
41template <typename R, typename Z, typename A>
42struct is_member_function_pointer<R(Z::*)(A)> : true_type {};
43template <typename R, typename Z, typename A>
44struct is_member_function_pointer<R(Z::*)(A) const> : true_type {};
45
46template <typename R, typename Z, typename A, typename B>
47struct is_member_function_pointer<R(Z::*)(A, B)> : true_type {};
48template <typename R, typename Z, typename A, typename B>
49struct is_member_function_pointer<R(Z::*)(A, B) const> : true_type {};
50
51template <typename R, typename Z, typename A, typename B, typename C>
52struct is_member_function_pointer<R(Z::*)(A, B, C)> : true_type {};
53template <typename R, typename Z, typename A, typename B, typename C>
54struct is_member_function_pointer<R(Z::*)(A, B, C) const> : true_type {};
55
56template <typename R, typename Z, typename A, typename B, typename C,
57 typename D>
58struct is_member_function_pointer<R(Z::*)(A, B, C, D)> : true_type {};
59template <typename R, typename Z, typename A, typename B, typename C,
60 typename D>
61struct is_member_function_pointer<R(Z::*)(A, B, C, D) const> : true_type {};
62
63
ajwong@chromium.orgec1750a2011-06-27 01:22:50 +090064template <class T, class U> struct is_same : public false_type {};
65template <class T> struct is_same<T,T> : true_type {};
66
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090067template<class> struct is_array : public false_type {};
68template<class T, size_t n> struct is_array<T[n]> : public true_type {};
69template<class T> struct is_array<T[]> : public true_type {};
70
71template <class T> struct is_non_const_reference : false_type {};
72template <class T> struct is_non_const_reference<T&> : true_type {};
73template <class T> struct is_non_const_reference<const T&> : false_type {};
74
kinuko@chromium.org288f27a2013-10-22 18:18:20 +090075template <class T> struct is_const : false_type {};
76template <class T> struct is_const<const T> : true_type {};
77
ajwong@chromium.orgc711b822011-05-17 07:35:14 +090078template <class T> struct is_void : false_type {};
79template <> struct is_void<void> : true_type {};
80
willchan@chromium.org97d688c2010-12-15 18:55:35 +090081namespace internal {
82
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090083// Types YesType and NoType are guaranteed such that sizeof(YesType) <
84// sizeof(NoType).
85typedef char YesType;
willchan@chromium.org97d688c2010-12-15 18:55:35 +090086
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090087struct NoType {
88 YesType dummy[2];
willchan@chromium.org97d688c2010-12-15 18:55:35 +090089};
90
willchan@chromium.org97d688c2010-12-15 18:55:35 +090091// This class is an implementation detail for is_convertible, and you
92// don't need to know how it works to use is_convertible. For those
93// who care: we declare two different functions, one whose argument is
94// of type To and one with a variadic argument list. We give them
95// return types of different size, so we can use sizeof to trick the
96// compiler into telling us which function it would have chosen if we
97// had called it with an argument of type From. See Alexandrescu's
98// _Modern C++ Design_ for more details on this sort of trick.
99
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900100struct ConvertHelper {
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +0900101 template <typename To>
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900102 static YesType Test(To);
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +0900103
104 template <typename To>
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900105 static NoType Test(...);
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +0900106
107 template <typename From>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900108 static From& Create();
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900109};
110
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900111// Used to determine if a type is a struct/union/class. Inspired by Boost's
112// is_class type_trait implementation.
113struct IsClassHelper {
114 template <typename C>
115 static YesType Test(void(C::*)(void));
116
117 template <typename C>
118 static NoType Test(...);
119};
120
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900121} // namespace internal
122
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900123// Inherits from true_type if From is convertible to To, false_type otherwise.
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +0900124//
125// Note that if the type is convertible, this will be a true_type REGARDLESS
126// of whether or not the conversion would emit a warning.
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900127template <typename From, typename To>
128struct is_convertible
129 : integral_constant<bool,
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +0900130 sizeof(internal::ConvertHelper::Test<To>(
131 internal::ConvertHelper::Create<From>())) ==
132 sizeof(internal::YesType)> {
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900133};
134
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900135template <typename T>
136struct is_class
137 : integral_constant<bool,
138 sizeof(internal::IsClassHelper::Test<T>(0)) ==
139 sizeof(internal::YesType)> {
140};
141
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900142template<bool B, class T = void>
143struct enable_if {};
144
145template<class T>
146struct enable_if<true, T> { typedef T type; };
147
willchan@chromium.orgb9b021a2010-06-16 04:39:24 +0900148} // namespace base
149
dpolukhin@chromium.org21b228d2010-09-16 16:59:27 +0900150#endif // BASE_TEMPLATE_UTIL_H_