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 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 fstreamPointer_H
57#define 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 or igzstream)
78 std::unique_ptr<std::istream> ptr_;
79
80protected:
81
82 // Protected Member Functions
83
84 //- Special 'rewind' method for compressed stream
85 void reopen_gz(const std::string& pathname_gz);
86
87public:
88
89 // Generated Methods
91 //- Default construct (empty)
92 ifstreamPointer() noexcept = default;
94 //- No copy construct
95 ifstreamPointer(const ifstreamPointer&) = delete;
97 //- Move construct
100 //- No copy assignment
101 void operator=(const ifstreamPointer&) = delete;
103 //- Move assignment
104 ifstreamPointer& operator=(ifstreamPointer&&) = default;
106 //- Destructor
107 ~ifstreamPointer() = default;
108
109
110 // Constructors
111
112 //- Construct from pathname
113 explicit ifstreamPointer(const fileName& pathname);
114
115
116 // Member Functions
117
118 //- True if compiled with libz support
119 static bool supports_gz();
120
121
122 // Access
124 //- The stream pointer (ifstream or igzstream)
125 std::istream* get() noexcept { return ptr_.get(); }
127 //- The stream pointer (ifstream or igzstream)
128 const std::istream* get() const noexcept { return ptr_.get(); }
129
130 //- Which compression type?
132
133
134 // Edit
136 //- Return managed pointer and release ownership
137 std::istream* release() noexcept { return ptr_.release(); }
139 //- Replace the managed pointer
140 void reset(std::istream* ptr) noexcept { ptr_.reset(ptr); }
141
142
143 // Operators
145 //- Reference to the stream (no nullptr checking)
146 std::istream& operator*() { return *ptr_; }
148 //- Const-reference to the stream (no nullptr checking)
149 const std::istream& operator*() const { return *ptr_; }
151 //- Pointer dereference
152 std::istream* operator->() noexcept { return ptr_.get(); }
154 //- Pointer dereference
155 const std::istream* operator->() const noexcept { return ptr_.get(); }
156};
157
158
159/*---------------------------------------------------------------------------*\
160 Class ofstreamPointer Declaration
161\*---------------------------------------------------------------------------*/
162
163class ofstreamPointer
164{
165 // Private Data
166
167 //- The stream pointer (ofstream | ogzstream | ocountstream)
168 std::unique_ptr<std::ostream> ptr_;
169
170protected:
171
172 // Protected Member Functions
173
174 //- Special 'rewind' method for compressed stream
175 void reopen_gz(const std::string& pathname_gz);
176
177 //- General 'rewind' method (non-compressed)
178 void reopen(const std::string& pathname);
179
180public:
181
182 // Generated Methods
184 //- Default construct (empty)
185 ofstreamPointer() noexcept = default;
187 //- No copy construct
188 ofstreamPointer(const ofstreamPointer&) = delete;
190 //- Move construct
191 ofstreamPointer(ofstreamPointer&&) = default;
193 //- No copy assignment
194 void operator=(const ofstreamPointer&) = delete;
196 //- Move assignment
197 ofstreamPointer& operator=(ofstreamPointer&&) = default;
199 //- Destructor
200 ~ofstreamPointer() = default;
201
202
203 // Constructors
204
205 //- Construct as null output stream using Foam::ocountstream
206 explicit ofstreamPointer(std::nullptr_t);
208 //- Construct from pathname, with specified append option
209 ofstreamPointer(const fileName& pathname, const bool append)
210 :
211 ofstreamPointer(pathname, IOstreamOption::UNCOMPRESSED, append)
212 {}
213
214 //- Construct from pathname,
215 //- with preferred compression and specified append option
216 explicit ofstreamPointer
217 (
218 const fileName& pathname,
220 const bool append = false
221 );
222
223
224 // Member Functions
225
226 //- True if compiled with libz support
227 static bool supports_gz();
228
229
230 // Access
232 //- The stream pointer (ofstream or ogzstream)
233 std::ostream* get() noexcept { return ptr_.get(); }
235 //- The stream pointer (ofstream or ogzstream)
236 const std::ostream* get() const noexcept { return ptr_.get(); }
237
238 //- Which compression type?
240
241
242 // Edit
244 //- Return managed pointer and release ownership
245 std::ostream* release() noexcept { return ptr_.release(); }
247 //- Replace the managed pointer
248 void reset(std::ostream* ptr) noexcept { ptr_.reset(ptr); }
249
250
251 // Operators
253 //- Reference to the stream (no nullptr checking)
254 std::ostream& operator*() { return *ptr_; }
256 //- Const-reference to the stream (no nullptr checking)
257 const std::ostream& operator*() const { return *ptr_; }
259 //- Pointer dereference
260 std::ostream* operator->() noexcept { return ptr_.get(); }
262 //- Pointer dereference
263 const std::ostream* operator->() const noexcept { return ptr_.get(); }
264};
265
266
267// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268
269} // End namespace Foam
270
271// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
272
273#endif
274
275// ************************************************************************* //
The IOstreamOption is a simple container for options an IOstream can normally have.
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
@ UNCOMPRESSED
compression = false
A class for handling file names.
Definition: fileName.H:76
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
std::istream * operator->() noexcept
Pointer dereference.
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
const std::istream * operator->() const noexcept
Pointer dereference.
const std::istream * get() const noexcept
The stream pointer (ifstream or igzstream)
std::istream * get() noexcept
The stream pointer (ifstream or igzstream)
IOstreamOption::compressionType whichCompression() const
Which compression type?
static bool supports_gz()
True if compiled with libz support.
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)
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::...
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
const std::ostream & operator*() const
Const-reference to the stream (no nullptr checking)
void reset(std::ostream *ptr) noexcept
Replace the managed pointer.
std::ostream * operator->() noexcept
Pointer dereference.
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream)
void reopen(const std::string &pathname)
General 'rewind' method (non-compressed)
const std::ostream * operator->() const noexcept
Pointer dereference.
IOstreamOption::compressionType whichCompression() const
Which compression type?
static bool supports_gz()
True if compiled with libz support.
const std::ostream * get() const noexcept
The stream pointer (ofstream or ogzstream)
std::ostream & operator*()
Reference to the stream (no nullptr checking)
std::ostream * release() noexcept
Return managed pointer and release ownership.
ofstreamPointer() noexcept=default
Default construct (empty)
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: Scalar.H:223