1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Google Test - The Google C++ Testing and Mocking Framework
32 // This file implements a universal value printer that can print a
33 // value of any type T:
35 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
37 // A user can teach this function how to print a class type T by
38 // defining either operator<<() or PrintTo() in the namespace that
39 // defines T. More specifically, the FIRST defined function in the
40 // following list will be used (assuming T is defined in namespace
43 // 1. foo::PrintTo(const T&, ostream*)
44 // 2. operator<<(ostream&, const T&) defined in either foo or the
47 // However if T is an STL-style container then it is printed element-wise
48 // unless foo::PrintTo(const T&, ostream*) is defined. Note that
49 // operator<<() is ignored for container types.
51 // If none of the above is defined, it will print the debug string of
52 // the value if it is a protocol buffer, or print the raw bytes in the
55 // To aid debugging: when T is a reference type, the address of the
56 // value is also printed; when T is a (const) char pointer, both the
57 // pointer value and the NUL-terminated string it points to are
60 // We also provide some convenient wrappers:
62 // // Prints a value to a string. For a (const or not) char
63 // // pointer, the NUL-terminated string (but not the pointer) is
65 // std::string ::testing::PrintToString(const T& value);
67 // // Prints a value tersely: for a reference type, the referenced
68 // // value (but not the address) is printed; for a (const or not) char
69 // // pointer, the NUL-terminated string (but not the pointer) is
71 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
73 // // Prints value using the type inferred by the compiler. The difference
74 // // from UniversalTersePrint() is that this function prints both the
75 // // pointer and the NUL-terminated string for a (const or not) char pointer.
76 // void ::testing::internal::UniversalPrint(const T& value, ostream*);
78 // // Prints the fields of a tuple tersely to a string vector, one
79 // // element for each field. Tuple support must be enabled in
81 // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
82 // const Tuple& value);
86 // The print primitives print the elements of an STL-style container
87 // using the compiler-inferred type of *iter where iter is a
88 // const_iterator of the container. When const_iterator is an input
89 // iterator but not a forward iterator, this inferred type may not
90 // match value_type, and the print output may be incorrect. In
91 // practice, this is rarely a problem as for most containers
92 // const_iterator is a forward iterator. We'll fix this if there's an
93 // actual need for it. Note that this fix cannot rely on value_type
94 // being defined as many user-defined container types don't have
97 // IWYU pragma: private, include "gtest/gtest.h"
98 // IWYU pragma: friend gtest/.*
99 // IWYU pragma: friend gmock/.*
101 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
102 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
104 #include <functional>
106 #include <ostream> // NOLINT
110 #include <type_traits>
114 #include "gtest/internal/gtest-internal.h"
115 #include "gtest/internal/gtest-port.h"
119 // Definitions in the internal* namespaces are subject to change without notice.
120 // DO NOT USE THEM IN USER CODE!
123 template <typename T>
124 void UniversalPrint(const T& value, ::std::ostream* os);
126 // Used to print an STL-style container when the user doesn't define
127 // a PrintTo() for it.
128 struct ContainerPrinter {
129 template <typename T,
130 typename = typename std::enable_if<
131 (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
132 !IsRecursiveContainer<T>::value>::type>
133 static void PrintValue(const T& container, std::ostream* os) {
134 const size_t kMaxCount = 32; // The maximum number of elements to print.
137 for (auto&& elem : container) {
140 if (count == kMaxCount) { // Enough has been printed.
146 // We cannot call PrintTo(elem, os) here as PrintTo() doesn't
147 // handle `elem` being a native array.
148 internal::UniversalPrint(elem, os);
159 // Used to print a pointer that is neither a char pointer nor a member
160 // pointer, when the user doesn't define PrintTo() for it. (A member
161 // variable pointer or member function pointer doesn't really point to
162 // a location in the address space. Their representation is
163 // implementation-defined. Therefore they will be printed as raw
165 struct FunctionPointerPrinter {
166 template <typename T, typename = typename std::enable_if<
167 std::is_function<T>::value>::type>
168 static void PrintValue(T* p, ::std::ostream* os) {
172 // T is a function type, so '*os << p' doesn't do what we want
173 // (it just prints p as bool). We want to print p as a const
175 *os << reinterpret_cast<const void*>(p);
180 struct PointerPrinter {
181 template <typename T>
182 static void PrintValue(T* p, ::std::ostream* os) {
186 // T is not a function type. We just call << to print p,
187 // relying on ADL to pick up user-defined << for their pointer
194 namespace internal_stream_operator_without_lexical_name_lookup {
196 // The presence of an operator<< here will terminate lexical scope lookup
197 // straight away (even though it cannot be a match because of its argument
198 // types). Thus, the two operator<< calls in StreamPrinter will find only ADL
200 struct LookupBlocker {};
201 void operator<<(LookupBlocker, LookupBlocker);
203 struct StreamPrinter {
204 template <typename T,
205 // Don't accept member pointers here. We'd print them via implicit
206 // conversion to bool, which isn't useful.
207 typename = typename std::enable_if<
208 !std::is_member_pointer<T>::value>::type,
209 // Only accept types for which we can find a streaming operator via
210 // ADL (possibly involving implicit conversions).
211 typename = decltype(std::declval<std::ostream&>()
212 << std::declval<const T&>())>
213 static void PrintValue(const T& value, ::std::ostream* os) {
214 // Call streaming operator found by ADL, possibly with implicit conversions
220 } // namespace internal_stream_operator_without_lexical_name_lookup
222 struct ProtobufPrinter {
223 // We print a protobuf using its ShortDebugString() when the string
224 // doesn't exceed this many characters; otherwise we print it using
225 // DebugString() for better readability.
226 static const size_t kProtobufOneLinerMaxLength = 50;
228 template <typename T,
229 typename = typename std::enable_if<
230 internal::HasDebugStringAndShortDebugString<T>::value>::type>
231 static void PrintValue(const T& value, ::std::ostream* os) {
232 std::string pretty_str = value.ShortDebugString();
233 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
234 pretty_str = "\n" + value.DebugString();
236 *os << ("<" + pretty_str + ">");
240 struct ConvertibleToIntegerPrinter {
241 // Since T has no << operator or PrintTo() but can be implicitly
242 // converted to BiggestInt, we print it as a BiggestInt.
244 // Most likely T is an enum type (either named or unnamed), in which
245 // case printing it as an integer is the desired behavior. In case
246 // T is not an enum, printing it as an integer is the best we can do
247 // given that it has no user-defined printer.
248 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
253 struct ConvertibleToStringViewPrinter {
254 #if GTEST_INTERNAL_HAS_STRING_VIEW
255 static void PrintValue(internal::StringView value, ::std::ostream* os) {
256 internal::UniversalPrint(value, os);
261 // Prints the given number of bytes in the given object to the given
263 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
264 size_t count, ::std::ostream* os);
265 struct RawBytesPrinter {
266 // SFINAE on `sizeof` to make sure we have a complete type.
267 template <typename T, size_t = sizeof(T)>
268 static void PrintValue(const T& value, ::std::ostream* os) {
269 PrintBytesInObjectTo(
270 static_cast<const unsigned char*>(
271 // Load bearing cast to void* to support iOS
272 reinterpret_cast<const void*>(std::addressof(value))),
277 struct FallbackPrinter {
278 template <typename T>
279 static void PrintValue(const T&, ::std::ostream* os) {
280 *os << "(incomplete type)";
284 // Try every printer in order and return the first one that works.
285 template <typename T, typename E, typename Printer, typename... Printers>
286 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
288 template <typename T, typename Printer, typename... Printers>
289 struct FindFirstPrinter<
290 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
291 Printer, Printers...> {
292 using type = Printer;
295 // Select the best printer in the following order:
296 // - Print containers (they have begin/end/etc).
297 // - Print function pointers.
298 // - Print object pointers.
299 // - Use the stream operator, if available.
300 // - Print protocol buffers.
301 // - Print types convertible to BiggestInt.
302 // - Print types convertible to StringView, if available.
303 // - Fallback to printing the raw bytes of the object.
304 template <typename T>
305 void PrintWithFallback(const T& value, ::std::ostream* os) {
306 using Printer = typename FindFirstPrinter<
307 T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
308 internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
309 ProtobufPrinter, ConvertibleToIntegerPrinter,
310 ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type;
311 Printer::PrintValue(value, os);
314 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
315 // value of type ToPrint that is an operand of a comparison assertion
316 // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
317 // the comparison, and is used to help determine the best way to
318 // format the value. In particular, when the value is a C string
319 // (char pointer) and the other operand is an STL string object, we
320 // want to format the C string as a string, since we know it is
321 // compared by value with the string object. If the value is a char
322 // pointer but the other operand is not an STL string object, we don't
323 // know whether the pointer is supposed to point to a NUL-terminated
324 // string, and thus want to print it as a pointer to be safe.
326 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
329 template <typename ToPrint, typename OtherOperand>
330 class FormatForComparison {
332 static ::std::string Format(const ToPrint& value) {
333 return ::testing::PrintToString(value);
338 template <typename ToPrint, size_t N, typename OtherOperand>
339 class FormatForComparison<ToPrint[N], OtherOperand> {
341 static ::std::string Format(const ToPrint* value) {
342 return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
346 // By default, print C string as pointers to be safe, as we don't know
347 // whether they actually point to a NUL-terminated string.
349 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
350 template <typename OtherOperand> \
351 class FormatForComparison<CharType*, OtherOperand> { \
353 static ::std::string Format(CharType* value) { \
354 return ::testing::PrintToString(static_cast<const void*>(value)); \
358 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
359 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
360 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
361 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
362 #ifdef __cpp_lib_char8_t
363 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
364 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
366 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
367 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
368 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
369 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
371 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
373 // If a C string is compared with an STL string object, we know it's meant
374 // to point to a NUL-terminated string, and thus can print it as a string.
376 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
378 class FormatForComparison<CharType*, OtherStringType> { \
380 static ::std::string Format(CharType* value) { \
381 return ::testing::PrintToString(value); \
385 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
386 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
388 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
389 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
391 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
392 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
393 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
394 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
396 #if GTEST_HAS_STD_WSTRING
397 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
398 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
401 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
403 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
404 // operand to be used in a failure message. The type (but not value)
405 // of the other operand may affect the format. This allows us to
406 // print a char* as a raw pointer when it is compared against another
407 // char* or void*, and print it as a C string when it is compared
408 // against an std::string object, for example.
410 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
411 template <typename T1, typename T2>
412 std::string FormatForComparisonFailureMessage(const T1& value,
413 const T2& /* other_operand */) {
414 return FormatForComparison<T1, T2>::Format(value);
417 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
418 // value to the given ostream. The caller must ensure that
419 // 'ostream_ptr' is not NULL, or the behavior is undefined.
421 // We define UniversalPrinter as a class template (as opposed to a
422 // function template), as we need to partially specialize it for
423 // reference types, which cannot be done with function templates.
424 template <typename T>
425 class UniversalPrinter;
427 // Prints the given value using the << operator if it has one;
428 // otherwise prints the bytes in it. This is what
429 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
430 // or overloaded for type T.
432 // A user can override this behavior for a class type Foo by defining
433 // an overload of PrintTo() in the namespace where Foo is defined. We
434 // give the user this option as sometimes defining a << operator for
435 // Foo is not desirable (e.g. the coding style may prevent doing it,
436 // or there is already a << operator but it doesn't do what the user
438 template <typename T>
439 void PrintTo(const T& value, ::std::ostream* os) {
440 internal::PrintWithFallback(value, os);
443 // The following list of PrintTo() overloads tells
444 // UniversalPrinter<T>::Print() how to print standard types (built-in
445 // types, strings, plain arrays, and pointers).
447 // Overloads for various char types.
448 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
449 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
450 inline void PrintTo(char c, ::std::ostream* os) {
451 // When printing a plain char, we always treat it as unsigned. This
452 // way, the output won't be affected by whether the compiler thinks
453 // char is signed or not.
454 PrintTo(static_cast<unsigned char>(c), os);
457 // Overloads for other simple built-in types.
458 inline void PrintTo(bool x, ::std::ostream* os) {
459 *os << (x ? "true" : "false");
462 // Overload for wchar_t type.
463 // Prints a wchar_t as a symbol if it is printable or as its internal
464 // code otherwise and also as its decimal code (except for L'\0').
465 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
466 // as signed integer when wchar_t is implemented by the compiler
467 // as a signed type and is printed as an unsigned integer when wchar_t
468 // is implemented as an unsigned type.
469 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
471 GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
472 inline void PrintTo(char16_t c, ::std::ostream* os) {
473 PrintTo(ImplicitCast_<char32_t>(c), os);
476 inline void PrintTo(char8_t c, ::std::ostream* os) {
477 PrintTo(ImplicitCast_<char32_t>(c), os);
481 // gcc/clang __{u,}int128_t
482 #if defined(__SIZEOF_INT128__)
483 GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os);
484 GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os);
485 #endif // __SIZEOF_INT128__
487 // Overloads for C strings.
488 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
489 inline void PrintTo(char* s, ::std::ostream* os) {
490 PrintTo(ImplicitCast_<const char*>(s), os);
493 // signed/unsigned char is often used for representing binary data, so
494 // we print pointers to it as void* to be safe.
495 inline void PrintTo(const signed char* s, ::std::ostream* os) {
496 PrintTo(ImplicitCast_<const void*>(s), os);
498 inline void PrintTo(signed char* s, ::std::ostream* os) {
499 PrintTo(ImplicitCast_<const void*>(s), os);
501 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
502 PrintTo(ImplicitCast_<const void*>(s), os);
504 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
505 PrintTo(ImplicitCast_<const void*>(s), os);
508 // Overloads for u8 strings.
509 GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
510 inline void PrintTo(char8_t* s, ::std::ostream* os) {
511 PrintTo(ImplicitCast_<const char8_t*>(s), os);
514 // Overloads for u16 strings.
515 GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os);
516 inline void PrintTo(char16_t* s, ::std::ostream* os) {
517 PrintTo(ImplicitCast_<const char16_t*>(s), os);
519 // Overloads for u32 strings.
520 GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os);
521 inline void PrintTo(char32_t* s, ::std::ostream* os) {
522 PrintTo(ImplicitCast_<const char32_t*>(s), os);
525 // MSVC can be configured to define wchar_t as a typedef of unsigned
526 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
527 // type. When wchar_t is a typedef, defining an overload for const
528 // wchar_t* would cause unsigned short* be printed as a wide string,
529 // possibly causing invalid memory accesses.
530 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
531 // Overloads for wide C strings
532 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
533 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
534 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
538 // Overload for C arrays. Multi-dimensional arrays are printed
541 // Prints the given number of elements in an array, without printing
543 template <typename T>
544 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
545 UniversalPrint(a[0], os);
546 for (size_t i = 1; i != count; i++) {
548 UniversalPrint(a[i], os);
552 // Overloads for ::std::string.
553 GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os);
554 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
555 PrintStringTo(s, os);
558 // Overloads for ::std::u8string
560 GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
561 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
562 PrintU8StringTo(s, os);
566 // Overloads for ::std::u16string
567 GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
568 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
569 PrintU16StringTo(s, os);
572 // Overloads for ::std::u32string
573 GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
574 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
575 PrintU32StringTo(s, os);
578 // Overloads for ::std::wstring.
579 #if GTEST_HAS_STD_WSTRING
580 GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os);
581 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
582 PrintWideStringTo(s, os);
584 #endif // GTEST_HAS_STD_WSTRING
586 #if GTEST_INTERNAL_HAS_STRING_VIEW
587 // Overload for internal::StringView.
588 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
589 PrintTo(::std::string(sp), os);
591 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
593 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
596 inline void PrintTo(const std::type_info& info, std::ostream* os) {
597 *os << internal::GetTypeName(info);
599 #endif // GTEST_HAS_RTTI
601 template <typename T>
602 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
603 UniversalPrinter<T&>::Print(ref.get(), os);
606 inline const void* VoidifyPointer(const void* p) { return p; }
607 inline const void* VoidifyPointer(volatile const void* p) {
608 return const_cast<const void*>(p);
611 template <typename T, typename Ptr>
612 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
613 if (ptr == nullptr) {
616 // We can't print the value. Just print the pointer..
617 *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
620 template <typename T, typename Ptr,
621 typename = typename std::enable_if<!std::is_void<T>::value &&
622 !std::is_array<T>::value>::type>
623 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
624 if (ptr == nullptr) {
627 *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
628 UniversalPrinter<T>::Print(*ptr, os);
633 template <typename T, typename D>
634 void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
635 (PrintSmartPointer<T>)(ptr, os, 0);
638 template <typename T>
639 void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
640 (PrintSmartPointer<T>)(ptr, os, 0);
643 // Helper function for printing a tuple. T must be instantiated with
645 template <typename T>
646 void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
649 template <typename T, size_t I>
650 void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
651 ::std::ostream* os) {
652 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
653 GTEST_INTENTIONAL_CONST_COND_PUSH_()
655 GTEST_INTENTIONAL_CONST_COND_POP_()
658 UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print(
659 std::get<I - 1>(t), os);
662 template <typename... Types>
663 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
665 PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
669 // Overload for std::pair.
670 template <typename T1, typename T2>
671 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
673 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
674 // a reference type. The same for printing value.second.
675 UniversalPrinter<T1>::Print(value.first, os);
677 UniversalPrinter<T2>::Print(value.second, os);
681 // Implements printing a non-reference type T by letting the compiler
682 // pick the right overload of PrintTo() for T.
683 template <typename T>
684 class UniversalPrinter {
686 // MSVC warns about adding const to a function type, so we want to
687 // disable the warning.
688 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
690 // Note: we deliberately don't call this PrintTo(), as that name
691 // conflicts with ::testing::internal::PrintTo in the body of the
693 static void Print(const T& value, ::std::ostream* os) {
694 // By default, ::testing::internal::PrintTo() is used for printing
697 // Thanks to Koenig look-up, if T is a class and has its own
698 // PrintTo() function defined in its namespace, that function will
699 // be visible here. Since it is more specific than the generic ones
700 // in ::testing::internal, it will be picked by the compiler in the
701 // following statement - exactly what we want.
705 GTEST_DISABLE_MSC_WARNINGS_POP_()
708 // Remove any const-qualifiers before passing a type to UniversalPrinter.
709 template <typename T>
710 class UniversalPrinter<const T> : public UniversalPrinter<T> {};
712 #if GTEST_INTERNAL_HAS_ANY
714 // Printer for std::any / absl::any
717 class UniversalPrinter<Any> {
719 static void Print(const Any& value, ::std::ostream* os) {
720 if (value.has_value()) {
721 *os << "value of type " << GetTypeName(value);
728 static std::string GetTypeName(const Any& value) {
730 return internal::GetTypeName(value.type());
732 static_cast<void>(value); // possibly unused
733 return "<unknown_type>";
734 #endif // GTEST_HAS_RTTI
738 #endif // GTEST_INTERNAL_HAS_ANY
740 #if GTEST_INTERNAL_HAS_OPTIONAL
742 // Printer for std::optional / absl::optional
744 template <typename T>
745 class UniversalPrinter<Optional<T>> {
747 static void Print(const Optional<T>& value, ::std::ostream* os) {
752 UniversalPrint(*value, os);
759 class UniversalPrinter<decltype(Nullopt())> {
761 static void Print(decltype(Nullopt()), ::std::ostream* os) {
766 #endif // GTEST_INTERNAL_HAS_OPTIONAL
768 #if GTEST_INTERNAL_HAS_VARIANT
770 // Printer for std::variant / absl::variant
772 template <typename... T>
773 class UniversalPrinter<Variant<T...>> {
775 static void Print(const Variant<T...>& value, ::std::ostream* os) {
778 absl::visit(Visitor{os, value.index()}, value);
780 std::visit(Visitor{os, value.index()}, value);
781 #endif // GTEST_HAS_ABSL
787 template <typename U>
788 void operator()(const U& u) const {
789 *os << "'" << GetTypeName<U>() << "(index = " << index
791 UniversalPrint(u, os);
798 #endif // GTEST_INTERNAL_HAS_VARIANT
800 // UniversalPrintArray(begin, len, os) prints an array of 'len'
801 // elements, starting at address 'begin'.
802 template <typename T>
803 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
808 const size_t kThreshold = 18;
809 const size_t kChunkSize = 8;
810 // If the array has more than kThreshold elements, we'll have to
811 // omit some details by printing only the first and the last
812 // kChunkSize elements.
813 if (len <= kThreshold) {
814 PrintRawArrayTo(begin, len, os);
816 PrintRawArrayTo(begin, kChunkSize, os);
818 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
823 // This overload prints a (const) char array compactly.
824 GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
828 // This overload prints a (const) char8_t array compactly.
829 GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
833 // This overload prints a (const) char16_t array compactly.
834 GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
837 // This overload prints a (const) char32_t array compactly.
838 GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
841 // This overload prints a (const) wchar_t array compactly.
842 GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len,
845 // Implements printing an array type T[N].
846 template <typename T, size_t N>
847 class UniversalPrinter<T[N]> {
849 // Prints the given array, omitting some elements when there are too
851 static void Print(const T (&a)[N], ::std::ostream* os) {
852 UniversalPrintArray(a, N, os);
856 // Implements printing a reference type T&.
857 template <typename T>
858 class UniversalPrinter<T&> {
860 // MSVC warns about adding const to a function type, so we want to
861 // disable the warning.
862 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
864 static void Print(const T& value, ::std::ostream* os) {
865 // Prints the address of the value. We use reinterpret_cast here
866 // as static_cast doesn't compile when T is a function type.
867 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
869 // Then prints the value itself.
870 UniversalPrint(value, os);
873 GTEST_DISABLE_MSC_WARNINGS_POP_()
876 // Prints a value tersely: for a reference type, the referenced value
877 // (but not the address) is printed; for a (const) char pointer, the
878 // NUL-terminated string (but not the pointer) is printed.
880 template <typename T>
881 class UniversalTersePrinter {
883 static void Print(const T& value, ::std::ostream* os) {
884 UniversalPrint(value, os);
887 template <typename T>
888 class UniversalTersePrinter<T&> {
890 static void Print(const T& value, ::std::ostream* os) {
891 UniversalPrint(value, os);
894 template <typename T, size_t N>
895 class UniversalTersePrinter<T[N]> {
897 static void Print(const T (&value)[N], ::std::ostream* os) {
898 UniversalPrinter<T[N]>::Print(value, os);
902 class UniversalTersePrinter<const char*> {
904 static void Print(const char* str, ::std::ostream* os) {
905 if (str == nullptr) {
908 UniversalPrint(std::string(str), os);
913 class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> {
918 class UniversalTersePrinter<const char8_t*> {
920 static void Print(const char8_t* str, ::std::ostream* os) {
921 if (str == nullptr) {
924 UniversalPrint(::std::u8string(str), os);
929 class UniversalTersePrinter<char8_t*>
930 : public UniversalTersePrinter<const char8_t*> {};
934 class UniversalTersePrinter<const char16_t*> {
936 static void Print(const char16_t* str, ::std::ostream* os) {
937 if (str == nullptr) {
940 UniversalPrint(::std::u16string(str), os);
945 class UniversalTersePrinter<char16_t*>
946 : public UniversalTersePrinter<const char16_t*> {};
949 class UniversalTersePrinter<const char32_t*> {
951 static void Print(const char32_t* str, ::std::ostream* os) {
952 if (str == nullptr) {
955 UniversalPrint(::std::u32string(str), os);
960 class UniversalTersePrinter<char32_t*>
961 : public UniversalTersePrinter<const char32_t*> {};
963 #if GTEST_HAS_STD_WSTRING
965 class UniversalTersePrinter<const wchar_t*> {
967 static void Print(const wchar_t* str, ::std::ostream* os) {
968 if (str == nullptr) {
971 UniversalPrint(::std::wstring(str), os);
978 class UniversalTersePrinter<wchar_t*> {
980 static void Print(wchar_t* str, ::std::ostream* os) {
981 UniversalTersePrinter<const wchar_t*>::Print(str, os);
985 template <typename T>
986 void UniversalTersePrint(const T& value, ::std::ostream* os) {
987 UniversalTersePrinter<T>::Print(value, os);
990 // Prints a value using the type inferred by the compiler. The
991 // difference between this and UniversalTersePrint() is that for a
992 // (const) char pointer, this prints both the pointer and the
993 // NUL-terminated string.
994 template <typename T>
995 void UniversalPrint(const T& value, ::std::ostream* os) {
996 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
997 // UniversalPrinter with T directly.
999 UniversalPrinter<T1>::Print(value, os);
1002 typedef ::std::vector<::std::string> Strings;
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>,
1009 template <typename Tuple, size_t I>
1010 void TersePrintPrefixToStrings(const Tuple& t,
1011 std::integral_constant<size_t, I>,
1013 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1015 ::std::stringstream ss;
1016 UniversalTersePrint(std::get<I - 1>(t), &ss);
1017 strings->push_back(ss.str());
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) {
1026 TersePrintPrefixToStrings(
1027 value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(),
1032 } // namespace internal
1034 template <typename T>
1035 ::std::string PrintToString(const T& value) {
1036 ::std::stringstream ss;
1037 internal::UniversalTersePrinter<T>::Print(value, &ss);
1041 } // namespace testing
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"
1048 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_