Home | History | Annotate | Download | only in base
      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 
     10 namespace i18n {
     11 namespace phonenumbers {
     12 
     13 // template definitions from tr1
     14 
     15 template<class T, T v>
     16 struct integral_constant {
     17   static const T value = v;
     18   typedef T value_type;
     19   typedef integral_constant<T, v> type;
     20 };
     21 
     22 template <class T, T v> const T integral_constant<T, v>::value;
     23 
     24 typedef integral_constant<bool, true> true_type;
     25 typedef integral_constant<bool, false> false_type;
     26 
     27 template <class T> struct is_pointer : false_type {};
     28 template <class T> struct is_pointer<T*> : true_type {};
     29 
     30 template <class T, class U> struct is_same : public false_type {};
     31 template <class T> struct is_same<T,T> : true_type {};
     32 
     33 template<class> struct is_array : public false_type {};
     34 template<class T, size_t n> struct is_array<T[n]> : public true_type {};
     35 template<class T> struct is_array<T[]> : public true_type {};
     36 
     37 template <class T> struct is_non_const_reference : false_type {};
     38 template <class T> struct is_non_const_reference<T&> : true_type {};
     39 template <class T> struct is_non_const_reference<const T&> : false_type {};
     40 
     41 template <class T> struct is_void : false_type {};
     42 template <> struct is_void<void> : true_type {};
     43 
     44 namespace internal {
     45 
     46 // Types YesType and NoType are guaranteed such that sizeof(YesType) <
     47 // sizeof(NoType).
     48 typedef char YesType;
     49 
     50 struct 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 
     63 struct 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.
     76 struct 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.
     90 template <typename From, typename To>
     91 struct is_convertible
     92     : integral_constant<bool,
     93                         sizeof(internal::ConvertHelper::Test<To>(
     94                                    internal::ConvertHelper::Create<From>())) ==
     95                         sizeof(internal::YesType)> {
     96 };
     97 
     98 template <typename T>
     99 struct 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