diff options
author | Abseil Team <absl-team@google.com> | 2021-01-13 10:57:58 -0500 |
---|---|---|
committer | Derek Mauro <dmauro@google.com> | 2021-01-13 20:59:36 -0500 |
commit | 6b2e74905e9a7e70c7d1017ee0dfe5a8e88a1300 (patch) | |
tree | 2bc58b9f9d515a9c6e70fc33ce75001f10d49598 /googletest/include | |
parent | 50ce52016139a4346a94df71249c14c5d286e000 (diff) | |
download | googletest-6b2e74905e9a7e70c7d1017ee0dfe5a8e88a1300.tar.gz googletest-6b2e74905e9a7e70c7d1017ee0dfe5a8e88a1300.zip |
Googletest export
Print unique_ptr/shared_ptr recursively.
Given that they are smart pointers, it is unlikely that the inner object is
invalid.
PiperOrigin-RevId: 351586888
Diffstat (limited to 'googletest/include')
-rw-r--r-- | googletest/include/gtest/gtest-printers.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 1f72b780..37e949b6 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -101,6 +101,7 @@ #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ #include <functional> +#include <memory> #include <ostream> // NOLINT #include <sstream> #include <string> @@ -108,6 +109,7 @@ #include <type_traits> #include <utility> #include <vector> + #include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-port.h" @@ -573,6 +575,43 @@ void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) { UniversalPrinter<T&>::Print(ref.get(), os); } +inline const void* VoidifyPointer(const void* p) { return p; } +inline const void* VoidifyPointer(volatile const void* p) { + return const_cast<const void*>(p); +} + +template <typename T, typename Ptr> +void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) { + if (ptr == nullptr) { + *os << "(nullptr)"; + } else { + // We can't print the value. Just print the pointer.. + *os << "(" << (VoidifyPointer)(ptr.get()) << ")"; + } +} +template <typename T, typename Ptr, + typename = typename std::enable_if<!std::is_void<T>::value && + !std::is_array<T>::value>::type> +void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) { + if (ptr == nullptr) { + *os << "(nullptr)"; + } else { + *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = "; + UniversalPrinter<T>::Print(*ptr, os); + *os << ")"; + } +} + +template <typename T, typename D> +void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) { + (PrintSmartPointer<T>)(ptr, os, 0); +} + +template <typename T> +void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) { + (PrintSmartPointer<T>)(ptr, os, 0); +} + // Helper function for printing a tuple. T must be instantiated with // a tuple type. template <typename T> |