Xmipp  v3.23.11-Nereus
gtest-type-util.h
Go to the documentation of this file.
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 // IWYU pragma: private, include "gtest/gtest.h"
34 // IWYU pragma: friend gtest/.*
35 // IWYU pragma: friend gmock/.*
36 
37 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
38 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
39 
40 #include <string>
41 #include <type_traits>
42 #include <typeinfo>
43 
45 
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_
49 #include <cxxabi.h>
50 #elif defined(__HP_aCC)
51 #include <acxx_demangle.h>
52 #endif // GTEST_HASH_CXXABI_H_
53 
54 namespace testing {
55 namespace internal {
56 
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));
65  if (end != s.npos) {
66  // Erase everything between the initial `std` and the second `::`.
67  s.erase(strlen("std"), end - strlen("std"));
68  }
69  }
70  return s;
71 }
72 
73 #if GTEST_HAS_RTTI
74 // GetTypeName(const std::type_info&) returns a human-readable name of type T.
75 inline std::string GetTypeName(const std::type_info& type) {
76  const char* const name = type.name();
77 #if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
78  int status = 0;
79  // gcc's implementation of typeid(T).name() mangles the type name,
80  // so we have to demangle it.
81 #if GTEST_HAS_CXXABI_H_
82  using abi::__cxa_demangle;
83 #endif // GTEST_HAS_CXXABI_H_
84  char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status);
85  const std::string name_str(status == 0 ? readable_name : name);
86  free(readable_name);
87  return CanonicalizeForStdLibVersioning(name_str);
88 #else
89  return name;
90 #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
91 }
92 #endif // GTEST_HAS_RTTI
93 
94 // GetTypeName<T>() returns a human-readable name of type T if and only if
95 // RTTI is enabled, otherwise it returns a dummy type name.
96 // NB: This function is also used in Google Mock, so don't move it inside of
97 // the typed-test-only section below.
98 template <typename T>
99 std::string GetTypeName() {
100 #if GTEST_HAS_RTTI
101  return GetTypeName(typeid(T));
102 #else
103  return "<type>";
104 #endif // GTEST_HAS_RTTI
105 }
106 
107 // A unique type indicating an empty node
108 struct None {};
109 
110 #define GTEST_TEMPLATE_ \
111  template <typename T> \
112  class
113 
114 // The template "selector" struct TemplateSel<Tmpl> is used to
115 // represent Tmpl, which must be a class template with one type
116 // parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined
117 // as the type Tmpl<T>. This allows us to actually instantiate the
118 // template "selected" by TemplateSel<Tmpl>.
119 //
120 // This trick is necessary for simulating typedef for class templates,
121 // which C++ doesn't support directly.
122 template <GTEST_TEMPLATE_ Tmpl>
123 struct TemplateSel {
124  template <typename T>
125  struct Bind {
126  typedef Tmpl<T> type;
127  };
128 };
129 
130 #define GTEST_BIND_(TmplSel, T) TmplSel::template Bind<T>::type
131 
132 template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_>
133 struct Templates {
135  using Tail = Templates<Tail_...>;
136 };
137 
138 template <GTEST_TEMPLATE_ Head_>
139 struct Templates<Head_> {
141  using Tail = None;
142 };
143 
144 // Tuple-like type lists
145 template <typename Head_, typename... Tail_>
146 struct Types {
147  using Head = Head_;
148  using Tail = Types<Tail_...>;
149 };
150 
151 template <typename Head_>
152 struct Types<Head_> {
153  using Head = Head_;
154  using Tail = None;
155 };
156 
157 // Helper metafunctions to tell apart a single type from types
158 // generated by ::testing::Types
159 template <typename... Ts>
161  using type = Types<Ts...>;
162 };
163 
164 template <typename>
165 struct is_proxy_type_list : std::false_type {};
166 
167 template <typename... Ts>
168 struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {};
169 
170 // Generator which conditionally creates type lists.
171 // It recognizes if a requested type list should be created
172 // and prevents creating a new type list nested within another one.
173 template <typename T>
175  private:
176  using proxy = typename std::conditional<is_proxy_type_list<T>::value, T,
178 
179  public:
180  using type = typename proxy::type;
181 };
182 
183 } // namespace internal
184 
185 template <typename... Ts>
187 
188 } // namespace testing
189 
190 #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
#define GTEST_TEMPLATE_
std::string GetTypeName()
std::string CanonicalizeForStdLibVersioning(std::string s)
viol type
free((char *) ob)