• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- FormatVariadicDetails.h - Helpers for FormatVariadic.h ----*- C++-*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_FORMATVARIADIC_DETAILS_H
11 #define LLVM_SUPPORT_FORMATVARIADIC_DETAILS_H
12 
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/raw_ostream.h"
15 
16 #include <type_traits>
17 
18 namespace llvm {
19 template <typename T, typename Enable = void> struct format_provider {};
20 
21 namespace detail {
22 class format_adapter {
23 protected:
~format_adapter()24   virtual ~format_adapter() {}
25 
26 public:
27   virtual void format(raw_ostream &S, StringRef Options) = 0;
28 };
29 
30 template <typename T> class provider_format_adapter : public format_adapter {
31   T Item;
32 
33 public:
provider_format_adapter(T && Item)34   explicit provider_format_adapter(T &&Item) : Item(Item) {}
35 
format(llvm::raw_ostream & S,StringRef Options)36   void format(llvm::raw_ostream &S, StringRef Options) override {
37     format_provider<typename std::decay<T>::type>::format(Item, S, Options);
38   }
39 };
40 
41 template <typename T> class missing_format_adapter;
42 
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);
46 //
47 template <class T> class has_FormatProvider {
48 public:
49   using Decayed = typename std::decay<T>::type;
50   typedef void (*Signature_format)(const Decayed &, llvm::raw_ostream &,
51                                    StringRef);
52 
53   template <typename U>
54   static char test(SameType<Signature_format, &U::format> *);
55 
56   template <typename U> static double test(...);
57 
58   static bool const value =
59       (sizeof(test<llvm::format_provider<Decayed>>(nullptr)) == 1);
60 };
61 
62 // Simple template that decides whether a type T should use the member-function
63 // based format() invocation.
64 template <typename T>
65 struct uses_format_member
66     : public std::integral_constant<
67           bool,
68           std::is_base_of<format_adapter,
69                           typename std::remove_reference<T>::type>::value> {};
70 
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.
74 template <typename T>
75 struct uses_format_provider
76     : public std::integral_constant<
77           bool, !uses_format_member<T>::value && has_FormatProvider<T>::value> {
78 };
79 
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.
84 template <typename T>
85 struct uses_missing_provider
86     : public std::integral_constant<bool,
87                                     !uses_format_member<T>::value &&
88                                         !uses_format_provider<T>::value> {};
89 
90 template <typename T>
91 typename std::enable_if<uses_format_member<T>::value, T>::type
build_format_adapter(T && Item)92 build_format_adapter(T &&Item) {
93   return std::forward<T>(Item);
94 }
95 
96 template <typename T>
97 typename std::enable_if<uses_format_provider<T>::value,
98                         provider_format_adapter<T>>::type
build_format_adapter(T && Item)99 build_format_adapter(T &&Item) {
100   return provider_format_adapter<T>(std::forward<T>(Item));
101 }
102 
103 template <typename T>
104 typename std::enable_if<uses_missing_provider<T>::value,
105                         missing_format_adapter<T>>::type
build_format_adapter(T && Item)106 build_format_adapter(T &&Item) {
107   return missing_format_adapter<T>();
108 }
109 }
110 }
111 
112 #endif
113