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-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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 
26 Class
27  Foam::ifstreamPointer
28 
29 Description
30  A wrapped \c std::ifstream with possible compression handling
31  (igzstream) that behaves much like a \c std::unique_ptr.
32 
33 Note
34  No <tt>operator bool</tt> to avoid inheritance ambiguity with
35  <tt>std::ios::operator bool</tt>.
36 
37 SourceFiles
38  fstreamPointers.C
39 
40 Class
41  Foam::ofstreamPointer
42 
43 Description
44  A wrapped \c std::ofstream with possible compression handling
45  (ogzstream) that behaves much like a \c std::unique_ptr.
46 
47 Note
48  No <tt>operator bool</tt> to avoid inheritance ambiguity with
49  <tt>std::ios::operator bool</tt>.
50 
51 SourceFiles
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 
66 namespace Foam
67 {
68 
69 /*---------------------------------------------------------------------------*\
70  Class ifstreamPointer Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 class ifstreamPointer
74 {
75  // Private Data
76 
77  //- The stream pointer (ifstream | igzstream, ...)
78  std::unique_ptr<std::istream> ptr_;
79 
80 
81 protected:
82 
83  // Protected Member Functions
84 
85  //- Special 'rewind' method for compressed stream
86  void reopen_gz(const std::string& pathname);
87 
88 
89 public:
90 
91  // Generated Methods
92 
93  //- Default construct (empty)
94  ifstreamPointer() noexcept = default;
95 
96  //- No copy construct
97  ifstreamPointer(const ifstreamPointer&) = delete;
98 
99  //- Move construct
100  ifstreamPointer(ifstreamPointer&&) = default;
101 
102  //- No copy assignment
103  void operator=(const ifstreamPointer&) = delete;
104 
105  //- Move assignment
107 
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();
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 
195 class ofstreamPointer
196 {
197  // Private Data
198 
199  //- The stream pointer (ofstream | ogzstream | ocountstream, ...)
200  std::unique_ptr<std::ostream> ptr_;
201 
202  //- Atomic file creation
203  bool atomic_;
204 
205 
206 protected:
207 
208  // Protected Member Functions
209 
210  //- Reopen for compressed/non-compressed
211  void reopen(const std::string& pathname);
212 
213  //- Close stream and rename file
214  void close(const std::string& pathname);
215 
217 public:
218 
219  // Generated Methods
220 
221  //- No copy construct
222  ofstreamPointer(const ofstreamPointer&) = delete;
223 
224  //- Move construct
225  ofstreamPointer(ofstreamPointer&&) = default;
227  //- No copy assignment
228  void operator=(const ofstreamPointer&) = delete;
229 
230  //- Move assignment
232 
233  //- Destructor
234  ~ofstreamPointer() = default;
235 
236 
237  // Constructors
238 
239  //- Default construct (empty)
241 
242  //- Construct as null output stream (Foam::ocountstream)
243  explicit ofstreamPointer(std::nullptr_t);
244 
245  //- Construct from pathname, option, append, file handling atomic
246  // \param pathname The file name to open for writing
247  // \param streamOpt Respects (UNCOMPRESSED | COMPRESSED)
248  // \param append Open in append mode
249  // \param atomic Write into temporary file (not target file).
250  // This option should only be used with a stream wrapper
251  // (eg, OFstream) that handles the final renaming.
252  explicit ofstreamPointer
253  (
254  const fileName& pathname,
255  IOstreamOption streamOpt = IOstreamOption(),
256  const bool append = false,
257  const bool atomic = false
258  );
259 
260  //- Construct from pathname, compression, append, file handling atomic
261  // \param pathname The file name to open for writing
262  // \param comp UNCOMPRESSED | COMPRESSED
263  // \param append Open in append mode
264  // \param atomic Write into temporary file (not target file).
265  // This option should only be used with a stream wrapper
266  // (eg, OFstream) that handles the final renaming.
268  (
269  const fileName& pathname,
270  IOstreamOption::compressionType comp,
271  const bool append = false,
272  const bool atomic = false
273  );
274 
275 
276  // Member Functions
277 
278  //- True if compiled with libz support
279  static bool supports_gz();
280 
281 
282  // Access
283 
284  //- True if it holds a valid pointer
285  explicit operator bool() const noexcept { return bool(ptr_); }
286 
287  //- The stream pointer (ofstream or ogzstream)
288  std::ostream* get() noexcept { return ptr_.get(); }
289 
290  //- The stream pointer (ofstream or ogzstream)
291  const std::ostream* get() const noexcept { return ptr_.get(); }
292 
293  //- Which compression type?
295 
296 
297  // Edit
298 
299  //- Return managed pointer and release ownership
300  std::ostream* release() noexcept { return ptr_.release(); }
301 
302  //- Replace the managed pointer
303  void reset(std::ostream* ptr) noexcept { ptr_.reset(ptr); }
304 
305 
306  // Operators
307 
308  //- Reference to the stream (no nullptr checking)
309  std::ostream& operator*() { return *ptr_; }
310 
311  //- Const-reference to the stream (no nullptr checking)
312  const std::ostream& operator*() const { return *ptr_; }
313 
314  //- Pointer dereference
315  std::ostream* operator->() noexcept { return ptr_.get(); }
316 
317  //- Pointer dereference
318  const std::ostream* operator->() const noexcept { return ptr_.get(); }
319 };
320 
321 
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 
324 } // End namespace Foam
325 
326 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
327 
328 #endif
329 
330 // ************************************************************************* //
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
std::istream * operator->() noexcept
Pointer dereference.
A wrapped std::ofstream with possible compression handling (ogzstream) that behaves much like a std::...
A class for handling file names.
Definition: fileName.H:72
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
IOstreamOption::compressionType whichCompression() const
Which compression type?
static bool supports_gz()
True if compiled with libz support.
ofstreamPointer() noexcept
Default construct (empty)
static bool supports_gz()
True if compiled with libz support.
void reopen_gz(const std::string &pathname)
Special &#39;rewind&#39; method for compressed stream.
A simple container for options an IOstream can normally have.
std::ostream * operator->() noexcept
Pointer dereference.
void reset(std::istream *ptr) noexcept
Replace the managed pointer.
std::istream * release() noexcept
Return managed pointer and release ownership.
std::ostream & operator*()
Reference to the stream (no nullptr checking)
~ifstreamPointer()=default
Destructor.
std::ostream * release() noexcept
Return managed pointer and release ownership.
void reopen(const std::string &pathname)
Reopen for compressed/non-compressed.
const direction noexcept
Definition: Scalar.H:258
void operator=(const ofstreamPointer &)=delete
No copy assignment.
ifstreamPointer() noexcept=default
Default construct (empty)
void close(const std::string &pathname)
Close stream and rename file.
std::istream & operator*()
Reference to the stream (no nullptr checking)
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
void operator=(const ifstreamPointer &)=delete
No copy assignment.
IOstreamOption::compressionType whichCompression() const
Which compression type?
void open(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Attempts to open the specified file for reading.
~ofstreamPointer()=default
Destructor.
void reset(std::ostream *ptr) noexcept
Replace the managed pointer.
Namespace for OpenFOAM.