gtest-type-util.h.pump revision 96839103cf05c81525e57ef00456e0afac90823f
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__
51#include <cxxabi.h>
52#endif  // __GNUC__
53
54#include <typeinfo>
55
56namespace testing {
57namespace internal {
58
59// AssertyTypeEq<T1, T2>::type is defined iff T1 and T2 are the same
60// type.  This can be used as a compile-time assertion to ensure that
61// two types are equal.
62
63template <typename T1, typename T2>
64struct AssertTypeEq;
65
66template <typename T>
67struct AssertTypeEq<T, T> {
68  typedef bool type;
69};
70
71// GetTypeName<T>() returns a human-readable name of type T.
72template <typename T>
73String GetTypeName() {
74#if GTEST_HAS_RTTI
75
76  const char* const name = typeid(T).name();
77#ifdef __GNUC__
78  int status = 0;
79  // gcc's implementation of typeid(T).name() mangles the type name,
80  // so we have to demangle it.
81  char* const readable_name = abi::__cxa_demangle(name, 0, 0, &status);
82  const String name_str(status == 0 ? readable_name : name);
83  free(readable_name);
84  return name_str;
85#else
86  return name;
87#endif  // __GNUC__
88
89#else
90  return "<type>";
91#endif  // GTEST_HAS_RTTI
92}
93
94// A unique type used as the default value for the arguments of class
95// template Types.  This allows us to simulate variadic templates
96// (e.g. Types<int>, Type<int, double>, and etc), which C++ doesn't
97// support directly.
98struct None {};
99
100// The following family of struct and struct templates are used to
101// represent type lists.  In particular, TypesN<T1, T2, ..., TN>
102// represents a type list with N types (T1, T2, ..., and TN) in it.
103// Except for Types0, every struct in the family has two member types:
104// Head for the first type in the list, and Tail for the rest of the
105// list.
106
107// The empty type list.
108struct Types0 {};
109
110// Type lists of length 1, 2, 3, and so on.
111
112template <typename T1>
113struct Types1 {
114  typedef T1 Head;
115  typedef Types0 Tail;
116};
117
118$range i 2..n
119
120$for i [[
121$range j 1..i
122$range k 2..i
123template <$for j, [[typename T$j]]>
124struct Types$i {
125  typedef T1 Head;
126  typedef Types$(i-1)<$for k, [[T$k]]> Tail;
127};
128
129
130]]
131
132}  // namespace internal
133
134// We don't want to require the users to write TypesN<...> directly,
135// as that would require them to count the length.  Types<...> is much
136// easier to write, but generates horrible messages when there is a
137// compiler error, as gcc insists on printing out each template
138// argument, even if it has the default value (this means Types<int>
139// will appear as Types<int, None, None, ..., None> in the compiler
140// errors).
141//
142// Our solution is to combine the best part of the two approaches: a
143// user would write Types<T1, ..., TN>, and Google Test will translate
144// that to TypesN<T1, ..., TN> internally to make error messages
145// readable.  The translation is done by the 'type' member of the
146// Types template.
147
148$range i 1..n
149template <$for i, [[typename T$i = internal::None]]>
150struct Types {
151  typedef internal::Types$n<$for i, [[T$i]]> type;
152};
153
154template <>
155struct Types<$for i, [[internal::None]]> {
156  typedef internal::Types0 type;
157};
158
159$range i 1..n-1
160$for i [[
161$range j 1..i
162$range k i+1..n
163template <$for j, [[typename T$j]]>
164struct Types<$for j, [[T$j]]$for k[[, internal::None]]> {
165  typedef internal::Types$i<$for j, [[T$j]]> type;
166};
167
168]]
169
170namespace internal {
171
172#define GTEST_TEMPLATE_ template <typename T> class
173
174// The template "selector" struct TemplateSel<Tmpl> is used to
175// represent Tmpl, which must be a class template with one type
176// parameter, as a type.  TemplateSel<Tmpl>::Bind<T>::type is defined
177// as the type Tmpl<T>.  This allows us to actually instantiate the
178// template "selected" by TemplateSel<Tmpl>.
179//
180// This trick is necessary for simulating typedef for class templates,
181// which C++ doesn't support directly.
182template <GTEST_TEMPLATE_ Tmpl>
183struct TemplateSel {
184  template <typename T>
185  struct Bind {
186    typedef Tmpl<T> type;
187  };
188};
189
190#define GTEST_BIND_(TmplSel, T) \
191  TmplSel::template Bind<T>::type
192
193// A unique struct template used as the default value for the
194// arguments of class template Templates.  This allows us to simulate
195// variadic templates (e.g. Templates<int>, Templates<int, double>,
196// and etc), which C++ doesn't support directly.
197template <typename T>
198struct NoneT {};
199
200// The following family of struct and struct templates are used to
201// represent template lists.  In particular, TemplatesN<T1, T2, ...,
202// TN> represents a list of N templates (T1, T2, ..., and TN).  Except
203// for Templates0, every struct in the family has two member types:
204// Head for the selector of the first template in the list, and Tail
205// for the rest of the list.
206
207// The empty template list.
208struct Templates0 {};
209
210// Template lists of length 1, 2, 3, and so on.
211
212template <GTEST_TEMPLATE_ T1>
213struct Templates1 {
214  typedef TemplateSel<T1> Head;
215  typedef Templates0 Tail;
216};
217
218$range i 2..n
219
220$for i [[
221$range j 1..i
222$range k 2..i
223template <$for j, [[GTEST_TEMPLATE_ T$j]]>
224struct Templates$i {
225  typedef TemplateSel<T1> Head;
226  typedef Templates$(i-1)<$for k, [[T$k]]> Tail;
227};
228
229
230]]
231
232// We don't want to require the users to write TemplatesN<...> directly,
233// as that would require them to count the length.  Templates<...> is much
234// easier to write, but generates horrible messages when there is a
235// compiler error, as gcc insists on printing out each template
236// argument, even if it has the default value (this means Templates<list>
237// will appear as Templates<list, NoneT, NoneT, ..., NoneT> in the compiler
238// errors).
239//
240// Our solution is to combine the best part of the two approaches: a
241// user would write Templates<T1, ..., TN>, and Google Test will translate
242// that to TemplatesN<T1, ..., TN> internally to make error messages
243// readable.  The translation is done by the 'type' member of the
244// Templates template.
245
246$range i 1..n
247template <$for i, [[GTEST_TEMPLATE_ T$i = NoneT]]>
248struct Templates {
249  typedef Templates$n<$for i, [[T$i]]> type;
250};
251
252template <>
253struct Templates<$for i, [[NoneT]]> {
254  typedef Templates0 type;
255};
256
257$range i 1..n-1
258$for i [[
259$range j 1..i
260$range k i+1..n
261template <$for j, [[GTEST_TEMPLATE_ T$j]]>
262struct Templates<$for j, [[T$j]]$for k[[, NoneT]]> {
263  typedef Templates$i<$for j, [[T$j]]> type;
264};
265
266]]
267
268// The TypeList template makes it possible to use either a single type
269// or a Types<...> list in TYPED_TEST_CASE() and
270// INSTANTIATE_TYPED_TEST_CASE_P().
271
272template <typename T>
273struct TypeList { typedef Types1<T> type; };
274
275
276$range i 1..n
277template <$for i, [[typename T$i]]>
278struct TypeList<Types<$for i, [[T$i]]> > {
279  typedef typename Types<$for i, [[T$i]]>::type type;
280};
281
282}  // namespace internal
283}  // namespace testing
284
285#endif  // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
286
287#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
288