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 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. For example, ascii/binary, uncompressed/compressed, ...
33 
34 SourceFiles
35  IOstreamOption.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef IOstreamOption_H
40 #define IOstreamOption_H
41 
42 #include "scalar.H"
43 #include "word.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declarations
51 class Ostream;
52 template<class EnumType> class Enum;
53 
54 /*---------------------------------------------------------------------------*\
55  Class IOstreamOption Declaration
56 \*---------------------------------------------------------------------------*/
57 
58 class IOstreamOption
59 {
60 public:
61 
62  // Public Data Types
63 
64  //- Data format (ascii | binary)
65  enum streamFormat : char
66  {
68  BINARY
69  };
70 
71  //- Compression treatment (UNCOMPRESSED | COMPRESSED)
72  enum compressionType : char
73  {
75  COMPRESSED
76  };
77 
78 
79  //- Representation of a major/minor version number
80  class versionNumber
81  {
82  //- The combined major/version number.
83  short number_;
84 
85  public:
86 
87  // Constructors
88 
89  //- Construct from major, number
90  constexpr versionNumber(int major, int minor) noexcept
91  :
92  number_(10*major + (minor % 10))
93  {}
94 
95  //- Construct from floating-point version number
96  explicit constexpr versionNumber(const float ver) noexcept
97  :
98  number_(10*ver + 0.001) // Allow some rounding
99  {}
100 
101  //- Construct from Istream by reading in a float.
102  // Non-explicit for convenience
104  :
105  versionNumber(readFloat(is))
106  {}
107 
108  //- Construct by parsing string "major.minor"
109  explicit versionNumber(const std::string& verNum)
110  :
111  versionNumber(readFloat(verNum))
112  {}
113 
114 
115  // Member Functions
116 
117  //- The canonical major/minor pair as an integer value.
118  inline int canonical() noexcept
119  {
120  return number_;
121  }
122 
123  //- Return the major version number.
124  inline int getMajor() const noexcept
125  {
126  return int(number_ / 10);
127  }
128 
129  //- Return the minor version number
130  inline int getMinor() const noexcept
131  {
132  return int(number_ % 10);
133  }
134 
135  //- A string representation of major.minor
136  std::string str() const
137  {
138  return
139  std::to_string(getMajor())
140  + '.'
141  + std::to_string(getMinor());
142  }
143 
144 
145  // Member Operators
146 
147  //- Version number equality
148  bool operator==(const versionNumber& rhs) const noexcept
149  {
150  return number_ == rhs.number_;
151  }
152 
153  //- Version number inequality
154  bool operator!=(const versionNumber& rhs) const noexcept
155  {
156  return number_ != rhs.number_;
157  }
158 
159  //- Version number older than rhs
160  bool operator<(const versionNumber& rhs) const noexcept
161  {
162  return number_ < rhs.number_;
163  }
164 
165  //- Version number is the same or older than rhs
166  bool operator<=(const versionNumber& rhs) const noexcept
167  {
168  return number_ <= rhs.number_;
169  }
170 
171  //- Version number newer than rhs
172  bool operator>(const versionNumber& rhs) const noexcept
173  {
174  return number_ > rhs.number_;
175  }
176 
177  //- Version number same or newer than rhs
178  bool operator>=(const versionNumber& rhs) const noexcept
179  {
180  return number_ >= rhs.number_;
181  }
182  };
183 
184 
185  // Public Static Data
186 
187  //- Stream format names (ascii, binary)
188  static const Enum<streamFormat> formatNames;
189 
190  //- The original version number
191  static const versionNumber originalVersion;
192 
193  //- The current version number
194  static const versionNumber currentVersion;
195 
196 
197 private:
198 
199  // Private Data
200 
201  // NB: ordered with adjacent enums to minimize gaps
202 
203  //- Stream version number (eg, 2.0 for current dictionary format)
204  versionNumber version_;
205 
206  //- Format: (ascii | binary)
207  streamFormat format_;
208 
209  //- Compression: (on | off)
210  compressionType compression_;
211 
212 
213 public:
214 
215  // Constructors
216 
217  //- Construct null. (default: ASCII, uncompressed, currentVersion)
218  IOstreamOption() noexcept
219  :
220  version_(currentVersion),
221  format_(ASCII),
222  compression_(compressionType::UNCOMPRESSED)
223  {}
224 
225  //- Construct with format. (default: uncompressed, currentVersion)
226  explicit IOstreamOption(streamFormat format) noexcept
227  :
228  version_(currentVersion),
229  format_(format),
230  compression_(compressionType::UNCOMPRESSED)
231  {}
232 
233  //- Construct with format and compression, optionally with version.
235  (
239  ) noexcept
240  :
241  version_(version),
242  format_(format),
243  compression_(compression)
244  {}
245 
246  //- Construct with format, version, compression
248  (
252  ) noexcept
253  :
254  version_(version),
255  format_(format),
256  compression_(compression)
257  {}
258 
259 
260  // Static Member Functions
261 
262  //- The stream format enum corresponding to the string
263  // Expected "ascii", "binary"
264  static streamFormat formatEnum(const word& formatName);
265 
266  //- The compression enum corresponding to the string
267  // Expected "true", "false", "on", "off", etc.
268  static compressionType compressionEnum(const word& compName);
269 
270 
271  // Member Functions
272 
273  //- Get the current stream format
274  streamFormat format() const noexcept
275  {
276  return format_;
277  }
278 
279  //- Set the stream format
280  // \return the previous value
281  streamFormat format(const streamFormat format) noexcept
282  {
283  streamFormat old(format_);
284  format_ = format;
285  return old;
286  }
287 
288  //- Set the stream format, from string value
289  // \return the previous value
290  streamFormat format(const word& formatName)
291  {
292  streamFormat old(format_);
293  format_ = formatEnum(formatName);
294  return old;
295  }
296 
297  //- Get the stream compression
298  compressionType compression() const noexcept
299  {
300  return compression_;
301  }
302 
303  //- Set the stream compression
304  // \return the previous value
305  compressionType compression(const compressionType comp) noexcept
306  {
307  compressionType old(compression_);
308  compression_ = comp;
309  return old;
310  }
311 
312  //- Set the stream compression, from string value.
313  // \return the previous value
314  compressionType compression(const word& compressionName)
315  {
316  compressionType old(compression_);
317  compression_ = compressionEnum(compressionName);
318  return old;
319  }
320 
321  //- Get the stream version
322  versionNumber version() const noexcept
323  {
324  return version_;
325  }
326 
327  //- Set the stream version
328  // \return the previous value
329  versionNumber version(const versionNumber verNum) noexcept
330  {
331  versionNumber old(version_);
332  version_ = verNum;
333  return old;
334  }
335 };
336 
337 
338 //- Output the format as text string (ascii | binary)
339 Ostream& operator<<(Ostream& os, const IOstreamOption::streamFormat& sf);
340 
341 //- Output the version as major.minor
342 Ostream& operator<<(Ostream& os, const IOstreamOption::versionNumber& vn);
343 
344 
345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 
347 } // End namespace Foam
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 #endif
352 
353 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:73
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:51
Foam::IOstreamOption::versionNumber::operator>=
bool operator>=(const versionNumber &rhs) const noexcept
Version number same or newer than rhs.
Definition: IOstreamOption.H:177
Foam::IOstreamOption::versionNumber::operator<=
bool operator<=(const versionNumber &rhs) const noexcept
Version number is the same or older than rhs.
Definition: IOstreamOption.H:165
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOstreamOption::versionNumber::canonical
int canonical() noexcept
The canonical major/minor pair as an integer value.
Definition: IOstreamOption.H:117
Foam::IOstreamOption::versionNumber::operator<
bool operator<(const versionNumber &rhs) const noexcept
Version number older than rhs.
Definition: IOstreamOption.H:159
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number.
Definition: IOstreamOption.H:193
Foam::IOstreamOption::IOstreamOption
IOstreamOption(streamFormat format) noexcept
Construct with format. (default: uncompressed, currentVersion)
Definition: IOstreamOption.H:225
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:89
Foam::IOstreamOption::versionNumber::versionNumber
versionNumber(const std::string &verNum)
Construct by parsing string "major.minor".
Definition: IOstreamOption.H:108
Foam::IOstreamOption::versionNumber::operator!=
bool operator!=(const versionNumber &rhs) const noexcept
Version number inequality.
Definition: IOstreamOption.H:153
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstreamOption::versionNumber::getMajor
int getMajor() const noexcept
Return the major version number.
Definition: IOstreamOption.H:123
Foam::IOstreamOption::versionNumber::operator>
bool operator>(const versionNumber &rhs) const noexcept
Version number newer than rhs.
Definition: IOstreamOption.H:171
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have....
Definition: IOstreamOption.H:57
Foam::IOstreamOption::versionNumber::operator==
bool operator==(const versionNumber &rhs) const noexcept
Version number equality.
Definition: IOstreamOption.H:147
Foam::IOstreamOption::compressionEnum
static compressionType compressionEnum(const word &compName)
The compression enum corresponding to the string.
Definition: IOstreamOption.C:73
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:321
Foam::IOstreamOption::originalVersion
static const versionNumber originalVersion
The original version number.
Definition: IOstreamOption.H:190
Foam::IOstreamOption::versionNumber::str
std::string str() const
A string representation of major.minor.
Definition: IOstreamOption.H:135
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
scalar.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:67
Foam::IOstreamOption::IOstreamOption
IOstreamOption() noexcept
Construct null. (default: ASCII, uncompressed, currentVersion)
Definition: IOstreamOption.H:217
Foam::IOstreamOption::versionNumber::getMinor
int getMinor() const noexcept
Return the minor version number.
Definition: IOstreamOption.H:129
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:74
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName)
The stream format enum corresponding to the string.
Definition: IOstreamOption.C:56
minor
#define minor(dev)
Definition: fileStat.C:40
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
word.H
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:297
Foam::IOstreamOption::versionNumber::versionNumber
constexpr versionNumber(const float ver) noexcept
Construct from floating-point version number.
Definition: IOstreamOption.H:95
Foam::IOstreamOption::formatNames
static const Enum< streamFormat > formatNames
Stream format names (ascii, binary)
Definition: IOstreamOption.H:187
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102