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 -------------------------------------------------------------------------------
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 InClass
27  Foam::StringStream
28 
29 Description
30  Input/output from string buffers.
31 
32 SourceFiles
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 
46 namespace Foam
47 {
48 
49 namespace Detail
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class Detail::StringStreamAllocator Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 //- Allocator for variants of a std stringstream
57 template<class StreamType>
59 {
60 protected:
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
74  StringStreamAllocator() = default;
75 
76  //- Copy construct from string
77  StringStreamAllocator(const std::string& s)
78  :
79  stream_(s)
80  {}
81 
82 
83 public:
84 
85  // Member Functions
86 
87  //- Get the string - as Foam::string rather than std::string
88  Foam::string str() const
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 
115 public:
116 
117  // Constructors
118 
119  //- Default construct or with specified stream option
120  explicit IStringStream
121  (
122  IOstreamOption streamOpt = IOstreamOption()
123  )
124  :
125  allocator_type(),
126  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
127  {}
128 
129  //- Construct from std::string
130  explicit IStringStream
131  (
132  const std::string& s,
133  IOstreamOption streamOpt = IOstreamOption()
134  )
135  :
136  allocator_type(s),
137  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
138  {}
139 
140  //- Construct from char*
141  explicit IStringStream
142  (
143  const char* s,
144  IOstreamOption streamOpt = IOstreamOption()
145  )
146  :
147  allocator_type(s),
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  :
193  IStringStream(IOstreamOption(fmt, ver))
194  {}
195 
196  //- Construct from std::string
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 
234 public:
235 
236  // Constructors
237 
238  //- Default construct or with specified stream option
239  explicit OStringStream
240  (
241  IOstreamOption streamOpt = IOstreamOption()
242  )
243  :
244  allocator_type(),
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  :
286  OStringStream(IOstreamOption(fmt, ver))
287  {}
288 
289  #endif /* Foam_IOstream_extras */
290 };
291 
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 } // End namespace Foam
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 
299 #endif
300 
301 // ************************************************************************* //
Foam::OStringStream::OStringStream
OStringStream(const OStringStream &str)
Copy construct, copies content and format.
Definition: StringStream.H:249
Foam::OStringStream::OStringStream
OStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:240
Foam::IStringStream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: StringStream.C:32
Foam::Detail::StringStreamAllocator::str
void str(const std::string &s)
Set the string.
Definition: StringStream.H:94
Foam::Detail::StringStreamAllocator::StringStreamAllocator
StringStreamAllocator()=default
Default construct.
s
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))
Definition: gmvOutputSpray.H:25
Foam::IStringStream::IStringStream
IStringStream(const IStringStream &str)
Copy construct, copies content and format.
Definition: StringStream.H:152
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
Foam::OStringStream::rewind
virtual void rewind()
Rewind the output stream.
Definition: StringStream.H:266
ISstream.H
Foam::OStringStream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: StringStream.C:43
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::Detail::StringStreamAllocator::StringStreamAllocator
StringStreamAllocator(const std::string &s)
Copy construct from string.
Definition: StringStream.H:77
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::Detail::StringStreamAllocator::stream_type
StreamType stream_type
The stream type.
Definition: StringStream.H:65
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:1064
Foam::Detail::StringStreamAllocator
Allocator for variants of a std stringstream.
Definition: StringStream.H:58
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::OSstream::name
virtual const fileName & name() const
Get the name of the stream.
Definition: OSstream.H:107
Foam::IStringStream::operator()
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: StringStream.H:176
Foam::OSstream::OSstream
OSstream(const OSstream &)=default
Copy construct.
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IStringStream::IStringStream
IStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:121
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::IStringStream::reset
virtual void reset(const std::string &s)
Reset the input buffer and rewind the stream.
Definition: StringStream.H:162
Foam::ISstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:113
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:338
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
os
OBJstream os(runTime.globalPath()/outputName)
Foam::ISstream::ISstream
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition: ISstreamI.H:32
Foam::IStringStream
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:108
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Detail::StringStreamAllocator::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:88
Foam::OStringStream
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:227
Foam::OStringStream::reset
void reset()
Reset the output buffer and rewind the stream.
Definition: StringStream.H:259
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Detail::StringStreamAllocator::stream_
stream_type stream_
The input/output stream.
Definition: StringStream.H:68
OSstream.H