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-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 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
75  :
76  stream_()
77  {}
78 
79  //- Copy construct from string
80  StringStreamAllocator(const std::string& s)
81  :
82  stream_(s)
83  {}
84 
85 
86 public:
87 
88  // Member Functions
89 
90  //- Get the string - as Foam::string rather than std::string
91  Foam::string str() const
92  {
93  return Foam::string(stream_.str());
94  }
95 
96  //- Set the string
97  void str(const std::string& s)
98  {
99  stream_.str(s);
100  }
101 };
102 
103 } // End namespace Detail
104 
105 
106 /*---------------------------------------------------------------------------*\
107  Class IStringStream Declaration
108 \*---------------------------------------------------------------------------*/
109 
110 //- Input from string buffer, using a ISstream
112 :
113  public Detail::StringStreamAllocator<std::istringstream>,
114  public ISstream
115 {
117 
118 public:
119 
120  // Constructors
121 
122  //- Default construct
123  explicit IStringStream
124  (
127  )
128  :
129  allocator_type(),
130  ISstream(stream_, "input", format, version)
131  {}
132 
133  //- Construct from std::string
134  explicit IStringStream
135  (
136  const std::string& s,
139  const Foam::string& name="input"
140  )
141  :
142  allocator_type(s),
144  {}
145 
146  //- Construct from char*
147  explicit IStringStream
148  (
149  const char* s,
152  const Foam::string& name="input"
153  )
154  :
155  allocator_type(s),
157  {}
158 
159  //- Copy construct, copies content and format
161  :
164  {}
165 
166 
167  // Member Functions
168 
169  //- Reset the input buffer and rewind the stream
170  virtual void reset(const std::string& s)
171  {
172  this->str(s);
173  this->rewind();
174  }
175 
176  //- Print stream description to Ostream
177  virtual void print(Ostream& os) const;
178 
179 
180  // Member Operators
181 
182  //- Return a non-const reference to const Istream
183  // Needed for read-constructors where the stream argument is temporary.
185  {
186  return const_cast<IStringStream&>(*this);
187  }
188 };
189 
190 
191 /*---------------------------------------------------------------------------*\
192  Class OStringStream Declaration
193 \*---------------------------------------------------------------------------*/
194 
195 //- Output to string buffer, using a OSstream
197 :
198  public Detail::StringStreamAllocator<std::ostringstream>,
199  public OSstream
200 {
202 
203 public:
204 
205  // Constructors
206 
207  //- Default construct
208  explicit OStringStream
209  (
212  )
213  :
214  allocator_type(),
215  OSstream(stream_, "output", format, version)
216  {}
217 
218  //- Copy construct, copies content and format
220  :
223  {}
224 
225 
226  // Member Functions
227 
228  //- Reset the output buffer and rewind the stream
229  void reset()
230  {
231  this->str(""); // No other way to reset the end
232  this->rewind();
233  }
234 
235  //- Rewind the output stream
236  virtual void rewind()
237  {
238  // pubseekpos() instead of seekp() for symmetry with other classes
239  stream_.rdbuf()->pubseekpos(0, std::ios_base::out);
240  }
241 
242  //- Print stream description to Ostream
243  virtual void print(Ostream& os) const;
244 };
245 
246 
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 
249 } // End namespace Foam
250 
251 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252 
253 #endif
254 
255 // ************************************************************************* //
Foam::OStringStream::OStringStream
OStringStream(const OStringStream &str)
Copy construct, copies content and format.
Definition: StringStream.H:219
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:97
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:160
Foam::OStringStream::rewind
virtual void rewind()
Rewind the output stream.
Definition: StringStream.H:236
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:289
Foam::Detail::StringStreamAllocator::StringStreamAllocator
StringStreamAllocator(const std::string &s)
Copy construct from string.
Definition: StringStream.H:80
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
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:73
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:874
Foam::Detail::StringStreamAllocator::StringStreamAllocator
StringStreamAllocator()
Default construct.
Definition: StringStream.H:74
Foam::Detail::StringStreamAllocator
Allocator for variants of a std stringstream.
Definition: StringStream.H:58
Foam::OStringStream::OStringStream
OStringStream(streamFormat format=ASCII, versionNumber version=currentVersion)
Default construct.
Definition: StringStream.H:209
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
Return 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:184
Foam::OSstream::OSstream
OSstream(const OSstream &)=default
Copy construct.
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:170
Foam::ISstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:124
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:341
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
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::IStringStream
IStringStream(streamFormat format=ASCII, versionNumber version=currentVersion)
Default construct.
Definition: StringStream.H:124
Foam::IStringStream
Input from string buffer, using a ISstream.
Definition: StringStream.H:111
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:91
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:196
Foam::OStringStream::reset
void reset()
Reset the output buffer and rewind the stream.
Definition: StringStream.H:229
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