blob: 30572f1635857bc9a9c21da19500ca3ef90283ae [file] [log] [blame]
[email protected]93540582011-05-16 22:35:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]951269b2010-06-15 19:39:242// 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_
[email protected]951269b2010-06-15 19:39:247
[email protected]b38d3572011-02-15 01:27:388#include <cstddef> // For size_t.
9
[email protected]792cdcf2010-12-15 09:55:3510#include "build/build_config.h"
11
[email protected]951269b2010-06-15 19:39:2412namespace 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
[email protected]6a341fb2011-06-26 16:22:5031template <class T, class U> struct is_same : public false_type {};
32template <class T> struct is_same<T,T> : true_type {};
33
[email protected]b38d3572011-02-15 01:27:3834template<class> struct is_array : public false_type {};
35template<class T, size_t n> struct is_array<T[n]> : public true_type {};
36template<class T> struct is_array<T[]> : public true_type {};
37
38template <class T> struct is_non_const_reference : false_type {};
39template <class T> struct is_non_const_reference<T&> : true_type {};
40template <class T> struct is_non_const_reference<const T&> : false_type {};
41
[email protected]dbea3882013-10-22 09:18:2042template <class T> struct is_const : false_type {};
43template <class T> struct is_const<const T> : true_type {};
44
[email protected]93540582011-05-16 22:35:1445template <class T> struct is_void : false_type {};
46template <> struct is_void<void> : true_type {};
47
[email protected]792cdcf2010-12-15 09:55:3548namespace internal {
49
[email protected]b38d3572011-02-15 01:27:3850// Types YesType and NoType are guaranteed such that sizeof(YesType) <
51// sizeof(NoType).
52typedef char YesType;
[email protected]792cdcf2010-12-15 09:55:3553
[email protected]b38d3572011-02-15 01:27:3854struct NoType {
55 YesType dummy[2];
[email protected]792cdcf2010-12-15 09:55:3556};
57
[email protected]792cdcf2010-12-15 09:55:3558// 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.
66
[email protected]792cdcf2010-12-15 09:55:3567struct ConvertHelper {
[email protected]ba019ac2011-02-16 22:37:5868 template <typename To>
[email protected]b38d3572011-02-15 01:27:3869 static YesType Test(To);
[email protected]ba019ac2011-02-16 22:37:5870
71 template <typename To>
[email protected]b38d3572011-02-15 01:27:3872 static NoType Test(...);
[email protected]ba019ac2011-02-16 22:37:5873
74 template <typename From>
[email protected]7296f2762011-11-21 19:23:4475 static From& Create();
[email protected]792cdcf2010-12-15 09:55:3576};
77
[email protected]b38d3572011-02-15 01:27:3878// Used to determine if a type is a struct/union/class. Inspired by Boost's
79// is_class type_trait implementation.
80struct IsClassHelper {
81 template <typename C>
82 static YesType Test(void(C::*)(void));
83
84 template <typename C>
85 static NoType Test(...);
86};
87
[email protected]792cdcf2010-12-15 09:55:3588} // namespace internal
89
[email protected]792cdcf2010-12-15 09:55:3590// Inherits from true_type if From is convertible to To, false_type otherwise.
[email protected]ba019ac2011-02-16 22:37:5891//
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.
[email protected]792cdcf2010-12-15 09:55:3594template <typename From, typename To>
95struct is_convertible
96 : integral_constant<bool,
[email protected]ba019ac2011-02-16 22:37:5897 sizeof(internal::ConvertHelper::Test<To>(
98 internal::ConvertHelper::Create<From>())) ==
99 sizeof(internal::YesType)> {
[email protected]792cdcf2010-12-15 09:55:35100};
101
[email protected]b38d3572011-02-15 01:27:38102template <typename T>
103struct is_class
104 : integral_constant<bool,
105 sizeof(internal::IsClassHelper::Test<T>(0)) ==
106 sizeof(internal::YesType)> {
107};
108
[email protected]dbea3882013-10-22 09:18:20109template<bool B, class T = void>
110struct enable_if {};
111
112template<class T>
113struct enable_if<true, T> { typedef T type; };
114
[email protected]951269b2010-06-15 19:39:24115} // namespace base
116
[email protected]81e8ebf2010-09-16 07:59:27117#endif // BASE_TEMPLATE_UTIL_H_