1b89a7cc2SEnji Cooper // Copyright 2008 Google Inc.
2b89a7cc2SEnji Cooper // All Rights Reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper // Type utilities needed for implementing typed and type-parameterized
3128f6c2f2SEnji Cooper // tests.
32b89a7cc2SEnji Cooper 
3328f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h"
3428f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.*
3528f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.*
36b89a7cc2SEnji Cooper 
3728f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
3828f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
3928f6c2f2SEnji Cooper 
4028f6c2f2SEnji Cooper #include <string>
4128f6c2f2SEnji Cooper #include <type_traits>
4228f6c2f2SEnji Cooper #include <typeinfo>
43b89a7cc2SEnji Cooper 
44b89a7cc2SEnji Cooper #include "gtest/internal/gtest-port.h"
45b89a7cc2SEnji Cooper 
46b89a7cc2SEnji Cooper // #ifdef __GNUC__ is too general here.  It is possible to use gcc without using
47b89a7cc2SEnji Cooper // libstdc++ (which is where cxxabi.h comes from).
48b89a7cc2SEnji Cooper #if GTEST_HAS_CXXABI_H_
49b89a7cc2SEnji Cooper #include <cxxabi.h>
50b89a7cc2SEnji Cooper #elif defined(__HP_aCC)
51b89a7cc2SEnji Cooper #include <acxx_demangle.h>
52b89a7cc2SEnji Cooper #endif  // GTEST_HASH_CXXABI_H_
53b89a7cc2SEnji Cooper 
54b89a7cc2SEnji Cooper namespace testing {
55b89a7cc2SEnji Cooper namespace internal {
56b89a7cc2SEnji Cooper 
57b89a7cc2SEnji Cooper // Canonicalizes a given name with respect to the Standard C++ Library.
58b89a7cc2SEnji Cooper // This handles removing the inline namespace within `std` that is
59b89a7cc2SEnji Cooper // used by various standard libraries (e.g., `std::__1`).  Names outside
60b89a7cc2SEnji Cooper // of namespace std are returned unmodified.
CanonicalizeForStdLibVersioning(std::string s)61b89a7cc2SEnji Cooper inline std::string CanonicalizeForStdLibVersioning(std::string s) {
62b89a7cc2SEnji Cooper   static const char prefix[] = "std::__";
63b89a7cc2SEnji Cooper   if (s.compare(0, strlen(prefix), prefix) == 0) {
64b89a7cc2SEnji Cooper     std::string::size_type end = s.find("::", strlen(prefix));
65b89a7cc2SEnji Cooper     if (end != s.npos) {
66b89a7cc2SEnji Cooper       // Erase everything between the initial `std` and the second `::`.
67b89a7cc2SEnji Cooper       s.erase(strlen("std"), end - strlen("std"));
68b89a7cc2SEnji Cooper     }
69b89a7cc2SEnji Cooper   }
7028f6c2f2SEnji Cooper 
7128f6c2f2SEnji Cooper   // Strip redundant spaces in typename to match MSVC
7228f6c2f2SEnji Cooper   // For example, std::pair<int, bool> -> std::pair<int,bool>
7328f6c2f2SEnji Cooper   static const char to_search[] = ", ";
7428f6c2f2SEnji Cooper   static const char replace_str[] = ",";
7528f6c2f2SEnji Cooper   size_t pos = 0;
7628f6c2f2SEnji Cooper   while (true) {
7728f6c2f2SEnji Cooper     // Get the next occurrence from the current position
7828f6c2f2SEnji Cooper     pos = s.find(to_search, pos);
7928f6c2f2SEnji Cooper     if (pos == std::string::npos) {
8028f6c2f2SEnji Cooper       break;
8128f6c2f2SEnji Cooper     }
8228f6c2f2SEnji Cooper     // Replace this occurrence of substring
8328f6c2f2SEnji Cooper     s.replace(pos, strlen(to_search), replace_str);
8428f6c2f2SEnji Cooper     pos += strlen(replace_str);
8528f6c2f2SEnji Cooper   }
86b89a7cc2SEnji Cooper   return s;
87b89a7cc2SEnji Cooper }
88b89a7cc2SEnji Cooper 
89b89a7cc2SEnji Cooper #if GTEST_HAS_RTTI
9028f6c2f2SEnji Cooper // GetTypeName(const std::type_info&) returns a human-readable name of type T.
GetTypeName(const std::type_info & type)9128f6c2f2SEnji Cooper inline std::string GetTypeName(const std::type_info& type) {
9228f6c2f2SEnji Cooper   const char* const name = type.name();
93b89a7cc2SEnji Cooper #if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
94b89a7cc2SEnji Cooper   int status = 0;
95b89a7cc2SEnji Cooper   // gcc's implementation of typeid(T).name() mangles the type name,
96b89a7cc2SEnji Cooper   // so we have to demangle it.
97b89a7cc2SEnji Cooper #if GTEST_HAS_CXXABI_H_
98b89a7cc2SEnji Cooper   using abi::__cxa_demangle;
99b89a7cc2SEnji Cooper #endif  // GTEST_HAS_CXXABI_H_
10028f6c2f2SEnji Cooper   char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status);
101b89a7cc2SEnji Cooper   const std::string name_str(status == 0 ? readable_name : name);
102b89a7cc2SEnji Cooper   free(readable_name);
103b89a7cc2SEnji Cooper   return CanonicalizeForStdLibVersioning(name_str);
10428f6c2f2SEnji Cooper #elif defined(_MSC_VER)
10528f6c2f2SEnji Cooper   // Strip struct and class due to differences between
10628f6c2f2SEnji Cooper   // MSVC and other compilers. std::pair<int,bool> is printed as
10728f6c2f2SEnji Cooper   // "struct std::pair<int,bool>" when using MSVC vs "std::pair<int, bool>" with
10828f6c2f2SEnji Cooper   // other compilers.
10928f6c2f2SEnji Cooper   std::string s = name;
11028f6c2f2SEnji Cooper   // Only strip the leading "struct " and "class ", so uses rfind == 0 to
11128f6c2f2SEnji Cooper   // ensure that
11228f6c2f2SEnji Cooper   if (s.rfind("struct ", 0) == 0) {
11328f6c2f2SEnji Cooper     s = s.substr(strlen("struct "));
11428f6c2f2SEnji Cooper   } else if (s.rfind("class ", 0) == 0) {
11528f6c2f2SEnji Cooper     s = s.substr(strlen("class "));
11628f6c2f2SEnji Cooper   }
11728f6c2f2SEnji Cooper   return s;
118b89a7cc2SEnji Cooper #else
119b89a7cc2SEnji Cooper   return name;
120b89a7cc2SEnji Cooper #endif  // GTEST_HAS_CXXABI_H_ || __HP_aCC
12128f6c2f2SEnji Cooper }
12228f6c2f2SEnji Cooper #endif  // GTEST_HAS_RTTI
123b89a7cc2SEnji Cooper 
12428f6c2f2SEnji Cooper // GetTypeName<T>() returns a human-readable name of type T if and only if
12528f6c2f2SEnji Cooper // RTTI is enabled, otherwise it returns a dummy type name.
12628f6c2f2SEnji Cooper // NB: This function is also used in Google Mock, so don't move it inside of
12728f6c2f2SEnji Cooper // the typed-test-only section below.
12828f6c2f2SEnji Cooper template <typename T>
GetTypeName()12928f6c2f2SEnji Cooper std::string GetTypeName() {
13028f6c2f2SEnji Cooper #if GTEST_HAS_RTTI
13128f6c2f2SEnji Cooper   return GetTypeName(typeid(T));
132b89a7cc2SEnji Cooper #else
133b89a7cc2SEnji Cooper   return "<type>";
134b89a7cc2SEnji Cooper #endif  // GTEST_HAS_RTTI
135b89a7cc2SEnji Cooper }
136b89a7cc2SEnji Cooper 
13728f6c2f2SEnji Cooper // A unique type indicating an empty node
138b89a7cc2SEnji Cooper struct None {};
139b89a7cc2SEnji Cooper 
14028f6c2f2SEnji Cooper #define GTEST_TEMPLATE_ \
14128f6c2f2SEnji Cooper   template <typename T> \
14228f6c2f2SEnji Cooper   class
143b89a7cc2SEnji Cooper 
144b89a7cc2SEnji Cooper // The template "selector" struct TemplateSel<Tmpl> is used to
145b89a7cc2SEnji Cooper // represent Tmpl, which must be a class template with one type
146b89a7cc2SEnji Cooper // parameter, as a type.  TemplateSel<Tmpl>::Bind<T>::type is defined
147b89a7cc2SEnji Cooper // as the type Tmpl<T>.  This allows us to actually instantiate the
148b89a7cc2SEnji Cooper // template "selected" by TemplateSel<Tmpl>.
149b89a7cc2SEnji Cooper //
150b89a7cc2SEnji Cooper // This trick is necessary for simulating typedef for class templates,
151b89a7cc2SEnji Cooper // which C++ doesn't support directly.
152b89a7cc2SEnji Cooper template <GTEST_TEMPLATE_ Tmpl>
153b89a7cc2SEnji Cooper struct TemplateSel {
154b89a7cc2SEnji Cooper   template <typename T>
155b89a7cc2SEnji Cooper   struct Bind {
156b89a7cc2SEnji Cooper     typedef Tmpl<T> type;
157b89a7cc2SEnji Cooper   };
158b89a7cc2SEnji Cooper };
159b89a7cc2SEnji Cooper 
16028f6c2f2SEnji Cooper #define GTEST_BIND_(TmplSel, T) TmplSel::template Bind<T>::type
161b89a7cc2SEnji Cooper 
16228f6c2f2SEnji Cooper template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_>
163b89a7cc2SEnji Cooper struct Templates {
16428f6c2f2SEnji Cooper   using Head = TemplateSel<Head_>;
16528f6c2f2SEnji Cooper   using Tail = Templates<Tail_...>;
166b89a7cc2SEnji Cooper };
167b89a7cc2SEnji Cooper 
16828f6c2f2SEnji Cooper template <GTEST_TEMPLATE_ Head_>
16928f6c2f2SEnji Cooper struct Templates<Head_> {
17028f6c2f2SEnji Cooper   using Head = TemplateSel<Head_>;
17128f6c2f2SEnji Cooper   using Tail = None;
172b89a7cc2SEnji Cooper };
173b89a7cc2SEnji Cooper 
17428f6c2f2SEnji Cooper // Tuple-like type lists
17528f6c2f2SEnji Cooper template <typename Head_, typename... Tail_>
17628f6c2f2SEnji Cooper struct Types {
17728f6c2f2SEnji Cooper   using Head = Head_;
17828f6c2f2SEnji Cooper   using Tail = Types<Tail_...>;
17928f6c2f2SEnji Cooper };
180b89a7cc2SEnji Cooper 
18128f6c2f2SEnji Cooper template <typename Head_>
18228f6c2f2SEnji Cooper struct Types<Head_> {
18328f6c2f2SEnji Cooper   using Head = Head_;
18428f6c2f2SEnji Cooper   using Tail = None;
18528f6c2f2SEnji Cooper };
18628f6c2f2SEnji Cooper 
18728f6c2f2SEnji Cooper // Helper metafunctions to tell apart a single type from types
18828f6c2f2SEnji Cooper // generated by ::testing::Types
18928f6c2f2SEnji Cooper template <typename... Ts>
19028f6c2f2SEnji Cooper struct ProxyTypeList {
19128f6c2f2SEnji Cooper   using type = Types<Ts...>;
19228f6c2f2SEnji Cooper };
19328f6c2f2SEnji Cooper 
19428f6c2f2SEnji Cooper template <typename>
19528f6c2f2SEnji Cooper struct is_proxy_type_list : std::false_type {};
19628f6c2f2SEnji Cooper 
19728f6c2f2SEnji Cooper template <typename... Ts>
19828f6c2f2SEnji Cooper struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {};
19928f6c2f2SEnji Cooper 
20028f6c2f2SEnji Cooper // Generator which conditionally creates type lists.
20128f6c2f2SEnji Cooper // It recognizes if a requested type list should be created
20228f6c2f2SEnji Cooper // and prevents creating a new type list nested within another one.
203b89a7cc2SEnji Cooper template <typename T>
20428f6c2f2SEnji Cooper struct GenerateTypeList {
20528f6c2f2SEnji Cooper  private:
20628f6c2f2SEnji Cooper   using proxy = typename std::conditional<is_proxy_type_list<T>::value, T,
20728f6c2f2SEnji Cooper                                           ProxyTypeList<T>>::type;
208b89a7cc2SEnji Cooper 
20928f6c2f2SEnji Cooper  public:
21028f6c2f2SEnji Cooper   using type = typename proxy::type;
211b89a7cc2SEnji Cooper };
212b89a7cc2SEnji Cooper 
213b89a7cc2SEnji Cooper }  // namespace internal
21428f6c2f2SEnji Cooper 
21528f6c2f2SEnji Cooper template <typename... Ts>
21628f6c2f2SEnji Cooper using Types = internal::ProxyTypeList<Ts...>;
21728f6c2f2SEnji Cooper 
218b89a7cc2SEnji Cooper }  // namespace testing
219b89a7cc2SEnji Cooper 
22028f6c2f2SEnji Cooper #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
221