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-2018 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 buffer for count bytes of output at specified alignment.
79  inline void prepareBuffer(const size_t count, const size_t align);
80 
81  //- Write data to the transfer buffer
82  template<class T>
83  inline void writeToBuffer(const T& val);
84 
85  //- Write a char to the transfer buffer
86  inline void writeToBuffer(const char& c);
87 
88  //- Write count bytes of data to the transfer buffer
89  // using align byte alignment
90  inline void writeToBuffer
91  (
92  const void* data,
93  const size_t count,
94  const size_t align
95  );
96 
97  //- Write string length and content.
98  // The content includes the trailing nul char.
99  inline void writeStringToBuffer(const std::string& str);
100 
101 
102 public:
103 
104  // Constructors
105 
106  //- Construct given process index to send to and optional buffer size,
107  // write format and IO version
108  UOPstream
109  (
110  const commsTypes commsType,
111  const int toProcNo,
112  DynamicList<char>& sendBuf,
113  const int tag = UPstream::msgType(),
114  const label comm = UPstream::worldComm,
115  const bool sendAtDestruct = true,
118  );
119 
120  //- Construct given buffers
121  UOPstream(const int toProcNo, PstreamBuffers& buffers);
122 
123 
124  //- Destructor
125  ~UOPstream();
126 
127 
128  // Member Functions
129 
130  // Inquiry
131 
132  //- Return flags of output stream
133  virtual ios_base::fmtflags flags() const
134  {
135  return ios_base::fmtflags(0);
136  }
137 
138 
139  // Write Functions
140 
141  //- Write given buffer to given processor
142  static bool write
143  (
144  const commsTypes commsType,
145  const int toProcNo,
146  const char* buf,
147  const std::streamsize bufSize,
148  const int tag = UPstream::msgType(),
149  const label communicator = 0
150  );
151 
152  //- Write token to stream or otherwise handle it.
153  // \return false if the token type was not handled by this method
154  virtual bool write(const token& tok);
155 
156  //- Write single character. Whitespace is suppressed.
157  virtual Ostream& write(const char c);
158 
159  //- Write the word-characters of a character string.
160  // Sends as a single char, or as word.
161  virtual Ostream& write(const char* str);
162 
163  //- Write word
164  virtual Ostream& write(const word& str);
165 
166  //- Write string
167  virtual Ostream& write(const string& str);
168 
169  //- Write std::string surrounded by quotes.
170  // Optional write without quotes.
171  virtual Ostream& writeQuoted
172  (
173  const std::string& str,
174  const bool quoted=true
175  );
176 
177  //- Write int32_t as a label
178  virtual Ostream& write(const int32_t val);
179 
180  //- Write int64_t as a label
181  virtual Ostream& write(const int64_t val);
182 
183  //- Write floatScalar
184  virtual Ostream& write(const floatScalar val);
185 
186  //- Write doubleScalar
187  virtual Ostream& write(const doubleScalar val);
188 
189  //- Write binary block with 8-byte alignment.
190  virtual Ostream& write(const char* data, std::streamsize count);
191 
192  //- Low-level raw binary output.
193  virtual Ostream& writeRaw(const char* data, std::streamsize count);
194 
195  //- Begin marker for low-level raw binary output.
196  // The count indicates the number of bytes for subsequent
197  // writeRaw calls.
198  virtual bool beginRawWrite(std::streamsize count);
199 
200  //- End marker for low-level raw binary output.
201  virtual bool endRawWrite()
202  {
203  return true;
204  }
205 
206  //- Add indentation characters
207  virtual void indent()
208  {}
209 
210 
211  // Stream state functions
212 
213  //- Flush stream
214  virtual void flush()
215  {}
216 
217  //- Add newline and flush stream
218  virtual void endl()
219  {}
220 
221  //- Get the current padding character
222  // \return previous padding character
223  virtual char fill() const
224  {
225  return 0;
226  }
227 
228  //- Set padding character for formatted field up to field width
229  virtual char fill(const char)
230  {
231  return 0;
232  }
233 
234  //- Get width of output field
235  virtual int width() const
236  {
237  return 0;
238  }
239 
240  //- Set width of output field
241  // \return previous width
242  virtual int width(const int)
243  {
244  return 0;
245  }
246 
247  //- Get precision of output field
248  virtual int precision() const
249  {
250  return 0;
251  }
252 
253  //- Set precision of output field
254  // \return old precision
255  virtual int precision(const int)
256  {
257  return 0;
258  }
259 
260 
261  // Edit
262 
263  //- Set flags of stream
264  ios_base::fmtflags flags(const ios_base::fmtflags)
265  {
266  return ios_base::fmtflags(0);
267  }
268 
269 
270  // Print
271 
272  //- Print stream description to Ostream
273  void print(Ostream& os) const;
274 };
275 
276 
277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 
279 } // End namespace Foam
280 
281 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
282 
283 #endif
284 
285 // ************************************************************************* //
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, streamFormat format=BINARY, versionNumber version=currentVersion)
Construct given process index to send to and optional buffer size,.
Definition: UOPstream.C:122
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Foam::UOPstream::flush
virtual void flush()
Flush stream.
Definition: UOPstream.H:213
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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:375
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:87
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
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:217
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=0)
Write given buffer to given processor.
Definition: UOPwrite.C:36
Foam::UOPstream::endRawWrite
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: UOPstream.H:200
Foam::UPstream
Inter-processor communications stream.
Definition: UPstream.H:61
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::UOPstream::flags
ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream.
Definition: UOPstream.H:263
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:228
Foam::UPstream::commsType
commsTypes commsType() const
Get the communications type of the stream.
Definition: UPstream.H:548
Foam::UOPstream::beginRawWrite
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: UOPstream.C:359
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:341
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:234
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:247
Ostream.H
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:541
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
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:295
Foam::UOPstream::fill
virtual char fill() const
Get the current padding character.
Definition: UOPstream.H:222
Foam::UOPstream::~UOPstream
~UOPstream()
Destructor.
Definition: UOPstream.C:163
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:277
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:344
Foam::UOPstream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: UOPstream.H:132
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:206