Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 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 | |
| 30 | // Type utilities needed for implementing typed and type-parameterized |
| 31 | // tests. |
| 32 | |
| 33 | // GOOGLETEST_CM0001 DO NOT DELETE |
| 34 | |
Martin Stjernholm | c5cb0bf | 2021-05-24 17:21:58 +0100 | [diff] [blame] | 35 | #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ |
| 36 | #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 37 | |
| 38 | #include "gtest/internal/gtest-port.h" |
| 39 | |
| 40 | // #ifdef __GNUC__ is too general here. It is possible to use gcc without using |
| 41 | // libstdc++ (which is where cxxabi.h comes from). |
| 42 | # if GTEST_HAS_CXXABI_H_ |
| 43 | # include <cxxabi.h> |
| 44 | # elif defined(__HP_aCC) |
| 45 | # include <acxx_demangle.h> |
| 46 | # endif // GTEST_HASH_CXXABI_H_ |
| 47 | |
| 48 | namespace testing { |
| 49 | namespace internal { |
| 50 | |
| 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 | |
| 67 | #if GTEST_HAS_RTTI |
| 68 | // GetTypeName(const std::type_info&) returns a human-readable name of type T. |
| 69 | inline std::string GetTypeName(const std::type_info& type) { |
| 70 | const char* const name = type.name(); |
| 71 | #if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC) |
| 72 | int status = 0; |
| 73 | // gcc's implementation of typeid(T).name() mangles the type name, |
| 74 | // so we have to demangle it. |
| 75 | #if GTEST_HAS_CXXABI_H_ |
| 76 | using abi::__cxa_demangle; |
| 77 | #endif // GTEST_HAS_CXXABI_H_ |
| 78 | char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status); |
| 79 | const std::string name_str(status == 0 ? readable_name : name); |
| 80 | free(readable_name); |
| 81 | return CanonicalizeForStdLibVersioning(name_str); |
| 82 | #else |
| 83 | return name; |
| 84 | #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC |
| 85 | } |
| 86 | #endif // GTEST_HAS_RTTI |
| 87 | |
| 88 | // GetTypeName<T>() returns a human-readable name of type T if and only if |
| 89 | // RTTI is enabled, otherwise it returns a dummy type name. |
| 90 | // NB: This function is also used in Google Mock, so don't move it inside of |
| 91 | // the typed-test-only section below. |
| 92 | template <typename T> |
| 93 | std::string GetTypeName() { |
| 94 | #if GTEST_HAS_RTTI |
| 95 | return GetTypeName(typeid(T)); |
| 96 | #else |
| 97 | return "<type>"; |
| 98 | #endif // GTEST_HAS_RTTI |
| 99 | } |
| 100 | |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 101 | // A unique type indicating an empty node |
| 102 | struct None {}; |
| 103 | |
| 104 | # define GTEST_TEMPLATE_ template <typename T> class |
| 105 | |
| 106 | // The template "selector" struct TemplateSel<Tmpl> is used to |
| 107 | // represent Tmpl, which must be a class template with one type |
| 108 | // parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined |
| 109 | // as the type Tmpl<T>. This allows us to actually instantiate the |
| 110 | // template "selected" by TemplateSel<Tmpl>. |
| 111 | // |
| 112 | // This trick is necessary for simulating typedef for class templates, |
| 113 | // which C++ doesn't support directly. |
| 114 | template <GTEST_TEMPLATE_ Tmpl> |
| 115 | struct TemplateSel { |
| 116 | template <typename T> |
| 117 | struct Bind { |
| 118 | typedef Tmpl<T> type; |
| 119 | }; |
| 120 | }; |
| 121 | |
| 122 | # define GTEST_BIND_(TmplSel, T) \ |
| 123 | TmplSel::template Bind<T>::type |
| 124 | |
| 125 | template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_> |
| 126 | struct Templates { |
| 127 | using Head = TemplateSel<Head_>; |
| 128 | using Tail = Templates<Tail_...>; |
| 129 | }; |
| 130 | |
| 131 | template <GTEST_TEMPLATE_ Head_> |
| 132 | struct Templates<Head_> { |
| 133 | using Head = TemplateSel<Head_>; |
| 134 | using Tail = None; |
| 135 | }; |
| 136 | |
| 137 | // Tuple-like type lists |
| 138 | template <typename Head_, typename... Tail_> |
| 139 | struct Types { |
| 140 | using Head = Head_; |
| 141 | using Tail = Types<Tail_...>; |
| 142 | }; |
| 143 | |
| 144 | template <typename Head_> |
| 145 | struct Types<Head_> { |
| 146 | using Head = Head_; |
| 147 | using Tail = None; |
| 148 | }; |
| 149 | |
| 150 | // Helper metafunctions to tell apart a single type from types |
| 151 | // generated by ::testing::Types |
| 152 | template <typename... Ts> |
| 153 | struct ProxyTypeList { |
| 154 | using type = Types<Ts...>; |
| 155 | }; |
| 156 | |
| 157 | template <typename> |
| 158 | struct is_proxy_type_list : std::false_type {}; |
| 159 | |
| 160 | template <typename... Ts> |
| 161 | struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {}; |
| 162 | |
| 163 | // Generator which conditionally creates type lists. |
| 164 | // It recognizes if a requested type list should be created |
| 165 | // and prevents creating a new type list nested within another one. |
| 166 | template <typename T> |
| 167 | struct GenerateTypeList { |
| 168 | private: |
| 169 | using proxy = typename std::conditional<is_proxy_type_list<T>::value, T, |
| 170 | ProxyTypeList<T>>::type; |
| 171 | |
| 172 | public: |
| 173 | using type = typename proxy::type; |
| 174 | }; |
| 175 | |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 176 | } // namespace internal |
| 177 | |
| 178 | template <typename... Ts> |
| 179 | using Types = internal::ProxyTypeList<Ts...>; |
| 180 | |
| 181 | } // namespace testing |
| 182 | |
Martin Stjernholm | c5cb0bf | 2021-05-24 17:21:58 +0100 | [diff] [blame] | 183 | #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ |