Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::OFstream
29
30Description
31 Output to file stream as an OSstream, normally using \c std::ofstream
32 for the actual output.
33
34Note
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
40Note
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
55SourceFiles
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
69namespace Foam
70{
72/*---------------------------------------------------------------------------*\
73 Class OFstream Declaration
74\*---------------------------------------------------------------------------*/
75
76class OFstream
77:
79 public OSstream
80{
81public:
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).
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
118 (
119 const fileName& pathname,
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).
132 (
134 const fileName& pathname,
138 )
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")
195 (
196 const fileName& pathname,
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"
213extern OFstream Snull;
214
215// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216
217} // End namespace Foam
218
219// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220
221#endif
222
223// ************************************************************************* //
Representation of a major/minor version number.
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.
streamFormat
Data format (ascii | binary | coherent).
atomicType
Atomic operations (output).
@ NON_ATOMIC
atomic = false
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED).
@ UNCOMPRESSED
compression = false
appendType
File appending (NO_APPEND | APPEND_APP | APPEND_ATE).
@ NO_APPEND
no append (truncates existing)
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
bool is_appending() const noexcept
True if opened in append mode and file already existed.
Definition OFstream.H:191
void print(Ostream &os) const override
Print stream description.
Definition OFstream.C:164
OFstream(std::nullptr_t)
Construct a null output file stream that behaves like /dev/null.
Definition OFstream.C:36
ClassName("OFstream")
Declare type-name (with debug switch).
OFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption(), IOstreamOption::appendType append=IOstreamOption::NO_APPEND)
Construct from pathname and other specifications.
Definition OFstream.H:109
~OFstream()
Destructor. Possibly invokes an atomic rename (preference defined during construction).
Definition OFstream.C:102
OFstream(IOstreamOption::atomicType atomic, const fileName &pathname, IOstreamOption::streamFormat fmt, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED, IOstreamOption::appendType append=IOstreamOption::NO_APPEND)
Construct with specified atomic behaviour from pathname, format (uncompressed), optional append (see ...
Definition OFstream.H:140
virtual const std::ostream & stdStream() const override
Const access to underlying std::ostream.
Definition OFstream.C:125
virtual void rewind()
Rewind the stream so that it may be written again. Reopens the file (truncation).
Definition OFstream.C:140
OFstream(const fileName &pathname, IOstreamOption::streamFormat fmt, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED, IOstreamOption::appendType append=IOstreamOption::NO_APPEND)
Construct from pathname, format (uncompressed), optional append (see note), atomic behaviour as per s...
Definition OFstream.H:124
bool is_atomic() const noexcept
True if file creation behaves as atomic.
Definition OFstream.H:199
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
OSstream(const OSstream &)=default
Copy construct.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Ostream(const Ostream &)=default
Copy construct.
A class for handling file names.
Definition fileName.H:75
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.
bool is_atomic() const noexcept
True if file creation behaves as atomic.
Macro definitions for declaring ClassName(), NamespaceName(), etc.
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition className.H:74
OBJstream os(runTime.globalPath()/outputName)
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Namespace for OpenFOAM.
OFstream Snull
Global predefined null output stream "/dev/null".
const direction noexcept
Definition scalarImpl.H:265
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43