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