File indexing completed on 2024-11-15 09:43:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0102 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0103
0104 #include <functional>
0105 #include <memory>
0106 #include <ostream> // NOLINT
0107 #include <sstream>
0108 #include <string>
0109 #include <tuple>
0110 #include <type_traits>
0111 #include <utility>
0112 #include <vector>
0113
0114 #include "gtest/internal/gtest-internal.h"
0115 #include "gtest/internal/gtest-port.h"
0116
0117 namespace testing {
0118
0119
0120
0121 namespace internal {
0122
0123 template <typename T>
0124 void UniversalPrint(const T& value, ::std::ostream* os);
0125
0126
0127
0128 struct ContainerPrinter {
0129 template <typename T,
0130 typename = typename std::enable_if<
0131 (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
0132 !IsRecursiveContainer<T>::value>::type>
0133 static void PrintValue(const T& container, std::ostream* os) {
0134 const size_t kMaxCount = 32;
0135 *os << '{';
0136 size_t count = 0;
0137 for (auto&& elem : container) {
0138 if (count > 0) {
0139 *os << ',';
0140 if (count == kMaxCount) {
0141 *os << " ...";
0142 break;
0143 }
0144 }
0145 *os << ' ';
0146
0147
0148 internal::UniversalPrint(elem, os);
0149 ++count;
0150 }
0151
0152 if (count > 0) {
0153 *os << ' ';
0154 }
0155 *os << '}';
0156 }
0157 };
0158
0159
0160
0161
0162
0163
0164
0165 struct FunctionPointerPrinter {
0166 template <typename T, typename = typename std::enable_if<
0167 std::is_function<T>::value>::type>
0168 static void PrintValue(T* p, ::std::ostream* os) {
0169 if (p == nullptr) {
0170 *os << "NULL";
0171 } else {
0172
0173
0174
0175 *os << reinterpret_cast<const void*>(p);
0176 }
0177 }
0178 };
0179
0180 struct PointerPrinter {
0181 template <typename T>
0182 static void PrintValue(T* p, ::std::ostream* os) {
0183 if (p == nullptr) {
0184 *os << "NULL";
0185 } else {
0186
0187
0188
0189 *os << p;
0190 }
0191 }
0192 };
0193
0194 namespace internal_stream_operator_without_lexical_name_lookup {
0195
0196
0197
0198
0199
0200 struct LookupBlocker {};
0201 void operator<<(LookupBlocker, LookupBlocker);
0202
0203 struct StreamPrinter {
0204 template <typename T,
0205
0206
0207 typename = typename std::enable_if<
0208 !std::is_member_pointer<T>::value>::type,
0209
0210
0211 typename = decltype(std::declval<std::ostream&>()
0212 << std::declval<const T&>())>
0213 static void PrintValue(const T& value, ::std::ostream* os) {
0214
0215
0216 *os << value;
0217 }
0218 };
0219
0220 }
0221
0222 struct ProtobufPrinter {
0223
0224
0225
0226 static const size_t kProtobufOneLinerMaxLength = 50;
0227
0228 template <typename T,
0229 typename = typename std::enable_if<
0230 internal::HasDebugStringAndShortDebugString<T>::value>::type>
0231 static void PrintValue(const T& value, ::std::ostream* os) {
0232 std::string pretty_str = value.ShortDebugString();
0233 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
0234 pretty_str = "\n" + value.DebugString();
0235 }
0236 *os << ("<" + pretty_str + ">");
0237 }
0238 };
0239
0240 struct ConvertibleToIntegerPrinter {
0241
0242
0243
0244
0245
0246
0247
0248 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
0249 *os << value;
0250 }
0251 };
0252
0253 struct ConvertibleToStringViewPrinter {
0254 #if GTEST_INTERNAL_HAS_STRING_VIEW
0255 static void PrintValue(internal::StringView value, ::std::ostream* os) {
0256 internal::UniversalPrint(value, os);
0257 }
0258 #endif
0259 };
0260
0261
0262
0263 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
0264 size_t count, ::std::ostream* os);
0265 struct RawBytesPrinter {
0266
0267 template <typename T, size_t = sizeof(T)>
0268 static void PrintValue(const T& value, ::std::ostream* os) {
0269 PrintBytesInObjectTo(
0270 static_cast<const unsigned char*>(
0271
0272 reinterpret_cast<const void*>(std::addressof(value))),
0273 sizeof(value), os);
0274 }
0275 };
0276
0277 struct FallbackPrinter {
0278 template <typename T>
0279 static void PrintValue(const T&, ::std::ostream* os) {
0280 *os << "(incomplete type)";
0281 }
0282 };
0283
0284
0285 template <typename T, typename E, typename Printer, typename... Printers>
0286 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
0287
0288 template <typename T, typename Printer, typename... Printers>
0289 struct FindFirstPrinter<
0290 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
0291 Printer, Printers...> {
0292 using type = Printer;
0293 };
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304 template <typename T>
0305 void PrintWithFallback(const T& value, ::std::ostream* os) {
0306 using Printer = typename FindFirstPrinter<
0307 T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
0308 internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
0309 ProtobufPrinter, ConvertibleToIntegerPrinter,
0310 ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type;
0311 Printer::PrintValue(value, os);
0312 }
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329 template <typename ToPrint, typename OtherOperand>
0330 class FormatForComparison {
0331 public:
0332 static ::std::string Format(const ToPrint& value) {
0333 return ::testing::PrintToString(value);
0334 }
0335 };
0336
0337
0338 template <typename ToPrint, size_t N, typename OtherOperand>
0339 class FormatForComparison<ToPrint[N], OtherOperand> {
0340 public:
0341 static ::std::string Format(const ToPrint* value) {
0342 return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
0343 }
0344 };
0345
0346
0347
0348
0349 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
0350 template <typename OtherOperand> \
0351 class FormatForComparison<CharType*, OtherOperand> { \
0352 public: \
0353 static ::std::string Format(CharType* value) { \
0354 return ::testing::PrintToString(static_cast<const void*>(value)); \
0355 } \
0356 }
0357
0358 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
0359 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
0360 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
0361 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
0362 #ifdef __cpp_lib_char8_t
0363 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
0364 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
0365 #endif
0366 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
0367 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
0368 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
0369 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
0370
0371 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
0372
0373
0374
0375
0376 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
0377 template <> \
0378 class FormatForComparison<CharType*, OtherStringType> { \
0379 public: \
0380 static ::std::string Format(CharType* value) { \
0381 return ::testing::PrintToString(value); \
0382 } \
0383 }
0384
0385 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
0386 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
0387 #ifdef __cpp_char8_t
0388 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
0389 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
0390 #endif
0391 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
0392 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
0393 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
0394 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
0395
0396 #if GTEST_HAS_STD_WSTRING
0397 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
0398 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
0399 #endif
0400
0401 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411 template <typename T1, typename T2>
0412 std::string FormatForComparisonFailureMessage(const T1& value,
0413 const T2& ) {
0414 return FormatForComparison<T1, T2>::Format(value);
0415 }
0416
0417
0418
0419
0420
0421
0422
0423
0424 template <typename T>
0425 class UniversalPrinter;
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438 template <typename T>
0439 void PrintTo(const T& value, ::std::ostream* os) {
0440 internal::PrintWithFallback(value, os);
0441 }
0442
0443
0444
0445
0446
0447
0448 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
0449 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
0450 inline void PrintTo(char c, ::std::ostream* os) {
0451
0452
0453
0454 PrintTo(static_cast<unsigned char>(c), os);
0455 }
0456
0457
0458 inline void PrintTo(bool x, ::std::ostream* os) {
0459 *os << (x ? "true" : "false");
0460 }
0461
0462
0463
0464
0465
0466
0467
0468
0469 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
0470
0471 GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
0472 inline void PrintTo(char16_t c, ::std::ostream* os) {
0473 PrintTo(ImplicitCast_<char32_t>(c), os);
0474 }
0475 #ifdef __cpp_char8_t
0476 inline void PrintTo(char8_t c, ::std::ostream* os) {
0477 PrintTo(ImplicitCast_<char32_t>(c), os);
0478 }
0479 #endif
0480
0481
0482 #if defined(__SIZEOF_INT128__)
0483 GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os);
0484 GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os);
0485 #endif
0486
0487
0488 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
0489 inline void PrintTo(char* s, ::std::ostream* os) {
0490 PrintTo(ImplicitCast_<const char*>(s), os);
0491 }
0492
0493
0494
0495 inline void PrintTo(const signed char* s, ::std::ostream* os) {
0496 PrintTo(ImplicitCast_<const void*>(s), os);
0497 }
0498 inline void PrintTo(signed char* s, ::std::ostream* os) {
0499 PrintTo(ImplicitCast_<const void*>(s), os);
0500 }
0501 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
0502 PrintTo(ImplicitCast_<const void*>(s), os);
0503 }
0504 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
0505 PrintTo(ImplicitCast_<const void*>(s), os);
0506 }
0507 #ifdef __cpp_char8_t
0508
0509 GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
0510 inline void PrintTo(char8_t* s, ::std::ostream* os) {
0511 PrintTo(ImplicitCast_<const char8_t*>(s), os);
0512 }
0513 #endif
0514
0515 GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os);
0516 inline void PrintTo(char16_t* s, ::std::ostream* os) {
0517 PrintTo(ImplicitCast_<const char16_t*>(s), os);
0518 }
0519
0520 GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os);
0521 inline void PrintTo(char32_t* s, ::std::ostream* os) {
0522 PrintTo(ImplicitCast_<const char32_t*>(s), os);
0523 }
0524
0525
0526
0527
0528
0529
0530 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
0531
0532 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
0533 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
0534 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
0535 }
0536 #endif
0537
0538
0539
0540
0541
0542
0543 template <typename T>
0544 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
0545 UniversalPrint(a[0], os);
0546 for (size_t i = 1; i != count; i++) {
0547 *os << ", ";
0548 UniversalPrint(a[i], os);
0549 }
0550 }
0551
0552
0553 GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os);
0554 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
0555 PrintStringTo(s, os);
0556 }
0557
0558
0559 #ifdef __cpp_char8_t
0560 GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
0561 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
0562 PrintU8StringTo(s, os);
0563 }
0564 #endif
0565
0566
0567 GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
0568 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
0569 PrintU16StringTo(s, os);
0570 }
0571
0572
0573 GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
0574 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
0575 PrintU32StringTo(s, os);
0576 }
0577
0578
0579 #if GTEST_HAS_STD_WSTRING
0580 GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os);
0581 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
0582 PrintWideStringTo(s, os);
0583 }
0584 #endif
0585
0586 #if GTEST_INTERNAL_HAS_STRING_VIEW
0587
0588 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
0589 PrintTo(::std::string(sp), os);
0590 }
0591 #endif
0592
0593 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
0594
0595 #if GTEST_HAS_RTTI
0596 inline void PrintTo(const std::type_info& info, std::ostream* os) {
0597 *os << internal::GetTypeName(info);
0598 }
0599 #endif
0600
0601 template <typename T>
0602 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
0603 UniversalPrinter<T&>::Print(ref.get(), os);
0604 }
0605
0606 inline const void* VoidifyPointer(const void* p) { return p; }
0607 inline const void* VoidifyPointer(volatile const void* p) {
0608 return const_cast<const void*>(p);
0609 }
0610
0611 template <typename T, typename Ptr>
0612 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
0613 if (ptr == nullptr) {
0614 *os << "(nullptr)";
0615 } else {
0616
0617 *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
0618 }
0619 }
0620 template <typename T, typename Ptr,
0621 typename = typename std::enable_if<!std::is_void<T>::value &&
0622 !std::is_array<T>::value>::type>
0623 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
0624 if (ptr == nullptr) {
0625 *os << "(nullptr)";
0626 } else {
0627 *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
0628 UniversalPrinter<T>::Print(*ptr, os);
0629 *os << ")";
0630 }
0631 }
0632
0633 template <typename T, typename D>
0634 void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
0635 (PrintSmartPointer<T>)(ptr, os, 0);
0636 }
0637
0638 template <typename T>
0639 void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
0640 (PrintSmartPointer<T>)(ptr, os, 0);
0641 }
0642
0643
0644
0645 template <typename T>
0646 void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
0647 ::std::ostream*) {}
0648
0649 template <typename T, size_t I>
0650 void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
0651 ::std::ostream* os) {
0652 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
0653 GTEST_INTENTIONAL_CONST_COND_PUSH_()
0654 if (I > 1) {
0655 GTEST_INTENTIONAL_CONST_COND_POP_()
0656 *os << ", ";
0657 }
0658 UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print(
0659 std::get<I - 1>(t), os);
0660 }
0661
0662 template <typename... Types>
0663 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
0664 *os << "(";
0665 PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
0666 *os << ")";
0667 }
0668
0669
0670 template <typename T1, typename T2>
0671 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
0672 *os << '(';
0673
0674
0675 UniversalPrinter<T1>::Print(value.first, os);
0676 *os << ", ";
0677 UniversalPrinter<T2>::Print(value.second, os);
0678 *os << ')';
0679 }
0680
0681
0682
0683 template <typename T>
0684 class UniversalPrinter {
0685 public:
0686
0687
0688 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
0689
0690
0691
0692
0693 static void Print(const T& value, ::std::ostream* os) {
0694
0695
0696
0697
0698
0699
0700
0701
0702 PrintTo(value, os);
0703 }
0704
0705 GTEST_DISABLE_MSC_WARNINGS_POP_()
0706 };
0707
0708
0709 template <typename T>
0710 class UniversalPrinter<const T> : public UniversalPrinter<T> {};
0711
0712 #if GTEST_INTERNAL_HAS_ANY
0713
0714
0715
0716 template <>
0717 class UniversalPrinter<Any> {
0718 public:
0719 static void Print(const Any& value, ::std::ostream* os) {
0720 if (value.has_value()) {
0721 *os << "value of type " << GetTypeName(value);
0722 } else {
0723 *os << "no value";
0724 }
0725 }
0726
0727 private:
0728 static std::string GetTypeName(const Any& value) {
0729 #if GTEST_HAS_RTTI
0730 return internal::GetTypeName(value.type());
0731 #else
0732 static_cast<void>(value);
0733 return "<unknown_type>";
0734 #endif
0735 }
0736 };
0737
0738 #endif
0739
0740 #if GTEST_INTERNAL_HAS_OPTIONAL
0741
0742
0743
0744 template <typename T>
0745 class UniversalPrinter<Optional<T>> {
0746 public:
0747 static void Print(const Optional<T>& value, ::std::ostream* os) {
0748 *os << '(';
0749 if (!value) {
0750 *os << "nullopt";
0751 } else {
0752 UniversalPrint(*value, os);
0753 }
0754 *os << ')';
0755 }
0756 };
0757
0758 template <>
0759 class UniversalPrinter<decltype(Nullopt())> {
0760 public:
0761 static void Print(decltype(Nullopt()), ::std::ostream* os) {
0762 *os << "(nullopt)";
0763 }
0764 };
0765
0766 #endif
0767
0768 #if GTEST_INTERNAL_HAS_VARIANT
0769
0770
0771
0772 template <typename... T>
0773 class UniversalPrinter<Variant<T...>> {
0774 public:
0775 static void Print(const Variant<T...>& value, ::std::ostream* os) {
0776 *os << '(';
0777 #if GTEST_HAS_ABSL
0778 absl::visit(Visitor{os, value.index()}, value);
0779 #else
0780 std::visit(Visitor{os, value.index()}, value);
0781 #endif
0782 *os << ')';
0783 }
0784
0785 private:
0786 struct Visitor {
0787 template <typename U>
0788 void operator()(const U& u) const {
0789 *os << "'" << GetTypeName<U>() << "(index = " << index
0790 << ")' with value ";
0791 UniversalPrint(u, os);
0792 }
0793 ::std::ostream* os;
0794 std::size_t index;
0795 };
0796 };
0797
0798 #endif
0799
0800
0801
0802 template <typename T>
0803 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
0804 if (len == 0) {
0805 *os << "{}";
0806 } else {
0807 *os << "{ ";
0808 const size_t kThreshold = 18;
0809 const size_t kChunkSize = 8;
0810
0811
0812
0813 if (len <= kThreshold) {
0814 PrintRawArrayTo(begin, len, os);
0815 } else {
0816 PrintRawArrayTo(begin, kChunkSize, os);
0817 *os << ", ..., ";
0818 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
0819 }
0820 *os << " }";
0821 }
0822 }
0823
0824 GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
0825 ::std::ostream* os);
0826
0827 #ifdef __cpp_char8_t
0828
0829 GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
0830 ::std::ostream* os);
0831 #endif
0832
0833
0834 GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
0835 ::std::ostream* os);
0836
0837
0838 GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
0839 ::std::ostream* os);
0840
0841
0842 GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len,
0843 ::std::ostream* os);
0844
0845
0846 template <typename T, size_t N>
0847 class UniversalPrinter<T[N]> {
0848 public:
0849
0850
0851 static void Print(const T (&a)[N], ::std::ostream* os) {
0852 UniversalPrintArray(a, N, os);
0853 }
0854 };
0855
0856
0857 template <typename T>
0858 class UniversalPrinter<T&> {
0859 public:
0860
0861
0862 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
0863
0864 static void Print(const T& value, ::std::ostream* os) {
0865
0866
0867 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
0868
0869
0870 UniversalPrint(value, os);
0871 }
0872
0873 GTEST_DISABLE_MSC_WARNINGS_POP_()
0874 };
0875
0876
0877
0878
0879
0880 template <typename T>
0881 class UniversalTersePrinter {
0882 public:
0883 static void Print(const T& value, ::std::ostream* os) {
0884 UniversalPrint(value, os);
0885 }
0886 };
0887 template <typename T>
0888 class UniversalTersePrinter<T&> {
0889 public:
0890 static void Print(const T& value, ::std::ostream* os) {
0891 UniversalPrint(value, os);
0892 }
0893 };
0894 template <typename T, size_t N>
0895 class UniversalTersePrinter<T[N]> {
0896 public:
0897 static void Print(const T (&value)[N], ::std::ostream* os) {
0898 UniversalPrinter<T[N]>::Print(value, os);
0899 }
0900 };
0901 template <>
0902 class UniversalTersePrinter<const char*> {
0903 public:
0904 static void Print(const char* str, ::std::ostream* os) {
0905 if (str == nullptr) {
0906 *os << "NULL";
0907 } else {
0908 UniversalPrint(std::string(str), os);
0909 }
0910 }
0911 };
0912 template <>
0913 class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> {
0914 };
0915
0916 #ifdef __cpp_char8_t
0917 template <>
0918 class UniversalTersePrinter<const char8_t*> {
0919 public:
0920 static void Print(const char8_t* str, ::std::ostream* os) {
0921 if (str == nullptr) {
0922 *os << "NULL";
0923 } else {
0924 UniversalPrint(::std::u8string(str), os);
0925 }
0926 }
0927 };
0928 template <>
0929 class UniversalTersePrinter<char8_t*>
0930 : public UniversalTersePrinter<const char8_t*> {};
0931 #endif
0932
0933 template <>
0934 class UniversalTersePrinter<const char16_t*> {
0935 public:
0936 static void Print(const char16_t* str, ::std::ostream* os) {
0937 if (str == nullptr) {
0938 *os << "NULL";
0939 } else {
0940 UniversalPrint(::std::u16string(str), os);
0941 }
0942 }
0943 };
0944 template <>
0945 class UniversalTersePrinter<char16_t*>
0946 : public UniversalTersePrinter<const char16_t*> {};
0947
0948 template <>
0949 class UniversalTersePrinter<const char32_t*> {
0950 public:
0951 static void Print(const char32_t* str, ::std::ostream* os) {
0952 if (str == nullptr) {
0953 *os << "NULL";
0954 } else {
0955 UniversalPrint(::std::u32string(str), os);
0956 }
0957 }
0958 };
0959 template <>
0960 class UniversalTersePrinter<char32_t*>
0961 : public UniversalTersePrinter<const char32_t*> {};
0962
0963 #if GTEST_HAS_STD_WSTRING
0964 template <>
0965 class UniversalTersePrinter<const wchar_t*> {
0966 public:
0967 static void Print(const wchar_t* str, ::std::ostream* os) {
0968 if (str == nullptr) {
0969 *os << "NULL";
0970 } else {
0971 UniversalPrint(::std::wstring(str), os);
0972 }
0973 }
0974 };
0975 #endif
0976
0977 template <>
0978 class UniversalTersePrinter<wchar_t*> {
0979 public:
0980 static void Print(wchar_t* str, ::std::ostream* os) {
0981 UniversalTersePrinter<const wchar_t*>::Print(str, os);
0982 }
0983 };
0984
0985 template <typename T>
0986 void UniversalTersePrint(const T& value, ::std::ostream* os) {
0987 UniversalTersePrinter<T>::Print(value, os);
0988 }
0989
0990
0991
0992
0993
0994 template <typename T>
0995 void UniversalPrint(const T& value, ::std::ostream* os) {
0996
0997
0998 typedef T T1;
0999 UniversalPrinter<T1>::Print(value, os);
1000 }
1001
1002 typedef ::std::vector<::std::string> Strings;
1003
1004
1005
1006 template <typename Tuple>
1007 void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
1008 Strings*) {}
1009 template <typename Tuple, size_t I>
1010 void TersePrintPrefixToStrings(const Tuple& t,
1011 std::integral_constant<size_t, I>,
1012 Strings* strings) {
1013 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1014 strings);
1015 ::std::stringstream ss;
1016 UniversalTersePrint(std::get<I - 1>(t), &ss);
1017 strings->push_back(ss.str());
1018 }
1019
1020
1021
1022
1023 template <typename Tuple>
1024 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
1025 Strings result;
1026 TersePrintPrefixToStrings(
1027 value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(),
1028 &result);
1029 return result;
1030 }
1031
1032 }
1033
1034 template <typename T>
1035 ::std::string PrintToString(const T& value) {
1036 ::std::stringstream ss;
1037 internal::UniversalTersePrinter<T>::Print(value, &ss);
1038 return ss.str();
1039 }
1040
1041 }
1042
1043
1044
1045
1046 #include "gtest/internal/custom/gtest-printers.h"
1047
1048 #endif