Loading...
Searching...
No Matches
OTstream.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2019-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::OTstream
28
29Description
30 A simple output token stream that can be used to build token lists.
31 Always UNCOMPRESSED.
32
33Note
34 Appending single characters to token list is fragile.
35
36SourceFiles
37 OTstream.C
38
39\*---------------------------------------------------------------------------*/
40
41#ifndef Foam_OTstream_H
42#define Foam_OTstream_H
43
44#include "token.H"
45#include "Ostream.H"
46#include "DynamicList.H"
47
48// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49
50namespace Foam
51{
53/*---------------------------------------------------------------------------*\
54 Class OTstream Declaration
55\*---------------------------------------------------------------------------*/
56
57class OTstream
58:
59 public Ostream,
60 public DynamicList<token>
61{
62public:
63
64 // Constructors
65
66 //- Default construct, set stream status
67 explicit OTstream(IOstreamOption streamOpt = IOstreamOption())
68 :
69 Ostream(IOstreamOption(streamOpt.format(), streamOpt.version())),
71 {
72 setOpened();
73 setGood();
74 }
75
76 //- Copy construct
77 OTstream(const OTstream& os)
78 :
79 Ostream(static_cast<IOstreamOption>(os)),
81 {
82 setOpened();
83 setGood();
84 }
85
86 //- Move construct
88 :
89 Ostream(static_cast<IOstreamOption>(os)),
90 DynamicList<token>(std::move(os.tokens()))
91 {
92 setOpened();
93 setGood();
94 }
95
96
97 //- Destructor
98 ~OTstream() = default;
99
100
101 // Member Functions
102
103 //- The tokens
104 const DynamicList<token>& tokens() const noexcept { return *this; }
105
106 //- The tokens
107 DynamicList<token>& tokens() noexcept { return *this; }
108
110 // Write
111
112 //- Inherit write methods from Ostream
115 //- Write token to stream or otherwise handle it.
116 // \return false if the token type was not handled by this method
117 virtual bool write(const token& tok) override;
118
119 //- Write single character. Whitespace is suppressed.
120 virtual Ostream& write(const char c) override;
121
122 //- Write character/string content, with/without surrounding quotes
123 virtual Ostream& writeQuoted
124 (
125 const char* str,
126 std::streamsize len,
127 const bool quoted=true
128 ) override;
129
130 //- Write the word-characters of a character string.
131 // Sends as a single char, or as word.
132 virtual Ostream& write(const char* str) override;
133
134 //- Write word
135 virtual Ostream& write(const word& str) override;
136
137 //- Write string
138 virtual Ostream& write(const std::string& str) override;
139
140 //- Write int32_t
141 virtual Ostream& write(const int32_t val) override;
142
143 //- Write int64_t
144 virtual Ostream& write(const int64_t val) override;
145
146 //- Write uint32_t
147 virtual Ostream& write(const uint32_t val) override;
148
149 //- Write uint64_t
150 virtual Ostream& write(const uint64_t val) override;
151
152 //- Write float
153 virtual Ostream& write(const float val) override;
154
155 //- Write double
156 virtual Ostream& write(const double val) override;
157
158 //- Write binary block with 8-byte alignment.
159 virtual Ostream& write
160 (
161 const char* data,
162 std::streamsize count
163 ) override;
164
165 //- Low-level raw binary output.
166 virtual Ostream& writeRaw
167 (
168 const char* data,
169 std::streamsize count
170 ) override;
171
172 //- Begin marker for low-level raw binary output.
173 // The count indicates the number of bytes for subsequent
174 // writeRaw calls.
175 virtual bool beginRawWrite(std::streamsize count) override;
176
177 //- End marker for low-level raw binary output.
178 virtual bool endRawWrite() override
179 {
180 return true;
181 }
182
183 //- Add indentation characters
184 virtual void indent() override
185 {}
186
187
188 // Stream State Functions
189
190 //- Return current stream flags.
191 //- Dummy for token stream, returns 0.
192 virtual std::ios_base::fmtflags flags() const override
193 {
194 return std::ios_base::fmtflags(0);
195 }
196
197 //- Set stream flags, return old stream flags.
198 //- Dummy for token stream, returns 0.
199 std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
200 {
201 return std::ios_base::fmtflags(0);
202 }
203
204 //- Flush stream
205 virtual void flush() override
206 {}
207
208 //- Add newline and flush stream
209 virtual void endl() override
210 {}
211
212 //- Get the current padding character
213 // \return previous padding character
214 virtual char fill() const override
215 {
216 return 0;
217 }
218
219 //- Set padding character for formatted field up to field width
220 virtual char fill(const char) override
221 {
222 return 0;
223 }
224
225 //- Get width of output field
226 virtual int width() const override
227 {
228 return 0;
229 }
231 //- Set width of output field
232 // \return previous width
233 virtual int width(const int) override
234 {
235 return 0;
236 }
237
238 //- Get precision of output field
239 virtual int precision() const override
241 return 0;
242 }
243
244 //- Set precision of output field
245 // \return old precision
246 virtual int precision(const int) override
247 {
248 return 0;
250
251
252 // Other
253
254 //- Rewind the output stream to position 0 (non-virtual!)
255 //- and adjust the stream status (open/good/eof ...)
256 //- Reset the output buffer and rewind the stream
258 {
260 setOpened();
261 setGood();
262 }
264 //- Rewind the output stream to position 0
265 //- and adjust the stream status (open/good/eof ...)
266 virtual void rewind() { OTstream::reset(); }
267
268 //- Print stream description to Ostream
269 void print(Ostream& os) const override;
270
272 // Additional constructors and methods (as per v2012 and earlier)
273 #ifdef Foam_IOstream_extras
274
275 //- Construct empty with format
277 :
279 {}
280
281 #endif /* Foam_IOstream_extras */
282};
283
284
285// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
286
287} // End namespace Foam
288
289// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
290
291#endif
292
293// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
constexpr DynamicList() noexcept
A simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression.
streamFormat format() const noexcept
Get the current stream format.
streamFormat
Data format (ascii | binary | coherent).
void setGood() noexcept
Set stream state to be good.
Definition IOstream.H:174
void setOpened() noexcept
Set stream opened.
Definition IOstream.H:150
A simple output token stream that can be used to build token lists. Always UNCOMPRESSED.
Definition OTstream.H:56
virtual int precision() const override
Get precision of output field.
Definition OTstream.H:305
virtual std::ios_base::fmtflags flags() const override
Return current stream flags. Dummy for token stream, returns 0.
Definition OTstream.H:240
OTstream(IOstreamOption streamOpt=IOstreamOption())
Default construct, set stream status.
Definition OTstream.H:64
virtual int width(const int) override
Set width of output field.
Definition OTstream.H:297
std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
Set stream flags, return old stream flags. Dummy for token stream, returns 0.
Definition OTstream.H:249
virtual void indent() override
Add indentation characters.
Definition OTstream.H:230
void reset() noexcept
Rewind the output stream to position 0 (non-virtual!) and adjust the stream status (open/good/eof ....
Definition OTstream.H:328
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition OTstream.C:52
void print(Ostream &os) const override
Print stream description to Ostream.
Definition OTstream.C:200
virtual bool endRawWrite() override
End marker for low-level raw binary output.
Definition OTstream.H:222
DynamicList< token > & tokens() noexcept
The tokens.
Definition OTstream.H:114
virtual int width() const override
Get width of output field.
Definition OTstream.H:287
OTstream(const OTstream &os)
Copy construct.
Definition OTstream.H:76
const DynamicList< token > & tokens() const noexcept
The tokens.
Definition OTstream.H:109
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
Definition OTstream.C:173
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
Definition OTstream.C:186
virtual char fill() const override
Get the current padding character.
Definition OTstream.H:271
~OTstream()=default
Destructor.
virtual int precision(const int) override
Set precision of output field.
Definition OTstream.H:315
virtual char fill(const char) override
Set padding character for formatted field up to field width.
Definition OTstream.H:279
OTstream(OTstream &&os)
Move construct.
Definition OTstream.H:88
virtual void rewind()
Rewind the output stream to position 0 and adjust the stream status (open/good/eof ....
Definition OTstream.H:339
virtual void flush() override
Flush stream.
Definition OTstream.H:257
virtual void endl() override
Add newline and flush stream.
Definition OTstream.H:263
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
Ostream(const Ostream &)=default
Copy construct.
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition UListI.H:274
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
const direction noexcept
Definition scalarImpl.H:265
runTime write()