1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Type utilities needed for implementing typed and type-parameterized
33 // IWYU pragma: private, include "gtest/gtest.h"
34 // IWYU pragma: friend gtest/.*
35 // IWYU pragma: friend gmock/.*
37 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
38 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
41 #include <type_traits>
44 #include "gtest/internal/gtest-port.h"
46 // #ifdef __GNUC__ is too general here. It is possible to use gcc without using
47 // libstdc++ (which is where cxxabi.h comes from).
48 #if GTEST_HAS_CXXABI_H_
50 #elif defined(__HP_aCC)
51 #include <acxx_demangle.h>
52 #endif // GTEST_HASH_CXXABI_H_
57 // Canonicalizes a given name with respect to the Standard C++ Library.
58 // This handles removing the inline namespace within `std` that is
59 // used by various standard libraries (e.g., `std::__1`). Names outside
60 // of namespace std are returned unmodified.
61 inline std::string CanonicalizeForStdLibVersioning(std::string s) {
62 static const char prefix[] = "std::__";
63 if (s.compare(0, strlen(prefix), prefix) == 0) {
64 std::string::size_type end = s.find("::", strlen(prefix));
66 // Erase everything between the initial `std` and the second `::`.
67 s.erase(strlen("std"), end - strlen("std"));
71 // Strip redundant spaces in typename to match MSVC
72 // For example, std::pair<int, bool> -> std::pair<int,bool>
73 static const char to_search[] = ", ";
74 static const char replace_str[] = ",";
77 // Get the next occurrence from the current position
78 pos = s.find(to_search, pos);
79 if (pos == std::string::npos) {
82 // Replace this occurrence of substring
83 s.replace(pos, strlen(to_search), replace_str);
84 pos += strlen(replace_str);
90 // GetTypeName(const std::type_info&) returns a human-readable name of type T.
91 inline std::string GetTypeName(const std::type_info& type) {
92 const char* const name = type.name();
93 #if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
95 // gcc's implementation of typeid(T).name() mangles the type name,
96 // so we have to demangle it.
97 #if GTEST_HAS_CXXABI_H_
98 using abi::__cxa_demangle;
99 #endif // GTEST_HAS_CXXABI_H_
100 char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status);
101 const std::string name_str(status == 0 ? readable_name : name);
103 return CanonicalizeForStdLibVersioning(name_str);
104 #elif defined(_MSC_VER)
105 // Strip struct and class due to differences between
106 // MSVC and other compilers. std::pair<int,bool> is printed as
107 // "struct std::pair<int,bool>" when using MSVC vs "std::pair<int, bool>" with
109 std::string s = name;
110 // Only strip the leading "struct " and "class ", so uses rfind == 0 to
112 if (s.rfind("struct ", 0) == 0) {
113 s = s.substr(strlen("struct "));
114 } else if (s.rfind("class ", 0) == 0) {
115 s = s.substr(strlen("class "));
120 #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
122 #endif // GTEST_HAS_RTTI
124 // GetTypeName<T>() returns a human-readable name of type T if and only if
125 // RTTI is enabled, otherwise it returns a dummy type name.
126 // NB: This function is also used in Google Mock, so don't move it inside of
127 // the typed-test-only section below.
128 template <typename T>
129 std::string GetTypeName() {
131 return GetTypeName(typeid(T));
134 #endif // GTEST_HAS_RTTI
137 // A unique type indicating an empty node
140 #define GTEST_TEMPLATE_ \
141 template <typename T> \
144 // The template "selector" struct TemplateSel<Tmpl> is used to
145 // represent Tmpl, which must be a class template with one type
146 // parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined
147 // as the type Tmpl<T>. This allows us to actually instantiate the
148 // template "selected" by TemplateSel<Tmpl>.
150 // This trick is necessary for simulating typedef for class templates,
151 // which C++ doesn't support directly.
152 template <GTEST_TEMPLATE_ Tmpl>
154 template <typename T>
156 typedef Tmpl<T> type;
160 #define GTEST_BIND_(TmplSel, T) TmplSel::template Bind<T>::type
162 template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_>
164 using Head = TemplateSel<Head_>;
165 using Tail = Templates<Tail_...>;
168 template <GTEST_TEMPLATE_ Head_>
169 struct Templates<Head_> {
170 using Head = TemplateSel<Head_>;
174 // Tuple-like type lists
175 template <typename Head_, typename... Tail_>
178 using Tail = Types<Tail_...>;
181 template <typename Head_>
182 struct Types<Head_> {
187 // Helper metafunctions to tell apart a single type from types
188 // generated by ::testing::Types
189 template <typename... Ts>
190 struct ProxyTypeList {
191 using type = Types<Ts...>;
195 struct is_proxy_type_list : std::false_type {};
197 template <typename... Ts>
198 struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {};
200 // Generator which conditionally creates type lists.
201 // It recognizes if a requested type list should be created
202 // and prevents creating a new type list nested within another one.
203 template <typename T>
204 struct GenerateTypeList {
206 using proxy = typename std::conditional<is_proxy_type_list<T>::value, T,
207 ProxyTypeList<T>>::type;
210 using type = typename proxy::type;
213 } // namespace internal
215 template <typename... Ts>
216 using Types = internal::ProxyTypeList<Ts...>;
218 } // namespace testing
220 #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_