OTstream.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) 2019-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 Class
27  Foam::OTstream
28 
29 Description
30  A simple output token stream that can be used to build token lists.
31  Always UNCOMPRESSED.
32 
33 Note
34  Appending single characters to token list is fragile.
35 
36 SourceFiles
37  OTstream.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef OTstream_H
42 #define OTstream_H
43 
44 #include "token.H"
45 #include "Ostream.H"
46 #include "DynamicList.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class OTstream Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class OTstream
58 :
59  public Ostream,
60  public DynamicList<token>
61 {
62 public:
63 
64  // Constructors
65 
66  //- Default construct, set stream status
67  explicit OTstream(IOstreamOption streamOpt = IOstreamOption())
68  :
69  Ostream(streamOpt.format(), streamOpt.version()),
71  {
72  setOpened();
73  setGood();
74  }
75 
76  //- Construct with format, version
77  explicit OTstream
78  (
79  streamFormat fmt,
81  )
82  :
83  Ostream(fmt, ver),
85  {
86  setOpened();
87  setGood();
88  }
89 
90  //- Copy construct
91  OTstream(const OTstream& os)
92  :
93  Ostream(os.format(), os.version()),
94  DynamicList<token>(os.tokens())
95  {
96  setOpened();
97  setGood();
98  }
99 
100  //- Move construct
101  OTstream(OTstream&& os)
102  :
103  Ostream(os.format(), os.version()),
104  DynamicList<token>(std::move(os.tokens()))
105  {
106  setOpened();
107  setGood();
108  }
109 
110 
111  //- Destructor
112  ~OTstream() = default;
113 
114 
115  // Member Functions
116 
117  //- The tokens
118  const DynamicList<token>& tokens() const
119  {
120  return *this;
121  }
122 
123  //- The tokens
125  {
126  return *this;
127  }
128 
129 
130  // Write
131 
132  //- Write token to stream or otherwise handle it.
133  // \return false if the token type was not handled by this method
134  virtual bool write(const token& tok);
135 
136  //- Write single character. Whitespace is suppressed.
137  virtual Ostream& write(const char c);
138 
139  //- Write the word-characters of a character string.
140  // Sends as a single char, or as word.
141  virtual Ostream& write(const char* str);
142 
143  //- Write word
144  virtual Ostream& write(const word& str);
145 
146  //- Write string
147  virtual Ostream& write(const string& str);
148 
149  //- Write std::string surrounded by quotes.
150  // Optional write without quotes.
151  virtual Ostream& writeQuoted
152  (
153  const std::string& str,
154  const bool quoted=true
155  );
156 
157  //- Write int32_t as a label
158  virtual Ostream& write(const int32_t val);
159 
160  //- Write int64_t as a label
161  virtual Ostream& write(const int64_t val);
162 
163  //- Write floatScalar
164  virtual Ostream& write(const floatScalar val);
165 
166  //- Write doubleScalar
167  virtual Ostream& write(const doubleScalar val);
168 
169  //- Write binary block with 8-byte alignment.
170  virtual Ostream& write(const char* data, std::streamsize count);
171 
172  //- Low-level raw binary output.
173  virtual Ostream& writeRaw(const char* data, std::streamsize count);
174 
175  //- Begin marker for low-level raw binary output.
176  // The count indicates the number of bytes for subsequent
177  // writeRaw calls.
178  virtual bool beginRawWrite(std::streamsize count);
179 
180  //- End marker for low-level raw binary output.
181  virtual bool endRawWrite()
182  {
183  return true;
184  }
185 
186  //- Add indentation characters
187  virtual void indent()
188  {}
189 
190 
191  // Stream state functions
192 
193  //- Flush stream
194  virtual void flush()
195  {}
196 
197  //- Add newline and flush stream
198  virtual void endl()
199  {}
200 
201  //- Get the current padding character
202  // \return previous padding character
203  virtual char fill() const
204  {
205  return 0;
206  }
207 
208  //- Set padding character for formatted field up to field width
209  virtual char fill(const char)
210  {
211  return 0;
212  }
213 
214  //- Get width of output field
215  virtual int width() const
216  {
217  return 0;
218  }
219 
220  //- Set width of output field
221  // \return previous width
222  virtual int width(const int)
223  {
224  return 0;
225  }
226 
227  //- Get precision of output field
228  virtual int precision() const
229  {
230  return 0;
231  }
232 
233  //- Set precision of output field
234  // \return old precision
235  virtual int precision(const int)
236  {
237  return 0;
238  }
239 
240 
241  // Inquiry
242 
243  //- Return flags of output stream
244  virtual ios_base::fmtflags flags() const
245  {
246  return ios_base::fmtflags(0);
247  }
248 
249 
250  // Edit
251 
252  //- Set flags of stream
253  ios_base::fmtflags flags(const ios_base::fmtflags)
254  {
255  return ios_base::fmtflags(0);
256  }
257 
258 
259  // Other
260 
261  //- Reset the output buffer and rewind the stream
262  void reset()
263  {
264  this->rewind();
265  }
266 
267  //- Rewind the output stream
268  virtual void rewind()
269  {
271  }
272 
273  //- Print stream description to Ostream
274  void print(Ostream& os) const;
275 };
276 
277 
278 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279 
280 } // End namespace Foam
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 #endif
285 
286 // ************************************************************************* //
Foam::OTstream::endl
virtual void endl()
Add newline and flush stream.
Definition: OTstream.H:197
Foam::OTstream::fill
virtual char fill(const char)
Set padding character for formatted field up to field width.
Definition: OTstream.H:208
Foam::OTstream::write
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
Definition: OTstream.C:34
token.H
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Foam::OTstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OTstream.C:94
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::OTstream::OTstream
OTstream(IOstreamOption streamOpt=IOstreamOption())
Default construct, set stream status.
Definition: OTstream.H:66
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:196
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::OTstream::endRawWrite
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OTstream.H:180
Foam::OTstream::rewind
virtual void rewind()
Rewind the output stream.
Definition: OTstream.H:267
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
Foam::OTstream::print
void print(Ostream &os) const
Print stream description to Ostream.
Definition: OTstream.C:189
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::IOstream::setOpened
void setOpened()
Set stream opened.
Definition: IOstream.H:123
Foam::OTstream::flags
ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream.
Definition: OTstream.H:252
Foam::OTstream::precision
virtual int precision() const
Get precision of output field.
Definition: OTstream.H:227
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::OTstream::width
virtual int width() const
Get width of output field.
Definition: OTstream.H:214
Foam::OTstream::reset
void reset()
Reset the output buffer and rewind the stream.
Definition: OTstream.H:261
Foam::OTstream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OTstream.C:160
Foam::OTstream::OTstream
OTstream(const OTstream &os)
Copy construct.
Definition: OTstream.H:90
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::OTstream::indent
virtual void indent()
Add indentation characters.
Definition: OTstream.H:186
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::OTstream::~OTstream
~OTstream()=default
Destructor.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Ostream.H
Foam::OTstream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OTstream.H:243
Foam::OTstream
A simple output token stream that can be used to build token lists. Always UNCOMPRESSED.
Definition: OTstream.H:56
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:141
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:74
Foam::OTstream::tokens
const DynamicList< token > & tokens() const
The tokens.
Definition: OTstream.H:117
Foam::OTstream::fill
virtual char fill() const
Get the current padding character.
Definition: OTstream.H:202
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
DynamicList.H
Foam::OTstream::flush
virtual void flush()
Flush stream.
Definition: OTstream.H:193
Foam::Ostream::Ostream
Ostream(const Ostream &)=default
Copy construct.
Foam::OTstream::OTstream
OTstream(OTstream &&os)
Move construct.
Definition: OTstream.H:100
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54
Foam::OTstream::beginRawWrite
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: OTstream.C:174
Foam::OTstream::tokens
DynamicList< token > & tokens()
The tokens.
Definition: OTstream.H:123