FormatAdapters.h revision 311116
1152909Sanholt//===- FormatAdapters.h - Formatters for common LLVM types -----*- C++ -*-===//
2152909Sanholt//
395584Sanholt//                     The LLVM Compiler Infrastructure
495584Sanholt//
5112015Sanholt// This file is distributed under the University of Illinois Open Source
695584Sanholt// License. See LICENSE.TXT for details.
795584Sanholt//
895584Sanholt//===----------------------------------------------------------------------===//
995584Sanholt
1095584Sanholt#ifndef LLVM_SUPPORT_FORMATADAPTERS_H
1195584Sanholt#define LLVM_SUPPORT_FORMATADAPTERS_H
1295584Sanholt
1395584Sanholt#include "llvm/ADT/SmallString.h"
1495584Sanholt#include "llvm/ADT/StringRef.h"
1595584Sanholt#include "llvm/Support/FormatCommon.h"
1695584Sanholt#include "llvm/Support/FormatVariadicDetails.h"
1795584Sanholt#include "llvm/Support/raw_ostream.h"
1895584Sanholt
1995584Sanholtnamespace llvm {
2095584Sanholttemplate <typename T> class FormatAdapter : public detail::format_adapter {
2195584Sanholtprotected:
2295584Sanholt  explicit FormatAdapter(T &&Item) : Item(Item) {}
2395584Sanholt
2495584Sanholt  T Item;
2595584Sanholt
2695584Sanholt  static_assert(!detail::uses_missing_provider<T>::value,
2795584Sanholt                "Item does not have a format provider!");
2895584Sanholt};
2995584Sanholt
30112015Sanholtnamespace detail {
3195584Sanholttemplate <typename T> class AlignAdapter final : public FormatAdapter<T> {
3295584Sanholt  AlignStyle Where;
33152909Sanholt  size_t Amount;
34152909Sanholt
35152909Sanholtpublic:
3695584Sanholt  AlignAdapter(T &&Item, AlignStyle Where, size_t Amount)
3795584Sanholt      : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount) {}
3895584Sanholt
3995584Sanholt  void format(llvm::raw_ostream &Stream, StringRef Style) {
4095584Sanholt    auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
4195584Sanholt    FmtAlign(Adapter, Where, Amount).format(Stream, Style);
4295584Sanholt  }
4395584Sanholt};
4495584Sanholt
45112015Sanholttemplate <typename T> class PadAdapter final : public FormatAdapter<T> {
46112015Sanholt  size_t Left;
4795584Sanholt  size_t Right;
4895584Sanholt
4995584Sanholtpublic:
5095584Sanholt  PadAdapter(T &&Item, size_t Left, size_t Right)
5195584Sanholt      : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
5295584Sanholt
5395584Sanholt  void format(llvm::raw_ostream &Stream, StringRef Style) {
5495584Sanholt    auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
5595584Sanholt    Stream.indent(Left);
5695584Sanholt    Adapter.format(Stream, Style);
5795584Sanholt    Stream.indent(Right);
5895584Sanholt  }
5995584Sanholt};
6095584Sanholt
6195584Sanholttemplate <typename T> class RepeatAdapter final : public FormatAdapter<T> {
6295584Sanholt  size_t Count;
63145132Sanholt
6495584Sanholtpublic:
65145132Sanholt  RepeatAdapter(T &&Item, size_t Count)
66112015Sanholt      : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
67112015Sanholt
6895584Sanholt  void format(llvm::raw_ostream &Stream, StringRef Style) {
69112015Sanholt    auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
70112015Sanholt    for (size_t I = 0; I < Count; ++I) {
71112015Sanholt      Adapter.format(Stream, Style);
72112015Sanholt    }
73145132Sanholt  }
74145132Sanholt};
75145132Sanholt}
76145132Sanholt
77145132Sanholttemplate <typename T>
78145132Sanholtdetail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount) {
79145132Sanholt  return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount);
80145132Sanholt}
81145132Sanholt
82145132Sanholttemplate <typename T>
83145132Sanholtdetail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
84145132Sanholt  return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
85145132Sanholt}
86145132Sanholt
87145132Sanholttemplate <typename T>
88145132Sanholtdetail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
89145132Sanholt  return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
90145132Sanholt}
91145132Sanholt}
92145132Sanholt
93145132Sanholt#endif
94145132Sanholt