/src/CMake/Source/cmStdIoStream.h
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #pragma once |
4 | | |
5 | | #include "cmConfigure.h" // IWYU pragma: keep |
6 | | |
7 | | #include <cstdio> |
8 | | #include <iosfwd> |
9 | | |
10 | | namespace cm { |
11 | | namespace StdIo { |
12 | | |
13 | | /** |
14 | | * Identify the kind of terminal to which a stream is attached, if any. |
15 | | */ |
16 | | enum class TermKind |
17 | | { |
18 | | /** Not an interactive terminal. */ |
19 | | None, |
20 | | /** A VT100 terminal. */ |
21 | | VT100, |
22 | | #ifdef _WIN32 |
23 | | /** A Windows Console that does not support VT100 sequences. */ |
24 | | Console, |
25 | | #endif |
26 | | }; |
27 | | |
28 | | /** |
29 | | * Represent stdin, stdout, or stderr stream metadata. |
30 | | */ |
31 | | class Stream |
32 | | { |
33 | | #ifdef _WIN32 |
34 | | friend class Globals; |
35 | | void Destroy(); |
36 | | #endif |
37 | | |
38 | | public: |
39 | | /** The kind of terminal to which the stream is attached, if any. */ |
40 | 0 | TermKind Kind() const { return this->Kind_; } |
41 | | |
42 | | /** The underlying C++ stream. */ |
43 | 0 | std::ios& IOS() const { return this->IOS_; } |
44 | | |
45 | | /** The underlying file descriptor. */ |
46 | 0 | int FD() const { return this->FD_; } |
47 | | |
48 | | #ifdef _WIN32 |
49 | | /** The underlying HANDLE of an attached Windows Console, if any. */ |
50 | | void* Console() const { return this->Console_; } |
51 | | #endif |
52 | | |
53 | | protected: |
54 | | enum class Direction |
55 | | { |
56 | | In, |
57 | | Out, |
58 | | }; |
59 | | |
60 | | Stream(std::ios& s, FILE* file, Direction direction); |
61 | | ~Stream(); // NOLINT(performance-trivially-destructible) |
62 | | |
63 | | private: |
64 | | std::ios& IOS_; |
65 | | int FD_ = -1; |
66 | | TermKind Kind_ = TermKind::None; |
67 | | |
68 | | #ifdef _WIN32 |
69 | | void* Console_ = nullptr; |
70 | | unsigned long ConsoleOrigMode_ = 0; |
71 | | #endif |
72 | | }; |
73 | | |
74 | | /** |
75 | | * Represent stdin metadata. |
76 | | */ |
77 | | class IStream : public Stream |
78 | | { |
79 | | friend class Globals; |
80 | | IStream(std::istream& is, FILE* file); |
81 | | |
82 | | public: |
83 | | /** The underlying C++ stream. */ |
84 | | std::istream& IOS() const; |
85 | | }; |
86 | | |
87 | | /** |
88 | | * Represent stdout or stderr metadata. |
89 | | */ |
90 | | class OStream : public Stream |
91 | | { |
92 | | friend class Globals; |
93 | | OStream(std::ostream& os, FILE* file); |
94 | | |
95 | | public: |
96 | | /** The underlying C++ stream. */ |
97 | | std::ostream& IOS() const; |
98 | | }; |
99 | | |
100 | | /** Metadata for stdin. */ |
101 | | IStream& In(); |
102 | | |
103 | | /** Metadata for stdout. */ |
104 | | OStream& Out(); |
105 | | |
106 | | /** Metadata for stderr. */ |
107 | | OStream& Err(); |
108 | | |
109 | | } |
110 | | } |