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 -------------------------------------------------------------------------------
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 fstreamPointer_H
57 #define 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 protected:
81 
82  // Protected Member Functions
83 
84  //- Special 'rewind' method for compressed stream
85  void reopen_gz(const std::string& pathname_gz);
86 
87 public:
88 
89  // Generated Methods
90 
91  //- Default construct (empty)
92  ifstreamPointer() noexcept = default;
93 
94  //- No copy construct
95  ifstreamPointer(const ifstreamPointer&) = delete;
96 
97  //- Move construct
98  ifstreamPointer(ifstreamPointer&&) = default;
99 
100  //- No copy assignment
101  void operator=(const ifstreamPointer&) = delete;
102 
103  //- Move assignment
105 
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
123 
124  //- The stream pointer (ifstream or igzstream)
125  std::istream* get() noexcept { return ptr_.get(); }
126 
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
135 
136  //- Return managed pointer and release ownership
137  std::istream* release() noexcept { return ptr_.release(); }
138 
139  //- Replace the managed pointer
140  void reset(std::istream* ptr) noexcept { ptr_.reset(ptr); }
141 
142 
143  // Operators
144 
145  //- Reference to the stream (no nullptr checking)
146  std::istream& operator*() { return *ptr_; }
147 
148  //- Const-reference to the stream (no nullptr checking)
149  const std::istream& operator*() const { return *ptr_; }
150 
151  //- Pointer dereference
152  std::istream* operator->() noexcept { return ptr_.get(); }
153 
154  //- Pointer dereference
155  const std::istream* operator->() const noexcept { return ptr_.get(); }
156 };
157 
158 
159 /*---------------------------------------------------------------------------*\
160  Class ofstreamPointer Declaration
161 \*---------------------------------------------------------------------------*/
162 
163 class ofstreamPointer
164 {
165  // Private Data
166 
167  //- The stream pointer (ofstream | ogzstream | ocountstream)
168  std::unique_ptr<std::ostream> ptr_;
169 
170 public:
171 
172  // Generated Methods
173 
174  //- Default construct (empty)
175  ofstreamPointer() noexcept = default;
176 
177  //- No copy construct
178  ofstreamPointer(const ofstreamPointer&) = delete;
179 
180  //- Move construct
181  ofstreamPointer(ofstreamPointer&&) = default;
182 
183  //- No copy assignment
184  void operator=(const ofstreamPointer&) = delete;
185 
186  //- Move assignment
188 
189  //- Destructor
190  ~ofstreamPointer() = default;
191 
192 
193  // Constructors
194 
195  //- Construct as null output stream using Foam::ocountstream
196  explicit ofstreamPointer(std::nullptr_t);
197 
198  //- Construct from pathname, with specified append option
199  ofstreamPointer(const fileName& pathname, const bool append)
200  :
201  ofstreamPointer(pathname, IOstreamOption::UNCOMPRESSED, append)
202  {}
203 
204  //- Construct from pathname,
205  //- with preferred compression and specified append option
206  explicit ofstreamPointer
207  (
208  const fileName& pathname,
210  const bool append = false
211  );
212 
213 
214  // Member Functions
215 
216  //- True if compiled with libz support
217  static bool supports_gz();
218 
219 
220  // Access
221 
222  //- The stream pointer (ofstream or ogzstream)
223  std::ostream* get() noexcept { return ptr_.get(); }
224 
225  //- The stream pointer (ofstream or ogzstream)
226  const std::ostream* get() const noexcept { return ptr_.get(); }
227 
228  //- Which compression type?
230 
231 
232  // Edit
233 
234  //- Return managed pointer and release ownership
235  std::ostream* release() noexcept { return ptr_.release(); }
236 
237  //- Replace the managed pointer
238  void reset(std::ostream* ptr) noexcept { ptr_.reset(ptr); }
239 
240 
241  // Operators
242 
243  //- Reference to the stream (no nullptr checking)
244  std::ostream& operator*() { return *ptr_; }
245 
246  //- Const-reference to the stream (no nullptr checking)
247  const std::ostream& operator*() const { return *ptr_; }
248 
249  //- Pointer dereference
250  std::ostream* operator->() noexcept { return ptr_.get(); }
251 
252  //- Pointer dereference
253  const std::ostream* operator->() const noexcept { return ptr_.get(); }
254 };
255 
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 
259 } // End namespace Foam
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 #endif
264 
265 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:79
Foam::ifstreamPointer::supports_gz
static bool supports_gz()
True if compiled with libz support.
Definition: fstreamPointers.C:73
Foam::ofstreamPointer::operator->
std::ostream * operator->() noexcept
Pointer dereference.
Definition: fstreamPointer.H:248
Foam::ofstreamPointer::supports_gz
static bool supports_gz()
True if compiled with libz support.
Definition: fstreamPointers.C:83
Foam::ofstreamPointer::get
const std::ostream * get() const noexcept
The stream pointer (ofstream or ogzstream)
Definition: fstreamPointer.H:224
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::ofstreamPointer::operator*
std::ostream & operator*()
Reference to the stream (no nullptr checking)
Definition: fstreamPointer.H:242
Foam::ofstreamPointer::~ofstreamPointer
~ofstreamPointer()=default
Destructor.
Foam::ifstreamPointer::operator*
const std::istream & operator*() const
Const-reference to the stream (no nullptr checking)
Definition: fstreamPointer.H:147
Foam::ofstreamPointer::reset
void reset(std::ostream *ptr) noexcept
Replace the managed pointer.
Definition: fstreamPointer.H:236
Foam::ifstreamPointer::reset
void reset(std::istream *ptr) noexcept
Replace the managed pointer.
Definition: fstreamPointer.H:138
Foam::ifstreamPointer::get
const std::istream * get() const noexcept
The stream pointer (ifstream or igzstream)
Definition: fstreamPointer.H:126
Foam::ifstreamPointer::release
std::istream * release() noexcept
Return managed pointer and release ownership.
Definition: fstreamPointer.H:135
Foam::ofstreamPointer::operator->
const std::ostream * operator->() const noexcept
Pointer dereference.
Definition: fstreamPointer.H:251
Foam::ifstreamPointer::~ifstreamPointer
~ifstreamPointer()=default
Destructor.
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::ifstreamPointer::whichCompression
IOstreamOption::compressionType whichCompression() const
Which compression type?
Definition: fstreamPointers.C:211
Foam::ofstreamPointer::get
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream)
Definition: fstreamPointer.H:221
Foam::ifstreamPointer::reopen_gz
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
Definition: fstreamPointers.C:194
Foam::ifstreamPointer::operator*
std::istream & operator*()
Reference to the stream (no nullptr checking)
Definition: fstreamPointer.H:144
IOstreamOption.H
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
fileName.H
Foam::ifstreamPointer::get
std::istream * get() noexcept
The stream pointer (ifstream or igzstream)
Definition: fstreamPointer.H:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::ofstreamPointer::ofstreamPointer
ofstreamPointer() noexcept=default
Default construct (empty)
Foam::ofstreamPointer::release
std::ostream * release() noexcept
Return managed pointer and release ownership.
Definition: fstreamPointer.H:233
Foam::ofstreamPointer::ofstreamPointer
ofstreamPointer(const fileName &pathname, const bool append)
Construct from pathname, with specified append option.
Definition: fstreamPointer.H:197
Foam::ifstreamPointer
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
Definition: fstreamPointer.H:71
Foam::ifstreamPointer::operator->
std::istream * operator->() noexcept
Pointer dereference.
Definition: fstreamPointer.H:150
Foam::ifstreamPointer::ifstreamPointer
ifstreamPointer() noexcept=default
Default construct (empty)
Foam::ofstreamPointer
A wrapped std::ofstream with possible compression handling (ogzstream) that behaves much like a std::...
Definition: fstreamPointer.H:161
Foam::ofstreamPointer::operator*
const std::ostream & operator*() const
Const-reference to the stream (no nullptr checking)
Definition: fstreamPointer.H:245
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::ofstreamPointer::operator=
void operator=(const ofstreamPointer &)=delete
No copy assignment.
Foam::ifstreamPointer::operator->
const std::istream * operator->() const noexcept
Pointer dereference.
Definition: fstreamPointer.H:153
Foam::ifstreamPointer::operator=
void operator=(const ifstreamPointer &)=delete
No copy assignment.
Foam::ofstreamPointer::whichCompression
IOstreamOption::compressionType whichCompression() const
Which compression type?
Definition: fstreamPointers.C:225