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 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  // Member Data
63 
64  //- The stream type
65  typedef StreamType stream_type;
66 
67  //- The input/output stream.
69 
70 
71  // Constructors
72 
73  //- Construct null
75  :
76  stream_()
77  {}
78 
79 
80  //- Copy construct from string
81  StringStreamAllocator(const std::string& buffer)
82  :
83  stream_(buffer)
84  {}
85 
86 
87 public:
88 
89  // Public Member Functions
90 
91  //- Get the string - as Foam::string rather than std::string
92  Foam::string str() const
93  {
94  return Foam::string(stream_.str());
95  }
96 
97  //- Set the string
98  void str(const std::string& s)
99  {
100  stream_.str(s);
101  }
102 };
103 
104 } // End namespace Detail
105 
106 
107 /*---------------------------------------------------------------------------*\
108  Class IStringStream Declaration
109 \*---------------------------------------------------------------------------*/
110 
111 //- Input from string buffer, using a ISstream
113 :
114  public Detail::StringStreamAllocator<std::istringstream>,
115  public ISstream
116 {
118 
119 public:
120 
121  // Constructors
122 
123  //- Construct from string
125  (
126  const Foam::string& buffer,
129  const Foam::string& name="input"
130  )
131  :
132  allocator_type(buffer),
134  {}
135 
136 
137  //- Construct from char*
139  (
140  const char* buffer,
143  const Foam::string& name="input"
144  )
145  :
146  allocator_type(buffer),
148  {}
149 
150 
151  //- Construct as copy of content
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 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  return const_cast<IStringStream&>(*this);
179  }
180 
181 };
182 
183 
184 /*---------------------------------------------------------------------------*\
185  Class OStringStream Declaration
186 \*---------------------------------------------------------------------------*/
187 
188 //- Output to string buffer, using a OSstream
190 :
191  public Detail::StringStreamAllocator<std::ostringstream>,
192  public OSstream
193 {
195 
196 public:
197 
198  // Constructors
199 
200  //- Construct and set stream status
202  (
205  )
206  :
207  allocator_type(),
208  OSstream(stream_, "output", format, version)
209  {}
210 
211 
212  //- Construct as copy of content
214  :
217  {}
218 
219 
220  // Member Functions
221 
222  //- Reset the output buffer and rewind the stream
223  void reset()
224  {
225  this->str(""); // No other way to reset the end
226  this->rewind();
227  }
228 
229  //- Rewind the output stream
230  virtual void rewind()
231  {
232  // pubseekpos() instead of seekp() for symmetry with other classes
233  stream_.rdbuf()->pubseekpos(0, std::ios_base::out);
234  }
235 
236  //- Print description to Ostream
237  virtual void print(Ostream& os) const;
238 
239 };
240 
241 
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243 
244 } // End namespace Foam
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 #endif
249 
250 // ************************************************************************* //
Foam::OStringStream::OStringStream
OStringStream(const OStringStream &str)
Construct as copy of content.
Definition: StringStream.H:213
Foam::IStringStream::print
virtual void print(Ostream &os) const
Print description to Ostream.
Definition: StringStream.C:32
Foam::Detail::StringStreamAllocator::str
void str(const std::string &s)
Set the string.
Definition: StringStream.H:98
Foam::ISstream::ISstream
ISstream(std::istream &is, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct as wrapper around std::istream.
Definition: ISstreamI.H:32
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)
Construct as copy of content.
Definition: StringStream.H:152
Foam::OStringStream::rewind
virtual void rewind()
Rewind the output stream.
Definition: StringStream.H:230
ISstream.H
Foam::OStringStream::print
virtual void print(Ostream &os) const
Print description to Ostream.
Definition: StringStream.C:43
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number.
Definition: IOstreamOption.H:193
Foam::ISstream
Generic input stream using standard (STL) streams.
Definition: ISstream.H:54
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:852
Foam::Detail::StringStreamAllocator::StringStreamAllocator
StringStreamAllocator()
Construct null.
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)
Construct and set stream status.
Definition: StringStream.H:202
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::OSstream::OSstream
OSstream(std::ostream &os, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct as wrapper around std::ostream and set stream status.
Definition: OSstreamI.H:33
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:91
Foam::IStringStream::operator()
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: StringStream.H:176
Foam::Detail::StringStreamAllocator::StringStreamAllocator
StringStreamAllocator(const std::string &buffer)
Copy construct from string.
Definition: StringStream.H:81
Foam::OSstream
Generic output 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:111
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:321
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::IStringStream
Input from string buffer, using a ISstream.
Definition: StringStream.H:112
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:92
Foam::IStringStream::IStringStream
IStringStream(const Foam::string &buffer, streamFormat format=ASCII, versionNumber version=currentVersion, const Foam::string &name="input")
Construct from string.
Definition: StringStream.H:125
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:189
Foam::OStringStream::reset
void reset()
Reset the output buffer and rewind the stream.
Definition: StringStream.H:223
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