Loading...
Searching...
No Matches
fstreamPointer.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) 2020-2024 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::ifstreamPointer
28
29Description
30 A wrapped \c std::ifstream with possible compression handling
31 (igzstream) that behaves much like a \c std::unique_ptr.
32
33Note
34 No <tt>operator bool</tt> to avoid inheritance ambiguity with
35 <tt>std::ios::operator bool</tt>.
36
37SourceFiles
38 fstreamPointers.C
39
40Class
41 Foam::ofstreamPointer
42
43Description
44 A wrapped \c std::ofstream with possible compression handling
45 (ogzstream) that behaves much like a \c std::unique_ptr.
46
47Note
48 No <tt>operator bool</tt> to avoid inheritance ambiguity with
49 <tt>std::ios::operator bool</tt>.
50
51SourceFiles
52 fstreamPointers.C
53
54\*---------------------------------------------------------------------------*/
55
56#ifndef Foam_fstreamPointer_H
57#define Foam_fstreamPointer_H
58
59#include "IOstreamOption.H"
60#include "fileName.H"
61#include <fstream>
62#include <memory>
63
64// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65
66namespace Foam
67{
68
69/*---------------------------------------------------------------------------*\
70 Class ifstreamPointer Declaration
71\*---------------------------------------------------------------------------*/
72
74{
75 // Private Data
76
77 //- The stream pointer (ifstream | igzstream, ...)
78 std::unique_ptr<std::istream> ptr_;
79
80
81protected:
82
83 // Protected Member Functions
84
85 //- Special 'rewind' method for compressed stream
86 void reopen_gz(const std::string& pathname);
87
88
89public:
90
91 // Generated Methods
92
93 //- Default construct (empty)
94 ifstreamPointer() noexcept = default;
95
96 //- No copy construct
98
99 //- Move construct
100 ifstreamPointer(ifstreamPointer&&) = default;
101
102 //- No copy assignment
103 void operator=(const ifstreamPointer&) = delete;
104
105 //- Move assignment
108 //- Destructor
109 ~ifstreamPointer() = default;
110
111
112 // Constructors
113
114 //- Construct from pathname.
115 // Attempts to read the specified file.
116 // If that fails, try as a compressed file (.gz ending).
117 // \param pathname The file name to open for reading
118 explicit ifstreamPointer(const fileName& pathname);
119
120 //- Construct from pathname, option.
121 // Attempts to read the specified file.
122 // If that fails, try as a compressed file (.gz ending).
123 // \param pathname The file name to open for reading
124 // \param streamOpt Currently unused
126 (
127 const fileName& pathname,
128 IOstreamOption streamOpt
129 );
130
131
132 // Member Functions
133
134 //- True if compiled with libz support
135 static bool supports_gz() noexcept;
136
137
138 // Access
139
140 //- True if it holds a valid pointer
141 explicit operator bool() const noexcept { return bool(ptr_); }
142
143 //- The stream pointer (ifstream or igzstream)
144 std::istream* get() noexcept { return ptr_.get(); }
145
146 //- The stream pointer (ifstream or igzstream)
147 const std::istream* get() const noexcept { return ptr_.get(); }
148
149 //- Which compression type?
151
152
153 // Wrapped Methods
154
155 //- Attempts to open the specified file for reading.
156 // If that fails, try as a compressed file (.gz ending).
157 // \param pathname The file name to open for reading
158 // \param streamOpt Currently unused
159 void open
160 (
161 const fileName& pathname,
162 IOstreamOption streamOpt = IOstreamOption()
163 );
165
166 // Edit
167
168 //- Return managed pointer and release ownership
169 std::istream* release() noexcept { return ptr_.release(); }
170
171 //- Replace the managed pointer
172 void reset(std::istream* ptr) noexcept { ptr_.reset(ptr); }
173
175 // Operators
176
177 //- Reference to the stream (no nullptr checking)
178 std::istream& operator*() { return *ptr_; }
179
180 //- Const-reference to the stream (no nullptr checking)
181 const std::istream& operator*() const { return *ptr_; }
182
183 //- Pointer dereference
184 std::istream* operator->() noexcept { return ptr_.get(); }
185
186 //- Pointer dereference
187 const std::istream* operator->() const noexcept { return ptr_.get(); }
188};
189
190
191/*---------------------------------------------------------------------------*\
192 Class ofstreamPointer Declaration
193\*---------------------------------------------------------------------------*/
194
195class ofstreamPointer
196{
197 // Private Data Types
198
199 //- The file open/creation type (bitmask)
200 enum modeType : char
201 {
202 NONE = 0, // Regular open (truncates existing)
203 ATOMIC = 0x1, // Atomic file creation
204 APPENDING = 0x2 // Is appending to an existing file
205 };
206
207
208 // Private Data
209
210 //- The stream pointer (ofstream | ogzstream | ocountstream, ...)
211 std::unique_ptr<std::ostream> ptr_;
212
213 //- File output/creation type (atomic, append etc)
214 char mode_;
215
217 // Private Member Functions
218
219 //- Clear any output mode information
220 void clear_mode() noexcept { mode_ = modeType::NONE; }
222
223protected:
224
225 // Protected Member Functions
227 //- Reopen for compressed/non-compressed. Discards append status.
228 void reopen(const std::string& pathname);
229
230 //- Close stream and rename file
231 void close(const std::string& pathname);
232
233
234public:
235
236 // Generated Methods
237
238 //- No copy construct
240
241 //- Move construct
242 ofstreamPointer(ofstreamPointer&&) = default;
243
244 //- No copy assignment
245 void operator=(const ofstreamPointer&) = delete;
246
247 //- Move assignment
249
250 //- Destructor
251 ~ofstreamPointer() = default;
252
253
254 // Constructors
255
256 //- Default construct (empty)
258
259 //- Construct as null output stream (Foam::ocountstream)
260 explicit ofstreamPointer(std::nullptr_t);
261
262 //- Construct from pathname, option, append, file handling atomic
263 // \param pathname The file name to open for writing
264 // \param streamOpt Respects (UNCOMPRESSED | COMPRESSED)
265 // \param append Open in specified append mode
266 // \param atomic Write into temporary file (not target file).
267 // This option should only be used with a stream wrapper
268 // (eg, OFstream) that handles the final renaming.
269 //
270 // \note
271 // There are two different append modes:
272 // append at every write, or only append after opening.
273 explicit ofstreamPointer
274 (
275 const fileName& pathname,
276 IOstreamOption streamOpt = IOstreamOption(),
277 IOstreamOption::appendType append = IOstreamOption::NO_APPEND,
278 bool atomic = false
279 );
280
281 //- Construct from pathname, compression, append, file handling atomic
282 // \param pathname The file name to open for writing
283 // \param comp UNCOMPRESSED | COMPRESSED
284 // \param append Open in append mode
285 // \param atomic Write into temporary file (not target file).
286 // This option should only be used with a stream wrapper
287 // (eg, OFstream) that handles the final renaming.
289 (
290 const fileName& pathname,
291 IOstreamOption::compressionType comp,
292 IOstreamOption::appendType append = IOstreamOption::NO_APPEND,
293 bool atomic = false
294 );
295
296
297 // Static Functions
298
299 //- True if compiled with libz support
300 static bool supports_gz() noexcept;
301
303 // Access
304
305 //- True if it holds a valid pointer
306 explicit operator bool() const noexcept { return bool(ptr_); }
308 //- The stream pointer (ofstream or ogzstream)
309 std::ostream* get() noexcept { return ptr_.get(); }
310
311 //- The stream pointer (ofstream or ogzstream)
312 const std::ostream* get() const noexcept { return ptr_.get(); }
313
314 //- Which compression type?
316
317 //- True if opened in append mode \em and file already existed
318 bool is_appending() const noexcept
319 {
320 return (mode_ & modeType::APPENDING);
321 }
322
323 //- True if file creation behaves as atomic
324 bool is_atomic() const noexcept
325 {
326 return (mode_ & modeType::ATOMIC);
327 }
328
329
330 // Edit
331
332 //- Return managed pointer and release ownership.
333 std::ostream* release() noexcept
334 {
335 clear_mode();
336 return ptr_.release();
337 }
338
339 //- Replace the managed pointer
340 void reset(std::ostream* ptr) noexcept
341 {
342 clear_mode();
343 ptr_.reset(ptr);
344 }
345
346
347 // Operators
348
349 //- Reference to the stream (no nullptr checking)
350 std::ostream& operator*() { return *ptr_; }
351
352 //- Const-reference to the stream (no nullptr checking)
353 const std::ostream& operator*() const { return *ptr_; }
354
355 //- Pointer dereference
356 std::ostream* operator->() noexcept { return ptr_.get(); }
357
358 //- Pointer dereference
359 const std::ostream* operator->() const noexcept { return ptr_.get(); }
360};
361
362
363// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364
365} // End namespace Foam
366
367// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
368
369#endif
370
371// ************************************************************************* //
A simple container for options an IOstream can normally have.
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED).
A class for handling file names.
Definition fileName.H:75
std::istream * operator->() noexcept
Pointer dereference.
const std::istream * operator->() const noexcept
Pointer dereference.
static bool supports_gz() noexcept
True if compiled with libz support.
~ifstreamPointer()=default
Destructor.
void open(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Attempts to open the specified file for reading.
const std::istream * get() const noexcept
The stream pointer (ifstream or igzstream).
void reopen_gz(const std::string &pathname)
Special 'rewind' method for compressed stream.
std::istream * get() noexcept
The stream pointer (ifstream or igzstream).
IOstreamOption::compressionType whichCompression() const
Which compression type?
void reset(std::istream *ptr) noexcept
Replace the managed pointer.
const std::istream & operator*() const
Const-reference to the stream (no nullptr checking).
std::istream & operator*()
Reference to the stream (no nullptr checking).
void operator=(const ifstreamPointer &)=delete
No copy assignment.
ifstreamPointer() noexcept=default
Default construct (empty).
std::istream * release() noexcept
Return managed pointer and release ownership.
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.
static bool supports_gz() noexcept
True if compiled with libz support.
const std::ostream & operator*() const
Const-reference to the stream (no nullptr checking).
ofstreamPointer(const ofstreamPointer &)=delete
No copy construct.
void reset(std::ostream *ptr) noexcept
Replace the managed pointer.
ofstreamPointer(ofstreamPointer &&)=default
Move construct.
void operator=(const ofstreamPointer &)=delete
No copy assignment.
std::ostream * operator->() noexcept
Pointer dereference.
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream).
void reopen(const std::string &pathname)
Reopen for compressed/non-compressed. Discards append status.
const std::ostream * operator->() const noexcept
Pointer dereference.
IOstreamOption::compressionType whichCompression() const
Which compression type?
void close(const std::string &pathname)
Close stream and rename file.
ofstreamPointer() noexcept
Default construct (empty).
const std::ostream * get() const noexcept
The stream pointer (ofstream or ogzstream).
ofstreamPointer & operator=(ofstreamPointer &&)=default
Move assignment.
std::ostream & operator*()
Reference to the stream (no nullptr checking).
bool is_atomic() const noexcept
True if file creation behaves as atomic.
~ofstreamPointer()=default
Destructor.
std::ostream * release() noexcept
Return managed pointer and release ownership.
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Namespace for OpenFOAM.
const direction noexcept
Definition scalarImpl.H:265