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 protected:
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 
180 public:
181 
182  // Generated Methods
183 
184  //- Default construct (empty)
185  ofstreamPointer() noexcept = default;
186 
187  //- No copy construct
188  ofstreamPointer(const ofstreamPointer&) = delete;
189 
190  //- Move construct
191  ofstreamPointer(ofstreamPointer&&) = default;
192 
193  //- No copy assignment
194  void operator=(const ofstreamPointer&) = delete;
195 
196  //- Move assignment
198 
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);
207 
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
231 
232  //- The stream pointer (ofstream or ogzstream)
233  std::ostream* get() noexcept { return ptr_.get(); }
234 
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
243 
244  //- Return managed pointer and release ownership
245  std::ostream* release() noexcept { return ptr_.release(); }
246 
247  //- Replace the managed pointer
248  void reset(std::ostream* ptr) noexcept { ptr_.reset(ptr); }
249 
250 
251  // Operators
252 
253  //- Reference to the stream (no nullptr checking)
254  std::ostream& operator*() { return *ptr_; }
255 
256  //- Const-reference to the stream (no nullptr checking)
257  const std::ostream& operator*() const { return *ptr_; }
258 
259  //- Pointer dereference
260  std::ostream* operator->() noexcept { return ptr_.get(); }
261 
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 // ************************************************************************* //
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:258
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:234
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::ofstreamPointer::operator*
std::ostream & operator*()
Reference to the stream (no nullptr checking)
Definition: fstreamPointer.H:252
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:246
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:261
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:243
Foam::ofstreamPointer::get
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream)
Definition: fstreamPointer.H:231
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
Foam::ofstreamPointer::reopen
void reopen(const std::string &pathname)
General 'rewind' method (non-compressed)
Definition: fstreamPointers.C:226
IOstreamOption.H
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
fileName.H
Foam::ofstreamPointer::reopen_gz
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
Definition: fstreamPointers.C:210
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:243
Foam::ofstreamPointer::ofstreamPointer
ofstreamPointer(const fileName &pathname, const bool append)
Construct from pathname, with specified append option.
Definition: fstreamPointer.H:207
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:255
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:257