1$$ -*- mode: c++; -*-
2$var n = 50  $$ Maximum length of type lists we want to support.
3// Copyright 2008 Google Inc.
4// All Rights Reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10//     * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//     * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16//     * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//
32// Author: wan@google.com (Zhanyong Wan)
33
34// Type utilities needed for implementing typed and type-parameterized
35// tests.  This file is generated by a SCRIPT.  DO NOT EDIT BY HAND!
36//
37// Currently we support at most $n types in a list, and at most $n
38// type-parameterized tests in one type-parameterized test case.
39// Please contact googletestframework@googlegroups.com if you need
40// more.
41
42#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
43#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
44
45#include <gtest/internal/gtest-port.h>
46#include <gtest/internal/gtest-string.h>
47
48#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
49
50// #ifdef __GNUC__ is too general here.  It is possible to use gcc without using
51// libstdc++ (which is where cxxabi.h comes from).
52#ifdef __GLIBCXX__
53#include <cxxabi.h>
54#endif  // __GLIBCXX__
55
56#include <typeinfo>
57
58namespace testing {
59namespace internal {
60
61// AssertyTypeEq<T1, T2>::type is defined iff T1 and T2 are the same
62// type.  This can be used as a compile-time assertion to ensure that
63// two types are equal.
64
65template <typename T1, typename T2>
66struct AssertTypeEq;
67
68template <typename T>
69struct AssertTypeEq<T, T> {
70  typedef bool type;
71};
72
73// GetTypeName<T>() returns a human-readable name of type T.
74template <typename T>
75String GetTypeName() {
76#if GTEST_HAS_RTTI
77
78  const char* const name = typeid(T).name();
79#ifdef __GLIBCXX__
80  int status = 0;
81  // gcc's implementation of typeid(T).name() mangles the type name,
82  // so we have to demangle it.
83  char* const readable_name = abi::__cxa_demangle(name, 0, 0, &status);
84  const String name_str(status == 0 ? readable_name : name);
85  free(readable_name);
86  return name_str;
87#else
88  return name;
89#endif  // __GLIBCXX__
90
91#else
92  return "<type>";
93#endif  // GTEST_HAS_RTTI
94}
95
96// A unique type used as the default value for the arguments of class
97// template Types.  This allows us to simulate variadic templates
98// (e.g. Types<int>, Type<int, double>, and etc), which C++ doesn't
99// support directly.
100struct None {};
101
102// The following family of struct and struct templates are used to
103// represent type lists.  In particular, TypesN<T1, T2, ..., TN>
104// represents a type list with N types (T1, T2, ..., and TN) in it.
105// Except for Types0, every struct in the family has two member types:
106// Head for the first type in the list, and Tail for the rest of the
107// list.
108
109// The empty type list.
110struct Types0 {};
111
112// Type lists of length 1, 2, 3, and so on.
113
114template <typename T1>
115struct Types1 {
116  typedef T1 Head;
117  typedef Types0 Tail;
118};
119
120$range i 2..n
121
122$for i [[
123$range j 1..i
124$range k 2..i
125template <$for j, [[typename T$j]]>
126struct Types$i {
127  typedef T1 Head;
128  typedef Types$(i-1)<$for k, [[T$k]]> Tail;
129};
130
131
132]]
133
134}  // namespace internal
135
136// We don't want to require the users to write TypesN<...> directly,
137// as that would require them to count the length.  Types<...> is much
138// easier to write, but generates horrible messages when there is a
139// compiler error, as gcc insists on printing out each template
140// argument, even if it has the default value (this means Types<int>
141// will appear as Types<int, None, None, ..., None> in the compiler
142// errors).
143//
144// Our solution is to combine the best part of the two approaches: a
145// user would write Types<T1, ..., TN>, and Google Test will translate
146// that to TypesN<T1, ..., TN> internally to make error messages
147// readable.  The translation is done by the 'type' member of the
148// Types template.
149
150$range i 1..n
151template <$for i, [[typename T$i = internal::None]]>
152struct Types {
153  typedef internal::Types$n<$for i, [[T$i]]> type;
154};
155
156template <>
157struct Types<$for i, [[internal::None]]> {
158  typedef internal::Types0 type;
159};
160
161$range i 1..n-1
162$for i [[
163$range j 1..i
164$range k i+1..n
165template <$for j, [[typename T$j]]>
166struct Types<$for j, [[T$j]]$for k[[, internal::None]]> {
167  typedef internal::Types$i<$for j, [[T$j]]> type;
168};
169
170]]
171
172namespace internal {
173
174#define GTEST_TEMPLATE_ template <typename T> class
175
176// The template "selector" struct TemplateSel<Tmpl> is used to
177// represent Tmpl, which must be a class template with one type
178// parameter, as a type.  TemplateSel<Tmpl>::Bind<T>::type is defined
179// as the type Tmpl<T>.  This allows us to actually instantiate the
180// template "selected" by TemplateSel<Tmpl>.
181//
182// This trick is necessary for simulating typedef for class templates,
183// which C++ doesn't support directly.
184template <GTEST_TEMPLATE_ Tmpl>
185struct TemplateSel {
186  template <typename T>
187  struct Bind {
188    typedef Tmpl<T> type;
189  };
190};
191
192#define GTEST_BIND_(TmplSel, T) \
193  TmplSel::template Bind<T>::type
194
195// A unique struct template used as the default value for the
196// arguments of class template Templates.  This allows us to simulate
197// variadic templates (e.g. Templates<int>, Templates<int, double>,
198// and etc), which C++ doesn't support directly.
199template <typename T>
200struct NoneT {};
201
202// The following family of struct and struct templates are used to
203// represent template lists.  In particular, TemplatesN<T1, T2, ...,
204// TN> represents a list of N templates (T1, T2, ..., and TN).  Except
205// for Templates0, every struct in the family has two member types:
206// Head for the selector of the first template in the list, and Tail
207// for the rest of the list.
208
209// The empty template list.
210struct Templates0 {};
211
212// Template lists of length 1, 2, 3, and so on.
213
214template <GTEST_TEMPLATE_ T1>
215struct Templates1 {
216  typedef TemplateSel<T1> Head;
217  typedef Templates0 Tail;
218};
219
220$range i 2..n
221
222$for i [[
223$range j 1..i
224$range k 2..i
225template <$for j, [[GTEST_TEMPLATE_ T$j]]>
226struct Templates$i {
227  typedef TemplateSel<T1> Head;
228  typedef Templates$(i-1)<$for k, [[T$k]]> Tail;
229};
230
231
232]]
233
234// We don't want to require the users to write TemplatesN<...> directly,
235// as that would require them to count the length.  Templates<...> is much
236// easier to write, but generates horrible messages when there is a
237// compiler error, as gcc insists on printing out each template
238// argument, even if it has the default value (this means Templates<list>
239// will appear as Templates<list, NoneT, NoneT, ..., NoneT> in the compiler
240// errors).
241//
242// Our solution is to combine the best part of the two approaches: a
243// user would write Templates<T1, ..., TN>, and Google Test will translate
244// that to TemplatesN<T1, ..., TN> internally to make error messages
245// readable.  The translation is done by the 'type' member of the
246// Templates template.
247
248$range i 1..n
249template <$for i, [[GTEST_TEMPLATE_ T$i = NoneT]]>
250struct Templates {
251  typedef Templates$n<$for i, [[T$i]]> type;
252};
253
254template <>
255struct Templates<$for i, [[NoneT]]> {
256  typedef Templates0 type;
257};
258
259$range i 1..n-1
260$for i [[
261$range j 1..i
262$range k i+1..n
263template <$for j, [[GTEST_TEMPLATE_ T$j]]>
264struct Templates<$for j, [[T$j]]$for k[[, NoneT]]> {
265  typedef Templates$i<$for j, [[T$j]]> type;
266};
267
268]]
269
270// The TypeList template makes it possible to use either a single type
271// or a Types<...> list in TYPED_TEST_CASE() and
272// INSTANTIATE_TYPED_TEST_CASE_P().
273
274template <typename T>
275struct TypeList { typedef Types1<T> type; };
276
277
278$range i 1..n
279template <$for i, [[typename T$i]]>
280struct TypeList<Types<$for i, [[T$i]]> > {
281  typedef typename Types<$for i, [[T$i]]>::type type;
282};
283
284}  // namespace internal
285}  // namespace testing
286
287#endif  // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
288
289#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
290