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 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
34 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
36 #include "gtest/internal/gtest-port.h"
38 // #ifdef __GNUC__ is too general here. It is possible to use gcc without using
39 // libstdc++ (which is where cxxabi.h comes from).
40 # if GTEST_HAS_CXXABI_H_
42 # elif defined(__HP_aCC)
43 # include <acxx_demangle.h>
44 # endif // GTEST_HASH_CXXABI_H_
49 // Canonicalizes a given name with respect to the Standard C++ Library.
50 // This handles removing the inline namespace within `std` that is
51 // used by various standard libraries (e.g., `std::__1`). Names outside
52 // of namespace std are returned unmodified.
53 inline std::string CanonicalizeForStdLibVersioning(std::string s) {
54 static const char prefix[] = "std::__";
55 if (s.compare(0, strlen(prefix), prefix) == 0) {
56 std::string::size_type end = s.find("::", strlen(prefix));
58 // Erase everything between the initial `std` and the second `::`.
59 s.erase(strlen("std"), end - strlen("std"));
66 // GetTypeName(const std::type_info&) returns a human-readable name of type T.
67 inline std::string GetTypeName(const std::type_info& type) {
68 const char* const name = type.name();
69 #if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
71 // gcc's implementation of typeid(T).name() mangles the type name,
72 // so we have to demangle it.
73 #if GTEST_HAS_CXXABI_H_
74 using abi::__cxa_demangle;
75 #endif // GTEST_HAS_CXXABI_H_
76 char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status);
77 const std::string name_str(status == 0 ? readable_name : name);
79 return CanonicalizeForStdLibVersioning(name_str);
82 #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
84 #endif // GTEST_HAS_RTTI
86 // GetTypeName<T>() returns a human-readable name of type T if and only if
87 // RTTI is enabled, otherwise it returns a dummy type name.
88 // NB: This function is also used in Google Mock, so don't move it inside of
89 // the typed-test-only section below.
91 std::string GetTypeName() {
93 return GetTypeName(typeid(T));
96 #endif // GTEST_HAS_RTTI
99 // A unique type indicating an empty node
102 # define GTEST_TEMPLATE_ template <typename T> class
104 // The template "selector" struct TemplateSel<Tmpl> is used to
105 // represent Tmpl, which must be a class template with one type
106 // parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined
107 // as the type Tmpl<T>. This allows us to actually instantiate the
108 // template "selected" by TemplateSel<Tmpl>.
110 // This trick is necessary for simulating typedef for class templates,
111 // which C++ doesn't support directly.
112 template <GTEST_TEMPLATE_ Tmpl>
114 template <typename T>
116 typedef Tmpl<T> type;
120 # define GTEST_BIND_(TmplSel, T) \
121 TmplSel::template Bind<T>::type
123 template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_>
125 using Head = TemplateSel<Head_>;
126 using Tail = Templates<Tail_...>;
129 template <GTEST_TEMPLATE_ Head_>
130 struct Templates<Head_> {
131 using Head = TemplateSel<Head_>;
135 // Tuple-like type lists
136 template <typename Head_, typename... Tail_>
139 using Tail = Types<Tail_...>;
142 template <typename Head_>
143 struct Types<Head_> {
148 // Helper metafunctions to tell apart a single type from types
149 // generated by ::testing::Types
150 template <typename... Ts>
151 struct ProxyTypeList {
152 using type = Types<Ts...>;
156 struct is_proxy_type_list : std::false_type {};
158 template <typename... Ts>
159 struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {};
161 // Generator which conditionally creates type lists.
162 // It recognizes if a requested type list should be created
163 // and prevents creating a new type list nested within another one.
164 template <typename T>
165 struct GenerateTypeList {
167 using proxy = typename std::conditional<is_proxy_type_list<T>::value, T,
168 ProxyTypeList<T>>::type;
171 using type = typename proxy::type;
174 } // namespace internal
176 template <typename... Ts>
177 using Types = internal::ProxyTypeList<Ts...>;
179 } // namespace testing
181 #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_