1 //===- FormatVariadicDetails.h - Helpers for FormatVariadic.h ----*- C++-*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_FORMATVARIADIC_DETAILS_H
11 #define LLVM_SUPPORT_FORMATVARIADIC_DETAILS_H
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/raw_ostream.h"
16 #include <type_traits>
19 template <typename T, typename Enable = void> struct format_provider {};
23 class format_adapter {
25 virtual ~format_adapter() {}
28 virtual void format(raw_ostream &S, StringRef Options) = 0;
31 template <typename T> class provider_format_adapter : public format_adapter {
35 explicit provider_format_adapter(T &&Item) : Item(std::forward<T>(Item)) {}
37 void format(llvm::raw_ostream &S, StringRef Options) override {
38 format_provider<typename std::decay<T>::type>::format(Item, S, Options);
43 class stream_operator_format_adapter : public format_adapter {
47 explicit stream_operator_format_adapter(T &&Item)
48 : Item(std::forward<T>(Item)) {}
50 void format(llvm::raw_ostream &S, StringRef Options) override { S << Item; }
53 template <typename T> class missing_format_adapter;
55 // Test if format_provider<T> is defined on T and contains a member function
56 // with the signature:
57 // static void format(const T&, raw_stream &, StringRef);
59 template <class T> class has_FormatProvider {
61 using Decayed = typename std::decay<T>::type;
62 typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
66 static char test(SameType<Signature_format, &U::format> *);
68 template <typename U> static double test(...);
70 static bool const value =
71 (sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
74 // Test if raw_ostream& << T -> raw_ostream& is findable via ADL.
75 template <class T> class has_StreamOperator {
77 using ConstRefT = const typename std::decay<T>::type &;
80 static char test(typename std::enable_if<
81 std::is_same<decltype(std::declval<llvm::raw_ostream &>()
82 << std::declval<U>()),
83 llvm::raw_ostream &>::value,
86 template <typename U> static double test(...);
88 static bool const value = (sizeof(test<ConstRefT>(nullptr)) == 1);
91 // Simple template that decides whether a type T should use the member-function
92 // based format() invocation.
94 struct uses_format_member
95 : public std::integral_constant<
97 std::is_base_of<format_adapter,
98 typename std::remove_reference<T>::type>::value> {};
100 // Simple template that decides whether a type T should use the format_provider
101 // based format() invocation. The member function takes priority, so this test
102 // will only be true if there is not ALSO a format member.
103 template <typename T>
104 struct uses_format_provider
105 : public std::integral_constant<
106 bool, !uses_format_member<T>::value && has_FormatProvider<T>::value> {
109 // Simple template that decides whether a type T should use the operator<<
110 // based format() invocation. This takes last priority.
111 template <typename T>
112 struct uses_stream_operator
113 : public std::integral_constant<bool, !uses_format_member<T>::value &&
114 !uses_format_provider<T>::value &&
115 has_StreamOperator<T>::value> {};
117 // Simple template that decides whether a type T has neither a member-function
118 // nor format_provider based implementation that it can use. Mostly used so
119 // that the compiler spits out a nice diagnostic when a type with no format
120 // implementation can be located.
121 template <typename T>
122 struct uses_missing_provider
123 : public std::integral_constant<bool, !uses_format_member<T>::value &&
124 !uses_format_provider<T>::value &&
125 !uses_stream_operator<T>::value> {
128 template <typename T>
129 typename std::enable_if<uses_format_member<T>::value, T>::type
130 build_format_adapter(T &&Item) {
131 return std::forward<T>(Item);
134 template <typename T>
135 typename std::enable_if<uses_format_provider<T>::value,
136 provider_format_adapter<T>>::type
137 build_format_adapter(T &&Item) {
138 return provider_format_adapter<T>(std::forward<T>(Item));
141 template <typename T>
142 typename std::enable_if<uses_stream_operator<T>::value,
143 stream_operator_format_adapter<T>>::type
144 build_format_adapter(T &&Item) {
145 // If the caller passed an Error by value, then stream_operator_format_adapter
146 // would be responsible for consuming it.
147 // Make the caller opt into this by calling fmt_consume().
149 !std::is_same<llvm::Error, typename std::remove_cv<T>::type>::value,
150 "llvm::Error-by-value must be wrapped in fmt_consume() for formatv");
151 return stream_operator_format_adapter<T>(std::forward<T>(Item));
154 template <typename T>
155 typename std::enable_if<uses_missing_provider<T>::value,
156 missing_format_adapter<T>>::type
157 build_format_adapter(T &&Item) {
158 return missing_format_adapter<T>();