• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //    * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //    * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //    * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef CEF_INCLUDE_BASE_CEF_TEMPLATE_UTIL_H_
32 #define CEF_INCLUDE_BASE_CEF_TEMPLATE_UTIL_H_
33 #pragma once
34 
35 #if defined(BASE_TEMPLATE_UTIL_H_)
36 // Do nothing if the Chromium header has already been included.
37 // This can happen in cases where Chromium code is used directly by the
38 // client application. When using Chromium code directly always include
39 // the Chromium header first to avoid type conflicts.
40 #elif defined(USING_CHROMIUM_INCLUDES)
41 // When building CEF include the Chromium header directly.
42 #include "base/template_util.h"
43 #else  // !USING_CHROMIUM_INCLUDES
44 // The following is substantially similar to the Chromium implementation.
45 // If the Chromium implementation diverges the below implementation should be
46 // updated to match.
47 
48 #include <cstddef>  // For size_t.
49 
50 #include "include/base/cef_build.h"
51 
52 namespace base {
53 
54 // template definitions from tr1
55 
56 template <class T, T v>
57 struct integral_constant {
58   static const T value = v;
59   typedef T value_type;
60   typedef integral_constant<T, v> type;
61 };
62 
63 template <class T, T v>
64 const T integral_constant<T, v>::value;
65 
66 typedef integral_constant<bool, true> true_type;
67 typedef integral_constant<bool, false> false_type;
68 
69 template <class T>
70 struct is_pointer : false_type {};
71 template <class T>
72 struct is_pointer<T*> : true_type {};
73 
74 // Member function pointer detection up to four params. Add more as needed
75 // below. This is built-in to C++ 11, and we can remove this when we switch.
76 template <typename T>
77 struct is_member_function_pointer : false_type {};
78 
79 template <typename R, typename Z>
80 struct is_member_function_pointer<R (Z::*)()> : true_type {};
81 template <typename R, typename Z>
82 struct is_member_function_pointer<R (Z::*)() const> : true_type {};
83 
84 template <typename R, typename Z, typename A>
85 struct is_member_function_pointer<R (Z::*)(A)> : true_type {};
86 template <typename R, typename Z, typename A>
87 struct is_member_function_pointer<R (Z::*)(A) const> : true_type {};
88 
89 template <typename R, typename Z, typename A, typename B>
90 struct is_member_function_pointer<R (Z::*)(A, B)> : true_type {};
91 template <typename R, typename Z, typename A, typename B>
92 struct is_member_function_pointer<R (Z::*)(A, B) const> : true_type {};
93 
94 template <typename R, typename Z, typename A, typename B, typename C>
95 struct is_member_function_pointer<R (Z::*)(A, B, C)> : true_type {};
96 template <typename R, typename Z, typename A, typename B, typename C>
97 struct is_member_function_pointer<R (Z::*)(A, B, C) const> : true_type {};
98 
99 template <typename R,
100           typename Z,
101           typename A,
102           typename B,
103           typename C,
104           typename D>
105 struct is_member_function_pointer<R (Z::*)(A, B, C, D)> : true_type {};
106 template <typename R,
107           typename Z,
108           typename A,
109           typename B,
110           typename C,
111           typename D>
112 struct is_member_function_pointer<R (Z::*)(A, B, C, D) const> : true_type {};
113 
114 template <class T, class U>
115 struct is_same : public false_type {};
116 template <class T>
117 struct is_same<T, T> : true_type {};
118 
119 template <class>
120 struct is_array : public false_type {};
121 template <class T, size_t n>
122 struct is_array<T[n]> : public true_type {};
123 template <class T>
124 struct is_array<T[]> : public true_type {};
125 
126 template <class T>
127 struct is_non_const_reference : false_type {};
128 template <class T>
129 struct is_non_const_reference<T&> : true_type {};
130 template <class T>
131 struct is_non_const_reference<const T&> : false_type {};
132 
133 template <class T>
134 struct is_const : false_type {};
135 template <class T>
136 struct is_const<const T> : true_type {};
137 
138 template <class T>
139 struct is_void : false_type {};
140 template <>
141 struct is_void<void> : true_type {};
142 
143 namespace cef_internal {
144 
145 // Types YesType and NoType are guaranteed such that sizeof(YesType) <
146 // sizeof(NoType).
147 typedef char YesType;
148 
149 struct NoType {
150   YesType dummy[2];
151 };
152 
153 // This class is an implementation detail for is_convertible, and you
154 // don't need to know how it works to use is_convertible. For those
155 // who care: we declare two different functions, one whose argument is
156 // of type To and one with a variadic argument list. We give them
157 // return types of different size, so we can use sizeof to trick the
158 // compiler into telling us which function it would have chosen if we
159 // had called it with an argument of type From.  See Alexandrescu's
160 // _Modern C++ Design_ for more details on this sort of trick.
161 
162 struct ConvertHelper {
163   template <typename To>
164   static YesType Test(To);
165 
166   template <typename To>
167   static NoType Test(...);
168 
169   template <typename From>
170   static From& Create();
171 };
172 
173 // Used to determine if a type is a struct/union/class. Inspired by Boost's
174 // is_class type_trait implementation.
175 struct IsClassHelper {
176   template <typename C>
177   static YesType Test(void (C::*)(void));
178 
179   template <typename C>
180   static NoType Test(...);
181 };
182 
183 }  // namespace cef_internal
184 
185 // Inherits from true_type if From is convertible to To, false_type otherwise.
186 //
187 // Note that if the type is convertible, this will be a true_type REGARDLESS
188 // of whether or not the conversion would emit a warning.
189 template <typename From, typename To>
190 struct is_convertible
191     : integral_constant<bool,
192                         sizeof(cef_internal::ConvertHelper::Test<To>(
193                             cef_internal::ConvertHelper::Create<From>())) ==
194                             sizeof(cef_internal::YesType)> {};
195 
196 template <typename T>
197 struct is_class
198     : integral_constant<bool,
199                         sizeof(cef_internal::IsClassHelper::Test<T>(0)) ==
200                             sizeof(cef_internal::YesType)> {};
201 
202 template <bool B, class T = void>
203 struct enable_if {};
204 
205 template <class T>
206 struct enable_if<true, T> {
207   typedef T type;
208 };
209 
210 }  // namespace base
211 
212 #endif  // !USING_CHROMIUM_INCLUDES
213 
214 #endif  // CEF_INCLUDE_BASE_CEF_TEMPLATE_UTIL_H_
215