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-2023 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
105  {
106  return *this;
107  }
108 
109  //- The tokens
111  {
112  return *this;
113  }
114 
115 
116  // Write
118  //- Inherit write methods from Ostream
119  using Ostream::writeQuoted;
120 
121  //- Write token to stream or otherwise handle it.
122  // \return false if the token type was not handled by this method
123  virtual bool write(const token& tok) override;
124 
125  //- Write single character. Whitespace is suppressed.
126  virtual Ostream& write(const char c) override;
127 
128  //- Write character/string content, with/without surrounding quotes
129  virtual Ostream& writeQuoted
130  (
131  const char* str,
132  std::streamsize len,
133  const bool quoted=true
134  ) override;
135 
136  //- Write the word-characters of a character string.
137  // Sends as a single char, or as word.
138  virtual Ostream& write(const char* str) override;
139 
140  //- Write word
141  virtual Ostream& write(const word& str) override;
142 
143  //- Write string
144  virtual Ostream& write(const std::string& str) override;
145 
146  //- Write int32_t as a label
147  virtual Ostream& write(const int32_t val) override;
148 
149  //- Write int64_t as a label
150  virtual Ostream& write(const int64_t val) override;
151 
152  //- Write float
153  virtual Ostream& write(const float val) override;
154 
155  //- Write double
156  virtual Ostream& write(const double val) override;
157 
158  //- Write binary block with 8-byte alignment.
159  virtual Ostream& write
160  (
161  const char* data,
162  std::streamsize count
163  ) override;
164 
165  //- Low-level raw binary output.
166  virtual Ostream& writeRaw
167  (
168  const char* data,
169  std::streamsize count
170  ) override;
171 
172  //- Begin marker for low-level raw binary output.
173  // The count indicates the number of bytes for subsequent
174  // writeRaw calls.
175  virtual bool beginRawWrite(std::streamsize count) override;
176 
177  //- End marker for low-level raw binary output.
178  virtual bool endRawWrite() override
179  {
180  return true;
181  }
182 
183  //- Add indentation characters
184  virtual void indent() override
185  {}
186 
187 
188  // Stream State Functions
189 
190  //- Get flags of output stream
191  virtual ios_base::fmtflags flags() const override
192  {
193  return ios_base::fmtflags(0);
194  }
195 
196  //- Set flags of stream - ignored
197  std::ios_base::fmtflags flags(const ios_base::fmtflags) override
198  {
199  return ios_base::fmtflags(0);
200  }
201 
202  //- Flush stream
203  virtual void flush() override
204  {}
205 
206  //- Add newline and flush stream
207  virtual void endl() override
208  {}
209 
210  //- Get the current padding character
211  // \return previous padding character
212  virtual char fill() const override
213  {
214  return 0;
215  }
216 
217  //- Set padding character for formatted field up to field width
218  virtual char fill(const char) override
219  {
220  return 0;
221  }
222 
223  //- Get width of output field
224  virtual int width() const override
225  {
226  return 0;
227  }
228 
229  //- Set width of output field
230  // \return previous width
231  virtual int width(const int) override
232  {
233  return 0;
234  }
236  //- Get precision of output field
237  virtual int precision() const override
238  {
239  return 0;
240  }
241 
242  //- Set precision of output field
243  // \return old precision
244  virtual int precision(const int) override
245  {
246  return 0;
247  }
248 
249 
250  // Other
252  //- Reset the output buffer and rewind the stream
253  void reset()
254  {
255  this->rewind();
256  }
258  //- Rewind the output stream
259  virtual void rewind()
260  {
262  setOpened();
263  setGood();
264  }
266  //- Print stream description to Ostream
267  void print(Ostream& os) const override;
268 
269 
270  // Additional constructors and methods (as per v2012 and earlier)
271  #ifdef Foam_IOstream_extras
272 
273  //- Construct empty with format
275  :
277  {}
278 
279  #endif /* Foam_IOstream_extras */
280 };
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 } // End namespace Foam
286 
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 
289 #endif
290 
291 // ************************************************************************* //
virtual void indent() override
Add indentation characters.
Definition: OTstream.H:226
virtual void flush() override
Flush stream.
Definition: OTstream.H:251
~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:272
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:281
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:257
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:218
virtual char fill() const override
Get the current padding character.
Definition: OTstream.H:265
virtual ios_base::fmtflags flags() const override
Get flags of output stream.
Definition: OTstream.H:235
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.
Definition: OTstream.H:328
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:299
void reset()
Reset the output buffer and rewind the stream.
Definition: OTstream.H:320