Lab_1 0.1.1
Matrix Library
Loading...
Searching...
No Matches
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// 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 "gtest/internal/gtest-port.h"
41
42// #ifdef __GNUC__ is too general here. It is possible to use gcc without using
43// libstdc++ (which is where cxxabi.h comes from).
44#if GTEST_HAS_CXXABI_H_
45#include <cxxabi.h>
46#elif defined(__HP_aCC)
47#include <acxx_demangle.h>
48#endif // GTEST_HASH_CXXABI_H_
49
50namespace testing {
51namespace internal {
52
53// Canonicalizes a given name with respect to the Standard C++ Library.
54// This handles removing the inline namespace within `std` that is
55// used by various standard libraries (e.g., `std::__1`). Names outside
56// of namespace std are returned unmodified.
57inline std::string CanonicalizeForStdLibVersioning(std::string s) {
58 static const char prefix[] = "std::__";
59 if (s.compare(0, strlen(prefix), prefix) == 0) {
60 std::string::size_type end = s.find("::", strlen(prefix));
61 if (end != s.npos) {
62 // Erase everything between the initial `std` and the second `::`.
63 s.erase(strlen("std"), end - strlen("std"));
64 }
65 }
66 return s;
67}
68
69#if GTEST_HAS_RTTI
70// GetTypeName(const std::type_info&) returns a human-readable name of type T.
71inline std::string GetTypeName(const std::type_info& type) {
72 const char* const name = type.name();
73#if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
74 int status = 0;
75 // gcc's implementation of typeid(T).name() mangles the type name,
76 // so we have to demangle it.
77#if GTEST_HAS_CXXABI_H_
78 using abi::__cxa_demangle;
79#endif // GTEST_HAS_CXXABI_H_
80 char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status);
81 const std::string name_str(status == 0 ? readable_name : name);
82 free(readable_name);
83 return CanonicalizeForStdLibVersioning(name_str);
84#else
85 return name;
86#endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
87}
88#endif // GTEST_HAS_RTTI
89
90// GetTypeName<T>() returns a human-readable name of type T if and only if
91// RTTI is enabled, otherwise it returns a dummy type name.
92// NB: This function is also used in Google Mock, so don't move it inside of
93// the typed-test-only section below.
94template <typename T>
95std::string GetTypeName() {
96#if GTEST_HAS_RTTI
97 return GetTypeName(typeid(T));
98#else
99 return "<type>";
100#endif // GTEST_HAS_RTTI
101}
102
103// A unique type indicating an empty node
104struct None {};
105
106#define GTEST_TEMPLATE_ \
107 template <typename T> \
108 class
109
110// The template "selector" struct TemplateSel<Tmpl> is used to
111// represent Tmpl, which must be a class template with one type
112// parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined
113// as the type Tmpl<T>. This allows us to actually instantiate the
114// template "selected" by TemplateSel<Tmpl>.
115//
116// This trick is necessary for simulating typedef for class templates,
117// which C++ doesn't support directly.
118template <GTEST_TEMPLATE_ Tmpl>
120 template <typename T>
121 struct Bind {
122 typedef Tmpl<T> type;
123 };
124};
125
126#define GTEST_BIND_(TmplSel, T) TmplSel::template Bind<T>::type
127
128template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_>
129struct Templates {
130 using Head = TemplateSel<Head_>;
131 using Tail = Templates<Tail_...>;
132};
133
134template <GTEST_TEMPLATE_ Head_>
135struct Templates<Head_> {
136 using Head = TemplateSel<Head_>;
137 using Tail = None;
138};
139
140// Tuple-like type lists
141template <typename Head_, typename... Tail_>
142struct Types {
143 using Head = Head_;
144 using Tail = Types<Tail_...>;
145};
146
147template <typename Head_>
148struct Types<Head_> {
149 using Head = Head_;
150 using Tail = None;
151};
152
153// Helper metafunctions to tell apart a single type from types
154// generated by ::testing::Types
155template <typename... Ts>
157 using type = Types<Ts...>;
158};
159
160template <typename>
161struct is_proxy_type_list : std::false_type {};
162
163template <typename... Ts>
164struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {};
165
166// Generator which conditionally creates type lists.
167// It recognizes if a requested type list should be created
168// and prevents creating a new type list nested within another one.
169template <typename T>
171 private:
172 using proxy = typename std::conditional<is_proxy_type_list<T>::value, T,
173 ProxyTypeList<T>>::type;
174
175 public:
176 using type = typename proxy::type;
177};
178
179} // namespace internal
180
181template <typename... Ts>
182using Types = internal::ProxyTypeList<Ts...>;
183
184} // namespace testing
185
186#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
Definition gtest-type-util.h:170
Definition gtest-type-util.h:104
Definition gtest-type-util.h:156
Definition gtest-type-util.h:121
Definition gtest-type-util.h:119
Definition gtest-type-util.h:129
Definition gtest-type-util.h:142
Definition gtest-type-util.h:161