Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:43:10

0001 // Copyright 2007, 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 // Google Test - The Google C++ Testing and Mocking Framework
0031 //
0032 // This file implements a universal value printer that can print a
0033 // value of any type T:
0034 //
0035 //   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
0036 //
0037 // A user can teach this function how to print a class type T by
0038 // defining either operator<<() or PrintTo() in the namespace that
0039 // defines T.  More specifically, the FIRST defined function in the
0040 // following list will be used (assuming T is defined in namespace
0041 // foo):
0042 //
0043 //   1. foo::PrintTo(const T&, ostream*)
0044 //   2. operator<<(ostream&, const T&) defined in either foo or the
0045 //      global namespace.
0046 //
0047 // However if T is an STL-style container then it is printed element-wise
0048 // unless foo::PrintTo(const T&, ostream*) is defined. Note that
0049 // operator<<() is ignored for container types.
0050 //
0051 // If none of the above is defined, it will print the debug string of
0052 // the value if it is a protocol buffer, or print the raw bytes in the
0053 // value otherwise.
0054 //
0055 // To aid debugging: when T is a reference type, the address of the
0056 // value is also printed; when T is a (const) char pointer, both the
0057 // pointer value and the NUL-terminated string it points to are
0058 // printed.
0059 //
0060 // We also provide some convenient wrappers:
0061 //
0062 //   // Prints a value to a string.  For a (const or not) char
0063 //   // pointer, the NUL-terminated string (but not the pointer) is
0064 //   // printed.
0065 //   std::string ::testing::PrintToString(const T& value);
0066 //
0067 //   // Prints a value tersely: for a reference type, the referenced
0068 //   // value (but not the address) is printed; for a (const or not) char
0069 //   // pointer, the NUL-terminated string (but not the pointer) is
0070 //   // printed.
0071 //   void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
0072 //
0073 //   // Prints value using the type inferred by the compiler.  The difference
0074 //   // from UniversalTersePrint() is that this function prints both the
0075 //   // pointer and the NUL-terminated string for a (const or not) char pointer.
0076 //   void ::testing::internal::UniversalPrint(const T& value, ostream*);
0077 //
0078 //   // Prints the fields of a tuple tersely to a string vector, one
0079 //   // element for each field. Tuple support must be enabled in
0080 //   // gtest-port.h.
0081 //   std::vector<string> UniversalTersePrintTupleFieldsToStrings(
0082 //       const Tuple& value);
0083 //
0084 // Known limitation:
0085 //
0086 // The print primitives print the elements of an STL-style container
0087 // using the compiler-inferred type of *iter where iter is a
0088 // const_iterator of the container.  When const_iterator is an input
0089 // iterator but not a forward iterator, this inferred type may not
0090 // match value_type, and the print output may be incorrect.  In
0091 // practice, this is rarely a problem as for most containers
0092 // const_iterator is a forward iterator.  We'll fix this if there's an
0093 // actual need for it.  Note that this fix cannot rely on value_type
0094 // being defined as many user-defined container types don't have
0095 // value_type.
0096 
0097 // IWYU pragma: private, include "gtest/gtest.h"
0098 // IWYU pragma: friend gtest/.*
0099 // IWYU pragma: friend gmock/.*
0100 
0101 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0102 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0103 
0104 #include <functional>
0105 #include <memory>
0106 #include <ostream>  // NOLINT
0107 #include <sstream>
0108 #include <string>
0109 #include <tuple>
0110 #include <type_traits>
0111 #include <utility>
0112 #include <vector>
0113 
0114 #include "gtest/internal/gtest-internal.h"
0115 #include "gtest/internal/gtest-port.h"
0116 
0117 namespace testing {
0118 
0119 // Definitions in the internal* namespaces are subject to change without notice.
0120 // DO NOT USE THEM IN USER CODE!
0121 namespace internal {
0122 
0123 template <typename T>
0124 void UniversalPrint(const T& value, ::std::ostream* os);
0125 
0126 // Used to print an STL-style container when the user doesn't define
0127 // a PrintTo() for it.
0128 struct ContainerPrinter {
0129   template <typename T,
0130             typename = typename std::enable_if<
0131                 (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
0132                 !IsRecursiveContainer<T>::value>::type>
0133   static void PrintValue(const T& container, std::ostream* os) {
0134     const size_t kMaxCount = 32;  // The maximum number of elements to print.
0135     *os << '{';
0136     size_t count = 0;
0137     for (auto&& elem : container) {
0138       if (count > 0) {
0139         *os << ',';
0140         if (count == kMaxCount) {  // Enough has been printed.
0141           *os << " ...";
0142           break;
0143         }
0144       }
0145       *os << ' ';
0146       // We cannot call PrintTo(elem, os) here as PrintTo() doesn't
0147       // handle `elem` being a native array.
0148       internal::UniversalPrint(elem, os);
0149       ++count;
0150     }
0151 
0152     if (count > 0) {
0153       *os << ' ';
0154     }
0155     *os << '}';
0156   }
0157 };
0158 
0159 // Used to print a pointer that is neither a char pointer nor a member
0160 // pointer, when the user doesn't define PrintTo() for it.  (A member
0161 // variable pointer or member function pointer doesn't really point to
0162 // a location in the address space.  Their representation is
0163 // implementation-defined.  Therefore they will be printed as raw
0164 // bytes.)
0165 struct FunctionPointerPrinter {
0166   template <typename T, typename = typename std::enable_if<
0167                             std::is_function<T>::value>::type>
0168   static void PrintValue(T* p, ::std::ostream* os) {
0169     if (p == nullptr) {
0170       *os << "NULL";
0171     } else {
0172       // T is a function type, so '*os << p' doesn't do what we want
0173       // (it just prints p as bool).  We want to print p as a const
0174       // void*.
0175       *os << reinterpret_cast<const void*>(p);
0176     }
0177   }
0178 };
0179 
0180 struct PointerPrinter {
0181   template <typename T>
0182   static void PrintValue(T* p, ::std::ostream* os) {
0183     if (p == nullptr) {
0184       *os << "NULL";
0185     } else {
0186       // T is not a function type.  We just call << to print p,
0187       // relying on ADL to pick up user-defined << for their pointer
0188       // types, if any.
0189       *os << p;
0190     }
0191   }
0192 };
0193 
0194 namespace internal_stream_operator_without_lexical_name_lookup {
0195 
0196 // The presence of an operator<< here will terminate lexical scope lookup
0197 // straight away (even though it cannot be a match because of its argument
0198 // types). Thus, the two operator<< calls in StreamPrinter will find only ADL
0199 // candidates.
0200 struct LookupBlocker {};
0201 void operator<<(LookupBlocker, LookupBlocker);
0202 
0203 struct StreamPrinter {
0204   template <typename T,
0205             // Don't accept member pointers here. We'd print them via implicit
0206             // conversion to bool, which isn't useful.
0207             typename = typename std::enable_if<
0208                 !std::is_member_pointer<T>::value>::type,
0209             // Only accept types for which we can find a streaming operator via
0210             // ADL (possibly involving implicit conversions).
0211             typename = decltype(std::declval<std::ostream&>()
0212                                 << std::declval<const T&>())>
0213   static void PrintValue(const T& value, ::std::ostream* os) {
0214     // Call streaming operator found by ADL, possibly with implicit conversions
0215     // of the arguments.
0216     *os << value;
0217   }
0218 };
0219 
0220 }  // namespace internal_stream_operator_without_lexical_name_lookup
0221 
0222 struct ProtobufPrinter {
0223   // We print a protobuf using its ShortDebugString() when the string
0224   // doesn't exceed this many characters; otherwise we print it using
0225   // DebugString() for better readability.
0226   static const size_t kProtobufOneLinerMaxLength = 50;
0227 
0228   template <typename T,
0229             typename = typename std::enable_if<
0230                 internal::HasDebugStringAndShortDebugString<T>::value>::type>
0231   static void PrintValue(const T& value, ::std::ostream* os) {
0232     std::string pretty_str = value.ShortDebugString();
0233     if (pretty_str.length() > kProtobufOneLinerMaxLength) {
0234       pretty_str = "\n" + value.DebugString();
0235     }
0236     *os << ("<" + pretty_str + ">");
0237   }
0238 };
0239 
0240 struct ConvertibleToIntegerPrinter {
0241   // Since T has no << operator or PrintTo() but can be implicitly
0242   // converted to BiggestInt, we print it as a BiggestInt.
0243   //
0244   // Most likely T is an enum type (either named or unnamed), in which
0245   // case printing it as an integer is the desired behavior.  In case
0246   // T is not an enum, printing it as an integer is the best we can do
0247   // given that it has no user-defined printer.
0248   static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
0249     *os << value;
0250   }
0251 };
0252 
0253 struct ConvertibleToStringViewPrinter {
0254 #if GTEST_INTERNAL_HAS_STRING_VIEW
0255   static void PrintValue(internal::StringView value, ::std::ostream* os) {
0256     internal::UniversalPrint(value, os);
0257   }
0258 #endif
0259 };
0260 
0261 // Prints the given number of bytes in the given object to the given
0262 // ostream.
0263 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
0264                                      size_t count, ::std::ostream* os);
0265 struct RawBytesPrinter {
0266   // SFINAE on `sizeof` to make sure we have a complete type.
0267   template <typename T, size_t = sizeof(T)>
0268   static void PrintValue(const T& value, ::std::ostream* os) {
0269     PrintBytesInObjectTo(
0270         static_cast<const unsigned char*>(
0271             // Load bearing cast to void* to support iOS
0272             reinterpret_cast<const void*>(std::addressof(value))),
0273         sizeof(value), os);
0274   }
0275 };
0276 
0277 struct FallbackPrinter {
0278   template <typename T>
0279   static void PrintValue(const T&, ::std::ostream* os) {
0280     *os << "(incomplete type)";
0281   }
0282 };
0283 
0284 // Try every printer in order and return the first one that works.
0285 template <typename T, typename E, typename Printer, typename... Printers>
0286 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
0287 
0288 template <typename T, typename Printer, typename... Printers>
0289 struct FindFirstPrinter<
0290     T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
0291     Printer, Printers...> {
0292   using type = Printer;
0293 };
0294 
0295 // Select the best printer in the following order:
0296 //  - Print containers (they have begin/end/etc).
0297 //  - Print function pointers.
0298 //  - Print object pointers.
0299 //  - Use the stream operator, if available.
0300 //  - Print protocol buffers.
0301 //  - Print types convertible to BiggestInt.
0302 //  - Print types convertible to StringView, if available.
0303 //  - Fallback to printing the raw bytes of the object.
0304 template <typename T>
0305 void PrintWithFallback(const T& value, ::std::ostream* os) {
0306   using Printer = typename FindFirstPrinter<
0307       T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
0308       internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
0309       ProtobufPrinter, ConvertibleToIntegerPrinter,
0310       ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type;
0311   Printer::PrintValue(value, os);
0312 }
0313 
0314 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
0315 // value of type ToPrint that is an operand of a comparison assertion
0316 // (e.g. ASSERT_EQ).  OtherOperand is the type of the other operand in
0317 // the comparison, and is used to help determine the best way to
0318 // format the value.  In particular, when the value is a C string
0319 // (char pointer) and the other operand is an STL string object, we
0320 // want to format the C string as a string, since we know it is
0321 // compared by value with the string object.  If the value is a char
0322 // pointer but the other operand is not an STL string object, we don't
0323 // know whether the pointer is supposed to point to a NUL-terminated
0324 // string, and thus want to print it as a pointer to be safe.
0325 //
0326 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
0327 
0328 // The default case.
0329 template <typename ToPrint, typename OtherOperand>
0330 class FormatForComparison {
0331  public:
0332   static ::std::string Format(const ToPrint& value) {
0333     return ::testing::PrintToString(value);
0334   }
0335 };
0336 
0337 // Array.
0338 template <typename ToPrint, size_t N, typename OtherOperand>
0339 class FormatForComparison<ToPrint[N], OtherOperand> {
0340  public:
0341   static ::std::string Format(const ToPrint* value) {
0342     return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
0343   }
0344 };
0345 
0346 // By default, print C string as pointers to be safe, as we don't know
0347 // whether they actually point to a NUL-terminated string.
0348 
0349 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType)                \
0350   template <typename OtherOperand>                                      \
0351   class FormatForComparison<CharType*, OtherOperand> {                  \
0352    public:                                                              \
0353     static ::std::string Format(CharType* value) {                      \
0354       return ::testing::PrintToString(static_cast<const void*>(value)); \
0355     }                                                                   \
0356   }
0357 
0358 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
0359 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
0360 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
0361 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
0362 #ifdef __cpp_lib_char8_t
0363 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
0364 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
0365 #endif
0366 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
0367 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
0368 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
0369 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
0370 
0371 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
0372 
0373 // If a C string is compared with an STL string object, we know it's meant
0374 // to point to a NUL-terminated string, and thus can print it as a string.
0375 
0376 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
0377   template <>                                                            \
0378   class FormatForComparison<CharType*, OtherStringType> {                \
0379    public:                                                               \
0380     static ::std::string Format(CharType* value) {                       \
0381       return ::testing::PrintToString(value);                            \
0382     }                                                                    \
0383   }
0384 
0385 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
0386 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
0387 #ifdef __cpp_char8_t
0388 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
0389 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
0390 #endif
0391 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
0392 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
0393 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
0394 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
0395 
0396 #if GTEST_HAS_STD_WSTRING
0397 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
0398 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
0399 #endif
0400 
0401 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
0402 
0403 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
0404 // operand to be used in a failure message.  The type (but not value)
0405 // of the other operand may affect the format.  This allows us to
0406 // print a char* as a raw pointer when it is compared against another
0407 // char* or void*, and print it as a C string when it is compared
0408 // against an std::string object, for example.
0409 //
0410 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
0411 template <typename T1, typename T2>
0412 std::string FormatForComparisonFailureMessage(const T1& value,
0413                                               const T2& /* other_operand */) {
0414   return FormatForComparison<T1, T2>::Format(value);
0415 }
0416 
0417 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
0418 // value to the given ostream.  The caller must ensure that
0419 // 'ostream_ptr' is not NULL, or the behavior is undefined.
0420 //
0421 // We define UniversalPrinter as a class template (as opposed to a
0422 // function template), as we need to partially specialize it for
0423 // reference types, which cannot be done with function templates.
0424 template <typename T>
0425 class UniversalPrinter;
0426 
0427 // Prints the given value using the << operator if it has one;
0428 // otherwise prints the bytes in it.  This is what
0429 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
0430 // or overloaded for type T.
0431 //
0432 // A user can override this behavior for a class type Foo by defining
0433 // an overload of PrintTo() in the namespace where Foo is defined.  We
0434 // give the user this option as sometimes defining a << operator for
0435 // Foo is not desirable (e.g. the coding style may prevent doing it,
0436 // or there is already a << operator but it doesn't do what the user
0437 // wants).
0438 template <typename T>
0439 void PrintTo(const T& value, ::std::ostream* os) {
0440   internal::PrintWithFallback(value, os);
0441 }
0442 
0443 // The following list of PrintTo() overloads tells
0444 // UniversalPrinter<T>::Print() how to print standard types (built-in
0445 // types, strings, plain arrays, and pointers).
0446 
0447 // Overloads for various char types.
0448 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
0449 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
0450 inline void PrintTo(char c, ::std::ostream* os) {
0451   // When printing a plain char, we always treat it as unsigned.  This
0452   // way, the output won't be affected by whether the compiler thinks
0453   // char is signed or not.
0454   PrintTo(static_cast<unsigned char>(c), os);
0455 }
0456 
0457 // Overloads for other simple built-in types.
0458 inline void PrintTo(bool x, ::std::ostream* os) {
0459   *os << (x ? "true" : "false");
0460 }
0461 
0462 // Overload for wchar_t type.
0463 // Prints a wchar_t as a symbol if it is printable or as its internal
0464 // code otherwise and also as its decimal code (except for L'\0').
0465 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
0466 // as signed integer when wchar_t is implemented by the compiler
0467 // as a signed type and is printed as an unsigned integer when wchar_t
0468 // is implemented as an unsigned type.
0469 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
0470 
0471 GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
0472 inline void PrintTo(char16_t c, ::std::ostream* os) {
0473   PrintTo(ImplicitCast_<char32_t>(c), os);
0474 }
0475 #ifdef __cpp_char8_t
0476 inline void PrintTo(char8_t c, ::std::ostream* os) {
0477   PrintTo(ImplicitCast_<char32_t>(c), os);
0478 }
0479 #endif
0480 
0481 // gcc/clang __{u,}int128_t
0482 #if defined(__SIZEOF_INT128__)
0483 GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os);
0484 GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os);
0485 #endif  // __SIZEOF_INT128__
0486 
0487 // Overloads for C strings.
0488 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
0489 inline void PrintTo(char* s, ::std::ostream* os) {
0490   PrintTo(ImplicitCast_<const char*>(s), os);
0491 }
0492 
0493 // signed/unsigned char is often used for representing binary data, so
0494 // we print pointers to it as void* to be safe.
0495 inline void PrintTo(const signed char* s, ::std::ostream* os) {
0496   PrintTo(ImplicitCast_<const void*>(s), os);
0497 }
0498 inline void PrintTo(signed char* s, ::std::ostream* os) {
0499   PrintTo(ImplicitCast_<const void*>(s), os);
0500 }
0501 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
0502   PrintTo(ImplicitCast_<const void*>(s), os);
0503 }
0504 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
0505   PrintTo(ImplicitCast_<const void*>(s), os);
0506 }
0507 #ifdef __cpp_char8_t
0508 // Overloads for u8 strings.
0509 GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
0510 inline void PrintTo(char8_t* s, ::std::ostream* os) {
0511   PrintTo(ImplicitCast_<const char8_t*>(s), os);
0512 }
0513 #endif
0514 // Overloads for u16 strings.
0515 GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os);
0516 inline void PrintTo(char16_t* s, ::std::ostream* os) {
0517   PrintTo(ImplicitCast_<const char16_t*>(s), os);
0518 }
0519 // Overloads for u32 strings.
0520 GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os);
0521 inline void PrintTo(char32_t* s, ::std::ostream* os) {
0522   PrintTo(ImplicitCast_<const char32_t*>(s), os);
0523 }
0524 
0525 // MSVC can be configured to define wchar_t as a typedef of unsigned
0526 // short.  It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
0527 // type.  When wchar_t is a typedef, defining an overload for const
0528 // wchar_t* would cause unsigned short* be printed as a wide string,
0529 // possibly causing invalid memory accesses.
0530 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
0531 // Overloads for wide C strings
0532 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
0533 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
0534   PrintTo(ImplicitCast_<const wchar_t*>(s), os);
0535 }
0536 #endif
0537 
0538 // Overload for C arrays.  Multi-dimensional arrays are printed
0539 // properly.
0540 
0541 // Prints the given number of elements in an array, without printing
0542 // the curly braces.
0543 template <typename T>
0544 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
0545   UniversalPrint(a[0], os);
0546   for (size_t i = 1; i != count; i++) {
0547     *os << ", ";
0548     UniversalPrint(a[i], os);
0549   }
0550 }
0551 
0552 // Overloads for ::std::string.
0553 GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os);
0554 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
0555   PrintStringTo(s, os);
0556 }
0557 
0558 // Overloads for ::std::u8string
0559 #ifdef __cpp_char8_t
0560 GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
0561 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
0562   PrintU8StringTo(s, os);
0563 }
0564 #endif
0565 
0566 // Overloads for ::std::u16string
0567 GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
0568 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
0569   PrintU16StringTo(s, os);
0570 }
0571 
0572 // Overloads for ::std::u32string
0573 GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
0574 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
0575   PrintU32StringTo(s, os);
0576 }
0577 
0578 // Overloads for ::std::wstring.
0579 #if GTEST_HAS_STD_WSTRING
0580 GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os);
0581 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
0582   PrintWideStringTo(s, os);
0583 }
0584 #endif  // GTEST_HAS_STD_WSTRING
0585 
0586 #if GTEST_INTERNAL_HAS_STRING_VIEW
0587 // Overload for internal::StringView.
0588 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
0589   PrintTo(::std::string(sp), os);
0590 }
0591 #endif  // GTEST_INTERNAL_HAS_STRING_VIEW
0592 
0593 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
0594 
0595 #if GTEST_HAS_RTTI
0596 inline void PrintTo(const std::type_info& info, std::ostream* os) {
0597   *os << internal::GetTypeName(info);
0598 }
0599 #endif  // GTEST_HAS_RTTI
0600 
0601 template <typename T>
0602 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
0603   UniversalPrinter<T&>::Print(ref.get(), os);
0604 }
0605 
0606 inline const void* VoidifyPointer(const void* p) { return p; }
0607 inline const void* VoidifyPointer(volatile const void* p) {
0608   return const_cast<const void*>(p);
0609 }
0610 
0611 template <typename T, typename Ptr>
0612 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
0613   if (ptr == nullptr) {
0614     *os << "(nullptr)";
0615   } else {
0616     // We can't print the value. Just print the pointer..
0617     *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
0618   }
0619 }
0620 template <typename T, typename Ptr,
0621           typename = typename std::enable_if<!std::is_void<T>::value &&
0622                                              !std::is_array<T>::value>::type>
0623 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
0624   if (ptr == nullptr) {
0625     *os << "(nullptr)";
0626   } else {
0627     *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
0628     UniversalPrinter<T>::Print(*ptr, os);
0629     *os << ")";
0630   }
0631 }
0632 
0633 template <typename T, typename D>
0634 void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
0635   (PrintSmartPointer<T>)(ptr, os, 0);
0636 }
0637 
0638 template <typename T>
0639 void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
0640   (PrintSmartPointer<T>)(ptr, os, 0);
0641 }
0642 
0643 // Helper function for printing a tuple.  T must be instantiated with
0644 // a tuple type.
0645 template <typename T>
0646 void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
0647                   ::std::ostream*) {}
0648 
0649 template <typename T, size_t I>
0650 void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
0651                   ::std::ostream* os) {
0652   PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
0653   GTEST_INTENTIONAL_CONST_COND_PUSH_()
0654   if (I > 1) {
0655     GTEST_INTENTIONAL_CONST_COND_POP_()
0656     *os << ", ";
0657   }
0658   UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print(
0659       std::get<I - 1>(t), os);
0660 }
0661 
0662 template <typename... Types>
0663 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
0664   *os << "(";
0665   PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
0666   *os << ")";
0667 }
0668 
0669 // Overload for std::pair.
0670 template <typename T1, typename T2>
0671 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
0672   *os << '(';
0673   // We cannot use UniversalPrint(value.first, os) here, as T1 may be
0674   // a reference type.  The same for printing value.second.
0675   UniversalPrinter<T1>::Print(value.first, os);
0676   *os << ", ";
0677   UniversalPrinter<T2>::Print(value.second, os);
0678   *os << ')';
0679 }
0680 
0681 // Implements printing a non-reference type T by letting the compiler
0682 // pick the right overload of PrintTo() for T.
0683 template <typename T>
0684 class UniversalPrinter {
0685  public:
0686   // MSVC warns about adding const to a function type, so we want to
0687   // disable the warning.
0688   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
0689 
0690   // Note: we deliberately don't call this PrintTo(), as that name
0691   // conflicts with ::testing::internal::PrintTo in the body of the
0692   // function.
0693   static void Print(const T& value, ::std::ostream* os) {
0694     // By default, ::testing::internal::PrintTo() is used for printing
0695     // the value.
0696     //
0697     // Thanks to Koenig look-up, if T is a class and has its own
0698     // PrintTo() function defined in its namespace, that function will
0699     // be visible here.  Since it is more specific than the generic ones
0700     // in ::testing::internal, it will be picked by the compiler in the
0701     // following statement - exactly what we want.
0702     PrintTo(value, os);
0703   }
0704 
0705   GTEST_DISABLE_MSC_WARNINGS_POP_()
0706 };
0707 
0708 // Remove any const-qualifiers before passing a type to UniversalPrinter.
0709 template <typename T>
0710 class UniversalPrinter<const T> : public UniversalPrinter<T> {};
0711 
0712 #if GTEST_INTERNAL_HAS_ANY
0713 
0714 // Printer for std::any / absl::any
0715 
0716 template <>
0717 class UniversalPrinter<Any> {
0718  public:
0719   static void Print(const Any& value, ::std::ostream* os) {
0720     if (value.has_value()) {
0721       *os << "value of type " << GetTypeName(value);
0722     } else {
0723       *os << "no value";
0724     }
0725   }
0726 
0727  private:
0728   static std::string GetTypeName(const Any& value) {
0729 #if GTEST_HAS_RTTI
0730     return internal::GetTypeName(value.type());
0731 #else
0732     static_cast<void>(value);  // possibly unused
0733     return "<unknown_type>";
0734 #endif  // GTEST_HAS_RTTI
0735   }
0736 };
0737 
0738 #endif  // GTEST_INTERNAL_HAS_ANY
0739 
0740 #if GTEST_INTERNAL_HAS_OPTIONAL
0741 
0742 // Printer for std::optional / absl::optional
0743 
0744 template <typename T>
0745 class UniversalPrinter<Optional<T>> {
0746  public:
0747   static void Print(const Optional<T>& value, ::std::ostream* os) {
0748     *os << '(';
0749     if (!value) {
0750       *os << "nullopt";
0751     } else {
0752       UniversalPrint(*value, os);
0753     }
0754     *os << ')';
0755   }
0756 };
0757 
0758 template <>
0759 class UniversalPrinter<decltype(Nullopt())> {
0760  public:
0761   static void Print(decltype(Nullopt()), ::std::ostream* os) {
0762     *os << "(nullopt)";
0763   }
0764 };
0765 
0766 #endif  // GTEST_INTERNAL_HAS_OPTIONAL
0767 
0768 #if GTEST_INTERNAL_HAS_VARIANT
0769 
0770 // Printer for std::variant / absl::variant
0771 
0772 template <typename... T>
0773 class UniversalPrinter<Variant<T...>> {
0774  public:
0775   static void Print(const Variant<T...>& value, ::std::ostream* os) {
0776     *os << '(';
0777 #if GTEST_HAS_ABSL
0778     absl::visit(Visitor{os, value.index()}, value);
0779 #else
0780     std::visit(Visitor{os, value.index()}, value);
0781 #endif  // GTEST_HAS_ABSL
0782     *os << ')';
0783   }
0784 
0785  private:
0786   struct Visitor {
0787     template <typename U>
0788     void operator()(const U& u) const {
0789       *os << "'" << GetTypeName<U>() << "(index = " << index
0790           << ")' with value ";
0791       UniversalPrint(u, os);
0792     }
0793     ::std::ostream* os;
0794     std::size_t index;
0795   };
0796 };
0797 
0798 #endif  // GTEST_INTERNAL_HAS_VARIANT
0799 
0800 // UniversalPrintArray(begin, len, os) prints an array of 'len'
0801 // elements, starting at address 'begin'.
0802 template <typename T>
0803 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
0804   if (len == 0) {
0805     *os << "{}";
0806   } else {
0807     *os << "{ ";
0808     const size_t kThreshold = 18;
0809     const size_t kChunkSize = 8;
0810     // If the array has more than kThreshold elements, we'll have to
0811     // omit some details by printing only the first and the last
0812     // kChunkSize elements.
0813     if (len <= kThreshold) {
0814       PrintRawArrayTo(begin, len, os);
0815     } else {
0816       PrintRawArrayTo(begin, kChunkSize, os);
0817       *os << ", ..., ";
0818       PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
0819     }
0820     *os << " }";
0821   }
0822 }
0823 // This overload prints a (const) char array compactly.
0824 GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
0825                                     ::std::ostream* os);
0826 
0827 #ifdef __cpp_char8_t
0828 // This overload prints a (const) char8_t array compactly.
0829 GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
0830                                     ::std::ostream* os);
0831 #endif
0832 
0833 // This overload prints a (const) char16_t array compactly.
0834 GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
0835                                     ::std::ostream* os);
0836 
0837 // This overload prints a (const) char32_t array compactly.
0838 GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
0839                                     ::std::ostream* os);
0840 
0841 // This overload prints a (const) wchar_t array compactly.
0842 GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len,
0843                                     ::std::ostream* os);
0844 
0845 // Implements printing an array type T[N].
0846 template <typename T, size_t N>
0847 class UniversalPrinter<T[N]> {
0848  public:
0849   // Prints the given array, omitting some elements when there are too
0850   // many.
0851   static void Print(const T (&a)[N], ::std::ostream* os) {
0852     UniversalPrintArray(a, N, os);
0853   }
0854 };
0855 
0856 // Implements printing a reference type T&.
0857 template <typename T>
0858 class UniversalPrinter<T&> {
0859  public:
0860   // MSVC warns about adding const to a function type, so we want to
0861   // disable the warning.
0862   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
0863 
0864   static void Print(const T& value, ::std::ostream* os) {
0865     // Prints the address of the value.  We use reinterpret_cast here
0866     // as static_cast doesn't compile when T is a function type.
0867     *os << "@" << reinterpret_cast<const void*>(&value) << " ";
0868 
0869     // Then prints the value itself.
0870     UniversalPrint(value, os);
0871   }
0872 
0873   GTEST_DISABLE_MSC_WARNINGS_POP_()
0874 };
0875 
0876 // Prints a value tersely: for a reference type, the referenced value
0877 // (but not the address) is printed; for a (const) char pointer, the
0878 // NUL-terminated string (but not the pointer) is printed.
0879 
0880 template <typename T>
0881 class UniversalTersePrinter {
0882  public:
0883   static void Print(const T& value, ::std::ostream* os) {
0884     UniversalPrint(value, os);
0885   }
0886 };
0887 template <typename T>
0888 class UniversalTersePrinter<T&> {
0889  public:
0890   static void Print(const T& value, ::std::ostream* os) {
0891     UniversalPrint(value, os);
0892   }
0893 };
0894 template <typename T, size_t N>
0895 class UniversalTersePrinter<T[N]> {
0896  public:
0897   static void Print(const T (&value)[N], ::std::ostream* os) {
0898     UniversalPrinter<T[N]>::Print(value, os);
0899   }
0900 };
0901 template <>
0902 class UniversalTersePrinter<const char*> {
0903  public:
0904   static void Print(const char* str, ::std::ostream* os) {
0905     if (str == nullptr) {
0906       *os << "NULL";
0907     } else {
0908       UniversalPrint(std::string(str), os);
0909     }
0910   }
0911 };
0912 template <>
0913 class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> {
0914 };
0915 
0916 #ifdef __cpp_char8_t
0917 template <>
0918 class UniversalTersePrinter<const char8_t*> {
0919  public:
0920   static void Print(const char8_t* str, ::std::ostream* os) {
0921     if (str == nullptr) {
0922       *os << "NULL";
0923     } else {
0924       UniversalPrint(::std::u8string(str), os);
0925     }
0926   }
0927 };
0928 template <>
0929 class UniversalTersePrinter<char8_t*>
0930     : public UniversalTersePrinter<const char8_t*> {};
0931 #endif
0932 
0933 template <>
0934 class UniversalTersePrinter<const char16_t*> {
0935  public:
0936   static void Print(const char16_t* str, ::std::ostream* os) {
0937     if (str == nullptr) {
0938       *os << "NULL";
0939     } else {
0940       UniversalPrint(::std::u16string(str), os);
0941     }
0942   }
0943 };
0944 template <>
0945 class UniversalTersePrinter<char16_t*>
0946     : public UniversalTersePrinter<const char16_t*> {};
0947 
0948 template <>
0949 class UniversalTersePrinter<const char32_t*> {
0950  public:
0951   static void Print(const char32_t* str, ::std::ostream* os) {
0952     if (str == nullptr) {
0953       *os << "NULL";
0954     } else {
0955       UniversalPrint(::std::u32string(str), os);
0956     }
0957   }
0958 };
0959 template <>
0960 class UniversalTersePrinter<char32_t*>
0961     : public UniversalTersePrinter<const char32_t*> {};
0962 
0963 #if GTEST_HAS_STD_WSTRING
0964 template <>
0965 class UniversalTersePrinter<const wchar_t*> {
0966  public:
0967   static void Print(const wchar_t* str, ::std::ostream* os) {
0968     if (str == nullptr) {
0969       *os << "NULL";
0970     } else {
0971       UniversalPrint(::std::wstring(str), os);
0972     }
0973   }
0974 };
0975 #endif
0976 
0977 template <>
0978 class UniversalTersePrinter<wchar_t*> {
0979  public:
0980   static void Print(wchar_t* str, ::std::ostream* os) {
0981     UniversalTersePrinter<const wchar_t*>::Print(str, os);
0982   }
0983 };
0984 
0985 template <typename T>
0986 void UniversalTersePrint(const T& value, ::std::ostream* os) {
0987   UniversalTersePrinter<T>::Print(value, os);
0988 }
0989 
0990 // Prints a value using the type inferred by the compiler.  The
0991 // difference between this and UniversalTersePrint() is that for a
0992 // (const) char pointer, this prints both the pointer and the
0993 // NUL-terminated string.
0994 template <typename T>
0995 void UniversalPrint(const T& value, ::std::ostream* os) {
0996   // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
0997   // UniversalPrinter with T directly.
0998   typedef T T1;
0999   UniversalPrinter<T1>::Print(value, os);
1000 }
1001 
1002 typedef ::std::vector<::std::string> Strings;
1003 
1004 // Tersely prints the first N fields of a tuple to a string vector,
1005 // one element for each field.
1006 template <typename Tuple>
1007 void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
1008                                Strings*) {}
1009 template <typename Tuple, size_t I>
1010 void TersePrintPrefixToStrings(const Tuple& t,
1011                                std::integral_constant<size_t, I>,
1012                                Strings* strings) {
1013   TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1014                             strings);
1015   ::std::stringstream ss;
1016   UniversalTersePrint(std::get<I - 1>(t), &ss);
1017   strings->push_back(ss.str());
1018 }
1019 
1020 // Prints the fields of a tuple tersely to a string vector, one
1021 // element for each field.  See the comment before
1022 // UniversalTersePrint() for how we define "tersely".
1023 template <typename Tuple>
1024 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
1025   Strings result;
1026   TersePrintPrefixToStrings(
1027       value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(),
1028       &result);
1029   return result;
1030 }
1031 
1032 }  // namespace internal
1033 
1034 template <typename T>
1035 ::std::string PrintToString(const T& value) {
1036   ::std::stringstream ss;
1037   internal::UniversalTersePrinter<T>::Print(value, &ss);
1038   return ss.str();
1039 }
1040 
1041 }  // namespace testing
1042 
1043 // Include any custom printer added by the local installation.
1044 // We must include this header at the end to make sure it can use the
1045 // declarations from this file.
1046 #include "gtest/internal/custom/gtest-printers.h"
1047 
1048 #endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_