Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 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 { |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 21 | ColoredRawOstream(const ColoredRawOstream &OS) = delete; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 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, |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 52 | bool IsColorUsed = true, |
| 53 | bool Bold = false, bool BG = false) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 54 | if (IsColorUsed) |
Justin Bogner | fe357c0 | 2014-09-17 18:23:47 +0000 | [diff] [blame] | 55 | OS.changeColor(Color, Bold, BG); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 56 | return ColoredRawOstream(OS, IsColorUsed); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #endif // LLVM_COV_RENDERINGSUPPORT_H |