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