Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / external / googletest / googletest / include / gtest / internal / gtest-type-util.h
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 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
34 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
35
36 #include "gtest/internal/gtest-port.h"
37
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_
41 #  include <cxxabi.h>
42 # elif defined(__HP_aCC)
43 #  include <acxx_demangle.h>
44 # endif  // GTEST_HASH_CXXABI_H_
45
46 namespace testing {
47 namespace internal {
48
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));
57     if (end != s.npos) {
58       // Erase everything between the initial `std` and the second `::`.
59       s.erase(strlen("std"), end - strlen("std"));
60     }
61   }
62   return s;
63 }
64
65 #if GTEST_HAS_RTTI
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)
70   int status = 0;
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);
78   free(readable_name);
79   return CanonicalizeForStdLibVersioning(name_str);
80 #else
81   return name;
82 #endif  // GTEST_HAS_CXXABI_H_ || __HP_aCC
83 }
84 #endif  // GTEST_HAS_RTTI
85
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.
90 template <typename T>
91 std::string GetTypeName() {
92 #if GTEST_HAS_RTTI
93   return GetTypeName(typeid(T));
94 #else
95   return "<type>";
96 #endif  // GTEST_HAS_RTTI
97 }
98
99 // A unique type indicating an empty node
100 struct None {};
101
102 # define GTEST_TEMPLATE_ template <typename T> class
103
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>.
109 //
110 // This trick is necessary for simulating typedef for class templates,
111 // which C++ doesn't support directly.
112 template <GTEST_TEMPLATE_ Tmpl>
113 struct TemplateSel {
114   template <typename T>
115   struct Bind {
116     typedef Tmpl<T> type;
117   };
118 };
119
120 # define GTEST_BIND_(TmplSel, T) \
121   TmplSel::template Bind<T>::type
122
123 template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_>
124 struct Templates {
125   using Head = TemplateSel<Head_>;
126   using Tail = Templates<Tail_...>;
127 };
128
129 template <GTEST_TEMPLATE_ Head_>
130 struct Templates<Head_> {
131   using Head = TemplateSel<Head_>;
132   using Tail = None;
133 };
134
135 // Tuple-like type lists
136 template <typename Head_, typename... Tail_>
137 struct Types {
138   using Head = Head_;
139   using Tail = Types<Tail_...>;
140 };
141
142 template <typename Head_>
143 struct Types<Head_> {
144   using Head = Head_;
145   using Tail = None;
146 };
147
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...>;
153 };
154
155 template <typename>
156 struct is_proxy_type_list : std::false_type {};
157
158 template <typename... Ts>
159 struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {};
160
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 {
166  private:
167   using proxy = typename std::conditional<is_proxy_type_list<T>::value, T,
168                                           ProxyTypeList<T>>::type;
169
170  public:
171   using type = typename proxy::type;
172 };
173
174 }  // namespace internal
175
176 template <typename... Ts>
177 using Types = internal::ProxyTypeList<Ts...>;
178
179 }  // namespace testing
180
181 #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_