UOPstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "UOPstream.H"
27 #include "int.H"
28 #include "token.H"
29 
30 #include <cctype>
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T>
35 inline void Foam::UOPstream::writeToBuffer(const T& t)
36 {
37  writeToBuffer(&t, sizeof(T), sizeof(T));
38 }
39 
40 
41 inline void Foam::UOPstream::writeToBuffer(const char& c)
42 {
43  if (!sendBuf_.capacity())
44  {
45  sendBuf_.setCapacity(1000);
46  }
47  sendBuf_.append(c);
48 }
49 
50 
51 inline void Foam::UOPstream::writeToBuffer
52 (
53  const void* data,
54  size_t count,
55  size_t align
56 )
57 {
58  if (!sendBuf_.capacity())
59  {
60  sendBuf_.setCapacity(1000);
61  }
62 
63  label alignedPos = sendBuf_.size();
64 
65  if (align > 1)
66  {
67  // Align bufPosition. Pads sendBuf_.size() - oldPos characters.
68  alignedPos = align + ((sendBuf_.size() - 1) & ~(align - 1));
69  }
70 
71  // Extend if necessary
72  sendBuf_.setSize(alignedPos + count);
73 
74  const char* dataPtr = reinterpret_cast<const char*>(data);
75  size_t i = count;
76  while (i--) sendBuf_[alignedPos++] = *dataPtr++;
77 }
78 
79 
80 
81 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
82 
84 (
85  const commsTypes commsType,
86  const int toProcNo,
87  DynamicList<char>& sendBuf,
88  const int tag,
89  const label comm,
90  const bool sendAtDestruct,
91  const streamFormat format,
92  const versionNumber version,
93  const bool global
94 )
95 :
96  UPstream(commsType),
97  Ostream(format, version, UNCOMPRESSED, global),
98  toProcNo_(toProcNo),
99  sendBuf_(sendBuf),
100  tag_(tag),
101  comm_(comm),
102  sendAtDestruct_(sendAtDestruct)
103 {
104  setOpened();
105  setGood();
106 }
107 
108 
109 Foam::UOPstream::UOPstream(const int toProcNo, PstreamBuffers& buffers)
110 :
111  UPstream(buffers.commsType_),
112  Ostream(buffers.format_, buffers.version_),
113  toProcNo_(toProcNo),
114  sendBuf_(buffers.sendBuf_[toProcNo]),
115  tag_(buffers.tag_),
116  comm_(buffers.comm_),
117  sendAtDestruct_(buffers.commsType_ != UPstream::commsTypes::nonBlocking)
118 {
119  setOpened();
120  setGood();
121 }
122 
123 
124 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
125 
127 {
128  if (sendAtDestruct_)
129  {
130  if
131  (
133  (
134  commsType_,
135  toProcNo_,
136  sendBuf_.begin(),
137  sendBuf_.size(),
138  tag_,
139  comm_
140  )
141  )
142  {
144  << "Failed sending outgoing message of size " << sendBuf_.size()
145  << " to processor " << toProcNo_
147  }
148  }
149 }
150 
151 
152 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
153 
155 {
156  if (!isspace(c))
157  {
158  writeToBuffer(c);
159  }
160 
161  return *this;
162 }
163 
164 
166 {
167  word nonWhiteChars(string::validate<word>(str));
168 
169  if (nonWhiteChars.size() == 1)
170  {
171  return write(nonWhiteChars[0]);
172  }
173  else if (nonWhiteChars.size())
174  {
175  return write(nonWhiteChars);
176  }
177  else
178  {
179  return *this;
180  }
181 }
182 
183 
185 {
186  writeToBuffer(char(token::WORD));
187 
188  size_t len = str.size();
189  writeToBuffer(len);
190  writeToBuffer(str.c_str(), len + 1, 1);
191 
192  return *this;
193 }
194 
195 
197 {
198  writeToBuffer(char(token::STRING));
199 
200  size_t len = str.size();
201  writeToBuffer(len);
202  writeToBuffer(str.c_str(), len + 1, 1);
203 
204  return *this;
205 }
206 
207 
209 {
210  writeQuoted(kt, kt.isPattern());
211  return *this;
212 }
213 
214 
216 {
217  writeToBuffer(char(token::VERBATIMSTRING));
218 
219  size_t len = vs.size();
220  writeToBuffer(len);
221  writeToBuffer(vs.c_str(), len + 1, 1);
222 
223  return *this;
224 }
225 
226 
228 (
229  const std::string& str,
230  const bool quoted
231 )
232 {
233  if (quoted)
234  {
235  writeToBuffer(char(token::STRING));
236  }
237  else
238  {
239  writeToBuffer(char(token::WORD));
240  }
241 
242  size_t len = str.size();
243  writeToBuffer(len);
244  writeToBuffer(str.c_str(), len + 1, 1);
245 
246  return *this;
247 }
248 
249 
251 {
252  writeToBuffer(char(token::INTEGER_32));
253  writeToBuffer(val);
254  return *this;
255 }
256 
257 
259 {
260  writeToBuffer(char(token::INTEGER_64));
261  writeToBuffer(val);
262  return *this;
263 }
264 
265 
267 {
268  writeToBuffer(char(token::UNSIGNED_INTEGER_32));
269  writeToBuffer(val);
270  return *this;
271 }
272 
273 
275 {
276  writeToBuffer(char(token::UNSIGNED_INTEGER_64));
277  writeToBuffer(val);
278  return *this;
279 }
280 
281 
283 {
284  writeToBuffer(char(token::FLOAT_SCALAR));
285  writeToBuffer(val);
286  return *this;
287 }
288 
289 
291 {
292  writeToBuffer(char(token::DOUBLE_SCALAR));
293  writeToBuffer(val);
294  return *this;
295 }
296 
297 
299 {
300  writeToBuffer(char(token::LONG_DOUBLE_SCALAR));
301  writeToBuffer(val);
302  return *this;
303 }
304 
305 
306 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
307 {
308  if (format() != BINARY)
309  {
311  << "stream format not binary"
313  }
314 
315  writeToBuffer(data, count, 8);
316 
317  return *this;
318 }
319 
320 
322 {
323  os << "Writing from processor " << toProcNo_
324  << " to processor " << myProcNo() << " in communicator " << comm_
325  << " and tag " << tag_ << Foam::endl;
326 }
327 
328 
329 // ************************************************************************* //
Version number type.
Definition: IOstream.H:97
void setGood()
Set stream to be good.
Definition: IOstream.H:255
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
void setOpened()
Set stream opened.
Definition: IOstream.H:237
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Buffers for inter-processor communications streams (UOPstream, UIPstream).
~UOPstream()
Destructor.
Definition: UOPstream.C:126
Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:228
UOPstream(const commsTypes commsType, const int toProcNo, DynamicList< char > &sendBuf, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool sendAtDestruct=true, const streamFormat format=BINARY, const versionNumber version=currentVersion, const bool global=false)
Construct given process index to send to and optional buffer size,.
Definition: UOPstream.C:84
virtual Ostream & write(const token &)
Write token.
Definition: Ostream.C:51
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UOPstream.C:321
Inter-processor communications stream.
Definition: UPstream.H:59
commsTypes
Types of communications.
Definition: UPstream.H:65
A class for handling keywords in dictionaries.
Definition: keyType.H:69
bool isPattern() const
Should be treated as a match rather than a literal string.
Definition: keyTypeI.H:97
@ WORD
Definition: token.H:84
@ UNSIGNED_INTEGER_32
Definition: token.H:91
@ FLOAT_SCALAR
Definition: token.H:93
@ INTEGER_64
Definition: token.H:90
@ DOUBLE_SCALAR
Definition: token.H:94
@ LONG_DOUBLE_SCALAR
Definition: token.H:95
@ VERBATIMSTRING
Definition: token.H:88
@ UNSIGNED_INTEGER_64
Definition: token.H:92
@ INTEGER_32
Definition: token.H:89
@ STRING
Definition: token.H:87
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:63
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
System integer.
const dimensionedScalar c
Speed of light in a vacuum.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
errorManip< error > abort(error &err)
Definition: errorManip.H:131
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
error FatalError
bool isspace(char c)
Definition: char.H:53
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
void T(GeometricField< Type, GeoMesh, PrimitiveField1 > &gf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf1)
long double longDoubleScalar
Lang double precision floating point scalar type.
word format(conversionProperties.lookup("format"))