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-2024 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  writeToBuffer(char(token::VERBATIMSTRING));
211 
212  size_t len = vs.size();
213  writeToBuffer(len);
214  writeToBuffer(vs.c_str(), len + 1, 1);
215 
216  return *this;
217 }
218 
219 
221 (
222  const std::string& str,
223  const bool quoted
224 )
225 {
226  if (quoted)
227  {
228  writeToBuffer(char(token::STRING));
229  }
230  else
231  {
232  writeToBuffer(char(token::WORD));
233  }
234 
235  size_t len = str.size();
236  writeToBuffer(len);
237  writeToBuffer(str.c_str(), len + 1, 1);
238 
239  return *this;
240 }
241 
242 
244 {
245  writeToBuffer(char(token::INTEGER_32));
246  writeToBuffer(val);
247  return *this;
248 }
249 
250 
252 {
253  writeToBuffer(char(token::INTEGER_64));
254  writeToBuffer(val);
255  return *this;
256 }
257 
258 
260 {
261  writeToBuffer(char(token::UNSIGNED_INTEGER_32));
262  writeToBuffer(val);
263  return *this;
264 }
265 
266 
268 {
269  writeToBuffer(char(token::UNSIGNED_INTEGER_64));
270  writeToBuffer(val);
271  return *this;
272 }
273 
274 
276 {
277  writeToBuffer(char(token::FLOAT_SCALAR));
278  writeToBuffer(val);
279  return *this;
280 }
281 
282 
284 {
285  writeToBuffer(char(token::DOUBLE_SCALAR));
286  writeToBuffer(val);
287  return *this;
288 }
289 
290 
292 {
293  writeToBuffer(char(token::LONG_DOUBLE_SCALAR));
294  writeToBuffer(val);
295  return *this;
296 }
297 
298 
299 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
300 {
301  if (format() != BINARY)
302  {
304  << "stream format not binary"
306  }
307 
308  writeToBuffer(data, count, 8);
309 
310  return *this;
311 }
312 
313 
315 {
316  os << "Writing from processor " << toProcNo_
317  << " to processor " << myProcNo() << " in communicator " << comm_
318  << " and tag " << tag_ << Foam::endl;
319 }
320 
321 
322 // ************************************************************************* //
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).
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:34
~UOPstream()
Destructor.
Definition: UOPstream.C:126
Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: UOPstream.C:221
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
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UOPstream.C:314
Inter-processor communications stream.
Definition: UPstream.H:59
commsTypes
Types of communications.
Definition: UPstream.H:65
@ WORD
Definition: token.H:83
@ UNSIGNED_INTEGER_32
Definition: token.H:90
@ FLOAT_SCALAR
Definition: token.H:92
@ INTEGER_64
Definition: token.H:89
@ DOUBLE_SCALAR
Definition: token.H:93
@ LONG_DOUBLE_SCALAR
Definition: token.H:94
@ VERBATIMSTRING
Definition: token.H:87
@ UNSIGNED_INTEGER_64
Definition: token.H:91
@ INTEGER_32
Definition: token.H:88
@ STRING
Definition: token.H:86
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:62
#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:258
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)
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.
long double longDoubleScalar
Lang double precision floating point scalar type.
word format(conversionProperties.lookup("format"))