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