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-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 InClass
27  Foam::StringStream
28 
29 Description
30  Input/output from string buffers.
31 
32 SourceFiles
33  StringStream.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Foam_StringStream_H
38 #define Foam_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.
108 class IStringStream
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  :
148  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
149  {}
150 
151  //- Copy construct, copies content and format
153  :
154  allocator_type(str.str()),
155  ISstream(stream_, str.name(), static_cast<IOstreamOption>(str))
156  {}
157 
158 
159  // Member Functions
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 override;
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.
176  Istream& operator()() const
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  //- Construct empty with given format
188  :
190  {}
191 
192  //- Construct from std::string with given format
194  (
195  const std::string& s,
197  )
198  :
200  {}
201 
202  //- Construct from char* with given format
204  (
205  const char* s,
207  )
208  :
210  {}
211 
212  #endif /* Foam_IOstream_extras */
213 };
214 
215 
216 /*---------------------------------------------------------------------------*\
217  Class OStringStream Declaration
218 \*---------------------------------------------------------------------------*/
219 
220 //- Output to string buffer, using a OSstream. Always UNCOMPRESSED.
221 class OStringStream
222 :
223  public Detail::StringStreamAllocator<std::ostringstream>,
224  public OSstream
225 {
226  typedef Detail::StringStreamAllocator<std::ostringstream> allocator_type;
227 
228 public:
229 
230  // Constructors
231 
232  //- Default construct or with specified stream option
233  explicit OStringStream
234  (
235  IOstreamOption streamOpt = IOstreamOption()
236  )
237  :
238  allocator_type(),
239  OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
240  {}
241 
242  //- Copy construct, copies content and format
244  :
245  allocator_type(str.str()),
246  OSstream(stream_, str.name(), static_cast<IOstreamOption>(str))
247  {}
248 
249 
250  // Member Functions
251 
252  //- Reset the output buffer and rewind the stream
253  void reset()
254  {
255  this->str(""); // No other way to reset the end
256  this->rewind();
257  }
258 
259  //- Rewind the output stream
260  virtual void rewind()
261  {
262  stream_.rdbuf()->pubseekpos(0, std::ios_base::out);
263  }
264 
265  //- Print stream description to Ostream
266  virtual void print(Ostream& os) const override;
267 
268 
269  // Older style, without stream option (including 2012 release)
270  #ifdef Foam_IOstream_extras
272  //- Construct empty with given format
274  :
276  {}
277 
278  #endif /* Foam_IOstream_extras */
279 };
280 
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 } // End namespace Foam
285 
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287 
288 #endif
289 
290 // ************************************************************************* //
void reset()
Reset the output buffer and rewind the stream.
Definition: StringStream.H:294
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: ISstream.C:1078
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:96
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: StringStream.H:203
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A simple container for options an IOstream can normally have.
virtual const fileName & name() const override
Get the name of the output serial stream. (eg, the name of the Fstream file name) ...
Definition: OSstream.H:128
virtual void rewind()
Rewind the output stream.
Definition: StringStream.H:303
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
virtual void reset(const std::string &s)
Reset the input buffer and rewind the stream.
Definition: StringStream.H:184
Allocator for variants of a std stringstream.
Definition: StringStream.H:56
StreamType stream_type
The stream type.
Definition: StringStream.H:65
OStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:271
virtual const fileName & name() const override
The name of the input serial stream. (eg, the name of the Fstream file name)
Definition: ISstream.H:141
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: StringStream.C:36
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
stream_type stream_
The input/output stream.
Definition: StringStream.H:70
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:120
versionNumber version() const noexcept
Get the stream version.
streamFormat
Data format (ascii | binary)
IStringStream(IOstreamOption streamOpt=IOstreamOption())
Default construct or with specified stream option.
Definition: StringStream.H:135
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: StringStream.C:25
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))
A class for handling character strings derived from std::string.
Definition: string.H:72
OSstream(const OSstream &)=default
Copy construct.
Ostream(const Ostream &)=default
Copy construct.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
streamFormat format() const noexcept
Get the current stream format.
StringStreamAllocator()=default
Default construct.
Namespace for OpenFOAM.