LLVM  6.0.0svn
FormatVariadicDetails.h
Go to the documentation of this file.
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"
15 
16 #include <type_traits>
17 
18 namespace llvm {
19 template <typename T, typename Enable = void> struct format_provider {};
20 
21 namespace detail {
23 protected:
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:
34  explicit provider_format_adapter(T &&Item) : Item(std::forward<T>(Item)) {}
35 
36  void format(llvm::raw_ostream &S, StringRef Options) override {
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>
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>
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>
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
93  return std::forward<T>(Item);
94 }
95 
96 template <typename T>
97 typename std::enable_if<uses_format_provider<T>::value,
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,
107  return missing_format_adapter<T>();
108 }
109 }
110 }
111 
112 #endif
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
Definition: BitVector.h:920
std::enable_if< uses_missing_provider< T >::value, missing_format_adapter< T > >::type build_format_adapter(T &&Item)
void format(llvm::raw_ostream &S, StringRef Options) override
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
typename std::decay< T >::type Decayed
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49