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-2021 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 The IOstreamOption is a simple container for options an IOstream
32 can normally have.
33
34 The format (ASCII | BINARY) is typically controlled by enumerated
35 names (ascii, binary).
36
37 The compression (UNCOMPRESSED | COMPRESSED) is typically controlled
38 by switch values (true/false, on/off, ...).
39
40SourceFiles
41 IOstreamOption.C
42
43\*---------------------------------------------------------------------------*/
44
45#ifndef IOstreamOption_H
46#define IOstreamOption_H
47
48#include "word.H"
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55// Forward Declarations
56class token;
57class dictionary;
58template<class EnumType> class Enum;
59
60/*---------------------------------------------------------------------------*\
61 Class IOstreamOption Declaration
62\*---------------------------------------------------------------------------*/
65{
66public:
67
68 // Public Data Types
69
70 //- Data format (ascii | binary)
71 enum streamFormat : char
72 {
73 ASCII = 0,
75 };
76
77 //- Compression treatment (UNCOMPRESSED | COMPRESSED)
78 enum compressionType : char
79 {
80 UNCOMPRESSED = 0,
82 };
83
84
85 //- Representation of a major/minor version number
86 class versionNumber
87 {
88 //- The combined major/version number.
89 short number_;
90
91 public:
92
93 // Constructors
94
95 //- Default construct \em current version.
96 //- The value (2.0) corresponds to the \em current version.
97 constexpr versionNumber() noexcept
98 :
99 number_(20)
100 {}
101
102 //- Construct from major, number
103 constexpr versionNumber(int major, int minor) noexcept
104 :
105 number_(10*major + (minor % 10))
106 {}
107
108 //- Construct from floating-point version number
109 explicit constexpr versionNumber(const float ver) noexcept
110 :
111 number_(10*ver + 0.001) // Allow some rounding
112 {}
113
114 //- Construct by parsing string "major.minor"
115 explicit versionNumber(const std::string& verNum);
116
117 //- Construct from token (float, word, string)
118 explicit versionNumber(const token& tok);
119
120 //- Failsafe construct from dictionary lookup.
121 versionNumber(const word& keyword, const dictionary& dict);
122
123
124 // Member Functions
125
126 //- A string representation as major.minor
127 std::string str() const
128 {
129 return
130 (
131 std::to_string(int(number_ / 10)) // major
132 + '.'
133 + std::to_string(int(number_ % 10)) // minor
134 );
135 }
136
137 //- From version to canonical integer value
138 int canonical() const noexcept
139 {
140 return number_;
141 }
142
143 //- From canonical integer value to version
144 static versionNumber canonical(int verNum) noexcept
145 {
146 // Split into major/minor
147 return versionNumber(int(verNum / 10), int(verNum % 10));
148 }
149
150 //- Compare differences in the versions
151 // Negative when 'this' is less than other.
152 // Positive when 'this' is greater than other.
153 int compare(const versionNumber& other) const noexcept
154 {
155 return number_ - other.number_;
156 }
157 };
158
159
160 // Public Static Data
161
162 //- Stream format names (ascii, binary)
163 static const Enum<streamFormat> formatNames;
164
165 //- The current version number (2.0)
166 static const versionNumber currentVersion;
167
168
169private:
170
171 // Private Data
172
173 // NB: ordered with versionNumber first (short) and
174 // adjacent enums to minimize gaps
175
176 //- Stream version number (eg, 2.0 for current dictionary format)
177 versionNumber version_;
178
179 //- Format: (ascii | binary)
180 streamFormat format_;
181
182 //- Compression: (on | off)
183 compressionType compression_;
184
185
186public:
187
188 // Constructors
189
190 //- Default construct (ASCII, UNCOMPRESSED, currentVersion)
191 //- or construct with format, compression
192 // \note non-explicit for convenient construction
193 constexpr IOstreamOption
194 (
197 ) noexcept
198 :
199 version_(),
200 format_(fmt),
201 compression_(comp)
202 {}
203
204 //- Construct from components (format, compression, version)
205 constexpr IOstreamOption
206 (
207 streamFormat fmt,
208 compressionType comp,
209 versionNumber ver
210 ) noexcept
211 :
212 version_(ver),
213 format_(fmt),
214 compression_(comp)
215 {}
216
217 //- Construct from components (format, version, compression)
218 constexpr IOstreamOption
219 (
220 streamFormat fmt,
221 versionNumber ver,
223 ) noexcept
224 :
225 version_(ver),
226 format_(fmt),
227 compression_(comp)
228 {}
229
230 //- Copy construct with change of format
232 :
233 version_(opt.version_),
234 format_(fmt),
235 compression_(opt.compression_)
236 {}
237
238
239 // Static Member Functions
240
241 //- The stream format enum corresponding to the string
242 //- (ascii | binary).
243 //
244 // If the string is not recognized, emit warning and return default.
245 // Silent if the string itself is empty.
246 //
247 // \note Can be used as constructor substitute for the enumeration
249 (
250 const word& formatName,
252 );
253
254 //- Failsafe construct streamFormat from optional dictionary lookup
256 (
257 const word& key,
258 const dictionary& dict,
260 );
261
262 //- The compression enum corresponding to the string.
263 // Expects switch values (true/false, on/off, ...)
264 //
265 // If the string is not recognized, emit warning and return default.
266 // Silent if the string itself is empty.
267 //
268 // \note Can be used as constructor substitute for the enumeration
270 (
271 const word& compName,
273 );
274
275 //- Failsafe construct compressionType from optional dictionary lookup
277 (
278 const word& key,
279 const dictionary& dict,
281 );
282
283
284 // Member Functions
285
286 //- Get the current stream format
288 {
289 return format_;
290 }
291
292 //- Set the stream format
293 // \return the previous value
294 streamFormat format(const streamFormat fmt) noexcept
295 {
296 streamFormat old(format_);
297 format_ = fmt;
298 return old;
299 }
300
301 //- Set the stream format from string value.
302 // If the string is not recognized, emit warning and leave unchanged.
303 // Silent if the string itself is empty.
304 // \return the previous value
305 streamFormat format(const word& formatName)
306 {
307 streamFormat old(format_);
308 format_ = formatEnum(formatName, format_);
309 return old;
310 }
311
312 //- Get the stream compression
314 {
315 return compression_;
316 }
317
318 //- Set the stream compression
319 // \return the previous value
320 compressionType compression(const compressionType comp) noexcept
321 {
322 compressionType old(compression_);
323 compression_ = comp;
324 return old;
325 }
326
327 //- Set the stream compression from string value.
328 // If the string is not recognized, emit warning and leave unchanged.
329 // Silent if the string itself is empty.
330 // \return the previous value
331 compressionType compression(const word& compName)
332 {
333 compressionType old(compression_);
334 compression_ = compressionEnum(compName, compression_);
335 return old;
336 }
337
338 //- Get the stream version
340 {
341 return version_;
342 }
343
344 //- Set the stream version
345 // \return the previous value
346 versionNumber version(const versionNumber ver) noexcept
347 {
348 versionNumber old(version_);
349 version_ = ver;
350 return old;
351 }
352
353 //- Set the stream version from token
354 // \return the previous value
355 versionNumber version(const token& tok)
356 {
357 versionNumber old(version_);
358 version_ = versionNumber(tok);
359 return old;
360 }
361};
362
363
364// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
365
366//- Output format type as text string (ascii | binary)
367Ostream& operator<<(Ostream& os, const IOstreamOption::streamFormat& fmt);
368
369//- Output version as major.minor text string
370Ostream& operator<<(Ostream& os, const IOstreamOption::versionNumber& ver);
371
372
373// * * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * //
374
375// Comparison Operators
376
377//- Version number equality
378inline bool operator==
379(
382) noexcept
383{
384 return a.compare(b) == 0;
385}
386
387//- Version number inequality
388inline bool operator!=
389(
392) noexcept
393{
394 return a.compare(b) != 0;
395}
396
397//- Version A older than B
398inline bool operator<
399(
402) noexcept
403{
404 return a.compare(b) < 0;
405}
406
407//- Version A same or older than B
408inline bool operator<=
409(
412) noexcept
413{
414 return a.compare(b) <= 0;
415}
416
417//- Version A newer than B
418inline bool operator>
419(
422) noexcept
423{
424 return a.compare(b) > 0;
425}
426
427//- Version A same or newer than B
428inline bool operator>=
429(
432) noexcept
433{
434 return a.compare(b) >= 0;
435}
436
437
438// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
439
440} // End namespace Foam
441
442// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
443
444#endif
445
446// ************************************************************************* //
#define minor(dev)
Definition: fileStat.C:40
#define major(dev)
Definition: fileStat.C:39
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
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
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.
The IOstreamOption is a simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
compressionType compression() const noexcept
Get the stream compression.
streamFormat format() const noexcept
Get the current stream format.
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)
@ ASCII
"ascii" (normal default)
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
@ UNCOMPRESSED
compression = false
@ COMPRESSED
compression = true
static const versionNumber currentVersion
The current version number (2.0)
constexpr IOstreamOption(streamFormat fmt, versionNumber ver, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Construct from components (format, version, compression)
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
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.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
const direction noexcept
Definition: Scalar.H:223
dictionary dict
volScalarField & b
Definition: createFields.H:27