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-2024 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 Foam_OTstream_H
42 #define Foam_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(IOstreamOption(streamOpt.format(), streamOpt.version())),
71  {
72  setOpened();
73  setGood();
74  }
75 
76  //- Copy construct
77  OTstream(const OTstream& os)
78  :
79  Ostream(static_cast<IOstreamOption>(os)),
81  {
82  setOpened();
83  setGood();
84  }
85 
86  //- Move construct
88  :
89  Ostream(static_cast<IOstreamOption>(os)),
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 noexcept { return *this; }
105 
106  //- The tokens
107  DynamicList<token>& tokens() noexcept { return *this; }
108 
110  // Write
111 
112  //- Inherit write methods from Ostream
113  using Ostream::writeQuoted;
115  //- Write token to stream or otherwise handle it.
116  // \return false if the token type was not handled by this method
117  virtual bool write(const token& tok) override;
118 
119  //- Write single character. Whitespace is suppressed.
120  virtual Ostream& write(const char c) override;
121 
122  //- Write character/string content, with/without surrounding quotes
123  virtual Ostream& writeQuoted
124  (
125  const char* str,
126  std::streamsize len,
127  const bool quoted=true
128  ) override;
129 
130  //- Write the word-characters of a character string.
131  // Sends as a single char, or as word.
132  virtual Ostream& write(const char* str) override;
133 
134  //- Write word
135  virtual Ostream& write(const word& str) override;
136 
137  //- Write string
138  virtual Ostream& write(const std::string& str) override;
139 
140  //- Write int32_t as a label
141  virtual Ostream& write(const int32_t val) override;
142 
143  //- Write int64_t as a label
144  virtual Ostream& write(const int64_t val) override;
145 
146  //- Write float
147  virtual Ostream& write(const float val) override;
148 
149  //- Write double
150  virtual Ostream& write(const double val) override;
151 
152  //- Write binary block with 8-byte alignment.
153  virtual Ostream& write
154  (
155  const char* data,
156  std::streamsize count
157  ) override;
158 
159  //- Low-level raw binary output.
160  virtual Ostream& writeRaw
161  (
162  const char* data,
163  std::streamsize count
164  ) override;
165 
166  //- Begin marker for low-level raw binary output.
167  // The count indicates the number of bytes for subsequent
168  // writeRaw calls.
169  virtual bool beginRawWrite(std::streamsize count) override;
170 
171  //- End marker for low-level raw binary output.
172  virtual bool endRawWrite() override
173  {
174  return true;
175  }
176 
177  //- Add indentation characters
178  virtual void indent() override
179  {}
180 
181 
182  // Stream State Functions
183 
184  //- Return current stream flags.
185  //- Dummy for token stream, returns 0.
186  virtual std::ios_base::fmtflags flags() const override
187  {
188  return std::ios_base::fmtflags(0);
189  }
190 
191  //- Set stream flags, return old stream flags.
192  //- Dummy for token stream, returns 0.
193  std::ios_base::fmtflags flags(std::ios_base::fmtflags) override
194  {
195  return std::ios_base::fmtflags(0);
196  }
197 
198  //- Flush stream
199  virtual void flush() override
200  {}
201 
202  //- Add newline and flush stream
203  virtual void endl() override
204  {}
205 
206  //- Get the current padding character
207  // \return previous padding character
208  virtual char fill() const override
209  {
210  return 0;
211  }
213  //- Set padding character for formatted field up to field width
214  virtual char fill(const char) override
215  {
216  return 0;
217  }
218 
219  //- Get width of output field
220  virtual int width() const override
221  {
222  return 0;
223  }
224 
225  //- Set width of output field
226  // \return previous width
227  virtual int width(const int) override
228  {
229  return 0;
230  }
231 
232  //- Get precision of output field
233  virtual int precision() const override
234  {
235  return 0;
236  }
237 
238  //- Set precision of output field
239  // \return old precision
240  virtual int precision(const int) override
241  {
242  return 0;
243  }
244 
245 
246  // Other
248  //- Rewind the output stream to position 0 (non-virtual!)
249  //- and adjust the stream status (open/good/eof ...)
250  //- Reset the output buffer and rewind the stream
251  void reset() noexcept
252  {
254  setOpened();
255  setGood();
256  }
257 
258  //- Rewind the output stream to position 0
259  //- and adjust the stream status (open/good/eof ...)
260  virtual void rewind() { OTstream::reset(); }
262  //- Print stream description to Ostream
263  void print(Ostream& os) const override;
264 
265 
266  // Additional constructors and methods (as per v2012 and earlier)
267  #ifdef Foam_IOstream_extras
268 
269  //- Construct empty with format
271  :
273  {}
274 
275  #endif /* Foam_IOstream_extras */
276 };
278 
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280 
281 } // End namespace Foam
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 #endif
286 
287 // ************************************************************************* //
virtual void indent() override
Add indentation characters.
Definition: OTstream.H:220
virtual std::ios_base::fmtflags flags() const override
Return current stream flags. Dummy for token stream, returns 0.
Definition: OTstream.H:230
void reset() noexcept
Rewind the output stream to position 0 (non-virtual!) and adjust the stream status (open/good/eof ...
Definition: OTstream.H:318
virtual void flush() override
Flush stream.
Definition: OTstream.H:247
~OTstream()=default
Destructor.
A token holds an item read from Istream.
Definition: token.H:65
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:265
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
Definition: OTstream.C:174
A simple output token stream that can be used to build token lists. Always UNCOMPRESSED.
Definition: OTstream.H:52
A simple container for options an IOstream can normally have.
virtual int width() const override
Get width of output field.
Definition: OTstream.H:277
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true)=0
Write character/string content, with/without surrounding quotes.
const DynamicList< token > & tokens() const noexcept
The tokens.
Definition: OTstream.H:109
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition: OTstream.C:27
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
Definition: OTstream.C:161
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition: OTstream.C:52
virtual void endl() override
Add newline and flush stream.
Definition: OTstream.H:253
OTstream(IOstreamOption streamOpt=IOstreamOption())
Default construct, set stream status.
Definition: OTstream.H:64
virtual bool endRawWrite() override
End marker for low-level raw binary output.
Definition: OTstream.H:212
virtual char fill() const override
Get the current padding character.
Definition: OTstream.H:261
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
virtual void rewind()
Rewind the output stream to position 0 and adjust the stream status (open/good/eof ...
Definition: OTstream.H:329
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:405
const dimensionedScalar c
Speed of light in a vacuum.
void print(Ostream &os) const override
Print stream description to Ostream.
Definition: OTstream.C:188
versionNumber version() const noexcept
Get the stream version.
streamFormat
Data format (ascii | binary)
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:150
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:174
streamFormat format() const noexcept
Get the current stream format.
Namespace for OpenFOAM.
virtual int precision() const override
Get precision of output field.
Definition: OTstream.H:295