[PM/AA] Put the 'final' keyword in the correct place. And actually
[oota-llvm.git] / tools / llvm-cov / RenderingSupport.h
1 //===- RenderingSupport.h - output stream rendering support functions  ----===//
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_COV_RENDERINGSUPPORT_H
11 #define LLVM_COV_RENDERINGSUPPORT_H
12
13 #include "llvm/Support/raw_ostream.h"
14 #include <utility>
15
16 namespace llvm {
17
18 /// \brief A helper class that resets the output stream's color if needed
19 /// when destroyed.
20 class ColoredRawOstream {
21   ColoredRawOstream(const ColoredRawOstream &OS) = delete;
22
23 public:
24   raw_ostream &OS;
25   bool IsColorUsed;
26
27   ColoredRawOstream(raw_ostream &OS, bool IsColorUsed)
28       : OS(OS), IsColorUsed(IsColorUsed) {}
29
30   ColoredRawOstream(ColoredRawOstream &&Other)
31       : OS(Other.OS), IsColorUsed(Other.IsColorUsed) {
32     // Reset the other IsColorUsed so that the other object won't reset the
33     // color when destroyed.
34     Other.IsColorUsed = false;
35   }
36
37   ~ColoredRawOstream() {
38     if (IsColorUsed)
39       OS.resetColor();
40   }
41 };
42
43 template <typename T>
44 inline raw_ostream &operator<<(const ColoredRawOstream &OS, T &&Value) {
45   return OS.OS << std::forward<T>(Value);
46 }
47
48 /// \brief Change the color of the output stream if the `IsColorUsed` flag
49 /// is true. Returns an object that resets the color when destroyed.
50 inline ColoredRawOstream colored_ostream(raw_ostream &OS,
51                                          raw_ostream::Colors Color,
52                                          bool IsColorUsed = true,
53                                          bool Bold = false, bool BG = false) {
54   if (IsColorUsed)
55     OS.changeColor(Color, Bold, BG);
56   return ColoredRawOstream(OS, IsColorUsed);
57 }
58 }
59
60 #endif // LLVM_COV_RENDERINGSUPPORT_H