OSDN Git Service

[Support] Introduce llvm::formatv() function.
[android-x86/external-llvm.git] / include / llvm / Support / FormatAdapters.h
1 //===- FormatAdapters.h - Formatters for common LLVM types -----*- 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_FORMATADAPTERS_H
11 #define LLVM_SUPPORT_FORMATADAPTERS_H
12
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/FormatCommon.h"
16 #include "llvm/Support/FormatVariadicDetails.h"
17 #include "llvm/Support/raw_ostream.h"
18
19 namespace llvm {
20 template <typename T> class AdapterBase {
21 protected:
22   explicit AdapterBase(T &&Item) : Item(Item) {}
23
24   T Item;
25   static_assert(!detail::uses_missing_provider<T>::value,
26                 "Item does not have a format provider!");
27 };
28
29 namespace detail {
30 template <typename T> class AlignAdapter : public AdapterBase<T> {
31   AlignStyle Where;
32   size_t Amount;
33
34 public:
35   AlignAdapter(T &&Item, AlignStyle Where, size_t Amount)
36       : AdapterBase(std::forward<T>(Item)), Where(Where), Amount(Amount) {}
37
38   void format(llvm::raw_ostream &Stream, StringRef Style) {
39     auto Wrapper = detail::build_format_wrapper(std::forward<T>(Item));
40     FmtAlign(Wrapper, Where, Amount).format(Stream, Style);
41   }
42 };
43
44 template <typename T> class PadAdapter : public AdapterBase<T> {
45   size_t Left;
46   size_t Right;
47
48 public:
49   PadAdapter(T &&Item, size_t Left, size_t Right)
50       : AdapterBase(std::forward<T>(Item)), Left(Left), Right(Right) {}
51
52   void format(llvm::raw_ostream &Stream, StringRef Style) {
53     auto Wrapper = detail::build_format_wrapper(std::forward<T>(Item));
54     Stream.indent(Left);
55     Wrapper.format(Stream, Style);
56     Stream.indent(Right);
57   }
58 };
59
60 template <typename T> class RepeatAdapter : public AdapterBase<T> {
61   size_t Count;
62
63 public:
64   RepeatAdapter(T &&Item, size_t Count)
65       : AdapterBase(std::forward<T>(Item)), Count(Count) {}
66
67   void format(llvm::raw_ostream &Stream, StringRef Style) {
68     auto Wrapper = detail::build_format_wrapper(std::forward<T>(Item));
69     for (size_t I = 0; I < Count; ++I) {
70       Wrapper.format(Stream, Style);
71     }
72   }
73 };
74 }
75
76 template <typename T>
77 detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount) {
78   return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount);
79 }
80
81 template <typename T>
82 detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
83   return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
84 }
85
86 template <typename T>
87 detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
88   return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
89 }
90 }
91
92 #endif