Loading...
Searching...
No Matches
IOstreamOption.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) 2011-2015 OpenFOAM Foundation
9 Copyright (C) 2018-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::IOstreamOption
29
30Description
31 A simple container for options an IOstream can normally have.
32
33 The format (ASCII | BINARY) is typically controlled by enumerated
34 names (ascii, binary).
35
36 The compression (UNCOMPRESSED | COMPRESSED) is typically controlled
37 by switch values (true/false, on/off, ...).
38
39SourceFiles
40 IOstreamOption.C
41
42\*---------------------------------------------------------------------------*/
43
44#ifndef Foam_IOstreamOption_H
45#define Foam_IOstreamOption_H
46
47#include "word.H"
48#include <ios>
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55// Forward Declarations
56class token;
57class dictionary;
58template<class EnumType> class Enum;
60/*---------------------------------------------------------------------------*\
61 Class IOstreamOption Declaration
62\*---------------------------------------------------------------------------*/
63
65{
66public:
67
68 // Public Data Types
69
70 //- Data format (ascii | binary | coherent)
71 enum streamFormat : char
72 {
73 ASCII = 0,
74 BINARY,
75 COHERENT,
77 };
78
79 //- Compression treatment (UNCOMPRESSED | COMPRESSED)
80 enum compressionType : char
81 {
84 };
85
86 //- File appending (NO_APPEND | APPEND_APP | APPEND_ATE)
87 enum appendType : char
96 //- Atomic operations (output)
97 enum atomicType : char
98 {
99 NON_ATOMIC = 0,
101 };
103 //- Float formats (eg, time directory name formats)
104 enum class floatFormat : unsigned
105 {
107 general = unsigned(0),
109 fixed = unsigned(std::ios_base::fixed),
111 scientific = unsigned(std::ios_base::scientific)
112 };
113
115 //- Representation of a major/minor version number
117 {
118 //- The combined major/version number.
119 short number_;
120
121 public:
122
123 // Constructors
124
125 //- Default construct \em current version.
126 //- The value (2.0) corresponds to the \em current version.
127 constexpr versionNumber() noexcept
128 :
129 number_(20)
130 {}
131
132 //- Construct from major, number
133 constexpr versionNumber(int major, int minor) noexcept
134 :
135 number_(10*major + (minor % 10))
136 {}
137
138 //- Construct from floating-point version number
139 explicit constexpr versionNumber(const float ver) noexcept
140 :
141 number_(10*ver + 0.001) // Allow some rounding
142 {}
143
144 //- Construct by parsing string "major.minor"
145 explicit versionNumber(const std::string& verNum);
147 //- Construct from token (float, word, string)
148 explicit versionNumber(const token& tok);
149
150 //- Failsafe construct from dictionary lookup.
151 versionNumber(const word& key, const dictionary& dict);
152
153
154 // Member Functions
155
156 //- A string representation as major.minor
157 std::string str() const
158 {
159 return
160 (
161 std::to_string(int(number_ / 10)) // major
162 + '.'
163 + std::to_string(int(number_ % 10)) // minor
164 );
165 }
166
167 //- From version to canonical integer value
168 int canonical() const noexcept
169 {
170 return number_;
171 }
172
173 //- From canonical integer value to version
174 static versionNumber canonical(int verNum) noexcept
175 {
176 // Split into major/minor
177 return versionNumber(int(verNum / 10), int(verNum % 10));
178 }
179
180 //- Compare differences in the versions
181 // Negative when 'this' is less than other.
182 // Positive when 'this' is greater than other.
183 int compare(const versionNumber& other) const noexcept
184 {
185 return (number_ - other.number_);
186 }
187 };
188
189
190 // Public Static Data
191
192 //- Names for float formats (general, fixed, scientific)
194
195 //- Stream format names (ascii, binary)
196 static const Enum<streamFormat> formatNames;
197
198 //- The current version number (2.0)
199 static const versionNumber currentVersion;
200
202 // Static Helpers
203
204 //- Lookup floatFormat enum corresponding to the string
205 //- (general | fixed | scientific).
206 //
207 // If the string is not recognized, emit warning and return default.
208 // Silent if the string itself is empty.
209 //
210 // \note Can be used as constructor substitute for the enumeration
212 (
213 const word& fmtName,
215 );
216
217 //- getOrDefault floatFormat from dictionary,
218 //- warn only on bad enumeration.
220 (
221 const word& key,
222 const dictionary& dict,
224 );
226 //- Lookup streamFormat enum corresponding to the string
227 //- (ascii | binary).
228 //
229 // If the string is not recognized, emit warning and return default.
230 // Silent if the string itself is empty.
231 //
232 // \note Can be used as constructor substitute for the enumeration
234 (
235 const word& fmtName,
237 );
238
239 //- getOrDefault streamFormat from dictionary,
240 //- warn only on bad enumeration.
242 (
243 const word& key,
244 const dictionary& dict,
246 );
247
248 //- The compression enum corresponding to the string.
249 // Expects switch values (true/false, on/off, ...)
250 //
251 // If the string is not recognized, emit warning and return default.
252 // Silent if the string itself is empty.
253 //
254 // \note Can be used as constructor substitute for the enumeration
256 (
257 const word& compName,
259 );
260
261 //- getOrDefault compressionType from dictionary,
262 //- warn only on bad enumeration.
264 (
265 const word& key,
266 const dictionary& dict,
268 );
269
270
271private:
272
273 // Private Data
274
275 // NB: ordered with versionNumber first (short) and
276 // adjacent enums to minimize gaps
277
278 //- Stream version number (eg, 2.0 for current dictionary format)
279 versionNumber version_;
280
281 //- Format: (ascii | binary)
282 streamFormat format_;
283
284 //- Compression: (on | off)
285 compressionType compression_;
286
287
288public:
289
290 // Constructors
291
292 //- Default construct (ASCII, UNCOMPRESSED, currentVersion)
293 //- or construct with format, compression
294 // \note non-explicit for convenient construction
295 constexpr IOstreamOption
296 (
299 ) noexcept
300 :
301 version_(),
302 format_(fmt),
303 compression_(comp)
304 {}
305
306 //- Construct from components (format, compression, version)
307 constexpr IOstreamOption
308 (
309 streamFormat fmt,
310 compressionType comp,
311 versionNumber ver
312 ) noexcept
313 :
314 version_(ver),
315 format_(fmt),
316 compression_(comp)
317 {}
318
319 //- Construct from components (format, version, compression)
320 constexpr IOstreamOption
321 (
322 streamFormat fmt,
323 versionNumber ver,
325 ) noexcept
326 :
327 version_(ver),
328 format_(fmt),
329 compression_(comp)
330 {}
331
332 //- Copy construct with change of format
334 :
335 version_(opt.version_),
336 format_(fmt),
337 compression_(opt.compression_)
338 {}
339
340
341 // Member Functions
342
343 //- Get the current stream format
344 streamFormat format() const noexcept { return format_; }
345
346 //- Set the stream format
347 // \return the previous value
348 streamFormat format(const streamFormat fmt) noexcept
349 {
350 streamFormat old(format_);
351 format_ = fmt;
352 return old;
353 }
354
355 //- Set the stream format from string value.
356 // If the string is not recognized, emit warning and leave unchanged.
357 // Silent if the string itself is empty.
358 // \return the previous value
359 streamFormat format(const word& formatName)
360 {
361 streamFormat old(format_);
362 format_ = formatEnum(formatName, format_);
363 return old;
364 }
365
366 //- Get the stream compression
367 compressionType compression() const noexcept { return compression_; }
368
369 //- Set the stream compression
370 // \return the previous value
371 compressionType compression(const compressionType comp) noexcept
372 {
373 compressionType old(compression_);
374 compression_ = comp;
375 return old;
376 }
377
378 //- Set the stream compression from string value.
379 // If the string is not recognized, emit warning and leave unchanged.
380 // Silent if the string itself is empty.
381 // \return the previous value
382 compressionType compression(const word& compName)
383 {
384 compressionType old(compression_);
385 compression_ = compressionEnum(compName, compression_);
386 return old;
387 }
388
389 //- Get the stream version
390 versionNumber version() const noexcept { return version_; }
391
392 //- Set the stream version
393 // \return the previous value
394 versionNumber version(const versionNumber ver) noexcept
395 {
396 versionNumber old(version_);
397 version_ = ver;
398 return old;
400
401 //- Set the stream version from token
402 // \return the previous value
403 versionNumber version(const token& tok)
404 {
405 versionNumber old(version_);
406 version_ = versionNumber(tok);
407 return old;
408 }
409};
410
411
412// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
413
414//- Output format type as text string (ascii | binary)
416
417//- Output version as major.minor text string
420
421// * * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * //
422
423// Comparison Operators
424
425//- Version number equality
426inline bool operator==
427(
430) noexcept
431{
432 return a.compare(b) == 0;
434
435//- Version number inequality
436inline bool operator!=
437(
440) noexcept
441{
442 return a.compare(b) != 0;
444
445//- Version A older than B
446inline bool operator<
447(
450) noexcept
451{
452 return a.compare(b) < 0;
453}
454
455//- Version A same or older than B
456inline bool operator<=
457(
460) noexcept
461{
462 return a.compare(b) <= 0;
463}
465//- Version A newer than B
466inline bool operator>
467(
470) noexcept
471{
472 return a.compare(b) > 0;
473}
475//- Version A same or newer than B
476inline bool operator>=
477(
480) noexcept
482 return a.compare(b) >= 0;
483}
484
485
486// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
487
488} // End namespace Foam
489
490// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
491
492#endif
494// ************************************************************************* //
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
Representation of a major/minor version number.
constexpr versionNumber(const float ver) noexcept
Construct from floating-point version number.
static versionNumber canonical(int verNum) noexcept
From canonical integer value to version.
constexpr versionNumber(int major, int minor) noexcept
Construct from major, number.
constexpr versionNumber() noexcept
Default construct current version. The value (2.0) corresponds to the current version.
int canonical() const noexcept
From version to canonical integer value.
int compare(const versionNumber &other) const noexcept
Compare differences in the versions.
std::string str() const
A string representation as major.minor.
streamFormat format(const streamFormat fmt) noexcept
Set the stream format.
versionNumber version() const noexcept
Get the stream version.
streamFormat format(const word &formatName)
Set the stream format from string value.
compressionType compression(const compressionType comp) noexcept
Set the stream compression.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression.
compressionType compression() const noexcept
Get the stream compression.
streamFormat format() const noexcept
Get the current stream format.
floatFormat
Float formats (eg, time directory name formats).
@ general
default float notation
static floatFormat floatFormatEnum(const word &fmtName, const floatFormat deflt=floatFormat::general)
Lookup floatFormat enum corresponding to the string (general | fixed | scientific).
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
IOstreamOption(const IOstreamOption &opt, streamFormat fmt) noexcept
Copy construct with change of format.
constexpr IOstreamOption(streamFormat fmt, compressionType comp, versionNumber ver) noexcept
Construct from components (format, compression, version).
streamFormat
Data format (ascii | binary | coherent).
@ ASCII
"ascii" (normal default)
static const Enum< floatFormat > floatFormatNames
Names for float formats (general, fixed, scientific).
static streamFormat formatEnum(const word &fmtName, const streamFormat deflt=streamFormat::ASCII)
Lookup streamFormat enum corresponding to the string (ascii | binary).
atomicType
Atomic operations (output).
@ NON_ATOMIC
atomic = false
@ ATOMIC
atomic = true
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary).
versionNumber version(const token &tok)
Set the stream version from token.
versionNumber version(const versionNumber ver) noexcept
Set the stream version.
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED).
@ UNCOMPRESSED
compression = false
@ COMPRESSED
compression = true
static const versionNumber currentVersion
The current version number (2.0).
appendType
File appending (NO_APPEND | APPEND_APP | APPEND_ATE).
@ NON_APPEND
old name for NO_APPEND
@ NO_APPEND
no append (truncates existing)
@ APPEND_ATE
append (seek end after open)
@ APPEND_APP
append (seek end each write)
@ APPEND
old name for APPEND_APP
compressionType compression(const word &compName)
Set the stream compression from string value.
constexpr IOstreamOption(streamFormat fmt, versionNumber ver, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Construct from components (format, version, compression).
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
General relative velocity model.
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.
IOstream & fixed(IOstream &io)
Definition IOstream.H:591
IOstream & scientific(IOstream &io)
Definition IOstream.H:597
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
const direction noexcept
Definition scalarImpl.H:265
dictionary dict
volScalarField & b