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