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  OTstream(IOstreamOption(fmt, ver))
84  {}
85 
86  //- Copy construct
87  OTstream(const OTstream& os)
88  :
89  Ostream(os.format(), os.version()),
90  DynamicList<token>(os.tokens())
91  {
92  setOpened();
93  setGood();
94  }
95 
96  //- Move construct
97  OTstream(OTstream&& os)
98  :
99  Ostream(os.format(), os.version()),
100  DynamicList<token>(std::move(os.tokens()))
101  {
102  setOpened();
103  setGood();
104  }
105 
106 
107  //- Destructor
108  ~OTstream() = default;
109 
110 
111  // Member Functions
112 
113  //- The tokens
114  const DynamicList<token>& tokens() const
115  {
116  return *this;
117  }
118 
119  //- The tokens
121  {
122  return *this;
123  }
124 
125 
126  // Write
127 
128  //- Write token to stream or otherwise handle it.
129  // \return false if the token type was not handled by this method
130  virtual bool write(const token& tok);
131 
132  //- Write single character. Whitespace is suppressed.
133  virtual Ostream& write(const char c);
134 
135  //- Write the word-characters of a character string.
136  // Sends as a single char, or as word.
137  virtual Ostream& write(const char* str);
138 
139  //- Write word
140  virtual Ostream& write(const word& str);
141 
142  //- Write string
143  virtual Ostream& write(const string& str);
144 
145  //- Write std::string surrounded by quotes.
146  // Optional write without quotes.
147  virtual Ostream& writeQuoted
148  (
149  const std::string& str,
150  const bool quoted=true
151  );
152 
153  //- Write int32_t as a label
154  virtual Ostream& write(const int32_t val);
155 
156  //- Write int64_t as a label
157  virtual Ostream& write(const int64_t val);
158 
159  //- Write floatScalar
160  virtual Ostream& write(const floatScalar val);
161 
162  //- Write doubleScalar
163  virtual Ostream& write(const doubleScalar val);
164 
165  //- Write binary block with 8-byte alignment.
166  virtual Ostream& write(const char* data, std::streamsize count);
167 
168  //- Low-level raw binary output.
169  virtual Ostream& writeRaw(const char* data, std::streamsize count);
170 
171  //- Begin marker for low-level raw binary output.
172  // The count indicates the number of bytes for subsequent
173  // writeRaw calls.
174  virtual bool beginRawWrite(std::streamsize count);
175 
176  //- End marker for low-level raw binary output.
177  virtual bool endRawWrite()
178  {
179  return true;
180  }
181 
182  //- Add indentation characters
183  virtual void indent()
184  {}
185 
186 
187  // Stream state functions
188 
189  //- Flush stream
190  virtual void flush()
191  {}
192 
193  //- Add newline and flush stream
194  virtual void endl()
195  {}
196 
197  //- Get the current padding character
198  // \return previous padding character
199  virtual char fill() const
200  {
201  return 0;
202  }
203 
204  //- Set padding character for formatted field up to field width
205  virtual char fill(const char)
206  {
207  return 0;
208  }
209 
210  //- Get width of output field
211  virtual int width() const
212  {
213  return 0;
214  }
215 
216  //- Set width of output field
217  // \return previous width
218  virtual int width(const int)
219  {
220  return 0;
221  }
222 
223  //- Get precision of output field
224  virtual int precision() const
225  {
226  return 0;
227  }
228 
229  //- Set precision of output field
230  // \return old precision
231  virtual int precision(const int)
232  {
233  return 0;
234  }
235 
236 
237  // Inquiry
238 
239  //- Return flags of output stream
240  virtual ios_base::fmtflags flags() const
241  {
242  return ios_base::fmtflags(0);
243  }
244 
245 
246  // Edit
247 
248  //- Set flags of stream
249  ios_base::fmtflags flags(const ios_base::fmtflags)
250  {
251  return ios_base::fmtflags(0);
252  }
253 
254 
255  // Other
256 
257  //- Reset the output buffer and rewind the stream
258  void reset()
259  {
260  this->rewind();
261  }
262 
263  //- Rewind the output stream
264  virtual void rewind()
265  {
267  }
268 
269  //- Print stream description to Ostream
270  void print(Ostream& os) const;
271 };
272 
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 } // End namespace Foam
277 
278 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279 
280 #endif
281 
282 // ************************************************************************* //
Foam::OTstream::endl
virtual void endl()
Add newline and flush stream.
Definition: OTstream.H:193
Foam::OTstream::fill
virtual char fill(const char)
Set padding character for formatted field up to field width.
Definition: OTstream.H:204
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:176
Foam::OTstream::rewind
virtual void rewind()
Rewind the output stream.
Definition: OTstream.H:263
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:68
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:248
Foam::OTstream::precision
virtual int precision() const
Get precision of output field.
Definition: OTstream.H:223
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:210
Foam::OTstream::reset
void reset()
Reset the output buffer and rewind the stream.
Definition: OTstream.H:257
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:86
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:182
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:239
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:77
Foam::OTstream::tokens
const DynamicList< token > & tokens() const
The tokens.
Definition: OTstream.H:113
Foam::OTstream::fill
virtual char fill() const
Get the current padding character.
Definition: OTstream.H:198
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:189
Foam::OTstream::OTstream
OTstream(OTstream &&os)
Move construct.
Definition: OTstream.H:96
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:55
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:119