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