OFstream.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-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2024 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::OFstream
29 
30 Description
31  Output to file stream as an OSstream, normally using \c std::ofstream
32  for the actual output.
33 
34 Note
35  The atomic output works by creating an intermediate temporary file,
36  which is renamed as an atomic operation when closing. It is not
37  possible, or particularly desirable, to have an atomic in combination
38  with append behaviour. If both are specified, append has priority.
39 
40 Note
41  An output file can be opened in two different \c append modes, both of
42  which preserve existing files:
43  -# A common append mode is APPEND_APP, which corresponds to the
44  \c std::ios_base::app flag.
45  A seek-to-end is performed at \em every write.
46  It is thus not possible to use any manual seeks to overwrite parts
47  of the file.
48  -# The other append mode is APPEND_ATE, which roughly corresponds to the
49  \c std::ios_base::ate flag behaviour.
50  A seek-to-end is performed immediately after opening,
51  but not subsequently.
52  Manual seeks can be used to overwrite parts of the file.
53  .
54 
55 SourceFiles
56  OFstream.C
57 
58 \*---------------------------------------------------------------------------*/
59 
60 #ifndef Foam_OFstream_H
61 #define Foam_OFstream_H
62 
63 #include "OSstream.H"
64 #include "className.H"
65 #include "fstreamPointer.H"
66 
67 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68 
69 namespace Foam
70 {
71 
72 /*---------------------------------------------------------------------------*\
73  Class OFstream Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 class OFstream
77 :
78  private Foam::ofstreamPointer,
79  public OSstream
80 {
81 public:
82 
83  //- Declare type-name (with debug switch)
84  ClassName("OFstream");
85 
86 
87  // Constructors
88 
89  //- Construct a null output file stream that behaves like \c /dev/null
90  explicit OFstream(std::nullptr_t);
91 
92  //- Construct with specified atomic behaviour
93  //- from pathname, stream option, optional append (see note).
94  OFstream
95  (
97  const fileName& pathname,
98  IOstreamOption streamOpt = IOstreamOption(),
100  );
101 
102  //- Construct from pathname and other specifications.
103  // See note on append mode.
104  explicit OFstream
105  (
106  const fileName& pathname,
107  IOstreamOption streamOpt = IOstreamOption(),
109  )
110  :
111  OFstream(IOstreamOption::NON_ATOMIC, pathname, streamOpt, append)
112  {}
113 
114  //- Construct from pathname, format (uncompressed),
115  //- optional append (see note),
116  //- atomic behaviour as per system default
117  OFstream
118  (
119  const fileName& pathname,
123  )
124  :
125  OFstream(pathname, IOstreamOption(fmt, cmp), append)
126  {}
127 
128  //- Construct with specified atomic behaviour
129  //- from pathname, format (uncompressed),
130  //- optional append (see note).
131  OFstream
132  (
134  const fileName& pathname,
138  )
139  :
140  OFstream(atomic, pathname, IOstreamOption(fmt, cmp), append)
141  {}
142 
143 
144  //- Destructor. Possibly invokes an atomic rename
145  //- (preference defined during construction)
146  ~OFstream();
147 
148 
149  // Member Functions
150 
151  //- Read/write access to the name of the stream
152  using OSstream::name;
153 
154 
155  // STL stream
156 
157  //- Const access to underlying std::ostream
158  virtual const std::ostream& stdStream() const override;
159 
160  //- Access to underlying std::ostream
161  virtual std::ostream& stdStream() override;
162 
163  //- Rewind the stream so that it may be written again.
164  //- Reopens the file (truncation)
165  virtual void rewind();
166 
167 
168  // Output stream modes
169 
170  //- True if opened in append mode \em and file already existed
171  bool is_appending() const noexcept
172  {
174  }
175 
176  //- True if file creation behaves as atomic
177  bool is_atomic() const noexcept
178  {
180  }
181 
182 
183  // Print
184 
185  //- Print stream description
186  void print(Ostream& os) const override;
187 
188 
189  // Additional constructors and methods
190  #ifdef Foam_IOstream_extras
192  //- Construct from pathname, format (version, compression)
193  FOAM_DEPRECATED_FOR(2022-09, "Construct without specifying version")
194  OFstream
195  (
196  const fileName& pathname,
201  )
202  :
203  OFstream(pathname, IOstreamOption(fmt, ver, cmp), append)
204  {}
205 
206  #endif /* Foam_IOstream_extras */
207 };
208 
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 //- Global predefined null output stream "/dev/null"
213 extern OFstream Snull;
214 
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216 
217 } // End namespace Foam
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 #endif
222 
223 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A wrapped std::ofstream with possible compression handling (ogzstream) that behaves much like a std::...
bool is_appending() const noexcept
True if opened in append mode and file already existed.
Definition: OFstream.H:191
A class for handling file names.
Definition: fileName.H:72
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
~OFstream()
Destructor. Possibly invokes an atomic rename (preference defined during construction) ...
Definition: OFstream.C:102
Output to file stream as an OSstream, normally using std::ofstream for the actual output...
Definition: OFstream.H:71
void print(Ostream &os) const override
Print stream description.
Definition: OFstream.C:164
bool is_appending() const noexcept
True if opened in append mode and file already existed.
OFstream Snull
Global predefined null output stream "/dev/null".
A simple container for options an IOstream can normally have.
virtual const fileName & name() const override
Get the name of the output serial stream. (eg, the name of the Fstream file name) ...
Definition: OSstream.H:134
bool is_atomic() const noexcept
True if file creation behaves as atomic.
Definition: OFstream.H:199
bool is_atomic() const noexcept
True if file creation behaves as atomic.
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
virtual const std::ostream & stdStream() const override
Const access to underlying std::ostream.
Definition: OFstream.C:125
atomicType
Atomic operations (output)
virtual void rewind()
Rewind the stream so that it may be written again. Reopens the file (truncation)
Definition: OFstream.C:140
no append (truncates existing)
Representation of a major/minor version number.
const direction noexcept
Definition: Scalar.H:258
ClassName("OFstream")
Declare type-name (with debug switch)
OBJstream os(runTime.globalPath()/outputName)
appendType
File appending (NO_APPEND | APPEND_APP | APPEND_ATE)
OFstream(std::nullptr_t)
Construct a null output file stream that behaves like /dev/null.
Definition: OFstream.C:36
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Macro definitions for declaring ClassName(), NamespaceName(), etc.
streamFormat
Data format (ascii | binary)
Ostream(const Ostream &)=default
Copy construct.
Namespace for OpenFOAM.