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-2020 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  //- Compare differences in the versions
127  // Negative when 'this' is less than other.
128  // Positive when 'this' is greater than other.
129  int compare(const versionNumber& other) const noexcept
130  {
131  return number_ - other.number_;
132  }
133 
134  //- The canonical major/minor pair as an integer value.
135  int canonical() noexcept
136  {
137  return number_;
138  }
139 
140  //- Return the major version number.
141  int getMajor() const noexcept
142  {
143  return int(number_ / 10);
144  }
145 
146  //- Return the minor version number
147  int getMinor() const noexcept
148  {
149  return int(number_ % 10);
150  }
151 
152  //- A string representation of major.minor
153  std::string str() const
154  {
155  return
156  std::to_string(getMajor())
157  + '.'
158  + std::to_string(getMinor());
159  }
160  };
161 
162 
163  // Public Static Data
164 
165  //- Stream format names (ascii, binary)
166  static const Enum<streamFormat> formatNames;
167 
168  //- The current version number (2.0)
169  static const versionNumber currentVersion;
170 
171 
172 private:
173 
174  // Private Data
175 
176  // NB: ordered with versionNumber first (short) and
177  // adjacent enums to minimize gaps
178 
179  //- Stream version number (eg, 2.0 for current dictionary format)
180  versionNumber version_;
181 
182  //- Format: (ascii | binary)
183  streamFormat format_;
184 
185  //- Compression: (on | off)
186  compressionType compression_;
187 
188 
189 public:
190 
191  // Constructors
192 
193  //- Default construct (ASCII, UNCOMPRESSED, currentVersion)
194  //- or construct with format, compression
195  // \note non-explicit for convenient construction
196  constexpr IOstreamOption
197  (
198  streamFormat fmt = streamFormat::ASCII,
199  compressionType comp = compressionType::UNCOMPRESSED
200  ) noexcept
201  :
202  version_(),
203  format_(fmt),
204  compression_(comp)
205  {}
206 
207  //- Construct from components (format, compression, version)
208  constexpr IOstreamOption
209  (
210  streamFormat fmt,
211  compressionType comp,
212  versionNumber ver
213  ) noexcept
214  :
215  version_(ver),
216  format_(fmt),
217  compression_(comp)
218  {}
219 
220  //- Construct from components (format, version, compression)
221  constexpr IOstreamOption
222  (
223  streamFormat fmt,
224  versionNumber ver,
225  compressionType comp = compressionType::UNCOMPRESSED
226  ) noexcept
227  :
228  version_(ver),
229  format_(fmt),
230  compression_(comp)
231  {}
232 
233  //- Copy construct with change of format
234  IOstreamOption(const IOstreamOption& opt, streamFormat fmt) noexcept
235  :
236  version_(opt.version_),
237  format_(fmt),
238  compression_(opt.compression_)
239  {}
240 
241 
242  // Static Member Functions
243 
244  //- The stream format enum corresponding to the string
245  //- (ascii | binary).
246  //
247  // If the string is not recognized, emit warning and return default.
248  // Silent if the string itself is empty.
249  //
250  // \note Can be used as constructor substitute for the enumeration
251  static streamFormat formatEnum
252  (
253  const word& formatName,
254  const streamFormat deflt = streamFormat::ASCII
255  );
256 
257  //- Failsafe construct streamFormat from optional dictionary lookup
258  static streamFormat formatEnum
259  (
260  const word& key,
261  const dictionary& dict,
262  const streamFormat deflt = streamFormat::ASCII
263  );
264 
265  //- The compression enum corresponding to the string.
266  // Expects switch values (true/false, on/off, ...)
267  //
268  // If the string is not recognized, emit warning and return default.
269  // Silent if the string itself is empty.
270  //
271  // \note Can be used as constructor substitute for the enumeration
273  (
274  const word& compName,
275  const compressionType deflt = compressionType::UNCOMPRESSED
276  );
277 
278  //- Failsafe construct compressionType from optional dictionary lookup
280  (
281  const word& key,
282  const dictionary& dict,
283  const compressionType deflt = compressionType::UNCOMPRESSED
284  );
285 
286 
287  // Member Functions
288 
289  //- Get the current stream format
290  streamFormat format() const noexcept
291  {
292  return format_;
293  }
294 
295  //- Set the stream format
296  // \return the previous value
297  streamFormat format(const streamFormat fmt) noexcept
298  {
299  streamFormat old(format_);
300  format_ = fmt;
301  return old;
302  }
303 
304  //- Set the stream format from string value.
305  // If the string is not recognized, emit warning and leave unchanged.
306  // Silent if the string itself is empty.
307  // \return the previous value
308  streamFormat format(const word& formatName)
309  {
310  streamFormat old(format_);
311  format_ = formatEnum(formatName, format_);
312  return old;
313  }
314 
315  //- Get the stream compression
316  compressionType compression() const noexcept
317  {
318  return compression_;
319  }
320 
321  //- Set the stream compression
322  // \return the previous value
323  compressionType compression(const compressionType comp) noexcept
324  {
325  compressionType old(compression_);
326  compression_ = comp;
327  return old;
328  }
329 
330  //- Set the stream compression from string value.
331  // If the string is not recognized, emit warning and leave unchanged.
332  // Silent if the string itself is empty.
333  // \return the previous value
334  compressionType compression(const word& compName)
335  {
336  compressionType old(compression_);
337  compression_ = compressionEnum(compName, compression_);
338  return old;
339  }
340 
341  //- Get the stream version
342  versionNumber version() const noexcept
343  {
344  return version_;
345  }
346 
347  //- Set the stream version
348  // \return the previous value
349  versionNumber version(const versionNumber ver) noexcept
350  {
351  versionNumber old(version_);
352  version_ = ver;
353  return old;
354  }
355 
356  //- Set the stream version from token
357  // \return the previous value
358  versionNumber version(const token& tok)
359  {
360  versionNumber old(version_);
361  version_ = versionNumber(tok);
362  return old;
363  }
364 };
365 
366 
367 //- Output format type as text string (ascii | binary)
368 Ostream& operator<<(Ostream& os, const IOstreamOption::streamFormat& fmt);
369 
370 //- Output version as major.minor text string
371 Ostream& operator<<(Ostream& os, const IOstreamOption::versionNumber& ver);
372 
373 
374 // Comparison Operators
375 
376 //- Version number equality
377 inline bool operator==
378 (
381 ) noexcept
382 {
383  return a.compare(b) == 0;
384 }
385 
386 //- Version number inequality
387 inline bool operator!=
388 (
391 ) noexcept
392 {
393  return a.compare(b) != 0;
394 }
395 
396 //- Version A older than B
397 inline bool operator<
398 (
401 ) noexcept
402 {
403  return a.compare(b) < 0;
404 }
405 
406 //- Version A same or older than B
407 inline bool operator<=
408 (
411 ) noexcept
412 {
413  return a.compare(b) <= 0;
414 }
415 
416 //- Version A newer than B
417 inline bool operator>
418 (
421 ) noexcept
422 {
423  return a.compare(b) > 0;
424 }
425 
426 //- Version A same or newer than B
427 inline bool operator>=
428 (
431 ) noexcept
432 {
433  return a.compare(b) >= 0;
434 }
435 
436 
437 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
438 
439 } // End namespace Foam
440 
441 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
442 
443 #endif
444 
445 // ************************************************************************* //
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::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOstreamOption::versionNumber::compare
int compare(const versionNumber &other) const noexcept
Compare differences in the versions.
Definition: IOstreamOption.H:128
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:196
Foam::IOstreamOption::versionNumber::canonical
int canonical() noexcept
The canonical major/minor pair as an integer value.
Definition: IOstreamOption.H:134
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
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::versionNumber::getMajor
int getMajor() const noexcept
Return the major version number.
Definition: IOstreamOption.H:140
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:341
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 of major.minor.
Definition: IOstreamOption.H:152
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:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::IOstreamOption::versionNumber::getMinor
int getMinor() const noexcept
Return the minor version number.
Definition: IOstreamOption.H:146
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:233
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:315
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:165