1 //===-- raw-ostream.h - Support for printing using raw_ostream --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 // This file is not part of gtest, but extends it to support LLVM libraries.
9 // This is not a public API for testing - it's a detail of LLVM's gtest.
11 // gtest allows providing printers for custom types by defining operator<<.
12 // In LLVM, operator<< usually takes llvm:raw_ostream& instead of std::ostream&.
14 // This file defines a template printable(V), which returns a version of V that
15 // can be streamed into a std::ostream.
17 // This interface is chosen so that in the default case (printable(V) is V),
18 // the main gtest code calls operator<<(OS, V) itself. gtest-printers carefully
19 // controls the lookup to enable fallback printing (see testing::internal2).
20 //===----------------------------------------------------------------------===//
22 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_RAW_OSTREAM_H_
23 #define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_RAW_OSTREAM_H_
25 namespace llvm_gtest
{
26 // StreamSwitch is a trait that tells us how to stream a T into a std::ostream.
27 // By default, we just stream the T directly. We'll specialize this later.
28 template <typename T
, typename Enable
= void> struct StreamSwitch
{
29 static const T
& printable(const T
& V
) { return V
; }
32 // printable() returns a version of its argument that can be streamed into a
33 // std::ostream. This may be the argument itself, or some other representation.
35 auto printable(const T
&V
) -> decltype(StreamSwitch
<T
>::printable(V
)) {
36 // We delegate to the trait, to allow partial specialization.
37 return StreamSwitch
<T
>::printable(V
);
39 } // namespace llvm_gtest
41 // If raw_ostream support is enabled, we specialize for types with operator<<
42 // that takes a raw_ostream.
43 #if !GTEST_NO_LLVM_SUPPORT
44 #include "llvm/ADT/Optional.h"
45 #include "llvm/Support/raw_os_ostream.h"
46 #include "llvm/Support/raw_ostream.h"
48 namespace llvm_gtest
{
50 // The printable() of a raw_ostream-enabled type T is a RawStreamProxy<T>.
51 // It uses raw_os_ostream to write the wrapped value to a std::ostream.
53 struct RawStreamProxy
{
55 friend std::ostream
&operator<<(std::ostream
&S
, const RawStreamProxy
<T
> &V
) {
56 llvm::raw_os_ostream
OS(S
);
62 // We enable raw_ostream treatment if `(raw_ostream&) << (const T&)` is valid.
63 // We don't want implicit conversions on the RHS (e.g. to bool!), so "consume"
64 // the possible conversion by passing something convertible to const T& instead.
65 template <typename T
> struct ConvertibleTo
{ operator T(); };
67 struct StreamSwitch
<T
, decltype((void)(std::declval
<llvm::raw_ostream
&>()
68 << ConvertibleTo
<const T
&>()))> {
69 static const RawStreamProxy
<T
> printable(const T
&V
) { return {V
}; }
72 // llvm::Optional has a template operator<<, which means it will not accept any
73 // implicit conversions, so we need to special-case it here.
75 struct StreamSwitch
<llvm::Optional
<T
>,
76 decltype((void)(std::declval
<llvm::raw_ostream
&>()
77 << std::declval
<llvm::Optional
<T
>>()))> {
78 static const RawStreamProxy
<llvm::Optional
<T
>>
79 printable(const llvm::Optional
<T
> &V
) {
83 } // namespace llvm_gtest
84 #endif // !GTEST_NO_LLVM_SUPPORT
86 #endif // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_RAW_OSTREAM_H_