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-2022 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 or 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  explicit ifstreamPointer(const fileName& pathname);
118 
119 
120  // Member Functions
121 
122  //- True if compiled with libz support
123  static bool supports_gz();
124 
125 
126  // Access
127 
128  //- The stream pointer (ifstream or igzstream)
129  std::istream* get() noexcept { return ptr_.get(); }
130 
131  //- The stream pointer (ifstream or igzstream)
132  const std::istream* get() const noexcept { return ptr_.get(); }
133 
134  //- Which compression type?
136 
137 
138  // Edit
139 
140  //- Return managed pointer and release ownership
141  std::istream* release() noexcept { return ptr_.release(); }
142 
143  //- Replace the managed pointer
144  void reset(std::istream* ptr) noexcept { ptr_.reset(ptr); }
145 
146 
147  // Operators
148 
149  //- Reference to the stream (no nullptr checking)
150  std::istream& operator*() { return *ptr_; }
151 
152  //- Const-reference to the stream (no nullptr checking)
153  const std::istream& operator*() const { return *ptr_; }
155  //- Pointer dereference
156  std::istream* operator->() noexcept { return ptr_.get(); }
157 
158  //- Pointer dereference
159  const std::istream* operator->() const noexcept { return ptr_.get(); }
160 };
161 
162 
163 /*---------------------------------------------------------------------------*\
164  Class ofstreamPointer Declaration
165 \*---------------------------------------------------------------------------*/
166 
168 {
169  // Private Data
170 
171  //- The stream pointer (ofstream | ogzstream | ocountstream)
172  std::unique_ptr<std::ostream> ptr_;
173 
174  //- Atomic file creation
175  bool atomic_;
176 
177 
178 protected:
179 
180  // Protected Member Functions
181 
182  //- Reopen for compressed/non-compressed
183  void reopen(const std::string& pathname);
184 
185  //- Close stream and rename file
186  void close(const std::string& pathname);
187 
188 
189 public:
191  // Generated Methods
192 
193  //- No copy construct
194  ofstreamPointer(const ofstreamPointer&) = delete;
196  //- Move construct
197  ofstreamPointer(ofstreamPointer&&) = default;
198 
199  //- No copy assignment
200  void operator=(const ofstreamPointer&) = delete;
201 
202  //- Move assignment
204 
205  //- Destructor
206  ~ofstreamPointer() = default;
207 
208 
209  // Constructors
210 
211  //- Default construct (empty)
213 
214  //- Construct as null output stream using Foam::ocountstream
215  explicit ofstreamPointer(std::nullptr_t);
216 
217  //- Construct from pathname, compression, append, file handling atomic
218  // \param pathname The file name to open for writing
219  // \param comp UNCOMPRESSED | COMPRESSED
220  // \param append Open in append mode
221  // \param atomic Write into temporary file (not target file).
222  // This option should only be used with a stream wrapper
223  // (eg, OFstream) that handles the final renaming.
224  explicit ofstreamPointer
225  (
226  const fileName& pathname,
227  IOstreamOption::compressionType comp = IOstreamOption::UNCOMPRESSED,
228  const bool append = false,
229  const bool atomic = false
230  );
231 
232 
233  // Member Functions
234 
235  //- True if compiled with libz support
236  static bool supports_gz();
237 
238 
239  // Access
240 
241  //- The stream pointer (ofstream or ogzstream)
242  std::ostream* get() noexcept { return ptr_.get(); }
243 
244  //- The stream pointer (ofstream or ogzstream)
245  const std::ostream* get() const noexcept { return ptr_.get(); }
246 
247  //- Which compression type?
249 
250 
251  // Edit
252 
253  //- Return managed pointer and release ownership
254  std::ostream* release() noexcept { return ptr_.release(); }
255 
256  //- Replace the managed pointer
257  void reset(std::ostream* ptr) noexcept { ptr_.reset(ptr); }
258 
259 
260  // Operators
261 
262  //- Reference to the stream (no nullptr checking)
263  std::ostream& operator*() { return *ptr_; }
264 
265  //- Const-reference to the stream (no nullptr checking)
266  const std::ostream& operator*() const { return *ptr_; }
267 
268  //- Pointer dereference
269  std::ostream* operator->() noexcept { return ptr_.get(); }
270 
271  //- Pointer dereference
272  const std::ostream* operator->() const noexcept { return ptr_.get(); }
273 };
274 
275 
276 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
277 
278 } // End namespace Foam
279 
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 
282 #endif
283 
284 // ************************************************************************* //
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:71
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream)
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?
~ofstreamPointer()=default
Destructor.
void reset(std::ostream *ptr) noexcept
Replace the managed pointer.
Namespace for OpenFOAM.