security/sandbox/chromium/base/template_util.h

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

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

mercurial