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