UIPstream.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-2013 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::UIPstream
29 
30 Description
31  Input inter-processor communications stream operating on external
32  buffer.
33 
34 SourceFiles
35  UIPstream.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #include "Pstream.H"
40 
41 #ifndef UIPstream_H
42 #define UIPstream_H
43 
44 #include "UPstream.H"
45 #include "Istream.H"
46 #include "PstreamBuffers.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class UIPstream Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class UIPstream
58 :
59  public UPstream,
60  public Istream
61 {
62  // Private Data
63 
64  int fromProcNo_;
65 
66  DynamicList<char>& recvBuf_;
67 
68  label& recvBufPos_;
69 
70  const int tag_;
71 
72  const label comm_;
73 
74  const bool clearAtEnd_;
75 
76  int messageSize_;
77 
78 
79  // Private Member Functions
80 
81  //- Check buffer position against messageSize_ for EOF
82  inline void checkEof();
83 
84  //- Prepare receive buffer by adjusting alignment
85  inline void prepareBuffer(const size_t align);
86 
87  //- Read a T from the receive buffer
88  template<class T>
89  inline void readFromBuffer(T& val);
90 
91  //- Read count bytes of data from the receive buffer.
92  // Prior data alignment is done by prepareBuffer
93  inline void readFromBuffer(void* data, const size_t count);
94 
95  //- Read string length and string content
96  inline Istream& readString(std::string& str);
97 
98 
99 public:
100 
101  // Constructors
102 
103  //- Construct given process index to read from using the given
104  //- attached receive buffer, optional communication characteristics
105  //- and IO format
106  UIPstream
107  (
108  const commsTypes commsType,
109  const int fromProcNo,
110  DynamicList<char>& receiveBuf,
111  label& receiveBufPosition,
112  const int tag = UPstream::msgType(),
113  const label comm = UPstream::worldComm,
114  const bool clearAtEnd = false, // destroy receiveBuf if at end
116  );
117 
118  //- Construct given buffers
119  UIPstream(const int fromProcNo, PstreamBuffers& buffers);
120 
121 
122  //- Destructor
123  ~UIPstream();
124 
125 
126  // Member Functions
127 
128  // Inquiry
129 
130  //- Return flags of output stream
131  ios_base::fmtflags flags() const
132  {
133  return ios_base::fmtflags(0);
134  }
135 
136 
137  // Read Functions
138 
139  //- Read into given buffer from given processor
140  // \return the message size
141  static label read
142  (
143  const commsTypes commsType,
144  const int fromProcNo,
145  char* buf,
146  const std::streamsize bufSize,
147  const int tag = UPstream::msgType(),
148  const label communicator = UPstream::worldComm
149  );
150 
151  //- Return next token from stream
152  Istream& read(token& t);
153 
154  //- Read a character
155  Istream& read(char& c);
156 
157  //- Read a word
158  Istream& read(word& str);
159 
160  // Read a string
161  Istream& read(string& str);
162 
163  //- Read a label
164  Istream& read(label& val);
165 
166  //- Read a floatScalar
167  Istream& read(floatScalar& val);
168 
169  //- Read a doubleScalar
170  Istream& read(doubleScalar& val);
171 
172  //- Read binary block with 8-byte alignment.
173  Istream& read(char* data, std::streamsize count);
174 
175  //- Low-level raw binary read
176  Istream& readRaw(char* data, std::streamsize count);
177 
178  //- Start of low-level raw binary read
179  bool beginRawRead();
180 
181  //- End of low-level raw binary read
182  bool endRawRead()
183  {
184  return true;
185  }
186 
187  //- Rewind the stream so that it may be read again
188  void rewind();
189 
190 
191  // Edit
192 
193  //- Set flags of stream
194  ios_base::fmtflags flags(const ios_base::fmtflags)
195  {
196  return ios_base::fmtflags(0);
197  }
198 
199 
200  // Print
201 
202  //- Print stream description to Ostream
203  void print(Ostream& os) const;
204 };
205 
206 
207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 
209 } // End namespace Foam
210 
211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 
213 #endif
214 
215 // ************************************************************************* //
Foam::UIPstream::flags
ios_base::fmtflags flags() const
Return flags of output stream.
Definition: UIPstream.H:130
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::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
UPstream.H
Foam::DynamicList< char >
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::UIPstream::rewind
void rewind()
Rewind the stream so that it may be read again.
Definition: UIPstream.C:430
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::UIPstream::read
static label read(const commsTypes commsType, const int fromProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=UPstream::worldComm)
Read into given buffer from given processor.
Definition: UIPread.C:81
Foam::UIPstream::UIPstream
UIPstream(const commsTypes commsType, const int fromProcNo, DynamicList< char > &receiveBuf, label &receiveBufPosition, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool clearAtEnd=false, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Definition: UIPread.C:37
Foam::UPstream
Inter-processor communications stream.
Definition: UPstream.H:61
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::UIPstream::~UIPstream
~UIPstream()
Destructor.
Definition: UIPstream.C:147
Istream.H
Foam::UIPstream::print
void print(Ostream &os) const
Print stream description to Ostream.
Definition: UIPstream.C:436
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
os
OBJstream os(runTime.globalPath()/outputName)
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
Foam::UIPstream::readRaw
Istream & readRaw(char *data, std::streamsize count)
Low-level raw binary read.
Definition: UIPstream.C:401
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::UIPstream::endRawRead
bool endRawRead()
End of low-level raw binary read.
Definition: UIPstream.H:181
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::UIPstream::flags
ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream.
Definition: UIPstream.H:193
PstreamBuffers.H
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56
Foam::UIPstream::beginRawRead
bool beginRawRead()
Start of low-level raw binary read.
Definition: UIPstream.C:412
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55