blob: 83fa322a67226d512b6a6b3974b76637ee2ffbc7 [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
aa3a835f52014-09-26 08:06:29 +090031// Member function pointer detection. This is built-in to C++ 11's stdlib, and
32// we can remove this when we switch to it.
aa@chromium.orgd05701e2013-12-06 15:55:25 +090033template<typename T>
34struct is_member_function_pointer : false_type {};
35
aa3a835f52014-09-26 08:06:29 +090036template <typename R, typename Z, typename... A>
37struct is_member_function_pointer<R(Z::*)(A...)> : true_type {};
38template <typename R, typename Z, typename... A>
39struct is_member_function_pointer<R(Z::*)(A...) const> : true_type {};
aa@chromium.orgd05701e2013-12-06 15:55:25 +090040
41
ajwong@chromium.orgec1750a2011-06-27 01:22:50 +090042template <class T, class U> struct is_same : public false_type {};
43template <class T> struct is_same<T,T> : true_type {};
44
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090045template<class> struct is_array : public false_type {};
46template<class T, size_t n> struct is_array<T[n]> : public true_type {};
47template<class T> struct is_array<T[]> : public true_type {};
48
49template <class T> struct is_non_const_reference : false_type {};
50template <class T> struct is_non_const_reference<T&> : true_type {};
51template <class T> struct is_non_const_reference<const T&> : false_type {};
52
kinuko@chromium.org288f27a2013-10-22 18:18:20 +090053template <class T> struct is_const : false_type {};
54template <class T> struct is_const<const T> : true_type {};
55
ajwong@chromium.orgc711b822011-05-17 07:35:14 +090056template <class T> struct is_void : false_type {};
57template <> struct is_void<void> : true_type {};
58
willchan@chromium.org97d688c2010-12-15 18:55:35 +090059namespace internal {
60
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090061// Types YesType and NoType are guaranteed such that sizeof(YesType) <
62// sizeof(NoType).
63typedef char YesType;
willchan@chromium.org97d688c2010-12-15 18:55:35 +090064
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090065struct NoType {
66 YesType dummy[2];
willchan@chromium.org97d688c2010-12-15 18:55:35 +090067};
68
willchan@chromium.org97d688c2010-12-15 18:55:35 +090069// This class is an implementation detail for is_convertible, and you
70// don't need to know how it works to use is_convertible. For those
71// who care: we declare two different functions, one whose argument is
72// of type To and one with a variadic argument list. We give them
73// return types of different size, so we can use sizeof to trick the
74// compiler into telling us which function it would have chosen if we
75// had called it with an argument of type From. See Alexandrescu's
76// _Modern C++ Design_ for more details on this sort of trick.
77
willchan@chromium.org97d688c2010-12-15 18:55:35 +090078struct ConvertHelper {
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +090079 template <typename To>
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090080 static YesType Test(To);
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +090081
82 template <typename To>
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090083 static NoType Test(...);
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +090084
85 template <typename From>
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090086 static From& Create();
willchan@chromium.org97d688c2010-12-15 18:55:35 +090087};
88
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090089// Used to determine if a type is a struct/union/class. Inspired by Boost's
90// is_class type_trait implementation.
91struct IsClassHelper {
92 template <typename C>
93 static YesType Test(void(C::*)(void));
94
95 template <typename C>
96 static NoType Test(...);
97};
98
willchan@chromium.org97d688c2010-12-15 18:55:35 +090099} // namespace internal
100
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900101// Inherits from true_type if From is convertible to To, false_type otherwise.
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +0900102//
103// Note that if the type is convertible, this will be a true_type REGARDLESS
104// of whether or not the conversion would emit a warning.
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900105template <typename From, typename To>
106struct is_convertible
107 : integral_constant<bool,
ajwong@chromium.orgd5f61112011-02-17 07:37:58 +0900108 sizeof(internal::ConvertHelper::Test<To>(
109 internal::ConvertHelper::Create<From>())) ==
110 sizeof(internal::YesType)> {
willchan@chromium.org97d688c2010-12-15 18:55:35 +0900111};
112
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900113template <typename T>
114struct is_class
115 : integral_constant<bool,
116 sizeof(internal::IsClassHelper::Test<T>(0)) ==
117 sizeof(internal::YesType)> {
118};
119
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900120template<bool B, class T = void>
121struct enable_if {};
122
123template<class T>
124struct enable_if<true, T> { typedef T type; };
125
willchan@chromium.orgb9b021a2010-06-16 04:39:24 +0900126} // namespace base
127
dpolukhin@chromium.org21b228d2010-09-16 16:59:27 +0900128#endif // BASE_TEMPLATE_UTIL_H_