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-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::IOstreamOption
29 
30 Description
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 
39  Additionally, some enumerations are defined (APPEND, NON_APPEND, ...)
40  that are useful, verbose alternatives to bool values.
41 
42 SourceFiles
43  IOstreamOption.C
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #ifndef Foam_IOstreamOption_H
48 #define Foam_IOstreamOption_H
49 
50 #include "word.H"
51 #include <ios>
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward Declarations
59 class token;
60 class dictionary;
61 template<class EnumType> class Enum;
62 
63 /*---------------------------------------------------------------------------*\
64  Class IOstreamOption Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 class IOstreamOption
68 {
69 public:
70 
71  // Public Data Types
72 
73  //- Data format (ascii | binary)
74  enum streamFormat : char
75  {
76  ASCII = 0,
77  BINARY
78  };
79 
80  //- Compression treatment (UNCOMPRESSED | COMPRESSED)
81  enum compressionType : char
82  {
84  COMPRESSED
85  };
86 
87  //- File appending (NON_APPEND | APPEND)
88  enum appendType : char
89  {
90  NON_APPEND = 0,
92  };
93 
94  //- Atomic operations (output)
95  enum atomicType : char
96  {
97  NON_ATOMIC = 0,
99  };
101  //- Float formats (eg, time directory name formats)
102  enum class floatFormat : unsigned
103  {
105  general = unsigned(0),
110  };
111 
112 
113  //- Representation of a major/minor version number
114  class versionNumber
115  {
116  //- The combined major/version number.
117  short number_;
118 
119  public:
120 
121  // Constructors
122 
123  //- Default construct \em current version.
124  //- The value (2.0) corresponds to the \em current version.
125  constexpr versionNumber() noexcept
126  :
127  number_(20)
128  {}
129 
130  //- Construct from major, number
131  constexpr versionNumber(int major, int minor) noexcept
132  :
133  number_(10*major + (minor % 10))
134  {}
135 
136  //- Construct from floating-point version number
137  explicit constexpr versionNumber(const float ver) noexcept
138  :
139  number_(10*ver + 0.001) // Allow some rounding
140  {}
141 
142  //- Construct by parsing string "major.minor"
143  explicit versionNumber(const std::string& verNum);
145  //- Construct from token (float, word, string)
146  explicit versionNumber(const token& tok);
147 
148  //- Failsafe construct from dictionary lookup.
149  versionNumber(const word& key, const dictionary& dict);
150 
151 
152  // Member Functions
153 
154  //- A string representation as major.minor
155  std::string str() const
156  {
157  return
158  (
159  std::to_string(int(number_ / 10)) // major
160  + '.'
161  + std::to_string(int(number_ % 10)) // minor
162  );
163  }
164 
165  //- From version to canonical integer value
166  int canonical() const noexcept
167  {
168  return number_;
169  }
170 
171  //- From canonical integer value to version
172  static versionNumber canonical(int verNum) noexcept
173  {
174  // Split into major/minor
175  return versionNumber(int(verNum / 10), int(verNum % 10));
176  }
177 
178  //- Compare differences in the versions
179  // Negative when 'this' is less than other.
180  // Positive when 'this' is greater than other.
181  int compare(const versionNumber& other) const noexcept
182  {
183  return (number_ - other.number_);
184  }
185  };
186 
187 
188  // Public Static Data
189 
190  //- Names for float formats (general, fixed, scientific)
192 
193  //- Stream format names (ascii, binary)
194  static const Enum<streamFormat> formatNames;
195 
196  //- The current version number (2.0)
197  static const versionNumber currentVersion;
198 
200  // Static Helpers
201 
202  //- Lookup floatFormat enum corresponding to the string
203  //- (general | fixed | scientific).
204  //
205  // If the string is not recognized, emit warning and return default.
206  // Silent if the string itself is empty.
207  //
208  // \note Can be used as constructor substitute for the enumeration
210  (
211  const word& fmtName,
212  const floatFormat deflt = floatFormat::general
213  );
214 
215  //- getOrDefault floatFormat from dictionary,
216  //- warn only on bad enumeration.
218  (
219  const word& key,
220  const dictionary& dict,
221  const floatFormat deflt = floatFormat::general
222  );
224  //- Lookup streamFormat enum corresponding to the string
225  //- (ascii | binary).
226  //
227  // If the string is not recognized, emit warning and return default.
228  // Silent if the string itself is empty.
229  //
230  // \note Can be used as constructor substitute for the enumeration
231  static streamFormat formatEnum
232  (
233  const word& fmtName,
234  const streamFormat deflt = streamFormat::ASCII
235  );
236 
237  //- getOrDefault streamFormat from dictionary,
238  //- warn only on bad enumeration.
239  static streamFormat formatEnum
240  (
241  const word& key,
242  const dictionary& dict,
243  const streamFormat deflt = streamFormat::ASCII
244  );
245 
246  //- The compression enum corresponding to the string.
247  // Expects switch values (true/false, on/off, ...)
248  //
249  // If the string is not recognized, emit warning and return default.
250  // Silent if the string itself is empty.
251  //
252  // \note Can be used as constructor substitute for the enumeration
254  (
255  const word& compName,
256  const compressionType deflt = compressionType::UNCOMPRESSED
257  );
258 
259  //- getOrDefault compressionType from dictionary,
260  //- warn only on bad enumeration.
262  (
263  const word& key,
264  const dictionary& dict,
265  const compressionType deflt = compressionType::UNCOMPRESSED
266  );
267 
268 
269 private:
270 
271  // Private Data
272 
273  // NB: ordered with versionNumber first (short) and
274  // adjacent enums to minimize gaps
275 
276  //- Stream version number (eg, 2.0 for current dictionary format)
277  versionNumber version_;
278 
279  //- Format: (ascii | binary)
280  streamFormat format_;
281 
282  //- Compression: (on | off)
283  compressionType compression_;
284 
285 
286 public:
287 
288  // Constructors
289 
290  //- Default construct (ASCII, UNCOMPRESSED, currentVersion)
291  //- or construct with format, compression
292  // \note non-explicit for convenient construction
293  constexpr IOstreamOption
294  (
295  streamFormat fmt = streamFormat::ASCII,
296  compressionType comp = compressionType::UNCOMPRESSED
297  ) noexcept
298  :
299  version_(),
300  format_(fmt),
301  compression_(comp)
302  {}
303 
304  //- Construct from components (format, compression, version)
305  constexpr IOstreamOption
306  (
307  streamFormat fmt,
308  compressionType comp,
309  versionNumber ver
310  ) noexcept
311  :
312  version_(ver),
313  format_(fmt),
314  compression_(comp)
315  {}
316 
317  //- Construct from components (format, version, compression)
318  constexpr IOstreamOption
319  (
320  streamFormat fmt,
321  versionNumber ver,
322  compressionType comp = compressionType::UNCOMPRESSED
323  ) noexcept
324  :
325  version_(ver),
326  format_(fmt),
327  compression_(comp)
328  {}
329 
330  //- Copy construct with change of format
332  :
333  version_(opt.version_),
334  format_(fmt),
335  compression_(opt.compression_)
336  {}
337 
338 
339  // Member Functions
340 
341  //- Get the current stream format
342  streamFormat format() const noexcept { return format_; }
343 
344  //- Set the stream format
345  // \return the previous value
347  {
348  streamFormat old(format_);
349  format_ = fmt;
350  return old;
351  }
352 
353  //- Set the stream format from string value.
354  // If the string is not recognized, emit warning and leave unchanged.
355  // Silent if the string itself is empty.
356  // \return the previous value
357  streamFormat format(const word& formatName)
358  {
359  streamFormat old(format_);
360  format_ = formatEnum(formatName, format_);
361  return old;
362  }
363 
364  //- Get the stream compression
365  compressionType compression() const noexcept { return compression_; }
366 
367  //- Set the stream compression
368  // \return the previous value
370  {
371  compressionType old(compression_);
372  compression_ = comp;
373  return old;
374  }
375 
376  //- Set the stream compression from string value.
377  // If the string is not recognized, emit warning and leave unchanged.
378  // Silent if the string itself is empty.
379  // \return the previous value
380  compressionType compression(const word& compName)
381  {
382  compressionType old(compression_);
383  compression_ = compressionEnum(compName, compression_);
384  return old;
385  }
386 
387  //- Get the stream version
388  versionNumber version() const noexcept { return version_; }
389 
390  //- Set the stream version
391  // \return the previous value
392  versionNumber version(const versionNumber ver) noexcept
393  {
394  versionNumber old(version_);
395  version_ = ver;
396  return old;
397  }
398 
399  //- Set the stream version from token
400  // \return the previous value
401  versionNumber version(const token& tok)
402  {
403  versionNumber old(version_);
404  version_ = versionNumber(tok);
405  return old;
406  }
407 };
408 
409 
410 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
411 
412 //- Output format type as text string (ascii | binary)
414 
415 //- Output version as major.minor text string
418 
419 // * * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * //
420 
421 // Comparison Operators
422 
423 //- Version number equality
424 inline bool operator==
425 (
428 ) noexcept
429 {
430  return a.compare(b) == 0;
431 }
432 
433 //- Version number inequality
434 inline bool operator!=
435 (
438 ) noexcept
439 {
440  return a.compare(b) != 0;
441 }
442 
443 //- Version A older than B
444 inline bool operator<
445 (
449 {
450  return a.compare(b) < 0;
451 }
452 
453 //- Version A same or older than B
454 inline bool operator<=
455 (
458 ) noexcept
459 {
460  return a.compare(b) <= 0;
461 }
463 //- Version A newer than B
464 inline bool operator>
465 (
468 ) noexcept
469 {
470  return a.compare(b) > 0;
471 }
473 //- Version A same or newer than B
474 inline bool operator>=
475 (
478 ) noexcept
479 {
480  return a.compare(b) >= 0;
481 }
482 
483 
484 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
485 
486 } // End namespace Foam
487 
488 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
489 
490 #endif
492 // ************************************************************************* //
dictionary dict
int compare(const versionNumber &other) const noexcept
Compare differences in the versions.
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
General relative velocity model.
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
"ascii" (normal default)
floatFormat
Float formats (eg, time directory name formats)
A token holds an item read from Istream.
Definition: token.H:65
A simple container for options an IOstream can normally have.
IOstream & fixed(IOstream &io)
Definition: IOstream.H:557
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
int canonical() const noexcept
From version to canonical integer value.
atomicType
Atomic operations (output)
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from Foam::string.
Definition: word.H:63
static const versionNumber currentVersion
The current version number (2.0)
Representation of a major/minor version number.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
compressionType compression() const noexcept
Get the stream compression.
appendType
File appending (NON_APPEND | APPEND)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
static floatFormat floatFormatEnum(const word &fmtName, const floatFormat deflt=floatFormat::general)
Lookup floatFormat enum corresponding to the string (general | fixed | scientific).
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
static const Enum< floatFormat > floatFormatNames
Names for float formats (general, fixed, scientific)
constexpr versionNumber() noexcept
Default construct current version. The value (2.0) corresponds to the current version.
static streamFormat formatEnum(const word &fmtName, const streamFormat deflt=streamFormat::ASCII)
Lookup streamFormat enum corresponding to the string (ascii | binary).
versionNumber version() const noexcept
Get the stream version.
streamFormat
Data format (ascii | binary)
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
std::string str() const
A string representation as major.minor.
streamFormat format() const noexcept
Get the current stream format.
IOstream & scientific(IOstream &io)
Definition: IOstream.H:563
Namespace for OpenFOAM.