1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/template_util.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +// Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_TEMPLATE_UTIL_H_ 1.9 +#define BASE_TEMPLATE_UTIL_H_ 1.10 + 1.11 +#include <cstddef> // For size_t. 1.12 + 1.13 +#include "build/build_config.h" 1.14 + 1.15 +namespace base { 1.16 + 1.17 +// template definitions from tr1 1.18 + 1.19 +template<class T, T v> 1.20 +struct integral_constant { 1.21 + static const T value = v; 1.22 + typedef T value_type; 1.23 + typedef integral_constant<T, v> type; 1.24 +}; 1.25 + 1.26 +template <class T, T v> const T integral_constant<T, v>::value; 1.27 + 1.28 +typedef integral_constant<bool, true> true_type; 1.29 +typedef integral_constant<bool, false> false_type; 1.30 + 1.31 +template <class T> struct is_pointer : false_type {}; 1.32 +template <class T> struct is_pointer<T*> : true_type {}; 1.33 + 1.34 +template <class T, class U> struct is_same : public false_type {}; 1.35 +template <class T> struct is_same<T,T> : true_type {}; 1.36 + 1.37 +template<class> struct is_array : public false_type {}; 1.38 +template<class T, size_t n> struct is_array<T[n]> : public true_type {}; 1.39 +template<class T> struct is_array<T[]> : public true_type {}; 1.40 + 1.41 +template <class T> struct is_non_const_reference : false_type {}; 1.42 +template <class T> struct is_non_const_reference<T&> : true_type {}; 1.43 +template <class T> struct is_non_const_reference<const T&> : false_type {}; 1.44 + 1.45 +template <class T> struct is_void : false_type {}; 1.46 +template <> struct is_void<void> : true_type {}; 1.47 + 1.48 +namespace internal { 1.49 + 1.50 +// Types YesType and NoType are guaranteed such that sizeof(YesType) < 1.51 +// sizeof(NoType). 1.52 +typedef char YesType; 1.53 + 1.54 +struct NoType { 1.55 + YesType dummy[2]; 1.56 +}; 1.57 + 1.58 +// This class is an implementation detail for is_convertible, and you 1.59 +// don't need to know how it works to use is_convertible. For those 1.60 +// who care: we declare two different functions, one whose argument is 1.61 +// of type To and one with a variadic argument list. We give them 1.62 +// return types of different size, so we can use sizeof to trick the 1.63 +// compiler into telling us which function it would have chosen if we 1.64 +// had called it with an argument of type From. See Alexandrescu's 1.65 +// _Modern C++ Design_ for more details on this sort of trick. 1.66 + 1.67 +struct ConvertHelper { 1.68 + template <typename To> 1.69 + static YesType Test(To); 1.70 + 1.71 + template <typename To> 1.72 + static NoType Test(...); 1.73 + 1.74 + template <typename From> 1.75 + static From& Create(); 1.76 +}; 1.77 + 1.78 +// Used to determine if a type is a struct/union/class. Inspired by Boost's 1.79 +// is_class type_trait implementation. 1.80 +struct IsClassHelper { 1.81 + template <typename C> 1.82 + static YesType Test(void(C::*)(void)); 1.83 + 1.84 + template <typename C> 1.85 + static NoType Test(...); 1.86 +}; 1.87 + 1.88 +} // namespace internal 1.89 + 1.90 +// Inherits from true_type if From is convertible to To, false_type otherwise. 1.91 +// 1.92 +// Note that if the type is convertible, this will be a true_type REGARDLESS 1.93 +// of whether or not the conversion would emit a warning. 1.94 +template <typename From, typename To> 1.95 +struct is_convertible 1.96 + : integral_constant<bool, 1.97 + sizeof(internal::ConvertHelper::Test<To>( 1.98 + internal::ConvertHelper::Create<From>())) == 1.99 + sizeof(internal::YesType)> { 1.100 +}; 1.101 + 1.102 +template <typename T> 1.103 +struct is_class 1.104 + : integral_constant<bool, 1.105 + sizeof(internal::IsClassHelper::Test<T>(0)) == 1.106 + sizeof(internal::YesType)> { 1.107 +}; 1.108 + 1.109 +} // namespace base 1.110 + 1.111 +#endif // BASE_TEMPLATE_UTIL_H_