Home | History | Annotate | Download | only in Support
      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 FormatAdapter : public detail::format_adapter {
     21 protected:
     22   explicit FormatAdapter(T &&Item) : Item(Item) {}
     23 
     24   T Item;
     25 };
     26 
     27 namespace detail {
     28 template <typename T> class AlignAdapter final : public FormatAdapter<T> {
     29   AlignStyle Where;
     30   size_t Amount;
     31 
     32 public:
     33   AlignAdapter(T &&Item, AlignStyle Where, size_t Amount)
     34       : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount) {}
     35 
     36   void format(llvm::raw_ostream &Stream, StringRef Style) {
     37     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
     38     FmtAlign(Adapter, Where, Amount).format(Stream, Style);
     39   }
     40 };
     41 
     42 template <typename T> class PadAdapter final : public FormatAdapter<T> {
     43   size_t Left;
     44   size_t Right;
     45 
     46 public:
     47   PadAdapter(T &&Item, size_t Left, size_t Right)
     48       : FormatAdapter<T>(std::forward<T>(Item)), Left(Left), Right(Right) {}
     49 
     50   void format(llvm::raw_ostream &Stream, StringRef Style) {
     51     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
     52     Stream.indent(Left);
     53     Adapter.format(Stream, Style);
     54     Stream.indent(Right);
     55   }
     56 };
     57 
     58 template <typename T> class RepeatAdapter final : public FormatAdapter<T> {
     59   size_t Count;
     60 
     61 public:
     62   RepeatAdapter(T &&Item, size_t Count)
     63       : FormatAdapter<T>(std::forward<T>(Item)), Count(Count) {}
     64 
     65   void format(llvm::raw_ostream &Stream, StringRef Style) {
     66     auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
     67     for (size_t I = 0; I < Count; ++I) {
     68       Adapter.format(Stream, Style);
     69     }
     70   }
     71 };
     72 }
     73 
     74 template <typename T>
     75 detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount) {
     76   return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount);
     77 }
     78 
     79 template <typename T>
     80 detail::PadAdapter<T> fmt_pad(T &&Item, size_t Left, size_t Right) {
     81   return detail::PadAdapter<T>(std::forward<T>(Item), Left, Right);
     82 }
     83 
     84 template <typename T>
     85 detail::RepeatAdapter<T> fmt_repeat(T &&Item, size_t Count) {
     86   return detail::RepeatAdapter<T>(std::forward<T>(Item), Count);
     87 }
     88 }
     89 
     90 #endif
     91