template_util.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Use of this source code is governed by a BSD-style license that can be 3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// found in the LICENSE file. 4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef BASE_TEMPLATE_UTIL_H_ 6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define BASE_TEMPLATE_UTIL_H_ 7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <cstddef> // For size_t. 9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "build/build_config.h" 11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace base { 13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// template definitions from tr1 15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<class T, T v> 17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct integral_constant { 18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static const T value = v; 19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org typedef T value_type; 20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org typedef integral_constant<T, v> type; 21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T, T v> const T integral_constant<T, v>::value; 24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtypedef integral_constant<bool, true> true_type; 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtypedef integral_constant<bool, false> false_type; 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_pointer : false_type {}; 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_pointer<T*> : true_type {}; 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Member function pointer detection. This is built-in to C++ 11's stdlib, and 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// we can remove this when we switch to it. 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<typename T> 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct is_member_function_pointer : false_type {}; 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename R, typename Z, typename... A> 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct is_member_function_pointer<R(Z::*)(A...)> : true_type {}; 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename R, typename Z, typename... A> 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct is_member_function_pointer<R(Z::*)(A...) const> : true_type {}; 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T, class U> struct is_same : public false_type {}; 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_same<T,T> : true_type {}; 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<class> struct is_array : public false_type {}; 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<class T, size_t n> struct is_array<T[n]> : public true_type {}; 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<class T> struct is_array<T[]> : public true_type {}; 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_non_const_reference : false_type {}; 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_non_const_reference<T&> : true_type {}; 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_non_const_reference<const T&> : false_type {}; 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_const : false_type {}; 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_const<const T> : true_type {}; 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T> struct is_void : false_type {}; 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <> struct is_void<void> : true_type {}; 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Types YesType and NoType are guaranteed such that sizeof(YesType) < 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// sizeof(NoType). 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtypedef char YesType; 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct NoType { 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org YesType dummy[2]; 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This class is an implementation detail for is_convertible, and you 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// don't need to know how it works to use is_convertible. For those 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// who care: we declare two different functions, one whose argument is 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// of type To and one with a variadic argument list. We give them 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// return types of different size, so we can use sizeof to trick the 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// compiler into telling us which function it would have chosen if we 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// had called it with an argument of type From. See Alexandrescu's 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// _Modern C++ Design_ for more details on this sort of trick. 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct ConvertHelper { 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org template <typename To> 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static YesType Test(To); 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org template <typename To> 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static NoType Test(...); 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org template <typename From> 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static From& Create(); 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Used to determine if a type is a struct/union/class. Inspired by Boost's 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// is_class type_trait implementation. 91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct IsClassHelper { 92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org template <typename C> 93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static YesType Test(void(C::*)(void)); 94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org template <typename C> 96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static NoType Test(...); 97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Inherits from true_type if From is convertible to To, false_type otherwise. 102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Note that if the type is convertible, this will be a true_type REGARDLESS 104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// of whether or not the conversion would emit a warning. 105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename From, typename To> 106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct is_convertible 107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : integral_constant<bool, 108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sizeof(internal::ConvertHelper::Test<To>( 109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::ConvertHelper::Create<From>())) == 110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sizeof(internal::YesType)> { 111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename T> 114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct is_class 115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : integral_constant<bool, 116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sizeof(internal::IsClassHelper::Test<T>(0)) == 117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sizeof(internal::YesType)> { 118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<bool B, class T = void> 121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct enable_if {}; 122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate<class T> 124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct enable_if<true, T> { typedef T type; }; 125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace base 127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // BASE_TEMPLATE_UTIL_H_ 129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org