StringStream.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) 2017-2021 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
26InClass
27 Foam::StringStream
28
29Description
30 Input/output from string buffers.
31
32SourceFiles
33 StringStream.C
34
35\*---------------------------------------------------------------------------*/
36
37#ifndef StringStream_H
38#define StringStream_H
39
40#include "ISstream.H"
41#include "OSstream.H"
42#include <sstream>
43
44// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45
46namespace Foam
47{
48
49namespace Detail
50{
51
52/*---------------------------------------------------------------------------*\
53 Class Detail::StringStreamAllocator Declaration
54\*---------------------------------------------------------------------------*/
55
56//- Allocator for variants of a std stringstream
57template<class StreamType>
59{
60protected:
61
62 // Protected Member Data
63
64 //- The stream type
65 typedef StreamType stream_type;
66
67 //- The input/output stream.
69
70
71 // Constructors
72
73 //- Default construct
75
76 //- Copy construct from string
77 StringStreamAllocator(const std::string& s)
78 :
79 stream_(s)
80 {}
81
82
83public:
84
85 // Member Functions
86
87 //- Get the string - as Foam::string rather than std::string
89 {
90 return Foam::string(stream_.str());
91 }
92
93 //- Set the string
94 void str(const std::string& s)
95 {
96 stream_.str(s);
97 }
98};
99
100} // End namespace Detail
101
102
103/*---------------------------------------------------------------------------*\
104 Class IStringStream Declaration
105\*---------------------------------------------------------------------------*/
106
107//- Input from string buffer, using a ISstream. Always UNCOMPRESSED.
109:
110 public Detail::StringStreamAllocator<std::istringstream>,
111 public ISstream
112{
114
115public:
116
117 // Constructors
118
119 //- Default construct or with specified stream option
121 (
122 IOstreamOption streamOpt = IOstreamOption()
123 )
124 :
126 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
127 {}
128
129 //- Construct from std::string
131 (
132 const std::string& s,
133 IOstreamOption streamOpt = IOstreamOption()
134 )
135 :
137 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
138 {}
139
140 //- Construct from char*
142 (
143 const char* s,
144 IOstreamOption streamOpt = IOstreamOption()
145 )
146 :
148 ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
149 {}
150
151 //- Copy construct, copies content and format
153 :
156 {}
157
158
159 // Member Functions
160
161 //- Reset the input buffer and rewind the stream
162 virtual void reset(const std::string& s)
163 {
164 this->str(s);
165 this->rewind();
166 }
167
168 //- Print stream description to Ostream
169 virtual void print(Ostream& os) const;
170
171
172 // Member Operators
173
174 //- Return a non-const reference to const Istream
175 // Needed for read-constructors where the stream argument is temporary.
177 {
178 // Could also rewind
179 return const_cast<IStringStream&>(*this);
180 }
181
182
183 // Additional constructors and methods (as per v2012 and earlier)
184 #ifdef Foam_IOstream_extras
185
186 //- Default construct
187 explicit IStringStream
188 (
191 )
192 :
194 {}
195
196 //- Construct from std::string
197 IStringStream
198 (
199 const std::string& s,
201 IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
202 )
203 :
204 IStringStream(s, IOstreamOption(fmt, ver))
205 {}
206
207 //- Construct from char*
209 (
210 const char* s,
212 IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
213 )
214 :
215 IStringStream(s, IOstreamOption(fmt, ver))
216 {}
217
218 #endif /* Foam_IOstream_extras */
219};
220
221
222/*---------------------------------------------------------------------------*\
223 Class OStringStream Declaration
224\*---------------------------------------------------------------------------*/
225
226//- Output to string buffer, using a OSstream. Always UNCOMPRESSED.
228:
229 public Detail::StringStreamAllocator<std::ostringstream>,
230 public OSstream
231{
233
234public:
235
236 // Constructors
237
238 //- Default construct or with specified stream option
240 (
241 IOstreamOption streamOpt = IOstreamOption()
242 )
243 :
245 OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
246 {}
247
248 //- Copy construct, copies content and format
250 :
253 {}
254
255
256 // Member Functions
257
258 //- Reset the output buffer and rewind the stream
259 void reset()
260 {
261 this->str(""); // No other way to reset the end
262 this->rewind();
263 }
264
265 //- Rewind the output stream
266 virtual void rewind()
267 {
268 // pubseekpos() instead of seekp() for symmetry with other classes
269 stream_.rdbuf()->pubseekpos(0, std::ios_base::out);
270 }
271
272 //- Print stream description to Ostream
273 virtual void print(Ostream& os) const;
274
275
276 // Older style, without stream option (including 2012 release)
277 #ifdef Foam_IOstream_extras
278
279 //- Default construct
280 explicit OStringStream
281 (
284 )
285 :
287 {}
288
289 #endif /* Foam_IOstream_extras */
290};
291
292
293// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294
295} // End namespace Foam
296
297// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298
299#endif
300
301// ************************************************************************* //
Allocator for variants of a std stringstream.
Definition: StringStream.H:59
StreamType stream_type
The stream type.
Definition: StringStream.H:65
StringStreamAllocator()=default
Default construct.
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:88
StringStreamAllocator(const std::string &s)
Copy construct from string.
Definition: StringStream.H:77
void str(const std::string &s)
Set the string.
Definition: StringStream.H:94
stream_type stream_
The input/output stream.
Definition: StringStream.H:68
Representation of a major/minor version number.
The IOstreamOption is a simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
streamFormat format() const noexcept
Get the current stream format.
streamFormat
Data format (ascii | binary)
static const versionNumber currentVersion
The current version number (2.0)
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:113
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:1064
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:112
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: StringStream.H:176
IStringStream(const std::string &s, IOstreamOption streamOpt=IOstreamOption())
Construct from std::string.
Definition: StringStream.H:131
virtual void reset(const std::string &s)
Reset the input buffer and rewind the stream.
Definition: StringStream.H:162
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: StringStream.C:32
IStringStream(const char *s, IOstreamOption streamOpt=IOstreamOption())
Construct from char*.
Definition: StringStream.H:142
IStringStream(const IStringStream &str)
Copy construct, copies content and format.
Definition: StringStream.H:152
IStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:121
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:57
virtual const fileName & name() const
Get the name of the stream.
Definition: OSstream.H:107
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:231
OStringStream(const OStringStream &str)
Copy construct, copies content and format.
Definition: StringStream.H:249
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: StringStream.C:43
OStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:240
void reset()
Reset the output buffer and rewind the stream.
Definition: StringStream.H:259
virtual void rewind()
Rewind the output stream.
Definition: StringStream.H:266
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
sha1buf * rdbuf()
This hides both signatures of std::basic_ios::rdbuf()
Definition: OSHA1stream.H:111
A class for handling character strings derived from std::string.
Definition: string.H:79
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.