|
|
@@ -7,25 +7,27 @@
|
|
|
|
|
|
#include <ostream>
|
|
|
|
|
|
+#include "common/metaprogramming.h"
|
|
|
#include "llvm/Support/raw_os_ostream.h"
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
namespace Carbon {
|
|
|
|
|
|
+// True if T has a method `void Print(llvm::raw_ostream& out) const`.
|
|
|
+template <typename T>
|
|
|
+static constexpr bool HasPrintMethod = Requires<const T, llvm::raw_ostream>(
|
|
|
+ [](auto&& t, auto&& out) -> decltype(t.Print(out)) {});
|
|
|
+
|
|
|
// Support raw_ostream << for types which implement:
|
|
|
// void Print(llvm::raw_ostream& out) const;
|
|
|
-template <typename T,
|
|
|
- typename = std::enable_if_t<
|
|
|
- std::is_member_function_pointer_v<decltype(&T::Print)>>>
|
|
|
+template <typename T, typename = std::enable_if_t<HasPrintMethod<T>>>
|
|
|
auto operator<<(llvm::raw_ostream& out, const T& obj) -> llvm::raw_ostream& {
|
|
|
obj.Print(out);
|
|
|
return out;
|
|
|
}
|
|
|
|
|
|
// Prevents raw_ostream << for pointers to printable types.
|
|
|
-template <typename T,
|
|
|
- typename = std::enable_if_t<
|
|
|
- std::is_member_function_pointer_v<decltype(&T::Print)>>>
|
|
|
+template <typename T, typename = std::enable_if_t<HasPrintMethod<T>>>
|
|
|
__attribute__((unavailable(
|
|
|
"Received a pointer to a printable type, are you missing a `*`? "
|
|
|
"To print as a pointer, cast to void*."))) auto
|
|
|
@@ -33,9 +35,7 @@ operator<<(llvm::raw_ostream& out, const T* /*obj*/) -> llvm::raw_ostream&;
|
|
|
|
|
|
// Support std::ostream << for types which implement:
|
|
|
// void Print(llvm::raw_ostream& out) const;
|
|
|
-template <typename T,
|
|
|
- typename = std::enable_if_t<
|
|
|
- std::is_member_function_pointer_v<decltype(&T::Print)>>>
|
|
|
+template <typename T, typename = std::enable_if_t<HasPrintMethod<T>>>
|
|
|
auto operator<<(std::ostream& out, const T& obj) -> std::ostream& {
|
|
|
llvm::raw_os_ostream raw_os(out);
|
|
|
obj.Print(raw_os);
|
|
|
@@ -43,9 +43,7 @@ auto operator<<(std::ostream& out, const T& obj) -> std::ostream& {
|
|
|
}
|
|
|
|
|
|
// Prevents std::ostream << for pointers to printable types.
|
|
|
-template <typename T,
|
|
|
- typename = std::enable_if_t<
|
|
|
- std::is_member_function_pointer_v<decltype(&T::Print)>>>
|
|
|
+template <typename T, typename = std::enable_if_t<HasPrintMethod<T>>>
|
|
|
__attribute__((unavailable(
|
|
|
"Received a pointer to a printable type, are you missing a `*`? "
|
|
|
"To print as a pointer, cast to void*."))) auto
|
|
|
@@ -53,9 +51,7 @@ operator<<(std::ostream& out, const T* /*obj*/) -> std::ostream&;
|
|
|
|
|
|
// Allow GoogleTest and GoogleMock to print even pointers by dereferencing them.
|
|
|
// This is important to allow automatic printing of arguments of mocked APIs.
|
|
|
-template <typename T,
|
|
|
- typename = std::enable_if_t<
|
|
|
- std::is_member_function_pointer_v<decltype(&T::Print)>>>
|
|
|
+template <typename T, typename = std::enable_if_t<HasPrintMethod<T>>>
|
|
|
void PrintTo(T* p, std::ostream* out) {
|
|
|
*out << static_cast<const void*>(p);
|
|
|
|