100#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
101#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
109#include <type_traits>
113#include "gtest/internal/gtest-internal.h"
114#include "gtest/internal/gtest-port.h"
128void UniversalPrint(
const T& value, ::std::ostream* os);
133 template <
typename T,
134 typename =
typename std::enable_if<
135 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(IsContainer)) &&
137 static void PrintValue(
const T& container, std::ostream* os) {
138 const size_t kMaxCount = 32;
141 for (
auto&& elem : container) {
144 if (count == kMaxCount) {
152 internal::UniversalPrint(elem, os);
170 template <
typename T,
typename =
typename std::enable_if<
171 std::is_function<T>::value>::type>
172 static void PrintValue(T* p, ::std::ostream* os) {
179 *os << reinterpret_cast<const void*>(p);
185 template <
typename T>
186 static void PrintValue(T* p, ::std::ostream* os) {
198namespace internal_stream_operator_without_lexical_name_lookup {
208 template <
typename T,
211 typename =
typename std::enable_if<
212 !std::is_member_pointer<T>::value>::type,
215 typename =
decltype(std::declval<std::ostream&>()
216 << std::declval<const T&>())>
217 static void PrintValue(
const T& value, ::std::ostream* os) {
230 static const size_t kProtobufOneLinerMaxLength = 50;
232 template <
typename T,
233 typename =
typename std::enable_if<
235 static void PrintValue(
const T& value, ::std::ostream* os) {
236 std::string pretty_str = value.ShortDebugString();
237 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
238 pretty_str =
"\n" + value.DebugString();
240 *os << (
"<" + pretty_str +
">");
252 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
258#if GTEST_INTERNAL_HAS_STRING_VIEW
259 static void PrintValue(internal::StringView value, ::std::ostream* os) {
260 internal::UniversalPrint(value, os);
268GTEST_API_
void PrintBytesInObjectTo(
const unsigned char* obj_bytes,
273 template <
typename T,
size_t = sizeof(T)>
274 static void PrintValue(
const T& value, ::std::ostream* os) {
275 PrintBytesInObjectTo(
276 static_cast<const unsigned char*
>(
278 reinterpret_cast<const void*
>(std::addressof(value))),
284 template <
typename T>
285 static void PrintValue(
const T&, ::std::ostream* os) {
286 *os <<
"(incomplete type)";
291template <
typename T,
typename E,
typename Printer,
typename... Printers>
294template <
typename T,
typename Printer,
typename... Printers>
296 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
297 Printer, Printers...> {
298 using type = Printer;
311void PrintWithFallback(
const T& value, ::std::ostream* os) {
317 Printer::PrintValue(value, os);
335template <
typename ToPr
int,
typename OtherOperand>
338 static ::std::string Format(
const ToPrint& value) {
339 return ::testing::PrintToString(value);
344template <
typename ToPr
int,
size_t N,
typename OtherOperand>
347 static ::std::string Format(
const ToPrint* value) {
355#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
356 template <typename OtherOperand> \
357 class FormatForComparison<CharType*, OtherOperand> { \
359 static ::std::string Format(CharType* value) { \
360 return ::testing::PrintToString(static_cast<const void*>(value)); \
364GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char);
365GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char);
366GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
wchar_t);
367GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const wchar_t);
369GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char8_t);
370GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char8_t);
372GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char16_t);
373GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char16_t);
374GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char32_t);
375GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char32_t);
377#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
382#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
384 class FormatForComparison<CharType*, OtherStringType> { \
386 static ::std::string Format(CharType* value) { \
387 return ::testing::PrintToString(value); \
391GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char, ::std::string);
392GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char, ::std::string);
394GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char8_t, ::std::u8string);
395GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char8_t, ::std::u8string);
397GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char16_t, ::std::u16string);
398GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char16_t, ::std::u16string);
399GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char32_t, ::std::u32string);
400GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char32_t, ::std::u32string);
402#if GTEST_HAS_STD_WSTRING
403GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
wchar_t, ::std::wstring);
404GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const wchar_t, ::std::wstring);
407#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
417template <
typename T1,
typename T2>
418std::string FormatForComparisonFailureMessage(
419 const T1& value,
const T2& ) {
431class UniversalPrinter;
445void PrintTo(
const T& value, ::std::ostream* os) {
446 internal::PrintWithFallback(value, os);
454GTEST_API_
void PrintTo(
unsigned char c, ::std::ostream* os);
455GTEST_API_
void PrintTo(
signed char c, ::std::ostream* os);
456inline void PrintTo(
char c, ::std::ostream* os) {
460 PrintTo(
static_cast<unsigned char>(c), os);
464inline void PrintTo(
bool x, ::std::ostream* os) {
465 *os << (x ?
"true" :
"false");
475GTEST_API_
void PrintTo(
wchar_t wc, ::std::ostream* os);
477GTEST_API_
void PrintTo(
char32_t c, ::std::ostream* os);
478inline void PrintTo(
char16_t c, ::std::ostream* os) {
479 PrintTo(ImplicitCast_<char32_t>(c), os);
482inline void PrintTo(
char8_t c, ::std::ostream* os) {
483 PrintTo(ImplicitCast_<char32_t>(c), os);
488GTEST_API_
void PrintTo(
const char* s, ::std::ostream* os);
489inline void PrintTo(
char* s, ::std::ostream* os) {
490 PrintTo(ImplicitCast_<const char*>(s), os);
495inline void PrintTo(
const signed char* s, ::std::ostream* os) {
496 PrintTo(ImplicitCast_<const void*>(s), os);
498inline void PrintTo(
signed char* s, ::std::ostream* os) {
499 PrintTo(ImplicitCast_<const void*>(s), os);
501inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
502 PrintTo(ImplicitCast_<const void*>(s), os);
504inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
505 PrintTo(ImplicitCast_<const void*>(s), os);
509void PrintTo(
const char8_t* s, ::std::ostream* os);
510inline void PrintTo(
char8_t* s, ::std::ostream* os) {
511 PrintTo(ImplicitCast_<const char8_t*>(s), os);
515void PrintTo(
const char16_t* s, ::std::ostream* os);
516inline void PrintTo(
char16_t* s, ::std::ostream* os) {
517 PrintTo(ImplicitCast_<const char16_t*>(s), os);
520void PrintTo(
const char32_t* s, ::std::ostream* os);
521inline void PrintTo(
char32_t* s, ::std::ostream* os) {
522 PrintTo(ImplicitCast_<const char32_t*>(s), os);
530#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
532GTEST_API_
void PrintTo(
const wchar_t* s, ::std::ostream* os);
533inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
534 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
544void 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);
553GTEST_API_
void PrintStringTo(const ::std::string&s, ::std::ostream* os);
554inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
555 PrintStringTo(s, os);
560GTEST_API_
void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
561inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
562 PrintU8StringTo(s, os);
567GTEST_API_
void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
568inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
569 PrintU16StringTo(s, os);
573GTEST_API_
void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
574inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
575 PrintU32StringTo(s, os);
579#if GTEST_HAS_STD_WSTRING
580GTEST_API_
void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
581inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
582 PrintWideStringTo(s, os);
586#if GTEST_INTERNAL_HAS_STRING_VIEW
588inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
589 PrintTo(::std::string(sp), os);
593inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
596void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
597 UniversalPrinter<T&>::Print(ref.get(), os);
600inline const void* VoidifyPointer(
const void* p) {
return p; }
601inline const void* VoidifyPointer(
volatile const void* p) {
602 return const_cast<const void*
>(p);
605template <
typename T,
typename Ptr>
606void PrintSmartPointer(
const Ptr& ptr, std::ostream* os,
char) {
607 if (ptr ==
nullptr) {
611 *os <<
"(" << (VoidifyPointer)(ptr.get()) <<
")";
614template <
typename T,
typename Ptr,
615 typename =
typename std::enable_if<!std::is_void<T>::value &&
616 !std::is_array<T>::value>::type>
617void PrintSmartPointer(
const Ptr& ptr, std::ostream* os,
int) {
618 if (ptr ==
nullptr) {
621 *os <<
"(ptr = " << (VoidifyPointer)(ptr.get()) <<
", value = ";
622 UniversalPrinter<T>::Print(*ptr, os);
627template <
typename T,
typename D>
628void PrintTo(
const std::unique_ptr<T, D>& ptr, std::ostream* os) {
629 (PrintSmartPointer<T>)(ptr, os, 0);
633void PrintTo(
const std::shared_ptr<T>& ptr, std::ostream* os) {
634 (PrintSmartPointer<T>)(ptr, os, 0);
640void PrintTupleTo(
const T&, std::integral_constant<size_t, 0>,
643template <
typename T,
size_t I>
644void PrintTupleTo(
const T& t, std::integral_constant<size_t, I>,
645 ::std::ostream* os) {
646 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
647 GTEST_INTENTIONAL_CONST_COND_PUSH_()
649 GTEST_INTENTIONAL_CONST_COND_POP_()
652 UniversalPrinter<
typename std::tuple_element<I - 1, T>::type>::Print(
653 std::get<I - 1>(t), os);
656template <
typename... Types>
657void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
659 PrintTupleTo(t, std::integral_constant<
size_t,
sizeof...(Types)>(), os);
664template <
typename T1,
typename T2>
665void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
669 UniversalPrinter<T1>::Print(value.first, os);
671 UniversalPrinter<T2>::Print(value.second, os);
676inline void PrintTo(const ::std::type_info& value, ::std::ostream* os) {
677 internal::PrintTo<::std::type_info>(value, os);
678 *os <<
" (\"" << value.name() <<
"\")";
681inline void PrintTo(const ::std::type_index& value, ::std::ostream* os) {
682 internal::PrintTo<::std::type_index>(value, os);
683 *os <<
" (\"" << value.name() <<
"\")";
694 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
699 static void Print(
const T& value, ::std::ostream* os) {
711 GTEST_DISABLE_MSC_WARNINGS_POP_()
718#if GTEST_INTERNAL_HAS_ANY
725 static void Print(
const Any& value, ::std::ostream* os) {
726 if (value.has_value()) {
727 *os <<
"value of type " << GetTypeName(value);
734 static std::string GetTypeName(
const Any& value) {
736 return internal::GetTypeName(value.type());
738 static_cast<void>(value);
739 return "<unknown_type>";
746#if GTEST_INTERNAL_HAS_OPTIONAL
751class UniversalPrinter<Optional<T>> {
753 static void Print(
const Optional<T>& value, ::std::ostream* os) {
758 UniversalPrint(*value, os);
766#if GTEST_INTERNAL_HAS_VARIANT
770template <
typename... T>
771class UniversalPrinter<Variant<T...>> {
773 static void Print(
const Variant<T...>& value, ::std::ostream* os) {
776 absl::visit(Visitor{os, value.index()}, value);
778 std::visit(Visitor{os, value.index()}, value);
785 template <
typename U>
786 void operator()(
const U& u)
const {
787 *os <<
"'" << GetTypeName<U>() <<
"(index = " << index
789 UniversalPrint(u, os);
801void UniversalPrintArray(
const T* begin,
size_t len, ::std::ostream* os) {
806 const size_t kThreshold = 18;
807 const size_t kChunkSize = 8;
811 if (len <= kThreshold) {
812 PrintRawArrayTo(begin, len, os);
814 PrintRawArrayTo(begin, kChunkSize, os);
816 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
822GTEST_API_
void UniversalPrintArray(
823 const char* begin,
size_t len, ::std::ostream* os);
827GTEST_API_
void UniversalPrintArray(
const char8_t* begin,
size_t len,
832GTEST_API_
void UniversalPrintArray(
const char16_t* begin,
size_t len,
836GTEST_API_
void UniversalPrintArray(
const char32_t* begin,
size_t len,
840GTEST_API_
void UniversalPrintArray(
841 const wchar_t* begin,
size_t len, ::std::ostream* os);
844template <
typename T,
size_t N>
849 static void Print(
const T (&a)[N], ::std::ostream* os) {
850 UniversalPrintArray(a, N, os);
860 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
862 static void Print(
const T& value, ::std::ostream* os) {
865 *os <<
"@" <<
reinterpret_cast<const void*
>(&value) <<
" ";
868 UniversalPrint(value, os);
871 GTEST_DISABLE_MSC_WARNINGS_POP_()
881 static void Print(
const T& value, ::std::ostream* os) {
882 UniversalPrint(value, os);
888 static void Print(
const T& value, ::std::ostream* os) {
889 UniversalPrint(value, os);
892template <
typename T,
size_t N>
895 static void Print(
const T (&value)[N], ::std::ostream* os) {
902 static void Print(
const char* str, ::std::ostream* os) {
903 if (str ==
nullptr) {
906 UniversalPrint(std::string(str), os);
918 static void Print(
const char8_t* str, ::std::ostream* os) {
919 if (str ==
nullptr) {
922 UniversalPrint(::std::u8string(str), os);
927class UniversalTersePrinter<char8_t*>
928 :
public UniversalTersePrinter<const char8_t*> {};
934 static void Print(
const char16_t* str, ::std::ostream* os) {
935 if (str ==
nullptr) {
938 UniversalPrint(::std::u16string(str), os);
949 static void Print(
const char32_t* str, ::std::ostream* os) {
950 if (str ==
nullptr) {
953 UniversalPrint(::std::u32string(str), os);
961#if GTEST_HAS_STD_WSTRING
965 static void Print(
const wchar_t* str, ::std::ostream* os) {
966 if (str ==
nullptr) {
969 UniversalPrint(::std::wstring(str), os);
978 static void Print(
wchar_t* str, ::std::ostream* os) {
984void UniversalTersePrint(
const T& value, ::std::ostream* os) {
993void UniversalPrint(
const T& value, ::std::ostream* os) {
997 UniversalPrinter<T1>::Print(value, os);
1000typedef ::std::vector< ::std::string> Strings;
1004template <
typename Tuple>
1005void TersePrintPrefixToStrings(
const Tuple&, std::integral_constant<size_t, 0>,
1007template <
typename Tuple,
size_t I>
1008void TersePrintPrefixToStrings(
const Tuple& t,
1009 std::integral_constant<size_t, I>,
1011 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1013 ::std::stringstream ss;
1014 UniversalTersePrint(std::get<I - 1>(t), &ss);
1015 strings->push_back(ss.str());
1021template <
typename Tuple>
1022Strings UniversalTersePrintTupleFieldsToStrings(
const Tuple& value) {
1024 TersePrintPrefixToStrings(
1025 value, std::integral_constant<
size_t, std::tuple_size<Tuple>::value>(),
1032template <
typename T>
1033::std::string PrintToString(
const T& value) {
1034 ::std::stringstream ss;
1035 internal::UniversalTersePrinter<T>::Print(value, &ss);
1044#include "gtest/internal/custom/gtest-printers.h"
Definition gtest-internal.h:895
Definition gtest-printers.h:690
Definition gtest-printers.h:879
Definition gtest-printers.h:132
Definition gtest-printers.h:244
Definition gtest-printers.h:257
Definition gtest-printers.h:283
Definition gtest-printers.h:292
Definition gtest-printers.h:169
Definition gtest-internal.h:1007
Definition gtest-printers.h:184
Definition gtest-printers.h:226
Definition gtest-printers.h:271
Definition gtest-printers.h:204
Definition gtest-printers.h:207