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