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 {};
22 class format_adapter {
24 virtual ~format_adapter() {}
27 virtual void format(raw_ostream &S, StringRef Options) = 0;
30 template <typename T> class provider_format_adapter : public format_adapter {
34 explicit provider_format_adapter(T &&Item) : Item(std::forward<T>(Item)) {}
36 void format(llvm::raw_ostream &S, StringRef Options) override {
37 format_provider<typename std::decay<T>::type>::format(Item, S, Options);
41 template <typename T> class missing_format_adapter;
43 // Test if format_provider<T> is defined on T and contains a member function
44 // with the signature:
45 // static void format(const T&, raw_stream &, StringRef);
47 template <class T> class has_FormatProvider {
49 using Decayed = typename std::decay<T>::type;
50 typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
54 static char test(SameType<Signature_format, &U::format> *);
56 template <typename U> static double test(...);
58 static bool const value =
59 (sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
62 // Simple template that decides whether a type T should use the member-function
63 // based format() invocation.
65 struct uses_format_member
66 : public std::integral_constant<
68 std::is_base_of<format_adapter,
69 typename std::remove_reference<T>::type>::value> {};
71 // Simple template that decides whether a type T should use the format_provider
72 // based format() invocation. The member function takes priority, so this test
73 // will only be true if there is not ALSO a format member.
75 struct uses_format_provider
76 : public std::integral_constant<
77 bool, !uses_format_member<T>::value && has_FormatProvider<T>::value> {
80 // Simple template that decides whether a type T has neither a member-function
81 // nor format_provider based implementation that it can use. Mostly used so
82 // that the compiler spits out a nice diagnostic when a type with no format
83 // implementation can be located.
85 struct uses_missing_provider
86 : public std::integral_constant<bool,
87 !uses_format_member<T>::value &&
88 !uses_format_provider<T>::value> {};
91 typename std::enable_if<uses_format_member<T>::value, T>::type
92 build_format_adapter(T &&Item) {
93 return std::forward<T>(Item);
97 typename std::enable_if<uses_format_provider<T>::value,
98 provider_format_adapter<T>>::type
99 build_format_adapter(T &&Item) {
100 return provider_format_adapter<T>(std::forward<T>(Item));
103 template <typename T>
104 typename std::enable_if<uses_missing_provider<T>::value,
105 missing_format_adapter<T>>::type
106 build_format_adapter(T &&Item) {
107 return missing_format_adapter<T>();