gtest-type-util.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2008 Google Inc.
  2. // All Rights Reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Type utilities needed for implementing typed and type-parameterized
  30. // tests.
  31. // GOOGLETEST_CM0001 DO NOT DELETE
  32. #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
  33. #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
  34. #include "gtest/internal/gtest-port.h"
  35. // #ifdef __GNUC__ is too general here. It is possible to use gcc without using
  36. // libstdc++ (which is where cxxabi.h comes from).
  37. # if GTEST_HAS_CXXABI_H_
  38. # include <cxxabi.h>
  39. # elif defined(__HP_aCC)
  40. # include <acxx_demangle.h>
  41. # endif // GTEST_HASH_CXXABI_H_
  42. namespace testing {
  43. namespace internal {
  44. // Canonicalizes a given name with respect to the Standard C++ Library.
  45. // This handles removing the inline namespace within `std` that is
  46. // used by various standard libraries (e.g., `std::__1`). Names outside
  47. // of namespace std are returned unmodified.
  48. inline std::string CanonicalizeForStdLibVersioning(std::string s) {
  49. static const char prefix[] = "std::__";
  50. if (s.compare(0, strlen(prefix), prefix) == 0) {
  51. std::string::size_type end = s.find("::", strlen(prefix));
  52. if (end != s.npos) {
  53. // Erase everything between the initial `std` and the second `::`.
  54. s.erase(strlen("std"), end - strlen("std"));
  55. }
  56. }
  57. return s;
  58. }
  59. #if GTEST_HAS_RTTI
  60. // GetTypeName(const std::type_info&) returns a human-readable name of type T.
  61. inline std::string GetTypeName(const std::type_info& type) {
  62. const char* const name = type.name();
  63. #if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
  64. int status = 0;
  65. // gcc's implementation of typeid(T).name() mangles the type name,
  66. // so we have to demangle it.
  67. #if GTEST_HAS_CXXABI_H_
  68. using abi::__cxa_demangle;
  69. #endif // GTEST_HAS_CXXABI_H_
  70. char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status);
  71. const std::string name_str(status == 0 ? readable_name : name);
  72. free(readable_name);
  73. return CanonicalizeForStdLibVersioning(name_str);
  74. #else
  75. return name;
  76. #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
  77. }
  78. #endif // GTEST_HAS_RTTI
  79. // GetTypeName<T>() returns a human-readable name of type T if and only if
  80. // RTTI is enabled, otherwise it returns a dummy type name.
  81. // NB: This function is also used in Google Mock, so don't move it inside of
  82. // the typed-test-only section below.
  83. template <typename T>
  84. std::string GetTypeName() {
  85. #if GTEST_HAS_RTTI
  86. return GetTypeName(typeid(T));
  87. #else
  88. return "<type>";
  89. #endif // GTEST_HAS_RTTI
  90. }
  91. // A unique type indicating an empty node
  92. struct None {};
  93. # define GTEST_TEMPLATE_ template <typename T> class
  94. // The template "selector" struct TemplateSel<Tmpl> is used to
  95. // represent Tmpl, which must be a class template with one type
  96. // parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined
  97. // as the type Tmpl<T>. This allows us to actually instantiate the
  98. // template "selected" by TemplateSel<Tmpl>.
  99. //
  100. // This trick is necessary for simulating typedef for class templates,
  101. // which C++ doesn't support directly.
  102. template <GTEST_TEMPLATE_ Tmpl>
  103. struct TemplateSel {
  104. template <typename T>
  105. struct Bind {
  106. typedef Tmpl<T> type;
  107. };
  108. };
  109. # define GTEST_BIND_(TmplSel, T) \
  110. TmplSel::template Bind<T>::type
  111. template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_>
  112. struct Templates {
  113. using Head = TemplateSel<Head_>;
  114. using Tail = Templates<Tail_...>;
  115. };
  116. template <GTEST_TEMPLATE_ Head_>
  117. struct Templates<Head_> {
  118. using Head = TemplateSel<Head_>;
  119. using Tail = None;
  120. };
  121. // Tuple-like type lists
  122. template <typename Head_, typename... Tail_>
  123. struct Types {
  124. using Head = Head_;
  125. using Tail = Types<Tail_...>;
  126. };
  127. template <typename Head_>
  128. struct Types<Head_> {
  129. using Head = Head_;
  130. using Tail = None;
  131. };
  132. // Helper metafunctions to tell apart a single type from types
  133. // generated by ::testing::Types
  134. template <typename... Ts>
  135. struct ProxyTypeList {
  136. using type = Types<Ts...>;
  137. };
  138. template <typename>
  139. struct is_proxy_type_list : std::false_type {};
  140. template <typename... Ts>
  141. struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {};
  142. // Generator which conditionally creates type lists.
  143. // It recognizes if a requested type list should be created
  144. // and prevents creating a new type list nested within another one.
  145. template <typename T>
  146. struct GenerateTypeList {
  147. private:
  148. using proxy = typename std::conditional<is_proxy_type_list<T>::value, T,
  149. ProxyTypeList<T>>::type;
  150. public:
  151. using type = typename proxy::type;
  152. };
  153. } // namespace internal
  154. template <typename... Ts>
  155. using Types = internal::ProxyTypeList<Ts...>;
  156. } // namespace testing
  157. #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_