|
||||
File indexing completed on 2025-01-30 10:11:23
0001 // Copyright 2008 Google Inc. 0002 // All Rights Reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 0030 // Type utilities needed for implementing typed and type-parameterized 0031 // tests. 0032 0033 // IWYU pragma: private, include "gtest/gtest.h" 0034 // IWYU pragma: friend gtest/.* 0035 // IWYU pragma: friend gmock/.* 0036 0037 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ 0038 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ 0039 0040 #include "gtest/internal/gtest-port.h" 0041 0042 // #ifdef __GNUC__ is too general here. It is possible to use gcc without using 0043 // libstdc++ (which is where cxxabi.h comes from). 0044 #if GTEST_HAS_CXXABI_H_ 0045 #include <cxxabi.h> 0046 #elif defined(__HP_aCC) 0047 #include <acxx_demangle.h> 0048 #endif // GTEST_HASH_CXXABI_H_ 0049 0050 namespace testing { 0051 namespace internal { 0052 0053 // Canonicalizes a given name with respect to the Standard C++ Library. 0054 // This handles removing the inline namespace within `std` that is 0055 // used by various standard libraries (e.g., `std::__1`). Names outside 0056 // of namespace std are returned unmodified. 0057 inline std::string CanonicalizeForStdLibVersioning(std::string s) { 0058 static const char prefix[] = "std::__"; 0059 if (s.compare(0, strlen(prefix), prefix) == 0) { 0060 std::string::size_type end = s.find("::", strlen(prefix)); 0061 if (end != s.npos) { 0062 // Erase everything between the initial `std` and the second `::`. 0063 s.erase(strlen("std"), end - strlen("std")); 0064 } 0065 } 0066 return s; 0067 } 0068 0069 #if GTEST_HAS_RTTI 0070 // GetTypeName(const std::type_info&) returns a human-readable name of type T. 0071 inline std::string GetTypeName(const std::type_info& type) { 0072 const char* const name = type.name(); 0073 #if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC) 0074 int status = 0; 0075 // gcc's implementation of typeid(T).name() mangles the type name, 0076 // so we have to demangle it. 0077 #if GTEST_HAS_CXXABI_H_ 0078 using abi::__cxa_demangle; 0079 #endif // GTEST_HAS_CXXABI_H_ 0080 char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status); 0081 const std::string name_str(status == 0 ? readable_name : name); 0082 free(readable_name); 0083 return CanonicalizeForStdLibVersioning(name_str); 0084 #else 0085 return name; 0086 #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC 0087 } 0088 #endif // GTEST_HAS_RTTI 0089 0090 // GetTypeName<T>() returns a human-readable name of type T if and only if 0091 // RTTI is enabled, otherwise it returns a dummy type name. 0092 // NB: This function is also used in Google Mock, so don't move it inside of 0093 // the typed-test-only section below. 0094 template <typename T> 0095 std::string GetTypeName() { 0096 #if GTEST_HAS_RTTI 0097 return GetTypeName(typeid(T)); 0098 #else 0099 return "<type>"; 0100 #endif // GTEST_HAS_RTTI 0101 } 0102 0103 // A unique type indicating an empty node 0104 struct None {}; 0105 0106 #define GTEST_TEMPLATE_ \ 0107 template <typename T> \ 0108 class 0109 0110 // The template "selector" struct TemplateSel<Tmpl> is used to 0111 // represent Tmpl, which must be a class template with one type 0112 // parameter, as a type. TemplateSel<Tmpl>::Bind<T>::type is defined 0113 // as the type Tmpl<T>. This allows us to actually instantiate the 0114 // template "selected" by TemplateSel<Tmpl>. 0115 // 0116 // This trick is necessary for simulating typedef for class templates, 0117 // which C++ doesn't support directly. 0118 template <GTEST_TEMPLATE_ Tmpl> 0119 struct TemplateSel { 0120 template <typename T> 0121 struct Bind { 0122 typedef Tmpl<T> type; 0123 }; 0124 }; 0125 0126 #define GTEST_BIND_(TmplSel, T) TmplSel::template Bind<T>::type 0127 0128 template <GTEST_TEMPLATE_ Head_, GTEST_TEMPLATE_... Tail_> 0129 struct Templates { 0130 using Head = TemplateSel<Head_>; 0131 using Tail = Templates<Tail_...>; 0132 }; 0133 0134 template <GTEST_TEMPLATE_ Head_> 0135 struct Templates<Head_> { 0136 using Head = TemplateSel<Head_>; 0137 using Tail = None; 0138 }; 0139 0140 // Tuple-like type lists 0141 template <typename Head_, typename... Tail_> 0142 struct Types { 0143 using Head = Head_; 0144 using Tail = Types<Tail_...>; 0145 }; 0146 0147 template <typename Head_> 0148 struct Types<Head_> { 0149 using Head = Head_; 0150 using Tail = None; 0151 }; 0152 0153 // Helper metafunctions to tell apart a single type from types 0154 // generated by ::testing::Types 0155 template <typename... Ts> 0156 struct ProxyTypeList { 0157 using type = Types<Ts...>; 0158 }; 0159 0160 template <typename> 0161 struct is_proxy_type_list : std::false_type {}; 0162 0163 template <typename... Ts> 0164 struct is_proxy_type_list<ProxyTypeList<Ts...>> : std::true_type {}; 0165 0166 // Generator which conditionally creates type lists. 0167 // It recognizes if a requested type list should be created 0168 // and prevents creating a new type list nested within another one. 0169 template <typename T> 0170 struct GenerateTypeList { 0171 private: 0172 using proxy = typename std::conditional<is_proxy_type_list<T>::value, T, 0173 ProxyTypeList<T>>::type; 0174 0175 public: 0176 using type = typename proxy::type; 0177 }; 0178 0179 } // namespace internal 0180 0181 template <typename... Ts> 0182 using Types = internal::ProxyTypeList<Ts...>; 0183 0184 } // namespace testing 0185 0186 #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |