gtest-type-util.h.pump 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. // Type utilities needed for implementing typed and type-parameterized
  32. // tests. This file is generated by a SCRIPT. DO NOT EDIT BY HAND!
  33. //
  34. // Currently we support at most $n types in a list, and at most $n
  35. // type-parameterized tests in one type-parameterized test case.
  36. // Please contact googletestframework@googlegroups.com if you need
  37. // more.
  38. // GOOGLETEST_CM0001 DO NOT DELETE
  39. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
  40. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
  41. #include "gtest/internal/gtest-port.h"
  42. // #ifdef __GNUC__ is too general here. It is possible to use gcc without using
  43. // libstdc++ (which is where cxxabi.h comes from).
  44. # if GTEST_HAS_CXXABI_H_
  45. # include <cxxabi.h>
  46. # elif defined(__HP_aCC)
  47. # include <acxx_demangle.h>
  48. # endif // GTEST_HASH_CXXABI_H_
  49. namespace testing {
  50. namespace internal {
  51. // Canonicalizes a given name with respect to the Standard C++ Library.
  52. // This handles removing the inline namespace within `std` that is
  53. // used by various standard libraries (e.g., `std::__1`). Names outside
  54. // of namespace std are returned unmodified.
  55. inline std::string CanonicalizeForStdLibVersioning(std::string s) {
  56. static const char prefix[] = "std::__";
  57. if (s.compare(0, strlen(prefix), prefix) == 0) {
  58. std::string::size_type end = s.find("::", strlen(prefix));
  59. if (end != s.npos) {
  60. // Erase everything between the initial `std` and the second `::`.
  61. s.erase(strlen("std"), end - strlen("std"));
  62. }
  63. }
  64. return s;
  65. }
  66. // GetTypeName<T>() returns a human-readable name of type T.
  67. // NB: This function is also used in Google Mock, so don't move it inside of
  68. // the typed-test-only section below.
  69. template <typename T>
  70. std::string GetTypeName() {
  71. # if GTEST_HAS_RTTI
  72. const char* const name = typeid(T).name();
  73. # if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
  74. int status = 0;
  75. // gcc's implementation of typeid(T).name() mangles the type name,
  76. // so we have to demangle it.
  77. # if GTEST_HAS_CXXABI_H_
  78. using abi::__cxa_demangle;
  79. # endif // GTEST_HAS_CXXABI_H_
  80. char* const readable_name = __cxa_demangle(name, 0, 0, &status);
  81. const std::string name_str(status == 0 ? readable_name : name);
  82. free(readable_name);
  83. return CanonicalizeForStdLibVersioning(name_str);
  84. # else
  85. return name;
  86. # endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
  87. # else
  88. return "<type>";
  89. # endif // GTEST_HAS_RTTI
  90. }
  91. #if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
  92. // AssertyTypeEq<T1, T2>::type is defined iff T1 and T2 are the same
  93. // type. This can be used as a compile-time assertion to ensure that
  94. // two types are equal.
  95. template <typename T1, typename T2>
  96. struct AssertTypeEq;
  97. template <typename T>
  98. struct AssertTypeEq<T, T> {
  99. typedef bool type;
  100. };
  101. // A unique type used as the default value for the arguments of class
  102. // template Types. This allows us to simulate variadic templates
  103. // (e.g. Types<int>, Type<int, double>, and etc), which C++ doesn't
  104. // support directly.
  105. struct None {};
  106. // The following family of struct and struct templates are used to
  107. // represent type lists. In particular, TypesN<T1, T2, ..., TN>
  108. // represents a type list with N types (T1, T2, ..., and TN) in it.
  109. // Except for Types0, every struct in the family has two member types:
  110. // Head for the first type in the list, and Tail for the rest of the
  111. // list.
  112. // The empty type list.
  113. struct Types0 {};
  114. // Type lists of length 1, 2, 3, and so on.
  115. template <typename T1>
  116. struct Types1 {
  117. typedef T1 Head;
  118. typedef Types0 Tail;
  119. };
  120. $range i 2..n
  121. $for i [[
  122. $range j 1..i
  123. $range k 2..i
  124. template <$for j, [[typename T$j]]>
  125. struct Types$i {
  126. typedef T1 Head;
  127. typedef Types$(i-1)<$for k, [[T$k]]> Tail;
  128. };
  129. ]]
  130. } // namespace internal
  131. // We don't want to require the users to write TypesN<...> directly,
  132. // as that would require them to count the length. Types<...> is much
  133. // easier to write, but generates horrible messages when there is a
  134. // compiler error, as gcc insists on printing out each template
  135. // argument, even if it has the default value (this means Types<int>
  136. // will appear as Types<int, None, None, ..., None> in the compiler
  137. // errors).
  138. //
  139. // Our solution is to combine the best part of the two approaches: a
  140. // user would write Types<T1, ..., TN>, and Google Test will translate
  141. // that to TypesN<T1, ..., TN> internally to make error messages
  142. // readable. The translation is done by the 'type' member of the
  143. // Types template.
  144. $range i 1..n
  145. template <$for i, [[typename T$i = internal::None]]>
  146. struct Types {
  147. typedef internal::Types$n<$for i, [[T$i]]> type;
  148. };
  149. template <>
  150. struct Types<$for i, [[internal::None]]> {
  151. typedef internal::Types0 type;
  152. };
  153. $range i 1..n-1
  154. $for i [[
  155. $range j 1..i
  156. $range k i+1..n
  157. template <$for j, [[typename T$j]]>
  158. struct Types<$for j, [[T$j]]$for k[[, internal::None]]> {
  159. typedef internal::Types$i<$for j, [[T$j]]> type;
  160. };
  161. ]]
  162. namespace internal {
  163. # define GTEST_TEMPLATE_ template <typename T> class
  164. // The template "selector" struct TemplateSel<Tmpl> is used to
  165. // represent Tmpl, which must be a class template with one type
  166. // parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined
  167. // as the type Tmpl<T>. This allows us to actually instantiate the
  168. // template "selected" by TemplateSel<Tmpl>.
  169. //
  170. // This trick is necessary for simulating typedef for class templates,
  171. // which C++ doesn't support directly.
  172. template <GTEST_TEMPLATE_ Tmpl>
  173. struct TemplateSel {
  174. template <typename T>
  175. struct Bind {
  176. typedef Tmpl<T> type;
  177. };
  178. };
  179. # define GTEST_BIND_(TmplSel, T) \
  180. TmplSel::template Bind<T>::type
  181. // A unique struct template used as the default value for the
  182. // arguments of class template Templates. This allows us to simulate
  183. // variadic templates (e.g. Templates<int>, Templates<int, double>,
  184. // and etc), which C++ doesn't support directly.
  185. template <typename T>
  186. struct NoneT {};
  187. // The following family of struct and struct templates are used to
  188. // represent template lists. In particular, TemplatesN<T1, T2, ...,
  189. // TN> represents a list of N templates (T1, T2, ..., and TN). Except
  190. // for Templates0, every struct in the family has two member types:
  191. // Head for the selector of the first template in the list, and Tail
  192. // for the rest of the list.
  193. // The empty template list.
  194. struct Templates0 {};
  195. // Template lists of length 1, 2, 3, and so on.
  196. template <GTEST_TEMPLATE_ T1>
  197. struct Templates1 {
  198. typedef TemplateSel<T1> Head;
  199. typedef Templates0 Tail;
  200. };
  201. $range i 2..n
  202. $for i [[
  203. $range j 1..i
  204. $range k 2..i
  205. template <$for j, [[GTEST_TEMPLATE_ T$j]]>
  206. struct Templates$i {
  207. typedef TemplateSel<T1> Head;
  208. typedef Templates$(i-1)<$for k, [[T$k]]> Tail;
  209. };
  210. ]]
  211. // We don't want to require the users to write TemplatesN<...> directly,
  212. // as that would require them to count the length. Templates<...> is much
  213. // easier to write, but generates horrible messages when there is a
  214. // compiler error, as gcc insists on printing out each template
  215. // argument, even if it has the default value (this means Templates<list>
  216. // will appear as Templates<list, NoneT, NoneT, ..., NoneT> in the compiler
  217. // errors).
  218. //
  219. // Our solution is to combine the best part of the two approaches: a
  220. // user would write Templates<T1, ..., TN>, and Google Test will translate
  221. // that to TemplatesN<T1, ..., TN> internally to make error messages
  222. // readable. The translation is done by the 'type' member of the
  223. // Templates template.
  224. $range i 1..n
  225. template <$for i, [[GTEST_TEMPLATE_ T$i = NoneT]]>
  226. struct Templates {
  227. typedef Templates$n<$for i, [[T$i]]> type;
  228. };
  229. template <>
  230. struct Templates<$for i, [[NoneT]]> {
  231. typedef Templates0 type;
  232. };
  233. $range i 1..n-1
  234. $for i [[
  235. $range j 1..i
  236. $range k i+1..n
  237. template <$for j, [[GTEST_TEMPLATE_ T$j]]>
  238. struct Templates<$for j, [[T$j]]$for k[[, NoneT]]> {
  239. typedef Templates$i<$for j, [[T$j]]> type;
  240. };
  241. ]]
  242. // The TypeList template makes it possible to use either a single type
  243. // or a Types<...> list in TYPED_TEST_CASE() and
  244. // INSTANTIATE_TYPED_TEST_CASE_P().
  245. template <typename T>
  246. struct TypeList {
  247. typedef Types1<T> type;
  248. };
  249. $range i 1..n
  250. template <$for i, [[typename T$i]]>
  251. struct TypeList<Types<$for i, [[T$i]]> > {
  252. typedef typename Types<$for i, [[T$i]]>::type type;
  253. };
  254. #endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
  255. } // namespace internal
  256. } // namespace testing
  257. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_