template_util.h revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 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.
4
5#ifndef BASE_TEMPLATE_UTIL_H_
6#define BASE_TEMPLATE_UTIL_H_
7#pragma once
8
9#include <cstddef>  // For size_t.
10
11#include "build/build_config.h"
12
13namespace base {
14
15// template definitions from tr1
16
17template<class T, T v>
18struct integral_constant {
19  static const T value = v;
20  typedef T value_type;
21  typedef integral_constant<T, v> type;
22};
23
24template <class T, T v> const T integral_constant<T, v>::value;
25
26typedef integral_constant<bool, true> true_type;
27typedef integral_constant<bool, false> false_type;
28
29template <class T> struct is_pointer : false_type {};
30template <class T> struct is_pointer<T*> : true_type {};
31
32template<class> struct is_array : public false_type {};
33template<class T, size_t n> struct is_array<T[n]> : public true_type {};
34template<class T> struct is_array<T[]> : public true_type {};
35
36template <class T> struct is_non_const_reference : false_type {};
37template <class T> struct is_non_const_reference<T&> : true_type {};
38template <class T> struct is_non_const_reference<const T&> : false_type {};
39
40namespace internal {
41
42// Types YesType and NoType are guaranteed such that sizeof(YesType) <
43// sizeof(NoType).
44typedef char YesType;
45
46struct NoType {
47  YesType dummy[2];
48};
49
50// This class is an implementation detail for is_convertible, and you
51// don't need to know how it works to use is_convertible. For those
52// who care: we declare two different functions, one whose argument is
53// of type To and one with a variadic argument list. We give them
54// return types of different size, so we can use sizeof to trick the
55// compiler into telling us which function it would have chosen if we
56// had called it with an argument of type From.  See Alexandrescu's
57// _Modern C++ Design_ for more details on this sort of trick.
58
59struct ConvertHelper {
60  template <typename To>
61  static YesType Test(To);
62
63  template <typename To>
64  static NoType Test(...);
65
66  template <typename From>
67  static From Create();
68};
69
70// Used to determine if a type is a struct/union/class. Inspired by Boost's
71// is_class type_trait implementation.
72struct IsClassHelper {
73  template <typename C>
74  static YesType Test(void(C::*)(void));
75
76  template <typename C>
77  static NoType Test(...);
78};
79
80}  // namespace internal
81
82// Inherits from true_type if From is convertible to To, false_type otherwise.
83//
84// Note that if the type is convertible, this will be a true_type REGARDLESS
85// of whether or not the conversion would emit a warning.
86template <typename From, typename To>
87struct is_convertible
88    : integral_constant<bool,
89                        sizeof(internal::ConvertHelper::Test<To>(
90                                   internal::ConvertHelper::Create<From>())) ==
91                        sizeof(internal::YesType)> {
92};
93
94template <typename T>
95struct is_class
96    : integral_constant<bool,
97                        sizeof(internal::IsClassHelper::Test<T>(0)) ==
98                            sizeof(internal::YesType)> {
99};
100
101}  // namespace base
102
103#endif  // BASE_TEMPLATE_UTIL_H_
104