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-2021 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 OTstream_H
42#define OTstream_H
43
44#include "token.H"
45#include "Ostream.H"
46#include "DynamicList.H"
47
48// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49
50namespace Foam
51{
52
53/*---------------------------------------------------------------------------*\
54 Class OTstream Declaration
55\*---------------------------------------------------------------------------*/
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(streamOpt.format(), streamOpt.version()),
71 {
72 setOpened();
73 setGood();
74 }
75
76 //- Copy construct
77 OTstream(const OTstream& os)
78 :
81 {
82 setOpened();
83 setGood();
84 }
85
86 //- Move construct
88 :
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
105 {
106 return *this;
107 }
108
109 //- The tokens
111 {
112 return *this;
113 }
114
115
116 // Write
117
118 //- Write token to stream or otherwise handle it.
119 // \return false if the token type was not handled by this method
120 virtual bool write(const token& tok);
121
122 //- Write single character. Whitespace is suppressed.
123 virtual Ostream& write(const char c);
124
125 //- Write the word-characters of a character string.
126 // Sends as a single char, or as word.
127 virtual Ostream& write(const char* str);
128
129 //- Write word
130 virtual Ostream& write(const word& str);
131
132 //- Write string
133 virtual Ostream& write(const string& str);
134
135 //- Write std::string surrounded by quotes.
136 // Optional write without quotes.
137 virtual Ostream& writeQuoted
138 (
139 const std::string& str,
140 const bool quoted=true
141 );
142
143 //- Write int32_t as a label
144 virtual Ostream& write(const int32_t val);
145
146 //- Write int64_t as a label
147 virtual Ostream& write(const int64_t val);
148
149 //- Write floatScalar
150 virtual Ostream& write(const floatScalar val);
151
152 //- Write doubleScalar
153 virtual Ostream& write(const doubleScalar val);
154
155 //- Write binary block with 8-byte alignment.
156 virtual Ostream& write(const char* data, std::streamsize count);
157
158 //- Low-level raw binary output.
159 virtual Ostream& writeRaw(const char* data, std::streamsize count);
160
161 //- Begin marker for low-level raw binary output.
162 // The count indicates the number of bytes for subsequent
163 // writeRaw calls.
164 virtual bool beginRawWrite(std::streamsize count);
165
166 //- End marker for low-level raw binary output.
167 virtual bool endRawWrite()
168 {
169 return true;
170 }
171
172 //- Add indentation characters
173 virtual void indent()
174 {}
175
176
177 // Stream State Functions
178
179 //- Get flags of output stream
180 virtual ios_base::fmtflags flags() const
181 {
182 return ios_base::fmtflags(0);
183 }
184
185 //- Set flags of stream - ignored
186 std::ios_base::fmtflags flags(const ios_base::fmtflags)
187 {
188 return ios_base::fmtflags(0);
189 }
190
191 //- Flush stream
192 virtual void flush()
193 {}
194
195 //- Add newline and flush stream
196 virtual void endl()
197 {}
198
199 //- Get the current padding character
200 // \return previous padding character
201 virtual char fill() const
202 {
203 return 0;
204 }
205
206 //- Set padding character for formatted field up to field width
207 virtual char fill(const char)
208 {
209 return 0;
210 }
211
212 //- Get width of output field
213 virtual int width() const
214 {
215 return 0;
216 }
217
218 //- Set width of output field
219 // \return previous width
220 virtual int width(const int)
221 {
222 return 0;
223 }
224
225 //- Get precision of output field
226 virtual int precision() const
227 {
228 return 0;
229 }
230
231 //- Set precision of output field
232 // \return old precision
233 virtual int precision(const int)
234 {
235 return 0;
236 }
237
238
239 // Other
240
241 //- Reset the output buffer and rewind the stream
242 void reset()
243 {
244 this->rewind();
245 }
246
247 //- Rewind the output stream
248 virtual void rewind()
249 {
251 setOpened();
252 setGood();
253 }
254
255 //- Print stream description to Ostream
256 void print(Ostream& os) const;
257
258
259 // Additional constructors and methods (as per v2012 and earlier)
260 #ifdef Foam_IOstream_extras
261
262 //- Construct empty with format, version
263 explicit OTstream
264 (
267 )
268 :
269 OTstream(IOstreamOption(fmt, ver))
270 {}
271
272 #endif /* Foam_IOstream_extras */
273};
274
275
276// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
277
278} // End namespace Foam
279
280// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281
282#endif
283
284// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Representation of a major/minor version number.
The IOstreamOption is a simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
streamFormat format() const noexcept
Get the current stream format.
streamFormat
Data format (ascii | binary)
static const versionNumber currentVersion
The current version number (2.0)
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:129
A simple output token stream that can be used to build token lists. Always UNCOMPRESSED.
Definition: OTstream.H:60
OTstream(IOstreamOption streamOpt=IOstreamOption())
Default construct, set stream status.
Definition: OTstream.H:66
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OTstream.C:160
virtual char fill() const
Get the current padding character.
Definition: OTstream.H:200
virtual void indent()
Add indentation characters.
Definition: OTstream.H:172
virtual char fill(const char)
Set padding character for formatted field up to field width.
Definition: OTstream.H:206
virtual void endl()
Add newline and flush stream.
Definition: OTstream.H:195
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: OTstream.C:174
void print(Ostream &os) const
Print stream description to Ostream.
Definition: OTstream.C:189
OTstream(const OTstream &os)
Copy construct.
Definition: OTstream.H:76
virtual int width() const
Get width of output field.
Definition: OTstream.H:212
~OTstream()=default
Destructor.
std::ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream - ignored.
Definition: OTstream.H:185
DynamicList< token > & tokens()
The tokens.
Definition: OTstream.H:109
void reset()
Reset the output buffer and rewind the stream.
Definition: OTstream.H:241
virtual void flush()
Flush stream.
Definition: OTstream.H:191
OTstream(OTstream &&os)
Move construct.
Definition: OTstream.H:86
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OTstream.C:94
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OTstream.H:166
virtual ios_base::fmtflags flags() const
Get flags of output stream.
Definition: OTstream.H:179
virtual void rewind()
Rewind the output stream.
Definition: OTstream.H:247
virtual int precision() const
Get precision of output field.
Definition: OTstream.H:225
const DynamicList< token > & tokens() const
The tokens.
Definition: OTstream.H:103
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
runTime write()