prefixOSstream.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) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2020-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::prefixOSstream
29 
30 Description
31  Version of OSstream that prints a prefix on each line.
32 
33  This is useful for running in parallel as it allows the processor number
34  to be automatically prepended to each message line.
35 
36 SourceFiles
37  prefixOSstream.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef Foam_prefixOSstream_H
42 #define Foam_prefixOSstream_H
43 
44 #include "OSstream.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class prefixOSstream Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class prefixOSstream
56 :
57  public OSstream
58 {
59  // Private Data
60 
61  bool printPrefix_;
62 
63  string prefix_;
64 
65 
66  // Private Member Functions
67 
68  //- Emit pending prefix before any output
69  inline void checkWritePrefix();
70 
71 
72 public:
73 
74  // Constructors
75 
76  //- Construct and set stream status
78  (
79  std::ostream& os,
80  const string& streamName,
81  IOstreamOption streamOpt = IOstreamOption()
82  );
83 
84 
85  // Member Functions
86 
87  // Enquiry
88 
89  //- Return the stream prefix
90  const string& prefix() const noexcept { return prefix_; }
91 
92  //- Return non-const access to the stream prefix
93  string& prefix() noexcept { return prefix_; }
94 
95 
96  // Write Functions
97 
98  //- Inherit write methods from OSstream
100 
101  //- Write token to stream or otherwise handle it.
102  // \return false if the token type was not handled by this method
103  virtual bool write(const token& tok) override;
104 
105  //- Write character
106  virtual Ostream& write(const char c) override;
107 
108  //- Write character/string content, with/without surrounding quotes
109  virtual Ostream& writeQuoted
110  (
111  const char* str,
112  std::streamsize len,
113  const bool quoted=true
114  ) override;
115 
116  //- Write character string
117  virtual Ostream& write(const char* str) override;
118 
119  //- Write word
120  virtual Ostream& write(const word& val) override;
121 
122  //- Write string
123  virtual Ostream& write(const std::string& val) override;
124 
125  //- Write int32_t
126  virtual Ostream& write(const int32_t val) override;
127 
128  //- Write int64_t
129  virtual Ostream& write(const int64_t val) override;
130 
131  //- Write float
132  virtual Ostream& write(const float val) override;
133 
134  //- Write double
135  virtual Ostream& write(const double val) override;
136 
137  //- Write binary block
138  virtual Ostream& write(const char* buf, std::streamsize count) override;
139 
140  //- Add indentation characters
141  virtual void indent() override;
142 
143 
144  // Print
145 
146  //- Print stream description to Ostream
147  virtual void print(Ostream& os) const override;
148 };
149 
150 
151 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
152 
153 } // End namespace Foam
154 
155 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
156 
157 #endif
158 
159 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
virtual void print(Ostream &os) const override
Print stream description to Ostream.
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition: OSstream.C:101
A token holds an item read from Istream.
Definition: token.H:65
A simple container for options an IOstream can normally have.
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
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Version of OSstream that prints a prefix on each line.
A class for handling words, derived from Foam::string.
Definition: word.H:63
const string & prefix() const noexcept
Return the stream prefix.
virtual void indent() override
Add indentation characters.
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)
const dimensionedScalar c
Speed of light in a vacuum.
prefixOSstream(std::ostream &os, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct and set stream status.
Namespace for OpenFOAM.
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.