101 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
102 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
104 #include <functional>
110 #include <type_traits>
114 #include "gtest/internal/gtest-internal.h"
115 #include "gtest/internal/gtest-port.h"
123 template <
typename T>
128 struct ContainerPrinter {
129 template <
typename T,
130 typename =
typename std::enable_if<
131 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(
IsContainer)) &&
134 const size_t kMaxCount = 32;
140 if (
count == kMaxCount) {
165 struct FunctionPointerPrinter {
166 template <
typename T,
typename =
typename std::enable_if<
175 *os << reinterpret_cast<const void*>(p);
180 struct PointerPrinter {
181 template <
typename T>
194 namespace internal_stream_operator_without_lexical_name_lookup {
200 struct LookupBlocker {};
201 void operator<<(LookupBlocker, LookupBlocker);
203 struct StreamPrinter {
204 template <
typename T,
207 typename =
typename std::enable_if<
211 typename = decltype(std::declval<std::ostream&>()
212 << std::declval<const T&>())>
222 struct ProtobufPrinter {
228 template <
typename T,
229 typename =
typename std::enable_if<
234 pretty_str =
"\n" +
value.DebugString();
236 *os << (
"<" + pretty_str +
">");
240 struct ConvertibleToIntegerPrinter {
253 struct ConvertibleToStringViewPrinter {
254 #if GTEST_INTERNAL_HAS_STRING_VIEW
255 static void PrintValue(internal::StringView
value, ::std::ostream* os) {
269 template <
typename T,
size_t = sizeof(T)>
272 static_cast<const unsigned char*
>(
274 reinterpret_cast<const void*
>(std::addressof(
value))),
279 struct FallbackPrinter {
280 template <
typename T>
282 *os <<
"(incomplete type)";
287 template <
typename T,
typename E,
typename Printer,
typename... Printers>
288 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
290 template <
typename T,
typename Printer,
typename... Printers>
291 struct FindFirstPrinter<
306 template <
typename T>
331 template <
typename ToPr
int,
typename OtherOperand>
332 class FormatForComparison {
340 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
341 class FormatForComparison<ToPrint[
N], OtherOperand> {
351 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
352 template <typename OtherOperand> \
353 class FormatForComparison<CharType*, OtherOperand> { \
355 static ::std::string Format(CharType* value) { \
356 return ::testing::PrintToString(static_cast<const void*>(value)); \
364 #ifdef __cpp_lib_char8_t
373 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
378 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
380 class FormatForComparison<CharType*, OtherStringType> { \
382 static ::std::string Format(CharType* value) { \
383 return ::testing::PrintToString(value); \
398 #if GTEST_HAS_STD_WSTRING
403 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
413 template <
typename T1,
typename T2>
426 template <
typename T>
427 class UniversalPrinter;
440 template <
typename T>
452 inline void PrintTo(
char c, ::std::ostream* os) {
456 PrintTo(
static_cast<unsigned char>(
c), os);
460 inline void PrintTo(
bool x, ::std::ostream* os) {
461 *os << (
x ?
"true" :
"false");
474 inline void PrintTo(char16_t
c, ::std::ostream* os) {
475 PrintTo(ImplicitCast_<char32_t>(
c), os);
478 inline void PrintTo(char8_t
c, ::std::ostream* os) {
479 PrintTo(ImplicitCast_<char32_t>(
c), os);
484 #if defined(__SIZEOF_INT128__)
487 #endif // __SIZEOF_INT128__
491 inline void PrintTo(
char* s, ::std::ostream* os) {
492 PrintTo(ImplicitCast_<const char*>(s), os);
497 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
498 PrintTo(ImplicitCast_<const void*>(s), os);
500 inline void PrintTo(
signed char* s, ::std::ostream* os) {
501 PrintTo(ImplicitCast_<const void*>(s), os);
503 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
504 PrintTo(ImplicitCast_<const void*>(s), os);
506 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
507 PrintTo(ImplicitCast_<const void*>(s), os);
512 inline void PrintTo(char8_t* s, ::std::ostream* os) {
513 PrintTo(ImplicitCast_<const char8_t*>(s), os);
518 inline void PrintTo(char16_t* s, ::std::ostream* os) {
519 PrintTo(ImplicitCast_<const char16_t*>(s), os);
523 inline void PrintTo(char32_t* s, ::std::ostream* os) {
524 PrintTo(ImplicitCast_<const char32_t*>(s), os);
532 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
535 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
536 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
545 template <
typename T>
548 for (
size_t i = 1;
i !=
count;
i++) {
562 GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
563 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
564 PrintU8StringTo(s, os);
570 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
576 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
581 #if GTEST_HAS_STD_WSTRING
586 #endif // GTEST_HAS_STD_WSTRING
588 #if GTEST_INTERNAL_HAS_STRING_VIEW
590 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
593 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
595 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
598 inline void PrintTo(
const std::type_info& info, std::ostream* os) {
601 #endif // GTEST_HAS_RTTI
603 template <
typename T>
604 void PrintTo(std::reference_wrapper<T>
ref, ::std::ostream* os) {
610 return const_cast<const void*
>(
p);
613 template <
typename T,
typename Ptr>
615 if (
ptr ==
nullptr) {
622 template <
typename T,
typename Ptr,
626 if (
ptr ==
nullptr) {
635 template <
typename T,
typename D>
636 void PrintTo(
const std::unique_ptr<T, D>&
ptr, std::ostream* os) {
637 (PrintSmartPointer<T>)(
ptr, os, 0);
640 template <
typename T>
641 void PrintTo(
const std::shared_ptr<T>&
ptr, std::ostream* os) {
642 (PrintSmartPointer<T>)(
ptr, os, 0);
647 template <
typename T>
648 void PrintTupleTo(
const T&, std::integral_constant<size_t, 0>,
651 template <
typename T,
size_t I>
652 void PrintTupleTo(
const T& t, std::integral_constant<size_t, I>,
653 ::std::ostream* os) {
654 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
661 std::get<I - 1>(t), os);
664 template <
typename...
Types>
665 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
672 template <
typename T1,
typename T2>
673 void PrintTo(const ::std::pair<T1, T2>&
value, ::std::ostream* os) {
685 template <
typename T>
686 class UniversalPrinter {
711 template <
typename T>
714 #if GTEST_INTERNAL_HAS_ANY
722 if (
value.has_value()) {
734 static_cast<void>(
value);
735 return "<unknown_type>";
736 #endif // GTEST_HAS_RTTI
740 #endif // GTEST_INTERNAL_HAS_ANY
742 #if GTEST_INTERNAL_HAS_OPTIONAL
746 template <
typename T>
749 static void Print(
const Optional<T>&
value, ::std::ostream* os) {
761 class UniversalPrinter<decltype(Nullopt())> {
763 static void Print(decltype(Nullopt()), ::std::ostream* os) {
768 #endif // GTEST_INTERNAL_HAS_OPTIONAL
770 #if GTEST_INTERNAL_HAS_VARIANT
774 template <
typename...
T>
775 class UniversalPrinter<Variant<
T...>> {
777 static void Print(
const Variant<T...>&
value, ::std::ostream* os) {
783 #endif // GTEST_HAS_ABSL
789 template <
typename U>
790 void operator()(
const U& u)
const {
791 *os <<
"'" << GetTypeName<U>() <<
"(index = " <<
index
800 #endif // GTEST_INTERNAL_HAS_VARIANT
804 template <
typename T>
810 const size_t kThreshold = 18;
815 if (
len <= kThreshold) {
827 const char*
begin,
size_t len, ::std::ostream* os);
845 const wchar_t*
begin,
size_t len, ::std::ostream* os);
848 template <
typename T,
size_t N>
849 class UniversalPrinter<
T[
N]> {
853 static void Print(
const T (&a)[
N], ::std::ostream* os) {
859 template <
typename T>
860 class UniversalPrinter<
T&> {
869 *os <<
"@" <<
reinterpret_cast<const void*
>(&
value) <<
" ";
882 template <
typename T>
883 class UniversalTersePrinter {
889 template <
typename T>
890 class UniversalTersePrinter<
T&> {
896 template <
typename T,
size_t N>
897 class UniversalTersePrinter<
T[
N]> {
904 class UniversalTersePrinter<
const char*> {
906 static void Print(
const char*
str, ::std::ostream* os) {
907 if (
str ==
nullptr) {
915 class UniversalTersePrinter<char*> :
public UniversalTersePrinter<const char*> {
920 class UniversalTersePrinter<
const char8_t*> {
922 static void Print(
const char8_t*
str, ::std::ostream* os) {
923 if (
str ==
nullptr) {
931 class UniversalTersePrinter<char8_t*>
932 :
public UniversalTersePrinter<const char8_t*> {};
938 static void Print(
const char16_t*
str, ::std::ostream* os) {
939 if (
str ==
nullptr) {
953 static void Print(
const char32_t*
str, ::std::ostream* os) {
954 if (
str ==
nullptr) {
965 #if GTEST_HAS_STD_WSTRING
969 static void Print(
const wchar_t*
str, ::std::ostream* os) {
970 if (
str ==
nullptr) {
980 class UniversalTersePrinter<wchar_t*> {
982 static void Print(
wchar_t*
str, ::std::ostream* os) {
987 template <
typename T>
996 template <
typename T>
1004 typedef ::std::vector< ::std::string>
Strings;
1008 template <
typename Tuple>
1011 template <
typename Tuple,
size_t I>
1013 std::integral_constant<size_t, I>,
1017 ::std::stringstream ss;
1019 strings->push_back(ss.str());
1025 template <
typename Tuple>
1036 template <
typename T>
1038 ::std::stringstream ss;
1048 #include "gtest/internal/custom/gtest-printers.h"
1050 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_