101#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
102#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
110#include <type_traits>
114#include "gtest/internal/gtest-internal.h"
115#include "gtest/internal/gtest-port.h"
124void UniversalPrint(
const T& value, ::std::ostream* os);
129 template <
typename T,
130 typename =
typename std::enable_if<
131 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(IsContainer)) &&
133 static void PrintValue(
const T& container, std::ostream* os) {
134 const size_t kMaxCount = 32;
137 for (
auto&& elem : container) {
140 if (count == kMaxCount) {
148 internal::UniversalPrint(elem, os);
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) {
175 *os << reinterpret_cast<const void*>(p);
181 template <
typename T>
182 static void PrintValue(T* p, ::std::ostream* os) {
194namespace internal_stream_operator_without_lexical_name_lookup {
204 template <
typename T,
207 typename =
typename std::enable_if<
208 !std::is_member_pointer<T>::value>::type,
211 typename =
decltype(std::declval<std::ostream&>()
212 << std::declval<const T&>())>
213 static void PrintValue(
const T& value, ::std::ostream* os) {
226 static const size_t kProtobufOneLinerMaxLength = 50;
228 template <
typename T,
229 typename =
typename std::enable_if<
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 +
">");
248 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
254#if GTEST_INTERNAL_HAS_STRING_VIEW
255 static void PrintValue(internal::StringView value, ::std::ostream* os) {
256 internal::UniversalPrint(value, os);
263GTEST_API_
void PrintBytesInObjectTo(
const unsigned char* obj_bytes,
264 size_t count, ::std::ostream* os);
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*
>(
272 reinterpret_cast<const void*
>(std::addressof(value))),
278 template <
typename T>
279 static void PrintValue(
const T&, ::std::ostream* os) {
280 *os <<
"(incomplete type)";
285template <
typename T,
typename E,
typename Printer,
typename... Printers>
288template <
typename T,
typename Printer,
typename... Printers>
290 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
291 Printer, Printers...> {
292 using type = Printer;
305void PrintWithFallback(
const T& value, ::std::ostream* os) {
311 Printer::PrintValue(value, os);
329template <
typename ToPr
int,
typename OtherOperand>
332 static ::std::string Format(
const ToPrint& value) {
333 return ::testing::PrintToString(value);
338template <
typename ToPr
int,
size_t N,
typename OtherOperand>
341 static ::std::string Format(
const ToPrint* value) {
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)); \
358GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char);
359GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char);
360GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
wchar_t);
361GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const wchar_t);
362#ifdef __cpp_lib_char8_t
363GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char8_t);
364GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char8_t);
366GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char16_t);
367GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char16_t);
368GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char32_t);
369GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char32_t);
371#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
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); \
385GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char, ::std::string);
386GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char, ::std::string);
387#ifdef __cpp_lib_char8_t
388GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char8_t, ::std::u8string);
389GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char8_t, ::std::u8string);
391GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char16_t, ::std::u16string);
392GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char16_t, ::std::u16string);
393GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char32_t, ::std::u32string);
394GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char32_t, ::std::u32string);
396#if GTEST_HAS_STD_WSTRING
397GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
wchar_t, ::std::wstring);
398GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const wchar_t, ::std::wstring);
401#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
411template <
typename T1,
typename T2>
412std::string FormatForComparisonFailureMessage(
const T1& value,
425class UniversalPrinter;
439void PrintTo(
const T& value, ::std::ostream* os) {
440 internal::PrintWithFallback(value, os);
448GTEST_API_
void PrintTo(
unsigned char c, ::std::ostream* os);
449GTEST_API_
void PrintTo(
signed char c, ::std::ostream* os);
450inline void PrintTo(
char c, ::std::ostream* os) {
454 PrintTo(
static_cast<unsigned char>(c), os);
458inline void PrintTo(
bool x, ::std::ostream* os) {
459 *os << (x ?
"true" :
"false");
469GTEST_API_
void PrintTo(
wchar_t wc, ::std::ostream* os);
471GTEST_API_
void PrintTo(
char32_t c, ::std::ostream* os);
472inline void PrintTo(
char16_t c, ::std::ostream* os) {
473 PrintTo(ImplicitCast_<char32_t>(c), os);
476inline void PrintTo(
char8_t c, ::std::ostream* os) {
477 PrintTo(ImplicitCast_<char32_t>(c), os);
482#if defined(__SIZEOF_INT128__)
483GTEST_API_
void PrintTo(__uint128_t v, ::std::ostream* os);
484GTEST_API_
void PrintTo(__int128_t v, ::std::ostream* 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);
509GTEST_API_
void 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);
515GTEST_API_
void 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);
520GTEST_API_
void 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);
559#ifdef __cpp_lib_char8_t
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)"; }
596inline void PrintTo(
const std::type_info& info, std::ostream* os) {
597 *os << internal::GetTypeName(info);
602void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
603 UniversalPrinter<T&>::Print(ref.get(), os);
606inline const void* VoidifyPointer(
const void* p) {
return p; }
607inline const void* VoidifyPointer(
volatile const void* p) {
608 return const_cast<const void*
>(p);
611template <
typename T,
typename Ptr>
612void PrintSmartPointer(
const Ptr& ptr, std::ostream* os,
char) {
613 if (ptr ==
nullptr) {
617 *os <<
"(" << (VoidifyPointer)(ptr.get()) <<
")";
620template <
typename T,
typename Ptr,
621 typename =
typename std::enable_if<!std::is_void<T>::value &&
622 !std::is_array<T>::value>::type>
623void 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);
633template <
typename T,
typename D>
634void PrintTo(
const std::unique_ptr<T, D>& ptr, std::ostream* os) {
635 (PrintSmartPointer<T>)(ptr, os, 0);
639void PrintTo(
const std::shared_ptr<T>& ptr, std::ostream* os) {
640 (PrintSmartPointer<T>)(ptr, os, 0);
646void PrintTupleTo(
const T&, std::integral_constant<size_t, 0>,
649template <
typename T,
size_t I>
650void 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);
662template <
typename... Types>
663void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
665 PrintTupleTo(t, std::integral_constant<
size_t,
sizeof...(Types)>(), os);
670template <
typename T1,
typename T2>
671void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
675 UniversalPrinter<T1>::Print(value.first, os);
677 UniversalPrinter<T2>::Print(value.second, os);
688 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
693 static void Print(
const T& value, ::std::ostream* os) {
705 GTEST_DISABLE_MSC_WARNINGS_POP_()
712#if GTEST_INTERNAL_HAS_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);
733 return "<unknown_type>";
740#if GTEST_INTERNAL_HAS_OPTIONAL
745class UniversalPrinter<Optional<T>> {
747 static void Print(
const Optional<T>& value, ::std::ostream* os) {
752 UniversalPrint(*value, os);
759class UniversalPrinter<decltype(Nullopt())> {
761 static void Print(
decltype(Nullopt()), ::std::ostream* os) {
768#if GTEST_INTERNAL_HAS_VARIANT
772template <
typename... T>
773class 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);
787 template <
typename U>
788 void operator()(
const U& u)
const {
789 *os <<
"'" << GetTypeName<U>() <<
"(index = " << index
791 UniversalPrint(u, os);
803void UniversalPrintArray(
const T* begin,
size_t len, ::std::ostream* os) {
808 const size_t kThreshold = 18;
809 const size_t kChunkSize = 8;
813 if (len <= kThreshold) {
814 PrintRawArrayTo(begin, len, os);
816 PrintRawArrayTo(begin, kChunkSize, os);
818 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
824GTEST_API_
void UniversalPrintArray(
const char* begin,
size_t len,
829GTEST_API_
void UniversalPrintArray(
const char8_t* begin,
size_t len,
834GTEST_API_
void UniversalPrintArray(
const char16_t* begin,
size_t len,
838GTEST_API_
void UniversalPrintArray(
const char32_t* begin,
size_t len,
842GTEST_API_
void UniversalPrintArray(
const wchar_t* begin,
size_t len,
846template <
typename T,
size_t N>
851 static void Print(
const T (&a)[N], ::std::ostream* os) {
852 UniversalPrintArray(a, N, os);
862 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
864 static void Print(
const T& value, ::std::ostream* os) {
867 *os <<
"@" <<
reinterpret_cast<const void*
>(&value) <<
" ";
870 UniversalPrint(value, os);
873 GTEST_DISABLE_MSC_WARNINGS_POP_()
883 static void Print(
const T& value, ::std::ostream* os) {
884 UniversalPrint(value, os);
890 static void Print(
const T& value, ::std::ostream* os) {
891 UniversalPrint(value, os);
897 static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
901template <
typename T,
size_t N>
904 static void Print(
const T (&value)[N], ::std::ostream* os) {
911 static void Print(
const char* str, ::std::ostream* os) {
912 if (str ==
nullptr) {
915 UniversalPrint(std::string(str), os);
927 static void Print(
const char8_t* str, ::std::ostream* os) {
928 if (str ==
nullptr) {
931 UniversalPrint(::std::u8string(str), os);
936class UniversalTersePrinter<char8_t*>
937 :
public UniversalTersePrinter<const char8_t*> {};
943 static void Print(
const char16_t* str, ::std::ostream* os) {
944 if (str ==
nullptr) {
947 UniversalPrint(::std::u16string(str), os);
958 static void Print(
const char32_t* str, ::std::ostream* os) {
959 if (str ==
nullptr) {
962 UniversalPrint(::std::u32string(str), os);
970#if GTEST_HAS_STD_WSTRING
974 static void Print(
const wchar_t* str, ::std::ostream* os) {
975 if (str ==
nullptr) {
978 UniversalPrint(::std::wstring(str), os);
987 static void Print(
wchar_t* str, ::std::ostream* os) {
993void UniversalTersePrint(
const T& value, ::std::ostream* os) {
1001template <
typename T>
1002void UniversalPrint(
const T& value, ::std::ostream* os) {
1006 UniversalPrinter<T1>::Print(value, os);
1009typedef ::std::vector<::std::string> Strings;
1013template <
typename Tuple>
1014void TersePrintPrefixToStrings(
const Tuple&, std::integral_constant<size_t, 0>,
1016template <
typename Tuple,
size_t I>
1017void TersePrintPrefixToStrings(
const Tuple& t,
1018 std::integral_constant<size_t, I>,
1020 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1022 ::std::stringstream ss;
1023 UniversalTersePrint(std::get<I - 1>(t), &ss);
1024 strings->push_back(ss.str());
1030template <
typename Tuple>
1031Strings UniversalTersePrintTupleFieldsToStrings(
const Tuple& value) {
1033 TersePrintPrefixToStrings(
1034 value, std::integral_constant<
size_t, std::tuple_size<Tuple>::value>(),
1041template <
typename T>
1042::std::string PrintToString(
const T& value) {
1043 ::std::stringstream ss;
1044 internal::UniversalTersePrinter<T>::Print(value, &ss);
1053#include "gtest/internal/custom/gtest-printers.h"
Definition gtest-internal.h:894
Definition gtest-printers.h:684
Definition gtest-printers.h:881
Definition gtest-printers.h:128
Definition gtest-printers.h:240
Definition gtest-printers.h:253
Definition gtest-printers.h:277
Definition gtest-printers.h:286
Definition gtest-printers.h:165
Definition gtest-internal.h:1008
Definition gtest-printers.h:180
Definition gtest-printers.h:222
Definition gtest-printers.h:265
Definition gtest-printers.h:200
Definition gtest-printers.h:203