UOPstream.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) 2017-2021 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::UOPstream
29 
30 Description
31  Output inter-processor communications stream operating on external
32  buffer.
33 
34 SourceFiles
35  UOPstream.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #include "Pstream.H"
40 
41 #ifndef UOPstream_H
42 #define UOPstream_H
43 
44 #include "UPstream.H"
45 #include "Ostream.H"
46 #include "DynamicList.H"
47 #include "PstreamBuffers.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 /*---------------------------------------------------------------------------*\
55  Class UOPstream Declaration
56 \*---------------------------------------------------------------------------*/
57 
58 class UOPstream
59 :
60  public UPstream,
61  public Ostream
62 {
63  // Private Data
64 
65  int toProcNo_;
66 
67  DynamicList<char>& sendBuf_;
68 
69  const int tag_;
70 
71  const label comm_;
72 
73  const bool sendAtDestruct_;
74 
75 
76  // Private Member Functions
77 
78  //- Prepare send buffer for count bytes of output,
79  //- with specified alignment.
80  inline void prepareBuffer(const size_t count, const size_t align);
81 
82  //- Generic write data to the send buffer, aligned by sizeof(T)
83  template<class T>
84  inline void writeToBuffer(const T& val);
85 
86  //- Write count bytes of data to the send buffer,
87  //- using align byte alignment
88  inline void writeToBuffer
89  (
90  const void* data,
91  const size_t count,
92  const size_t align
93  );
94 
95  //- Add a single char to the send buffer. No alignment needed
96  inline void putChar(const char c);
97 
98  //- Write string length and string content.
99  // The content includes the trailing nul char.
100  inline void putString(const std::string& str);
101 
102 
103 public:
104 
105  // Constructors
106 
107  //- Construct given process index to write to using the given
108  //- attached send buffer, optional communication characteristics
109  //- and IO format
110  UOPstream
111  (
112  const commsTypes commsType,
113  const int toProcNo,
114  DynamicList<char>& sendBuf,
115  const int tag = UPstream::msgType(),
116  const label comm = UPstream::worldComm,
117  const bool sendAtDestruct = true,
119  );
120 
121  //- Construct given buffers
122  UOPstream(const int toProcNo, PstreamBuffers& buffers);
123 
124 
125  //- Destructor.
126  ~UOPstream();
127 
128 
129  // Member Functions
130 
131  // Inquiry
132 
133  //- Return flags of output stream
134  virtual ios_base::fmtflags flags() const
135  {
136  return ios_base::fmtflags(0);
137  }
138 
139 
140  // Write Functions
141 
142  //- Write given buffer to given processor
143  static bool write
144  (
145  const commsTypes commsType,
146  const int toProcNo,
147  const char* buf,
148  const std::streamsize bufSize,
149  const int tag = UPstream::msgType(),
150  const label communicator = UPstream::worldComm
151  );
152 
153  //- Write token to stream or otherwise handle it.
154  // \return false if the token type was not handled by this method
155  virtual bool write(const token& tok);
156 
157  //- Write single character. Whitespace is suppressed.
158  virtual Ostream& write(const char c);
159 
160  //- Write the word-characters of a character string.
161  // Sends as a single char, or as word.
162  virtual Ostream& write(const char* str);
163 
164  //- Write word
165  virtual Ostream& write(const word& str);
166 
167  //- Write string
168  virtual Ostream& write(const string& str);
169 
170  //- Write std::string surrounded by quotes.
171  // Optional write without quotes.
172  virtual Ostream& writeQuoted
173  (
174  const std::string& str,
175  const bool quoted=true
176  );
177 
178  //- Write int32_t as a label
179  virtual Ostream& write(const int32_t val);
180 
181  //- Write int64_t as a label
182  virtual Ostream& write(const int64_t val);
183 
184  //- Write floatScalar
185  virtual Ostream& write(const floatScalar val);
186 
187  //- Write doubleScalar
188  virtual Ostream& write(const doubleScalar val);
189 
190  //- Write binary block with 8-byte alignment.
191  virtual Ostream& write(const char* data, std::streamsize count);
192 
193  //- Low-level raw binary output.
194  virtual Ostream& writeRaw(const char* data, std::streamsize count);
195 
196  //- Begin marker for low-level raw binary output.
197  // The count indicates the number of bytes for subsequent
198  // writeRaw calls.
199  virtual bool beginRawWrite(std::streamsize count);
200 
201  //- End marker for low-level raw binary output.
202  virtual bool endRawWrite()
203  {
204  return true;
205  }
206 
207  //- Add indentation characters
208  virtual void indent()
209  {}
210 
211 
212  // Stream state functions
213 
214  //- Flush stream
215  virtual void flush()
216  {}
217 
218  //- Add newline and flush stream
219  virtual void endl()
220  {}
221 
222  //- Get the current padding character
223  // \return previous padding character
224  virtual char fill() const
225  {
226  return 0;
227  }
228 
229  //- Set padding character for formatted field up to field width
230  virtual char fill(const char)
231  {
232  return 0;
233  }
234 
235  //- Get width of output field
236  virtual int width() const
237  {
238  return 0;
239  }
240 
241  //- Set width of output field
242  // \return previous width
243  virtual int width(const int)
244  {
245  return 0;
246  }
247 
248  //- Get precision of output field
249  virtual int precision() const
250  {
251  return 0;
252  }
253 
254  //- Set precision of output field
255  // \return old precision
256  virtual int precision(const int)
257  {
258  return 0;
259  }
260 
261 
262  // Edit
263 
264  //- Set flags of stream
265  ios_base::fmtflags flags(const ios_base::fmtflags)
266  {
267  return ios_base::fmtflags(0);
268  }
269 
270 
271  // Print
272 
273  //- Print stream description to Ostream
274  void print(Ostream& os) const;
275 };
276 
277 
278 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279 
280 } // End namespace Foam
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 #endif
285 
286 // ************************************************************************* //
Foam::UOPstream::UOPstream
UOPstream(const commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool sendAtDestruct=true, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Definition: UOPstream.C:135
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Foam::UPstream::commsType
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:547
Foam::UOPstream::flush
virtual void flush()
Flush stream.
Definition: UOPstream.H:214
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::UOPstream::write
static bool write(const commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=UPstream::worldComm)
Write given buffer to given processor.
Definition: UOPwrite.C:36
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:57
UPstream.H
Foam::DynamicList< char >
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::UOPstream::print
void print(Ostream &os) const
Print stream description to Ostream.
Definition: UOPstream.C:394
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::UOPstream::endl
virtual void endl()
Add newline and flush stream.
Definition: UOPstream.H:218
Foam::UOPstream::endRawWrite
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: UOPstream.H:201
Foam::UPstream
Inter-processor communications stream.
Definition: UPstream.H:61
Foam::UOPstream::flags
ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream.
Definition: UOPstream.H:264
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::UOPstream::fill
virtual char fill(const char)
Set padding character for formatted field up to field width.
Definition: UOPstream.H:229
Foam::UOPstream::beginRawWrite
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: UOPstream.C:377
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::UOPstream::width
virtual int width() const
Get width of output field.
Definition: UOPstream.H:235
os
OBJstream os(runTime.globalPath()/outputName)
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UOPstream::precision
virtual int precision() const
Get precision of output field.
Definition: UOPstream.H:248
Ostream.H
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Foam::UOPstream::fill
virtual char fill() const
Get the current padding character.
Definition: UOPstream.H:223
Foam::UOPstream::~UOPstream
~UOPstream()
Destructor.
Definition: UOPstream.C:175
PstreamBuffers.H
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::UOPstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:294
DynamicList.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::UOPstream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: UOPstream.C:362
Foam::UOPstream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: UOPstream.H:133
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::UOPstream::indent
virtual void indent()
Add indentation characters.
Definition: UOPstream.H:207