100 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
101 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
103 #include <functional>
108 #include <type_traits>
111 #include "gtest/internal/gtest-internal.h"
112 #include "gtest/internal/gtest-port.h"
115 #include "absl/strings/string_view.h"
116 #include "absl/types/optional.h"
117 #include "absl/types/variant.h"
118 #endif // GTEST_HAS_ABSL
124 namespace internal2 {
139 kConvertibleToStringView,
149 template <
typename T, TypeKind kTypeKind>
150 class TypeWithoutFormatter {
155 reinterpret_cast<const void*
>(&
value)),
165 template <
typename T>
171 pretty_str =
"\n" +
value.DebugString();
173 *os << (
"<" + pretty_str +
">");
177 template <
typename T>
194 template <
typename T>
195 class TypeWithoutFormatter<
T, kConvertibleToStringView> {
230 template <
typename Char,
typename CharTraits,
typename T>
231 ::std::basic_ostream<Char, CharTraits>&
operator<<(
232 ::std::basic_ostream<Char, CharTraits>& os,
const T& x) {
235 : std::is_convertible<
242 ? kConvertibleToStringView
258 template <
typename T>
309 template <
typename ToPr
int,
typename OtherOperand>
310 class FormatForComparison {
318 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
319 class FormatForComparison<ToPrint[
N], OtherOperand> {
329 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
330 template <typename OtherOperand> \
331 class FormatForComparison<CharType*, OtherOperand> { \
333 static ::std::string Format(CharType* value) { \
334 return ::testing::PrintToString(static_cast<const void*>(value)); \
343 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
348 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
350 class FormatForComparison<CharType*, OtherStringType> { \
352 static ::std::string Format(CharType* value) { \
353 return ::testing::PrintToString(value); \
360 #if GTEST_HAS_GLOBAL_STRING
365 #if GTEST_HAS_GLOBAL_WSTRING
370 #if GTEST_HAS_STD_WSTRING
375 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
385 template <
typename T1,
typename T2>
398 template <
typename T>
399 class UniversalPrinter;
401 template <
typename T>
410 template <DefaultPr
interType type>
struct WrapPrinterType {};
414 template <
typename C>
416 const C&
container, ::std::ostream* os) {
417 const size_t kMaxCount = 32;
420 for (
typename C::const_iterator
it =
container.begin();
424 if (
count == kMaxCount) {
447 template <
typename T>
449 T* p, ::std::ostream* os) {
459 template <
typename T>
461 T* p, ::std::ostream* os) {
468 *os << reinterpret_cast<const void*>(p);
474 template <
typename T>
476 const T&
value, ::std::ostream* os) {
491 template <
typename T>
514 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(
IsContainer)) &&
532 inline void PrintTo(
char c, ::std::ostream* os) {
536 PrintTo(
static_cast<unsigned char>(c), os);
540 inline void PrintTo(
bool x, ::std::ostream* os) {
541 *os << (
x ?
"true" :
"false");
555 inline void PrintTo(
char* s, ::std::ostream* os) {
556 PrintTo(ImplicitCast_<const char*>(s), os);
561 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
562 PrintTo(ImplicitCast_<const void*>(s), os);
564 inline void PrintTo(
signed char* s, ::std::ostream* os) {
565 PrintTo(ImplicitCast_<const void*>(s), os);
567 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
568 PrintTo(ImplicitCast_<const void*>(s), os);
570 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
571 PrintTo(ImplicitCast_<const void*>(s), os);
579 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
582 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
583 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
592 template <
typename T>
595 for (
size_t i = 1;
i !=
count;
i++) {
602 #if GTEST_HAS_GLOBAL_STRING
607 #endif // GTEST_HAS_GLOBAL_STRING
615 #if GTEST_HAS_GLOBAL_WSTRING
620 #endif // GTEST_HAS_GLOBAL_WSTRING
622 #if GTEST_HAS_STD_WSTRING
627 #endif // GTEST_HAS_STD_WSTRING
634 #endif // GTEST_HAS_ABSL
636 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
638 template <
typename T>
639 void PrintTo(std::reference_wrapper<T>
ref, ::std::ostream* os) {
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);
659 std::get<I - 1>(t), os);
662 template <
typename...
Types>
663 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
670 template <
typename T1,
typename T2>
671 void PrintTo(const ::std::pair<T1, T2>&
value, ::std::ostream* os) {
683 template <
typename T>
684 class UniversalPrinter {
712 template <
typename T>
715 static void Print(const ::absl::optional<T>&
value, ::std::ostream* os) {
728 template <
typename...
T>
731 static void Print(const ::absl::variant<T...>&
value, ::std::ostream* os) {
739 template <
typename U>
740 void operator()(
const U& u)
const {
741 *os <<
"'" << GetTypeName<U>() <<
"' with value ";
748 #endif // GTEST_HAS_ABSL
752 template <
typename T>
758 const size_t kThreshold = 18;
763 if (
len <= kThreshold) {
775 const char*
begin,
size_t len, ::std::ostream* os);
779 const wchar_t*
begin,
size_t len, ::std::ostream* os);
782 template <
typename T,
size_t N>
783 class UniversalPrinter<
T[
N]> {
787 static void Print(
const T (&a)[
N], ::std::ostream* os) {
793 template <
typename T>
794 class UniversalPrinter<
T&> {
803 *os <<
"@" <<
reinterpret_cast<const void*
>(&
value) <<
" ";
816 template <
typename T>
817 class UniversalTersePrinter {
823 template <
typename T>
824 class UniversalTersePrinter<
T&> {
830 template <
typename T,
size_t N>
831 class UniversalTersePrinter<
T[
N]> {
838 class UniversalTersePrinter<
const char*> {
840 static void Print(
const char*
str, ::std::ostream* os) {
841 if (
str ==
nullptr) {
849 class UniversalTersePrinter<char*> {
856 #if GTEST_HAS_STD_WSTRING
858 class UniversalTersePrinter<
const wchar_t*> {
860 static void Print(
const wchar_t*
str, ::std::ostream* os) {
861 if (
str ==
nullptr) {
871 class UniversalTersePrinter<wchar_t*> {
873 static void Print(
wchar_t*
str, ::std::ostream* os) {
878 template <
typename T>
887 template <
typename T>
895 typedef ::std::vector< ::std::string>
Strings;
899 template <
typename Tuple>
902 template <
typename Tuple,
size_t I>
904 std::integral_constant<size_t, I>,
908 ::std::stringstream ss;
910 strings->push_back(ss.str());
916 template <
typename Tuple>
928 namespace internal2 {
929 template <
typename T>
931 const T&
value, ::std::ostream* os) {
937 template <
typename T>
939 ::std::stringstream ss;
949 #include "gtest/internal/custom/gtest-printers.h"
951 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_