Home | History | Annotate | Download | only in internal
      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 (a] 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 (a] 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 namespace testing {
     57 namespace 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 
     63 template <typename T1, typename T2>
     64 struct AssertTypeEq;
     65 
     66 template <typename T>
     67 struct AssertTypeEq<T, T> {
     68   typedef bool type;
     69 };
     70 
     71 // GetTypeName<T>() returns a human-readable name of type T.
     72 template <typename T>
     73 String GetTypeName() {
     74 #if GTEST_HAS_RTTI
     75 
     76   const char* const name = typeid(T).name();
     77 #ifdef __GLIBCXX__
     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  // __GLIBCXX__
     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.
     98 struct 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.
    108 struct Types0 {};
    109 
    110 // Type lists of length 1, 2, 3, and so on.
    111 
    112 template <typename T1>
    113 struct 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
    123 template <$for j, [[typename T$j]]>
    124 struct 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
    149 template <$for i, [[typename T$i = internal::None]]>
    150 struct Types {
    151   typedef internal::Types$n<$for i, [[T$i]]> type;
    152 };
    153 
    154 template <>
    155 struct 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
    163 template <$for j, [[typename T$j]]>
    164 struct Types<$for j, [[T$j]]$for k[[, internal::None]]> {
    165   typedef internal::Types$i<$for j, [[T$j]]> type;
    166 };
    167 
    168 ]]
    169 
    170 namespace 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.
    182 template <GTEST_TEMPLATE_ Tmpl>
    183 struct 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.
    197 template <typename T>
    198 struct 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.
    208 struct Templates0 {};
    209 
    210 // Template lists of length 1, 2, 3, and so on.
    211 
    212 template <GTEST_TEMPLATE_ T1>
    213 struct 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
    223 template <$for j, [[GTEST_TEMPLATE_ T$j]]>
    224 struct 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
    247 template <$for i, [[GTEST_TEMPLATE_ T$i = NoneT]]>
    248 struct Templates {
    249   typedef Templates$n<$for i, [[T$i]]> type;
    250 };
    251 
    252 template <>
    253 struct 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
    261 template <$for j, [[GTEST_TEMPLATE_ T$j]]>
    262 struct 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 
    272 template <typename T>
    273 struct TypeList { typedef Types1<T> type; };
    274 
    275 
    276 $range i 1..n
    277 template <$for i, [[typename T$i]]>
    278 struct 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