95 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
96 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
103 #include "gtest/internal/gtest-port.h"
104 #include "gtest/internal/gtest-internal.h"
106 #if GTEST_HAS_STD_TUPLE_
114 namespace internal2 {
118 GTEST_API_
void PrintBytesInObjectTo(
const unsigned char* obj_bytes,
126 kConvertibleToInteger,
135 template <
typename T, TypeKind kTypeKind>
139 static void PrintValue(
const T& value, ::std::ostream* os) {
140 PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
148 const size_t kProtobufOneLinerMaxLength = 50;
150 template <
typename T>
153 static void PrintValue(
const T& value, ::std::ostream* os) {
154 const ::testing::internal::string short_str = value.ShortDebugString();
155 const ::testing::internal::string pretty_str =
156 short_str.length() <= kProtobufOneLinerMaxLength ?
157 short_str : (
"\n" + value.DebugString());
158 *os << (
"<" + pretty_str +
">");
162 template <
typename T>
172 static void PrintValue(
const T& value, ::std::ostream* os) {
173 const internal::BiggestInt kBigInt = value;
202 template <
typename Char,
typename CharTraits,
typename T>
203 ::std::basic_ostream<Char, CharTraits>& operator<<(
204 ::std::basic_ostream<Char, CharTraits>& os,
const T& x) {
208 kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
217 namespace testing_internal {
221 template <
typename T>
222 void DefaultPrintNonContainerTo(
const T& value, ::std::ostream* os) {
234 using namespace ::testing::internal2;
272 template <
typename ToPr
int,
typename OtherOperand>
275 static ::std::string Format(
const ToPrint& value) {
276 return ::testing::PrintToString(value);
281 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
284 static ::std::string Format(
const ToPrint* value) {
292 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
293 template <typename OtherOperand> \
294 class FormatForComparison<CharType*, OtherOperand> { \
296 static ::std::string Format(CharType* value) { \
297 return ::testing::PrintToString(static_cast<const void*>(value)); \
301 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char);
302 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char);
303 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
wchar_t);
304 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const wchar_t);
306 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
311 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
313 class FormatForComparison<CharType*, OtherStringType> { \
315 static ::std::string Format(CharType* value) { \
316 return ::testing::PrintToString(value); \
320 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char, ::std::string);
321 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char, ::std::string);
323 #if GTEST_HAS_GLOBAL_STRING
324 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char, ::
string);
325 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char, ::
string);
328 #if GTEST_HAS_GLOBAL_WSTRING
329 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
wchar_t, ::wstring);
330 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const wchar_t, ::wstring);
333 #if GTEST_HAS_STD_WSTRING
334 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
wchar_t, ::std::wstring);
335 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const wchar_t, ::std::wstring);
338 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
348 template <
typename T1,
typename T2>
349 std::string FormatForComparisonFailureMessage(
350 const T1& value,
const T2& ) {
361 template <
typename T>
364 template <
typename T>
365 void UniversalPrint(
const T& value, ::std::ostream* os);
369 template <
typename C>
370 void DefaultPrintTo(IsContainer ,
372 const C& container, ::std::ostream* os) {
373 const size_t kMaxCount = 32;
376 for (
typename C::const_iterator it = container.begin();
377 it != container.end(); ++it, ++count) {
380 if (count == kMaxCount) {
388 internal::UniversalPrint(*it, os);
403 template <
typename T>
404 void DefaultPrintTo(IsNotContainer ,
406 T* p, ::std::ostream* os) {
415 if (IsTrue(ImplicitlyConvertible<T*, const void*>::value)) {
427 *os << reinterpret_cast<const void*>(
428 reinterpret_cast<internal::UInt64
>(p));
435 template <
typename T>
436 void DefaultPrintTo(IsNotContainer ,
438 const T& value, ::std::ostream* os) {
439 ::testing_internal::DefaultPrintNonContainerTo(value, os);
453 template <
typename T>
454 void PrintTo(
const T& value, ::std::ostream* os) {
477 DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
485 GTEST_API_
void PrintTo(
unsigned char c, ::std::ostream* os);
486 GTEST_API_
void PrintTo(
signed char c, ::std::ostream* os);
487 inline void PrintTo(
char c, ::std::ostream* os) {
491 PrintTo(static_cast<unsigned char>(c), os);
495 inline void PrintTo(
bool x, ::std::ostream* os) {
496 *os << (x ?
"true" :
"false");
506 GTEST_API_
void PrintTo(
wchar_t wc, ::std::ostream* os);
509 GTEST_API_
void PrintTo(
const char* s, ::std::ostream* os);
510 inline void PrintTo(
char* s, ::std::ostream* os) {
511 PrintTo(ImplicitCast_<const char*>(s), os);
516 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
517 PrintTo(ImplicitCast_<const void*>(s), os);
519 inline void PrintTo(
signed char* s, ::std::ostream* os) {
520 PrintTo(ImplicitCast_<const void*>(s), os);
522 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
523 PrintTo(ImplicitCast_<const void*>(s), os);
525 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
526 PrintTo(ImplicitCast_<const void*>(s), os);
534 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
536 GTEST_API_
void PrintTo(
const wchar_t* s, ::std::ostream* os);
537 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
538 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
547 template <
typename T>
548 void PrintRawArrayTo(
const T a[],
size_t count, ::std::ostream* os) {
549 UniversalPrint(a[0], os);
550 for (
size_t i = 1; i != count; i++) {
552 UniversalPrint(a[i], os);
557 #if GTEST_HAS_GLOBAL_STRING
558 GTEST_API_
void PrintStringTo(const ::string&s, ::std::ostream* os);
559 inline void PrintTo(const ::string& s, ::std::ostream* os) {
560 PrintStringTo(s, os);
562 #endif // GTEST_HAS_GLOBAL_STRING
564 GTEST_API_
void PrintStringTo(const ::std::string&s, ::std::ostream* os);
565 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
566 PrintStringTo(s, os);
570 #if GTEST_HAS_GLOBAL_WSTRING
571 GTEST_API_
void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
572 inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
573 PrintWideStringTo(s, os);
575 #endif // GTEST_HAS_GLOBAL_WSTRING
577 #if GTEST_HAS_STD_WSTRING
578 GTEST_API_
void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
579 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
580 PrintWideStringTo(s, os);
582 #endif // GTEST_HAS_STD_WSTRING
584 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
587 template <
typename T>
588 void PrintTupleTo(
const T& t, ::std::ostream* os);
589 #endif // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
591 #if GTEST_HAS_TR1_TUPLE
600 inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
604 template <
typename T1>
605 void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
609 template <
typename T1,
typename T2>
610 void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
614 template <
typename T1,
typename T2,
typename T3>
615 void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
619 template <
typename T1,
typename T2,
typename T3,
typename T4>
620 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
624 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
625 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
626 ::std::ostream* os) {
630 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
632 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
633 ::std::ostream* os) {
637 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
638 typename T6,
typename T7>
639 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
640 ::std::ostream* os) {
644 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
645 typename T6,
typename T7,
typename T8>
646 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
647 ::std::ostream* os) {
651 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
652 typename T6,
typename T7,
typename T8,
typename T9>
653 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
654 ::std::ostream* os) {
658 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
659 typename T6,
typename T7,
typename T8,
typename T9,
typename T10>
661 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
662 ::std::ostream* os) {
665 #endif // GTEST_HAS_TR1_TUPLE
667 #if GTEST_HAS_STD_TUPLE_
668 template <
typename... Types>
669 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
672 #endif // GTEST_HAS_STD_TUPLE_
675 template <
typename T1,
typename T2>
676 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
680 UniversalPrinter<T1>::Print(value.first, os);
682 UniversalPrinter<T2>::Print(value.second, os);
688 template <
typename T>
689 class UniversalPrinter {
693 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
698 static
void Print(const T& value, ::std::ostream* os) {
710 GTEST_DISABLE_MSC_WARNINGS_POP_()
715 template <
typename T>
716 void UniversalPrintArray(
const T* begin,
size_t len, ::std::ostream* os) {
721 const size_t kThreshold = 18;
722 const size_t kChunkSize = 8;
727 if (len <= kThreshold) {
728 PrintRawArrayTo(begin, len, os);
730 PrintRawArrayTo(begin, kChunkSize, os);
732 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
738 GTEST_API_
void UniversalPrintArray(
739 const char* begin,
size_t len, ::std::ostream* os);
742 GTEST_API_
void UniversalPrintArray(
743 const wchar_t* begin,
size_t len, ::std::ostream* os);
746 template <
typename T,
size_t N>
751 static void Print(
const T (&a)[N], ::std::ostream* os) {
752 UniversalPrintArray(a, N, os);
757 template <
typename T>
762 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
764 static void Print(
const T& value, ::std::ostream* os) {
767 *os <<
"@" <<
reinterpret_cast<const void*
>(&value) <<
" ";
770 UniversalPrint(value, os);
773 GTEST_DISABLE_MSC_WARNINGS_POP_()
780 template <
typename T>
783 static void Print(
const T& value, ::std::ostream* os) {
784 UniversalPrint(value, os);
787 template <
typename T>
790 static void Print(
const T& value, ::std::ostream* os) {
791 UniversalPrint(value, os);
794 template <
typename T,
size_t N>
797 static void Print(
const T (&value)[N], ::std::ostream* os) {
804 static void Print(
const char* str, ::std::ostream* os) {
808 UniversalPrint(
string(str), os);
815 static void Print(
char* str, ::std::ostream* os) {
820 #if GTEST_HAS_STD_WSTRING
824 static void Print(
const wchar_t* str, ::std::ostream* os) {
828 UniversalPrint(::std::wstring(str), os);
837 static void Print(
wchar_t* str, ::std::ostream* os) {
842 template <
typename T>
843 void UniversalTersePrint(
const T& value, ::std::ostream* os) {
851 template <
typename T>
852 void UniversalPrint(
const T& value, ::std::ostream* os) {
856 UniversalPrinter<T1>::Print(value, os);
859 typedef ::std::vector<string> Strings;
868 template <
typename TupleT>
871 #if GTEST_HAS_TR1_TUPLE
872 template <
typename TupleT>
874 typedef TupleT Tuple;
882 const typename ::std::tr1::tuple_element<I, Tuple>::type>::type
get(
883 const Tuple& tuple) {
884 return ::std::tr1::get<I>(tuple);
887 template <
typename TupleT>
888 const size_t TuplePolicy<TupleT>::tuple_size;
889 #endif // GTEST_HAS_TR1_TUPLE
891 #if GTEST_HAS_STD_TUPLE_
892 template <
typename... Types>
893 struct TuplePolicy< ::std::tuple<Types...> > {
894 typedef ::std::tuple<Types...> Tuple;
895 static const size_t tuple_size = ::std::tuple_size<Tuple>::value;
898 struct tuple_element : ::std::tuple_element<I, Tuple> {};
901 static const typename ::std::tuple_element<I, Tuple>::type&
get(
902 const Tuple& tuple) {
903 return ::std::get<I>(tuple);
906 template <
typename... Types>
907 const size_t TuplePolicy< ::std::tuple<Types...> >::tuple_size;
908 #endif // GTEST_HAS_STD_TUPLE_
910 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
920 struct TuplePrefixPrinter {
922 template <
typename Tuple>
923 static void PrintPrefixTo(
const Tuple& t, ::std::ostream* os) {
924 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
925 GTEST_INTENTIONAL_CONST_COND_PUSH_()
927 GTEST_INTENTIONAL_CONST_COND_POP_()
931 typename TuplePolicy<Tuple>::template tuple_element<N - 1>::type>
932 ::Print(TuplePolicy<Tuple>::template get<N - 1>(t), os);
937 template <
typename Tuple>
938 static void TersePrintPrefixToStrings(
const Tuple& t, Strings* strings) {
939 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
940 ::std::stringstream ss;
941 UniversalTersePrint(TuplePolicy<Tuple>::template get<N - 1>(t), &ss);
942 strings->push_back(ss.str());
948 struct TuplePrefixPrinter<0> {
949 template <
typename Tuple>
950 static void PrintPrefixTo(
const Tuple&, ::std::ostream*) {}
952 template <
typename Tuple>
953 static void TersePrintPrefixToStrings(
const Tuple&, Strings*) {}
958 template <
typename Tuple>
959 void PrintTupleTo(
const Tuple& t, ::std::ostream* os) {
961 TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::PrintPrefixTo(t, os);
968 template <
typename Tuple>
969 Strings UniversalTersePrintTupleFieldsToStrings(
const Tuple& value) {
971 TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::
972 TersePrintPrefixToStrings(value, &result);
975 #endif // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
979 template <
typename T>
980 ::std::string PrintToString(
const T& value) {
981 ::std::stringstream ss;
982 internal::UniversalTersePrinter<T>::Print(value, &ss);
991 #include "gtest/internal/custom/gtest-printers.h"
993 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_