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 -------------------------------------------------------------------------------
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  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 
40 SourceFiles
41  IOstreamOption.C
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef IOstreamOption_H
46 #define IOstreamOption_H
47 
48 #include "word.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 class token;
57 class dictionary;
58 template<class EnumType> class Enum;
59 
60 /*---------------------------------------------------------------------------*\
61  Class IOstreamOption Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 class IOstreamOption
65 {
66 public:
67 
68  // Public Data Types
69 
70  //- Data format (ascii | binary)
71  enum streamFormat : char
72  {
73  ASCII = 0,
74  BINARY
75  };
76 
77  //- Compression treatment (UNCOMPRESSED | COMPRESSED)
78  enum compressionType : char
79  {
81  COMPRESSED
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 
169 private:
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 
186 public:
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  (
195  streamFormat fmt = streamFormat::ASCII,
196  compressionType comp = compressionType::UNCOMPRESSED
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,
222  compressionType comp = compressionType::UNCOMPRESSED
223  ) noexcept
224  :
225  version_(ver),
226  format_(fmt),
227  compression_(comp)
228  {}
229 
230  //- Copy construct with change of format
231  IOstreamOption(const IOstreamOption& opt, streamFormat fmt) noexcept
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
248  static streamFormat formatEnum
249  (
250  const word& formatName,
251  const streamFormat deflt = streamFormat::ASCII
252  );
253 
254  //- Failsafe construct streamFormat from optional dictionary lookup
255  static streamFormat formatEnum
256  (
257  const word& key,
258  const dictionary& dict,
259  const streamFormat deflt = streamFormat::ASCII
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,
272  const compressionType deflt = compressionType::UNCOMPRESSED
273  );
274 
275  //- Failsafe construct compressionType from optional dictionary lookup
277  (
278  const word& key,
279  const dictionary& dict,
280  const compressionType deflt = compressionType::UNCOMPRESSED
281  );
282 
283 
284  // Member Functions
285 
286  //- Get the current stream format
287  streamFormat format() const noexcept
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
313  compressionType compression() const noexcept
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
339  versionNumber version() const noexcept
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)
367 Ostream& operator<<(Ostream& os, const IOstreamOption::streamFormat& fmt);
368 
369 //- Output version as major.minor text string
370 Ostream& operator<<(Ostream& os, const IOstreamOption::versionNumber& ver);
371 
372 
373 // * * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * //
374 
375 // Comparison Operators
376 
377 //- Version number equality
378 inline bool operator==
379 (
382 ) noexcept
383 {
384  return a.compare(b) == 0;
385 }
386 
387 //- Version number inequality
388 inline bool operator!=
389 (
392 ) noexcept
393 {
394  return a.compare(b) != 0;
395 }
396 
397 //- Version A older than B
398 inline bool operator<
399 (
402 ) noexcept
403 {
404  return a.compare(b) < 0;
405 }
406 
407 //- Version A same or older than B
408 inline bool operator<=
409 (
412 ) noexcept
413 {
414  return a.compare(b) <= 0;
415 }
416 
417 //- Version A newer than B
418 inline bool operator>
419 (
422 ) noexcept
423 {
424  return a.compare(b) > 0;
425 }
426 
427 //- Version A same or newer than B
428 inline 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 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:79
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::IOstreamOption::versionNumber::canonical
int canonical() const noexcept
From version to canonical integer value.
Definition: IOstreamOption.H:137
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::IOstreamOption::versionNumber::compare
int compare(const versionNumber &other) const noexcept
Compare differences in the versions.
Definition: IOstreamOption.H:152
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
major
#define major(dev)
Definition: fileStat.C:39
Foam::IOstreamOption::versionNumber::versionNumber
constexpr versionNumber(int major, int minor) noexcept
Construct from major, number.
Definition: IOstreamOption.H:102
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:338
Foam::IOstreamOption::versionNumber::versionNumber
constexpr versionNumber() noexcept
Definition: IOstreamOption.H:96
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstreamOption::compressionEnum
static compressionType compressionEnum(const word &compName, const compressionType deflt=compressionType::UNCOMPRESSED)
The compression enum corresponding to the string.
Definition: IOstreamOption.C:92
Foam::IOstreamOption::versionNumber::str
std::string str() const
A string representation as major.minor.
Definition: IOstreamOption.H:126
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOstreamOption::versionNumber::canonical
static versionNumber canonical(int verNum) noexcept
From canonical integer value to version.
Definition: IOstreamOption.H:143
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:80
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
Definition: IOstreamOption.C:53
Foam::IOstreamOption::IOstreamOption
IOstreamOption(const IOstreamOption &opt, streamFormat fmt) noexcept
Copy construct with change of format.
Definition: IOstreamOption.H:230
minor
#define minor(dev)
Definition: fileStat.C:40
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
word.H
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:312
Foam::IOstreamOption::versionNumber::versionNumber
constexpr versionNumber(const float ver) noexcept
Construct from floating-point version number.
Definition: IOstreamOption.H:108
Foam::IOstreamOption::formatNames
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
Definition: IOstreamOption.H:162